OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
openproject/db/migrate/20190502102512_ensure_postg...

38 lines
1.1 KiB

class EnsurePostgresIndexNames < ActiveRecord::Migration[5.2]
disable_ddl_transaction!
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"
return
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_schema IN (select current_schema())
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']
ActiveRecord::Base.transaction do
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
end
def down
# Nothing to do
end
end