Skip to content

Commit

Permalink
added migration
Browse files Browse the repository at this point in the history
  • Loading branch information
PavloButynets committed Dec 17, 2024
1 parent eba059c commit 89edc7e
Showing 1 changed file with 21 additions and 0 deletions.
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}`)
}
},

async down() {}
}

0 comments on commit 89edc7e

Please sign in to comment.