diff --git a/db/post_migrate/20230214122717_fix_partition_ids_for_ci_job_variables.rb b/db/post_migrate/20230214122717_fix_partition_ids_for_ci_job_variables.rb new file mode 100644 index 0000000000000000000000000000000000000000..0a201c514674b50ae3df6490193c6546b74457f3 --- /dev/null +++ b/db/post_migrate/20230214122717_fix_partition_ids_for_ci_job_variables.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class FixPartitionIdsForCiJobVariables < Gitlab::Database::Migration[2.1] + disable_ddl_transaction! + restrict_gitlab_migration gitlab_schema: :gitlab_ci + + BATCH_SIZE = 50 + + def up + return unless Gitlab.com? + + define_batchable_model(:ci_job_variables) + .where(partition_id: 101) + .each_batch(of: BATCH_SIZE) do |batch| + batch.update_all(partition_id: 100) + sleep 0.1 + end + end + + def down + # no-op + end +end diff --git a/db/schema_migrations/20230214122717 b/db/schema_migrations/20230214122717 new file mode 100644 index 0000000000000000000000000000000000000000..6f69502caa3defa0c92e2399ab410fbcd2d6aea0 --- /dev/null +++ b/db/schema_migrations/20230214122717 @@ -0,0 +1 @@ +803a4aa4c28aecf498d2a70046850d8128327feb12fe1a42f1255cd08da7746e \ No newline at end of file diff --git a/spec/migrations/20230214122717_fix_partition_ids_for_ci_job_variables_spec.rb b/spec/migrations/20230214122717_fix_partition_ids_for_ci_job_variables_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..642758552621002cb9021f3e1ba572d4b365b499 --- /dev/null +++ b/spec/migrations/20230214122717_fix_partition_ids_for_ci_job_variables_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe FixPartitionIdsForCiJobVariables, migration: :gitlab_ci, feature_category: :continuous_integration do + let(:builds) { table(:ci_builds, database: :ci) } + let(:job_variables) { table(:ci_job_variables, database: :ci) } + let(:connection) { job_variables.connection } + + around do |example| + connection.execute "ALTER TABLE #{job_variables.quoted_table_name} DISABLE TRIGGER ALL;" + + example.run + ensure + connection.execute "ALTER TABLE #{job_variables.quoted_table_name} ENABLE TRIGGER ALL;" + end + + before do + job = builds.create!(partition_id: 100) + + job_variables.insert_all!([ + { job_id: job.id, partition_id: 100, key: 'variable-100' }, + { job_id: job.id, partition_id: 101, key: 'variable-101' } + ]) + end + + describe '#up', :aggregate_failures do + context 'when on sass' do + before do + allow(Gitlab).to receive(:com?).and_return(true) + end + + it 'fixes partition_id' do + expect { migrate! }.not_to raise_error + + expect(job_variables.where(partition_id: 100).count).to eq(2) + expect(job_variables.where(partition_id: 101).count).to eq(0) + end + end + + context 'when on self managed' do + it 'does not change partition_id' do + expect { migrate! }.not_to raise_error + + expect(job_variables.where(partition_id: 100).count).to eq(1) + expect(job_variables.where(partition_id: 101).count).to eq(1) + end + end + end +end