Commit 04e86156 authored by Krasimir Angelov's avatar Krasimir Angelov

Add revert_initialize_conversion_of_integer_to_bigint migration helper

for easier revert of `initialize_conversion_of_integer_to_bigint`.

See https://gitlab.com/gitlab-org/gitlab/-/issues/328600.
parent 2188d104
...@@ -969,6 +969,20 @@ module Gitlab ...@@ -969,6 +969,20 @@ module Gitlab
end end
end end
# Reverts `initialize_conversion_of_integer_to_bigint`
#
# table - The name of the database table containing the columns
# columns - The name, or array of names, of the column(s) that we're converting to bigint.
def revert_initialize_conversion_of_integer_to_bigint(table, columns)
columns = Array.wrap(columns)
temporary_columns = columns.map { |column| "#{column}_convert_to_bigint" }
trigger_name = rename_trigger_name(table, columns, temporary_columns)
remove_rename_triggers_for_postgresql(table, trigger_name)
temporary_columns.each { |column| remove_column(table, column) }
end
# Backfills the new columns used in an integer-to-bigint conversion using background migrations. # Backfills the new columns used in an integer-to-bigint conversion using background migrations.
# #
# - This helper should be called from a post-deployment migration. # - This helper should be called from a post-deployment migration.
......
...@@ -4,6 +4,7 @@ require 'spec_helper' ...@@ -4,6 +4,7 @@ require 'spec_helper'
RSpec.describe Gitlab::Database::MigrationHelpers do RSpec.describe Gitlab::Database::MigrationHelpers do
include Database::TableSchemaHelpers include Database::TableSchemaHelpers
include Database::TriggerHelpers
let(:model) do let(:model) do
ActiveRecord::Migration.new.extend(described_class) ActiveRecord::Migration.new.extend(described_class)
...@@ -1791,6 +1792,48 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -1791,6 +1792,48 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
end end
end end
describe '#revert_initialize_conversion_of_integer_to_bigint' do
let(:table) { :test_table }
before do
model.create_table table, id: false do |t|
t.integer :id, primary_key: true
t.integer :other_id
end
model.initialize_conversion_of_integer_to_bigint(table, columns)
end
context 'when single column is given' do
let(:columns) { :id }
it 'removes column, trigger, and function' do
trigger_name = model.rename_trigger_name(table, :id, :id_convert_to_bigint)
model.revert_initialize_conversion_of_integer_to_bigint(table, columns)
expect(model.column_exists?(table, :id_convert_to_bigint)).to eq(false)
expect_trigger_not_to_exist(table, trigger_name)
expect_function_not_to_exist(trigger_name)
end
end
context 'when multiple columns are given' do
let(:columns) { [:id, :other_id] }
it 'removes column, trigger, and function' do
trigger_name = model.rename_trigger_name(table, columns, [:id_convert_to_bigint, :other_id_convert_to_bigint])
model.revert_initialize_conversion_of_integer_to_bigint(table, columns)
expect(model.column_exists?(table, :id_convert_to_bigint)).to eq(false)
expect(model.column_exists?(table, :other_id_convert_to_bigint)).to eq(false)
expect_trigger_not_to_exist(table, trigger_name)
expect_function_not_to_exist(trigger_name)
end
end
end
describe '#backfill_conversion_of_integer_to_bigint' do describe '#backfill_conversion_of_integer_to_bigint' do
let(:table) { :_test_backfill_table } let(:table) { :_test_backfill_table }
let(:column) { :id } let(:column) { :id }
......
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