2023-06-14 23:18:51 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class DeleteDuplicatePollVotes < ActiveRecord::Migration[7.0]
|
|
|
|
def up
|
|
|
|
execute <<~SQL
|
2023-06-27 06:58:16 -04:00
|
|
|
DELETE FROM poll_votes
|
|
|
|
WHERE (poll_id, user_id, poll_option_id) IN (
|
|
|
|
SELECT pv.poll_id, pv.user_id, pv.poll_option_id
|
|
|
|
FROM poll_votes pv
|
|
|
|
JOIN polls p ON pv.poll_id = p.id
|
|
|
|
WHERE p.type = 0
|
|
|
|
AND EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM poll_votes pv2
|
|
|
|
WHERE pv.poll_id = pv2.poll_id
|
|
|
|
AND pv.user_id = pv2.user_id
|
|
|
|
AND pv.created_at < pv2.created_at
|
2023-06-14 23:18:51 -04:00
|
|
|
)
|
2023-06-27 06:58:16 -04:00
|
|
|
);
|
2023-06-14 23:18:51 -04:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
raise ActiveRecord::IrreversibleMigration
|
|
|
|
end
|
|
|
|
end
|