Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added migration to delete documents with non-existent author fields #1071

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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}`)
luiqor marked this conversation as resolved.
Show resolved Hide resolved
}
},

async down() {}
}
Original file line number Diff line number Diff line change
@@ -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)
}
})
})
Loading