Add migration to rename primary keys

[ci skip]
pull/7269/head
Oliver Günther 6 years ago
parent 85c20790b5
commit a4f8205c23
No known key found for this signature in database
GPG Key ID: A3A8BDAD7C0C552C
  1. 32
      db/migrate/20190502102512_ensure_postgres_index_names.rb
  2. 13
      docker/mysql-to-postgres/bin/migrate-mysql-to-postgres

@ -0,0 +1,32 @@
class EnsurePostgresIndexNames < ActiveRecord::Migration[5.2]
def up
if OpenProject::Database.mysql?
warn "You're using MySQL, skipping index renaming. You will need to re-run this when switching to PostgreSQL"
end
sql = <<~SQL
SELECT
FORMAT('%s_pkey', table_name) as new_name,
constraint_name as old_name
FROM information_schema.table_constraints
WHERE UPPER(constraint_type) = 'PRIMARY KEY'
AND constraint_name != FORMAT('%s_pkey', table_name)
ORDER BY table_name;
SQL
ActiveRecord::Base.connection.execute(sql).each do |entry|
old_name = entry['old_name']
new_name = entry['new_name']
begin
execute %(ALTER INDEX "#{old_name}" RENAME TO #{new_name};)
rescue StandardError => e
warn "Failed to rename index #{old_name} to #{new_name}: #{e.message}. Skipping"
end
end
end
def down
# Nothing to do
end
end

@ -131,6 +131,19 @@ if check_status != 0
exit 1
end
# We need to ensure primary keys get renamed to `<table_name>_pkey`
# because pgloader uses unique index names and we will otherwise be unable
# to rename them in the future
puts "Renaming primary keys"
_, redo_status = run "bundle exec rake db:migrate:redo VERSION=20190502102512", record_output: false
if redo_status != 0
puts "\nRenaming of indices for PostgreSQL failed. See above."
exit 1
end
# if the version was present on MySQL already we need to redo it to
# add the postgres specific columns.
needs_fulltext_migration = check_output.include?("true")

Loading…
Cancel
Save