Skip to content

Commit

Permalink
chore: adding 1st migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Burtey committed Dec 20, 2023
1 parent 8b22994 commit 1928f0e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions core/api/src/migrations/20231220170640-quiz-new-collection-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint @typescript-eslint/ban-ts-comment: "off" */
// @ts-nocheck
async function migrateAccounts(db, batchSize = 100) {
const cursor = db.collection("accounts").find()

let batchCount = 0
while (await cursor.hasNext()) {
const quizUpdates = []
for (let i = 0; i < batchSize && (await cursor.hasNext()); i++) {
const account = await cursor.next()
for (const quizId of account.earn) {
quizUpdates.push({
create: {
accountId: account._id,
quizId,
createAt: new Date(),
},
})
}
}

batchCount += quizUpdates.length

if (quizUpdates.length > 0) {
await db.collection("quizs").bulkWrite(quizUpdates)

Check warning on line 25 in core/api/src/migrations/20231220170640-quiz-new-collection-1.ts

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"quizs" should be "quizzes".
console.log(`Processed ${batchCount} accounts`)
}
}

console.log(`Processed ${batchCount} accounts`)
}

module.exports = {
async up(db) {
console.log("Begin migration to Quiz collection")
await migrateAccounts(db)
console.log("Migration completed")
},
}

0 comments on commit 1928f0e

Please sign in to comment.