Skip to content

Commit f991dd8

Browse files
author
Sachin Maheshwari
committed
Manually corrected duplicate broadcast rows in database with these SQLs.
1 parent 6efe6ce commit f991dd8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

migrations/v2.0.4.sql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
-- query to identify the duplicate rows in bulk_message_user_refs table
3+
SELECT count(*), bulk_message_id, user_id FROM "bulk_message_user_refs"
4+
GROUP BY bulk_message_id, user_id HAVING count(*) > 1;
5+
6+
-- create temp table and store duplicate broadcast notification rows
7+
SELECT * INTO temptable FROM "Notifications" WHERE id IN
8+
(
9+
SELECT a.notification_id FROM "bulk_message_user_refs" AS "a",
10+
"bulk_message_user_refs" AS "b"
11+
WHERE a.id < b.id
12+
AND a.bulk_message_id = b.bulk_message_id
13+
AND a.user_id = b.user_id
14+
);
15+
16+
-- DELETE duplicate rows from bulk_message_user_refs table
17+
DELETE FROM "bulk_message_user_refs" AS "a"
18+
USING "bulk_message_user_refs" AS "b"
19+
WHERE a.id < b.id
20+
AND a.bulk_message_id = b.bulk_message_id
21+
AND a.user_id = b.user_id;
22+
23+
-- DELETE duplicate rows from Notifications
24+
DELETE FROM "Notifications"
25+
WHERE id IN (SELECT id FROM temptable)
26+
AND type = 'admin.notification.broadcast';
27+
28+
-- make unique column to avoid duplicate insert
29+
ALTER TABLE bulk_message_user_refs ADD UNIQUE (bulk_message_id, user_id);

0 commit comments

Comments
 (0)