Commit 4da76958 authored by Krasimir Angelov's avatar Krasimir Angelov

Update swap_column_names to execute ALTER TABLE directly

instead of using `rename_column` which has side effects (renames
_some_ indexes).

Releated to https://gitlab.com/gitlab-org/gitlab/-/issues/288005.
parent b4c7e08d
......@@ -40,7 +40,7 @@ class FinalizePushEventPayloadsBigintConversion < ActiveRecord::Migration[6.1]
# Swap PK constraint
execute "ALTER TABLE #{TABLE_NAME} DROP CONSTRAINT push_event_payloads_pkey"
rename_index TABLE_NAME, index_name(TABLE_NAME, column: :event_id), 'push_event_payloads_pkey'
rename_index TABLE_NAME, INDEX_NAME, 'push_event_payloads_pkey'
execute "ALTER TABLE #{TABLE_NAME} ADD CONSTRAINT push_event_payloads_pkey PRIMARY KEY USING INDEX push_event_payloads_pkey"
# Drop FK fk_36c74129da
......
......@@ -1622,9 +1622,9 @@ into similar problems in the future (e.g. when new tables are created).
end
temp_name = "#{column_1}_tmp"
rename_column(table_name, column_1, temp_name)
rename_column(table_name, column_2, column_1)
rename_column(table_name, temp_name, column_2)
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
private
......
......@@ -2995,6 +2995,8 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
t.integer :column_1
t.bigint :column_2
end
model.add_index(table, :column_2)
end
after do
......@@ -3011,6 +3013,13 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
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)
......
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