From 89edc7ed0428b31cf3364c16d7155e45cad1b544 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 17 Dec 2024 03:16:08 +0200 Subject: [PATCH 1/3] added migration --- ...emove-documents-with-non-existent-users.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 migrations/20241216212401-remove-documents-with-non-existent-users.js diff --git a/migrations/20241216212401-remove-documents-with-non-existent-users.js b/migrations/20241216212401-remove-documents-with-non-existent-users.js new file mode 100644 index 00000000..678954df --- /dev/null +++ b/migrations/20241216212401-remove-documents-with-non-existent-users.js @@ -0,0 +1,21 @@ +module.exports = { + async up(db) { + const allUsers = await db + .collection('users') + .find({}, { projection: { _id: 1 } }) + .toArray() + const allUserIds = allUsers.map((user) => user._id.toString()) + const collections = ['attachments', 'courses', 'lessons', 'notes', 'questions', 'quizzes', 'resourcescategories'] + for (const collectionName of collections) { + const authorsFromCollection = await db.collection(collectionName).distinct('author') + const nonExistingUsers = await authorsFromCollection.filter((userId) => !allUserIds.includes(userId.toString())) + + if (nonExistingUsers.length > 0) { + await db.collection(collectionName).deleteMany({ author: { $in: nonExistingUsers } }) + } + console.log(`Removed ${nonExistingUsers.length} non-existing users from collection ${collectionName}`) + } + }, + + async down() {} +} From 980ef2aa515bd541270d4189e3c1094ba607a05a Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 17 Dec 2024 17:57:13 +0200 Subject: [PATCH 2/3] implemented tests for migration --- ...-documents-with-non-existent-users.spec.js | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/test/migrations/20241216212401-remove-documents-with-non-existent-users.spec.js diff --git a/src/test/migrations/20241216212401-remove-documents-with-non-existent-users.spec.js b/src/test/migrations/20241216212401-remove-documents-with-non-existent-users.spec.js new file mode 100644 index 00000000..b7c0e186 --- /dev/null +++ b/src/test/migrations/20241216212401-remove-documents-with-non-existent-users.spec.js @@ -0,0 +1,62 @@ +const { MongoClient, ObjectId } = require('mongodb') + +const { up } = require('@root/migrations/20241216212401-remove-documents-with-non-existent-users') + +require('~/initialization/envSetup') +const { + config: { MONGODB_URL } +} = require('~/configs/config') + +const collections = ['attachments', 'courses', 'lessons', 'notes', 'questions', 'quizzes', 'resourcescategories'] +const existingUser = { _id: new ObjectId(), firstName: 'Test', lastName: 'User' } +const fakeUser = new ObjectId() + +const url = MONGODB_URL.slice(0, MONGODB_URL.lastIndexOf('/')) +const databaseName = MONGODB_URL.slice(MONGODB_URL.lastIndexOf('/') + 1) + +describe('20241216212401-remove-documents-with-non-existent-users', () => { + let client, database + + beforeAll(() => { + client = new MongoClient(url) + database = client.db(databaseName) + }) + + afterAll(() => { + client.close() + }) + + it('should remove documents with non-existing users from all collections', async () => { + for (const collectionName of collections) { + await database.collection(collectionName).insertOne({ author: fakeUser.toString() }) + } + + await up(database) + + for (const collectionName of collections) { + const docsWithNonExistingUsers = await database + .collection(collectionName) + .find({ author: fakeUser.toString() }) + .toArray() + expect(docsWithNonExistingUsers).toHaveLength(0) + } + }) + + it('should not remove documents with existing users from all collections', async () => { + await database.collection('users').insertOne(existingUser) + + for (const collectionName of collections) { + await database.collection(collectionName).insertOne({ author: existingUser._id.toString() }) + } + + await up(database) + + for (const collectionName of collections) { + const docsWithNonExistingUsers = await database + .collection(collectionName) + .find({ author: existingUser._id.toString() }) + .toArray() + expect(docsWithNonExistingUsers).toHaveLength(1) + } + }) +}) From a9f09136c633468f0ba52e0d544b487299fce8a2 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 18 Dec 2024 14:28:37 +0200 Subject: [PATCH 3/3] removed console.log --- .../20241216212401-remove-documents-with-non-existent-users.js | 1 - 1 file changed, 1 deletion(-) diff --git a/migrations/20241216212401-remove-documents-with-non-existent-users.js b/migrations/20241216212401-remove-documents-with-non-existent-users.js index 678954df..701b1bcf 100644 --- a/migrations/20241216212401-remove-documents-with-non-existent-users.js +++ b/migrations/20241216212401-remove-documents-with-non-existent-users.js @@ -13,7 +13,6 @@ module.exports = { if (nonExistingUsers.length > 0) { await db.collection(collectionName).deleteMany({ author: { $in: nonExistingUsers } }) } - console.log(`Removed ${nonExistingUsers.length} non-existing users from collection ${collectionName}`) } },