From bf62b4e6126a75e248b3af6c5d62ff4f0395230f Mon Sep 17 00:00:00 2001 From: Maxime Orefice Date: Wed, 8 Feb 2023 14:29:22 +0100 Subject: [PATCH] Fix partition_id for ci_stage Changelog: fixed --- ...08132608_fix_partition_ids_for_ci_stage.rb | 30 ++++++++++ db/schema_migrations/20230208132608 | 1 + ...608_fix_partition_ids_for_ci_stage_spec.rb | 58 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 db/post_migrate/20230208132608_fix_partition_ids_for_ci_stage.rb create mode 100644 db/schema_migrations/20230208132608 create mode 100644 spec/migrations/20230208132608_fix_partition_ids_for_ci_stage_spec.rb diff --git a/db/post_migrate/20230208132608_fix_partition_ids_for_ci_stage.rb b/db/post_migrate/20230208132608_fix_partition_ids_for_ci_stage.rb new file mode 100644 index 00000000000000..c3ec614a37f6eb --- /dev/null +++ b/db/post_migrate/20230208132608_fix_partition_ids_for_ci_stage.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class FixPartitionIdsForCiStage < Gitlab::Database::Migration[2.1] + MIGRATION = 'RebalancePartitionId' + DELAY_INTERVAL = 2.minutes.freeze + TABLE = :ci_stages + BATCH_SIZE = 3_000 + SUB_BATCH_SIZE = 300 + + 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/20230208132608 b/db/schema_migrations/20230208132608 new file mode 100644 index 00000000000000..9ecc9531ca7752 --- /dev/null +++ b/db/schema_migrations/20230208132608 @@ -0,0 +1 @@ +a4709b533b0ef420e5a14088fcf7e2646d4601019f62429b528ea3c715da87ca \ No newline at end of file diff --git a/spec/migrations/20230208132608_fix_partition_ids_for_ci_stage_spec.rb b/spec/migrations/20230208132608_fix_partition_ids_for_ci_stage_spec.rb new file mode 100644 index 00000000000000..8b057afc1e92b0 --- /dev/null +++ b/spec/migrations/20230208132608_fix_partition_ids_for_ci_stage_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe FixPartitionIdsForCiStage, 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_stages' do + migrate! + + expect(migration).to have_scheduled_batched_migration( + gitlab_schema: :gitlab_ci, + table_name: :ci_stages, + 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 -- GitLab