FIX: Cater for polls that can have multiple votes per user (#22297)

Cater for polls that can have multiple votes per user.

This fixes an older UserMerge and migration which was intended to
de-duplicate poll votes but did not account for "multiple" type polls.
This commit is contained in:
Natalie Tay
2023-06-27 18:58:16 +08:00
committed by GitHub
parent 78d8bd7c81
commit 1384ba5a4e
5 changed files with 118 additions and 23 deletions

View File

@ -171,15 +171,13 @@ after_initialize do
on(:merging_users) do |source_user, target_user|
DB.exec(<<-SQL, source_user_id: source_user.id, target_user_id: target_user.id)
DELETE FROM poll_votes
WHERE poll_id IN (
SELECT poll_id
FROM poll_votes
WHERE user_id = :source_user_id
INTERSECT
SELECT poll_id
WHERE user_id = :source_user_id
AND EXISTS (
SELECT 1
FROM poll_votes
WHERE user_id = :target_user_id
) AND user_id = :source_user_id;
AND poll_votes.poll_id = poll_votes.poll_id
);
UPDATE poll_votes
SET user_id = :target_user_id