From f991dd85f5469ec075a0126e5f10f031a1254d88 Mon Sep 17 00:00:00 2001 From: Sachin Maheshwari Date: Wed, 3 Jun 2020 19:50:19 +0530 Subject: [PATCH 1/3] Manually corrected duplicate broadcast rows in database with these SQLs. --- migrations/v2.0.4.sql | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 migrations/v2.0.4.sql diff --git a/migrations/v2.0.4.sql b/migrations/v2.0.4.sql new file mode 100644 index 0000000..97d25a9 --- /dev/null +++ b/migrations/v2.0.4.sql @@ -0,0 +1,29 @@ + +-- query to identify the duplicate rows in bulk_message_user_refs table +SELECT count(*), bulk_message_id, user_id FROM "bulk_message_user_refs" + GROUP BY bulk_message_id, user_id HAVING count(*) > 1; + +-- create temp table and store duplicate broadcast notification rows +SELECT * INTO temptable FROM "Notifications" WHERE id IN + ( + SELECT a.notification_id FROM "bulk_message_user_refs" AS "a", + "bulk_message_user_refs" AS "b" + WHERE a.id < b.id + AND a.bulk_message_id = b.bulk_message_id + AND a.user_id = b.user_id + ); + +-- DELETE duplicate rows from bulk_message_user_refs table +DELETE FROM "bulk_message_user_refs" AS "a" + USING "bulk_message_user_refs" AS "b" + WHERE a.id < b.id + AND a.bulk_message_id = b.bulk_message_id + AND a.user_id = b.user_id; + +-- DELETE duplicate rows from Notifications +DELETE FROM "Notifications" +WHERE id IN (SELECT id FROM temptable) +AND type = 'admin.notification.broadcast'; + +-- make unique column to avoid duplicate insert +ALTER TABLE bulk_message_user_refs ADD UNIQUE (bulk_message_id, user_id); From 9b337d7562f22fd4508eeda3737a297ffe087a85 Mon Sep 17 00:00:00 2001 From: Sachin Maheshwari Date: Thu, 4 Jun 2020 11:47:21 +0530 Subject: [PATCH 2/3] deleting duplicate broadcast row if inserted in notifications table --- src/hooks/hookBulkMessage.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/hooks/hookBulkMessage.js b/src/hooks/hookBulkMessage.js index b4006f1..3026bf3 100644 --- a/src/hooks/hookBulkMessage.js +++ b/src/hooks/hookBulkMessage.js @@ -116,9 +116,13 @@ async function isBroadCastMessageForUser(userId, bulkMessage, memberInfo, userGr * * @param {Integer} userId * @param {Integer} bulkMessageId - * @param {Integer} notificationId + * @param {Object} notificationObj */ -async function insertUserRefs(userId, bulkMessageId, notificationId) { +async function insertUserRefs(userId, bulkMessageId, notificationObj) { + let notificationId = null + if (notificationObj) { + notificationId = notificationObj.id + } try { const r = await models.BulkMessageUserRefs.create({ bulk_message_id: bulkMessageId, @@ -129,6 +133,15 @@ async function insertUserRefs(userId, bulkMessageId, notificationId) { return r } catch (e) { logger.error(`${logPrefix} Failed to insert userRef record for user: ${userId}, error: ${e}`) + if (notificationId && notificationObj) { + try { + await notificationObj.destroy() + logger.info(`Deleted duplicate/ref-transaction failed, broadcast notification ${notificationId} for user: ${userid}`) + } catch (error) { + logger.error(`Error in deleting duplicate notification record, ${error}`) + } + + } throw new Error(`insertUserRefs() : ${e}`) } } @@ -155,7 +168,7 @@ async function createNotificationForUser(userId, bulkMessage) { }) logger.info(`${logPrefix} Inserted notification record ${n.id} for current user ${userId}`) // TODO need to be in transaction so that rollback will be possible - const result = await insertUserRefs(userId, bulkMessage.id, n.id) + const result = await insertUserRefs(userId, bulkMessage.id, n) return result } catch (e) { logger.error(`${logPrefix} insert broadcast notification error: ${e} `) From b14e31419460c917ef7b2a95afaa9c15feba5741 Mon Sep 17 00:00:00 2001 From: Sachin Maheshwari Date: Thu, 4 Jun 2020 13:40:11 +0530 Subject: [PATCH 3/3] typo and adding sql query to get duplicate broadcast notifications --- migrations/v2.0.4.sql | 8 ++++++++ src/hooks/hookBulkMessage.js | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/migrations/v2.0.4.sql b/migrations/v2.0.4.sql index 97d25a9..db9a0b8 100644 --- a/migrations/v2.0.4.sql +++ b/migrations/v2.0.4.sql @@ -27,3 +27,11 @@ AND type = 'admin.notification.broadcast'; -- make unique column to avoid duplicate insert ALTER TABLE bulk_message_user_refs ADD UNIQUE (bulk_message_id, user_id); + +-- get duplicate broadcast rows +SELECT * FROM "Notifications" AS a + LEFT JOIN "bulk_message_user_refs" AS b + ON a.id=b.notification_id + WHERE a.type='admin.notification.broadcast' + AND b.id IS NULL; + diff --git a/src/hooks/hookBulkMessage.js b/src/hooks/hookBulkMessage.js index 3026bf3..70801c0 100644 --- a/src/hooks/hookBulkMessage.js +++ b/src/hooks/hookBulkMessage.js @@ -136,7 +136,7 @@ async function insertUserRefs(userId, bulkMessageId, notificationObj) { if (notificationId && notificationObj) { try { await notificationObj.destroy() - logger.info(`Deleted duplicate/ref-transaction failed, broadcast notification ${notificationId} for user: ${userid}`) + logger.info(`Deleted/reverted duplicate/ref-transaction failed, broadcast notification ${notificationId} for user: ${userId}`) } catch (error) { logger.error(`Error in deleting duplicate notification record, ${error}`) }