diff --git a/db/post_migrate/20230209090702_fix_partition_ids_for_ci_build_report_result.rb b/db/post_migrate/20230209090702_fix_partition_ids_for_ci_build_report_result.rb new file mode 100644 index 0000000000000000000000000000000000000000..d21d986ee31e25ca8688ab417ff49930e5aa74c7 --- /dev/null +++ b/db/post_migrate/20230209090702_fix_partition_ids_for_ci_build_report_result.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class FixPartitionIdsForCiBuildReportResult < Gitlab::Database::Migration[2.1] + MIGRATION = 'RebalancePartitionId' + DELAY_INTERVAL = 2.minutes.freeze + TABLE = :ci_build_report_results + BATCH_SIZE = 2_000 + SUB_BATCH_SIZE = 200 + + restrict_gitlab_migration gitlab_schema: :gitlab_ci + + def up + return unless Gitlab.com? + + queue_batched_background_migration( + MIGRATION, + TABLE, + :build_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, :build_id, []) + end +end diff --git a/db/schema_migrations/20230209090702 b/db/schema_migrations/20230209090702 new file mode 100644 index 0000000000000000000000000000000000000000..9eb84a3b5d058d1e256d9f86ec3a91f68f532bf3 --- /dev/null +++ b/db/schema_migrations/20230209090702 @@ -0,0 +1 @@ +f3f0611b503bf2be35a201541826a33dd0ff58a0b41c6588303317f2b171d567 \ No newline at end of file diff --git a/spec/migrations/20230209090702_fix_partition_ids_for_ci_build_report_result_spec.rb b/spec/migrations/20230209090702_fix_partition_ids_for_ci_build_report_result_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..f0ac8239f581007b5264bbb8e646a2746311e6b5 --- /dev/null +++ b/spec/migrations/20230209090702_fix_partition_ids_for_ci_build_report_result_spec.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe FixPartitionIdsForCiBuildReportResult, + 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 ci_build_report_results' do + migrate! + + expect(migration).to have_scheduled_batched_migration( + gitlab_schema: :gitlab_ci, + table_name: :ci_build_report_results, + column_name: :build_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