Commit 6e5fc9ee authored by Krasimir Angelov's avatar Krasimir Angelov

Extract generic rename_constraint helper

To be used for PK conversion.

Related to https://gitlab.com/gitlab-org/gitlab/-/issues/288005.
parent 4da76958
......@@ -46,11 +46,11 @@ class FinalizePushEventPayloadsBigintConversion < ActiveRecord::Migration[6.1]
# Drop FK fk_36c74129da
remove_foreign_key TABLE_NAME, name: concurrent_foreign_key_name(TABLE_NAME, :event_id)
# Change the name of the FK for event_id_convert_to_bigint to the FK name for event_id
execute <<~SQL
ALTER TABLE #{TABLE_NAME}
RENAME CONSTRAINT #{concurrent_foreign_key_name(TABLE_NAME, :event_id_convert_to_bigint)}
TO #{concurrent_foreign_key_name(TABLE_NAME, :event_id)}
SQL
rename_constraint(
TABLE_NAME,
concurrent_foreign_key_name(TABLE_NAME, :event_id_convert_to_bigint),
concurrent_foreign_key_name(TABLE_NAME, :event_id)
)
end
end
end
......@@ -1627,6 +1627,13 @@ into similar problems in the future (e.g. when new tables are created).
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)
execute <<~SQL
ALTER TABLE #{quote_table_name(table_name)}
RENAME CONSTRAINT #{quote_column_name(old_name)} TO #{quote_column_name(new_name)}
SQL
end
private
def validate_check_constraint_name!(constraint_name)
......
......@@ -22,6 +22,7 @@ module RuboCop
remove_foreign_key_if_exists
remove_foreign_key_without_error
rename_index
rename_constraint
table_exists?
index_exists_by_name?
foreign_key_exists?
......
......@@ -3027,4 +3027,12 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
.to raise_error 'Cannot call swap_column_names without a transaction open or outside of a transaction block.'
end
end
describe '#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"/
model.rename_constraint(:test_table, :fk_old_name, :fk_new_name)
end
end
end
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