retrospring/db/migrate/20231220100445_remove_duplicate_reactions.rb
2023-12-20 10:28:49 +00:00

20 lines
548 B
Ruby

# frozen_string_literal: true
class RemoveDuplicateReactions < ActiveRecord::Migration[7.0]
def up
execute <<~SQUIRREL
DELETE FROM reactions
WHERE id IN (
SELECT id FROM (
SELECT id, row_number() over (PARTITION BY parent_type, parent_id, user_id ORDER BY id) AS row_number FROM reactions
)s WHERE row_number >= 2
)
SQUIRREL
add_index :reactions, %i[parent_type parent_id user_id], unique: true
end
def down
remove_index :reactions, %i[parent_type parent_id user_id]
end
end