From 83c41f54f28064cc756aedc357a935d61e12418d Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Thu, 29 Feb 2024 16:32:33 +0100 Subject: [PATCH] fix: [vps] add more code to drop indexes and mitigate some migration issues --- bin/vps | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/bin/vps b/bin/vps index 265e327..d8f9a4a 100755 --- a/bin/vps +++ b/bin/vps @@ -887,8 +887,44 @@ export -f cyclos:unlock rocketchat:drop-indexes() { local project_name="$1" dbname="$2" - echo "db.users.dropIndexes()" | - compose:mongo "${project_name}" "${dbname}" + compose:mongo "${project_name}" "${dbname}" <<'EOF' +db.users.dropIndexes(); +// Check if the 'rocketchat_uploads' collection exists +var collections = db.getCollectionNames(); +if (collections.indexOf('rocketchat_uploads') !== -1) { + db.rocketchat_uploads.dropIndexes(); +} +if (collections.indexOf('rocketchat_read_receipts') !== -1) { + db.rocketchat_read_receipts.dropIndexes(); + var duplicates = []; + + db.getCollection("rocketchat_read_receipts").aggregate([ + { + "$group": { + "_id": { "roomId": "$roomId", "userId": "$userId", "messageId": "$messageId" }, + "uniqueIds": { "$addToSet": "$_id" }, + "count": { "$sum": 1 } + } + }, + { "$match": { "count": { "$gt": 1 } } } + ], + { allowDiskUse: true } + ).forEach(function (doc) { + // remove 1st element + doc.uniqueIds.shift(); + doc.uniqueIds.forEach(function (dupId) { + duplicates.push(dupId); + } + ) + }) + + // printjson(duplicates); + + db.getCollection("rocketchat_read_receipts").remove({ _id: { $in: duplicates } }); +} + +EOF + } export -f rocketchat:drop-indexes