diff --git a/db/docs/batched_background_migrations/backfill_spam_logs_organization_id.yml b/db/docs/batched_background_migrations/backfill_spam_logs_organization_id.yml new file mode 100644 index 0000000000000000000000000000000000000000..751afb89a3ac94cd4b9a392283a84f0e0ccf6bc5 --- /dev/null +++ b/db/docs/batched_background_migrations/backfill_spam_logs_organization_id.yml @@ -0,0 +1,8 @@ +--- +migration_job_name: BackfillSpamLogsOrganizationId +description: Backfills sharding key `spam_logs.organization_id` from `users`. +feature_category: instance_resiliency +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/210030 +milestone: '18.6' +queued_migration_version: 20251023230133 +finalized_by: # version of the migration that finalized this BBM diff --git a/db/docs/spam_logs.yml b/db/docs/spam_logs.yml index 6738fe6f613bd6dabec41ec828f81d46555ad35b..7b01568c96641123b8c4900c36db2af12fa77c67 100644 --- a/db/docs/spam_logs.yml +++ b/db/docs/spam_logs.yml @@ -19,3 +19,4 @@ desired_sharding_key: sharding_key: organization_id belongs_to: user table_size: small +desired_sharding_key_migration_job_name: BackfillSpamLogsOrganizationId diff --git a/db/post_migrate/20251023230133_queue_backfill_spam_logs_organization_id.rb b/db/post_migrate/20251023230133_queue_backfill_spam_logs_organization_id.rb new file mode 100644 index 0000000000000000000000000000000000000000..80355947681420e7cb273bf5b3d0944ae1f9bdba --- /dev/null +++ b/db/post_migrate/20251023230133_queue_backfill_spam_logs_organization_id.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +class QueueBackfillSpamLogsOrganizationId < Gitlab::Database::Migration[2.3] + milestone '18.6' + restrict_gitlab_migration gitlab_schema: :gitlab_main_org + + MIGRATION = "BackfillSpamLogsOrganizationId" + DELAY_INTERVAL = 2.minutes + BATCH_SIZE = 1000 + SUB_BATCH_SIZE = 100 + + def up + queue_batched_background_migration( + MIGRATION, + :spam_logs, + :id, + :organization_id, + :users, + :organization_id, + :user_id, + job_interval: DELAY_INTERVAL, + batch_size: BATCH_SIZE, + sub_batch_size: SUB_BATCH_SIZE + ) + end + + def down + delete_batched_background_migration( + MIGRATION, + :spam_logs, + :id, + [ + :organization_id, + :users, + :organization_id, + :user_id + ] + ) + end +end diff --git a/db/schema_migrations/20251023230133 b/db/schema_migrations/20251023230133 new file mode 100644 index 0000000000000000000000000000000000000000..e2c20d596669e20e235a7f113d1c7bc57942b611 --- /dev/null +++ b/db/schema_migrations/20251023230133 @@ -0,0 +1 @@ +fda696fcb5d092e2f6a8e3310af7ed0075f4a32978ff3559a5491e4fa2214dcf \ No newline at end of file diff --git a/lib/gitlab/background_migration/backfill_spam_logs_organization_id.rb b/lib/gitlab/background_migration/backfill_spam_logs_organization_id.rb new file mode 100644 index 0000000000000000000000000000000000000000..81557171dab7e613f8e1bbc46e21f20795cb65fa --- /dev/null +++ b/lib/gitlab/background_migration/backfill_spam_logs_organization_id.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Gitlab + module BackgroundMigration + class BackfillSpamLogsOrganizationId < BackfillDesiredShardingKeyJob + operation_name :backfill_spam_logs_organization_id + feature_category :instance_resiliency + end + end +end diff --git a/spec/lib/gitlab/background_migration/backfill_spam_logs_organization_id_spec.rb b/spec/lib/gitlab/background_migration/backfill_spam_logs_organization_id_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..33e47552613b1170e157822b9dffc8f5b5b1eff6 --- /dev/null +++ b/spec/lib/gitlab/background_migration/backfill_spam_logs_organization_id_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::BackfillSpamLogsOrganizationId, feature_category: :instance_resiliency do + include_examples 'desired sharding key backfill job' do + let(:batch_table) { :spam_logs } + let(:backfill_column) { :organization_id } + let(:backfill_via_table) { :users } + let(:backfill_via_column) { :organization_id } + let(:backfill_via_foreign_key) { :user_id } + end +end diff --git a/spec/migrations/20251023230133_queue_backfill_spam_logs_organization_id_spec.rb b/spec/migrations/20251023230133_queue_backfill_spam_logs_organization_id_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..20226a2dca890868680280e9ca7548c3059a5923 --- /dev/null +++ b/spec/migrations/20251023230133_queue_backfill_spam_logs_organization_id_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe QueueBackfillSpamLogsOrganizationId, feature_category: :instance_resiliency do + let!(:batched_migration) { described_class::MIGRATION } + + it 'schedules a new batched migration' do + reversible_migration do |migration| + migration.before -> { + expect(batched_migration).not_to have_scheduled_batched_migration + } + + migration.after -> { + expect(batched_migration).to have_scheduled_batched_migration( + table_name: :spam_logs, + column_name: :id, + interval: described_class::DELAY_INTERVAL, + batch_size: described_class::BATCH_SIZE, + sub_batch_size: described_class::SUB_BATCH_SIZE, + gitlab_schema: :gitlab_main_org, + job_arguments: [ + :organization_id, + :users, + :organization_id, + :user_id + ] + ) + } + end + end +end