Commit 73539f28 authored by Krasimir Angelov's avatar Krasimir Angelov

Add unique index on configuration for batched_background_migrations

Also remove existing redundant index, the new index can be used the same
way.

See
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/62634#note_591636460.

Changelog: other
parent cfdcc0dc
# frozen_string_literal: true
class AddUniqueIndexForBatchedBackgroundMigrations < ActiveRecord::Migration[6.1]
include Gitlab::Database::MigrationHelpers
disable_ddl_transaction!
TABLE_NAME = :batched_background_migrations
INDEX_NAME = 'index_batched_background_migrations_on_unique_configuration'
REDUNDANT_INDEX_NAME = 'index_batched_migrations_on_job_table_and_column_name'
def up
add_concurrent_index TABLE_NAME,
%i[job_class_name table_name column_name job_arguments],
unique: true,
name: INDEX_NAME
remove_concurrent_index_by_name TABLE_NAME, REDUNDANT_INDEX_NAME
end
def down
add_concurrent_index TABLE_NAME,
%i[job_class_name table_name column_name],
name: REDUNDANT_INDEX_NAME
remove_concurrent_index_by_name TABLE_NAME, INDEX_NAME
end
end
ddd40d4fb063b3a728f4b9a214d6033f70ee3719ac769957f5295a0c5f62a5c0
\ No newline at end of file
...@@ -22611,12 +22611,12 @@ CREATE INDEX index_badges_on_group_id ON badges USING btree (group_id); ...@@ -22611,12 +22611,12 @@ CREATE INDEX index_badges_on_group_id ON badges USING btree (group_id);
CREATE INDEX index_badges_on_project_id ON badges USING btree (project_id); CREATE INDEX index_badges_on_project_id ON badges USING btree (project_id);
CREATE UNIQUE INDEX index_batched_background_migrations_on_unique_configuration ON batched_background_migrations USING btree (job_class_name, table_name, column_name, job_arguments);
CREATE INDEX index_batched_jobs_by_batched_migration_id_and_id ON batched_background_migration_jobs USING btree (batched_background_migration_id, id); CREATE INDEX index_batched_jobs_by_batched_migration_id_and_id ON batched_background_migration_jobs USING btree (batched_background_migration_id, id);
CREATE INDEX index_batched_jobs_on_batched_migration_id_and_status ON batched_background_migration_jobs USING btree (batched_background_migration_id, status); CREATE INDEX index_batched_jobs_on_batched_migration_id_and_status ON batched_background_migration_jobs USING btree (batched_background_migration_id, status);
CREATE INDEX index_batched_migrations_on_job_table_and_column_name ON batched_background_migrations USING btree (job_class_name, table_name, column_name);
CREATE INDEX index_board_assignees_on_assignee_id ON board_assignees USING btree (assignee_id); CREATE INDEX index_board_assignees_on_assignee_id ON board_assignees USING btree (assignee_id);
CREATE UNIQUE INDEX index_board_assignees_on_board_id_and_assignee_id ON board_assignees USING btree (board_id, assignee_id); CREATE UNIQUE INDEX index_board_assignees_on_board_id_and_assignee_id ON board_assignees USING btree (board_id, assignee_id);
...@@ -14,6 +14,10 @@ module Gitlab ...@@ -14,6 +14,10 @@ module Gitlab
class_name: 'Gitlab::Database::BackgroundMigration::BatchedJob', class_name: 'Gitlab::Database::BackgroundMigration::BatchedJob',
foreign_key: :batched_background_migration_id foreign_key: :batched_background_migration_id
validates :job_arguments, uniqueness: {
scope: [:job_class_name, :table_name, :column_name]
}
scope :queue_order, -> { order(id: :asc) } scope :queue_order, -> { order(id: :asc) }
scope :queued, -> { where(status: [:active, :paused]) } scope :queued, -> { where(status: [:active, :paused]) }
scope :for_configuration, ->(job_class_name, table_name, column_name, job_arguments) do scope :for_configuration, ->(job_class_name, table_name, column_name, job_arguments) do
......
...@@ -9,6 +9,7 @@ FactoryBot.define do ...@@ -9,6 +9,7 @@ FactoryBot.define do
job_class_name { 'CopyColumnUsingBackgroundMigrationJob' } job_class_name { 'CopyColumnUsingBackgroundMigrationJob' }
table_name { :events } table_name { :events }
column_name { :id } column_name { :id }
sequence(:job_arguments) { |n| [["column_#{n}"], ["column_#{n}_convert_to_bigint"]] }
total_tuple_count { 10_000 } total_tuple_count { 10_000 }
pause_ms { 100 } pause_ms { 100 }
end end
......
...@@ -19,6 +19,12 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigration, type: :m ...@@ -19,6 +19,12 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigration, type: :m
end end
end end
describe 'validations' do
subject { build(:batched_background_migration) }
it { is_expected.to validate_uniqueness_of(:job_arguments).scoped_to(:job_class_name, :table_name, :column_name) }
end
describe '.queue_order' do describe '.queue_order' do
let!(:migration1) { create(:batched_background_migration) } let!(:migration1) { create(:batched_background_migration) }
let!(:migration2) { create(:batched_background_migration) } let!(:migration2) { create(:batched_background_migration) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment