Commit 73b764b8 authored by Krasimir Angelov's avatar Krasimir Angelov

Remove swap_column_names migration helper

as per code review.

https://gitlab.com/gitlab-org/gitlab/-/merge_requests/64577.
parent f5157ae5
...@@ -32,7 +32,11 @@ class FinalizePushEventPayloadsBigintConversion < ActiveRecord::Migration[6.1] ...@@ -32,7 +32,11 @@ class FinalizePushEventPayloadsBigintConversion < ActiveRecord::Migration[6.1]
add_concurrent_foreign_key TABLE_NAME, :events, column: :event_id_convert_to_bigint, on_delete: :cascade add_concurrent_foreign_key TABLE_NAME, :events, column: :event_id_convert_to_bigint, on_delete: :cascade
with_lock_retries(raise_on_exhaustion: true) do with_lock_retries(raise_on_exhaustion: true) do
swap_column_names TABLE_NAME, :event_id, :event_id_convert_to_bigint # rubocop:disable Migration/WithLockRetriesDisallowedMethod # Swap column names
temp_name = 'event_id_tmp'
execute "ALTER TABLE #{quote_table_name(TABLE_NAME)} RENAME COLUMN #{quote_column_name(:event_id)} TO #{quote_column_name(temp_name)}"
execute "ALTER TABLE #{quote_table_name(TABLE_NAME)} RENAME COLUMN #{quote_column_name(:event_id_convert_to_bigint)} TO #{quote_column_name(:event_id)}"
execute "ALTER TABLE #{quote_table_name(TABLE_NAME)} RENAME COLUMN #{quote_column_name(temp_name)} TO #{quote_column_name(:event_id_convert_to_bigint)}"
# Swap defaults # Swap defaults
change_column_default TABLE_NAME, :event_id, nil change_column_default TABLE_NAME, :event_id, nil
......
...@@ -1616,17 +1616,6 @@ into similar problems in the future (e.g. when new tables are created). ...@@ -1616,17 +1616,6 @@ into similar problems in the future (e.g. when new tables are created).
raise raise
end end
def swap_column_names(table_name, column_1, column_2)
unless transaction_open?
raise 'Cannot call swap_column_names without a transaction open or outside of a transaction block.'
end
temp_name = "#{column_1}_tmp"
execute "ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(column_1)} TO #{quote_column_name(temp_name)}"
execute "ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(column_2)} TO #{quote_column_name(column_1)}"
execute "ALTER TABLE #{quote_table_name(table_name)} RENAME COLUMN #{quote_column_name(temp_name)} TO #{quote_column_name(column_2)}"
end
def rename_constraint(table_name, old_name, new_name) def rename_constraint(table_name, old_name, new_name)
execute <<~SQL execute <<~SQL
ALTER TABLE #{quote_table_name(table_name)} ALTER TABLE #{quote_table_name(table_name)}
......
...@@ -2978,53 +2978,6 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -2978,53 +2978,6 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
end end
end end
describe '#swap_column_names' do
let(:table) { :_test_table }
let(:ar_model) do
Class.new(ActiveRecord::Base) do
self.table_name = :_test_table
end
end
before do
model.drop_table table, if_exists: true
model.create_table table, id: false do |t|
t.integer :column_1
t.bigint :column_2
end
model.add_index(table, :column_2)
end
after do
model.drop_table table, if_exists: true
end
it 'swaps column names as requested' do
model.swap_column_names(table, :column_1, :column_2)
column_1 = ar_model.column_for_attribute(:column_1)
column_2 = ar_model.column_for_attribute(:column_2)
expect(column_1.sql_type).to eq('bigint')
expect(column_2.sql_type).to eq('integer')
end
it 'does not rename indexes' do
model.swap_column_names(table, :column_1, :column_2)
index = model.indexes_for(table, :column_1).first
expect(index.name).to eq("index_#{table}_on_column_2")
end
it 'raises an error when not in transaction' do
expect(model).to receive(:transaction_open?).and_return(false)
expect { model.swap_column_names(table, :column_1, :column_2) }
.to raise_error 'Cannot call swap_column_names without a transaction open or outside of a transaction block.'
end
end
describe '#rename_constraint' do describe '#rename_constraint' do
it "executes the statement to rename constraint" do it "executes the statement to rename constraint" do
expect(model).to receive(:execute).with /ALTER TABLE "test_table"\nRENAME CONSTRAINT "fk_old_name" TO "fk_new_name"/ expect(model).to receive(:execute).with /ALTER TABLE "test_table"\nRENAME CONSTRAINT "fk_old_name" TO "fk_new_name"/
......
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