From 800b9ed492206c02a7378acc979d6737f45b54c8 Mon Sep 17 00:00:00 2001 From: JandereDev Date: Thu, 26 May 2022 18:04:42 +0200 Subject: [PATCH] add script to delete message ids from unlinked channels from db --- bridge/src/data_deletion.ts | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 bridge/src/data_deletion.ts diff --git a/bridge/src/data_deletion.ts b/bridge/src/data_deletion.ts new file mode 100644 index 0000000..b1894bb --- /dev/null +++ b/bridge/src/data_deletion.ts @@ -0,0 +1,50 @@ +/** + * When executed, this file scans the entire `messages` database collection + * and deletes entries belonging to channels which do no longer have a bridge + * configuration associated. Reads mongo URI from $DB_STRING env var. + */ + +import Mongo from 'mongodb'; + +if (!process.env.DB_STRING) { + console.error('$DB_STRING not provided.'); + process.exit(1); +} + +const mongo = new Mongo.MongoClient(process.env.DB_STRING); + +(async () => { + await mongo.connect(); + const client = mongo.db(); + const messages = client.collection('bridged_messages'); + + const res = messages.aggregate([{ + $lookup: { + from: 'bridge_config', + localField: 'channels.discord', + foreignField: 'discord', + as: 'bridgeConfig', + } + }]); + + let buf: string[] = []; + + const execute = async () => { + const ids = [ ...buf ]; + buf.length = 0; + + if (ids.length) { + console.log('Deleting ' + ids.length + ' entries'); + await messages.deleteMany({ _id: { $in: ids } }); + } + } + + res.on('data', data => { + if (!data.bridgeConfig?.length) buf.push(data._id); + if (buf.length >= 500) execute(); + }); + + res.on('end', () => { + execute().then(() => process.exit(0)); + }); +})();