discourse/db/migrate/20191128222140_add_unique_index_to_developers.rb
2023-01-09 11:59:41 +00:00

25 lines
502 B
Ruby

# frozen_string_literal: true
class AddUniqueIndexToDevelopers < ActiveRecord::Migration[6.0]
def up
execute <<~SQL
DELETE FROM developers d1
USING (
SELECT MAX(id) as id, user_id
FROM developers
GROUP BY user_id
HAVING COUNT(*) > 1
) d2
WHERE
d1.user_id = d2.user_id AND
d1.id <> d2.id
SQL
add_index :developers, %i[user_id], unique: true
end
def down
raise ActiveRecord::IrreversibleMigration
end
end