Commit 2188d104 authored by Krasimir Angelov's avatar Krasimir Angelov

Add revert_backfill_conversion_of_integer_to_bigint migration helper

for easier revert of `backfill_conversion_of_integer_to_bigint`.

See https://gitlab.com/gitlab-org/gitlab/-/issues/328600.
parent ec5edc5f
......@@ -1048,6 +1048,25 @@ module Gitlab
end
end
# Reverts `backfill_conversion_of_integer_to_bigint`
#
# table - The name of the database table containing the column
# columns - The name, or an array of names, of the column(s) we want to convert to bigint.
# primary_key - The name of the primary key column (most often :id)
def revert_backfill_conversion_of_integer_to_bigint(table, columns, primary_key: :id)
columns = Array.wrap(columns)
conditions = ActiveRecord::Base.sanitize_sql([
'job_class_name = :job_class_name AND table_name = :table_name AND column_name = :column_name AND job_arguments = :job_arguments',
job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
table_name: table,
column_name: primary_key,
job_arguments: [columns, columns.map { |c| "#{c}_convert_to_bigint" }].to_json
])
execute("DELETE FROM batched_background_migrations WHERE #{conditions}")
end
# Performs a concurrent column rename when using PostgreSQL.
def install_rename_triggers_for_postgresql(table, old, new, trigger_name: nil)
Gitlab::Database::UnidirectionalCopyTrigger.on_table(table).create(old, new, trigger_name: trigger_name)
......
......@@ -1921,6 +1921,54 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
end
end
describe '#revert_backfill_conversion_of_integer_to_bigint' do
let(:table) { :_test_backfill_table }
let(:primary_key) { :id }
before do
model.create_table table, id: false do |t|
t.integer primary_key, primary_key: true
t.text :message, null: false
t.integer :other_id
t.timestamps
end
model.initialize_conversion_of_integer_to_bigint(table, columns, primary_key: primary_key)
model.backfill_conversion_of_integer_to_bigint(table, columns, primary_key: primary_key)
end
context 'when a single column is being converted' do
let(:columns) { :id }
it 'deletes the batched migration tracking record' do
expect do
model.revert_backfill_conversion_of_integer_to_bigint(table, columns)
end.to change { Gitlab::Database::BackgroundMigration::BatchedMigration.count }.by(-1)
end
end
context 'when a multiple columns are being converted' do
let(:columns) { [:id, :other_id] }
it 'deletes the batched migration tracking record' do
expect do
model.revert_backfill_conversion_of_integer_to_bigint(table, columns)
end.to change { Gitlab::Database::BackgroundMigration::BatchedMigration.count }.by(-1)
end
end
context 'when primary key column has custom name' do
let(:primary_key) { :other_pk }
let(:columns) { :other_id }
it 'deletes the batched migration tracking record' do
expect do
model.revert_backfill_conversion_of_integer_to_bigint(table, columns, primary_key: primary_key)
end.to change { Gitlab::Database::BackgroundMigration::BatchedMigration.count }.by(-1)
end
end
end
describe '#index_exists_by_name?' do
it 'returns true if an index exists' do
ActiveRecord::Base.connection.execute(
......
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