diff --git a/db/post_migrate/20230209140102_fix_partition_ids_for_ci_build_metadata.rb b/db/post_migrate/20230209140102_fix_partition_ids_for_ci_build_metadata.rb new file mode 100644 index 0000000000000000000000000000000000000000..07fcbcc3ad78552b298bb2dbe498ceb4d36a3a8e --- /dev/null +++ b/db/post_migrate/20230209140102_fix_partition_ids_for_ci_build_metadata.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class FixPartitionIdsForCiBuildMetadata < Gitlab::Database::Migration[2.1] + MIGRATION = 'RebalancePartitionId' + DELAY_INTERVAL = 2.minutes + TABLE = :p_ci_builds_metadata + BATCH_SIZE = 5_000 + SUB_BATCH_SIZE = 500 + + restrict_gitlab_migration gitlab_schema: :gitlab_ci + + def up + return unless Gitlab.com? + + queue_batched_background_migration( + MIGRATION, + TABLE, + :id, + job_interval: DELAY_INTERVAL, + batch_size: BATCH_SIZE, + sub_batch_size: SUB_BATCH_SIZE + ) + end + + def down + return unless Gitlab.com? + + delete_batched_background_migration(MIGRATION, TABLE, :id, []) + end +end diff --git a/db/schema_migrations/20230209140102 b/db/schema_migrations/20230209140102 new file mode 100644 index 0000000000000000000000000000000000000000..b1938fc21a34458fd9dedaf1079cf93ce23df1a4 --- /dev/null +++ b/db/schema_migrations/20230209140102 @@ -0,0 +1 @@ +c9f92b576436a473380ad2035030a108c60efd4c2f46a259a46246d2019c4285 \ No newline at end of file diff --git a/spec/migrations/20230209140102_fix_partition_ids_for_ci_build_metadata_spec.rb b/spec/migrations/20230209140102_fix_partition_ids_for_ci_build_metadata_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..c354d68749f33f63edf5f0b738830004885e0742 --- /dev/null +++ b/spec/migrations/20230209140102_fix_partition_ids_for_ci_build_metadata_spec.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe FixPartitionIdsForCiBuildMetadata, + migration: :gitlab_ci, + feature_category: :continuous_integration do + let(:migration) { described_class::MIGRATION } + + context 'when on saas' do + before do + allow(Gitlab).to receive(:com?).and_return(true) + end + + describe '#up' do + it 'schedules background jobs for each batch of p_ci_builds_metadata' do + migrate! + + expect(migration).to have_scheduled_batched_migration( + gitlab_schema: :gitlab_ci, + table_name: :p_ci_builds_metadata, + column_name: :id, + interval: described_class::DELAY_INTERVAL, + batch_size: described_class::BATCH_SIZE, + sub_batch_size: described_class::SUB_BATCH_SIZE + ) + end + end + + describe '#down' do + it 'deletes all batched migration records' do + migrate! + schema_migrate_down! + + expect(migration).not_to have_scheduled_batched_migration + end + end + end + + context 'when on self-managed instance' do + let(:migration) { described_class.new } + + describe '#up' do + it 'does not schedule background job' do + expect(migration).not_to receive(:queue_batched_background_migration) + + migration.up + end + end + + describe '#down' do + it 'does not delete background job' do + expect(migration).not_to receive(:delete_batched_background_migration) + + migration.down + end + end + end +end