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 21, 2023
1 parent 316627b commit 639abc3
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 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,44 @@
/* 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()
const uniqueEarn = [...new Set(account.earn)]
for (const quizId of uniqueEarn) {
quizUpdates.push({
insertOne: {
accountId: account.id,
quizId,
createAt: new Date(),
},
})
}
}

batchCount += quizUpdates.length

if (quizUpdates.length > 0) {
await db.collection("quizs").bulkWrite(quizUpdates)
console.log(`Processed ${batchCount} accounts`)
}
}

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

module.exports = {
async up(db) {
await db
.collection("quizs")
.createIndex({ accountId: 1, quizId: 1 }, { unique: true })

console.log("Begin migration to Quiz collection")
await migrateAccounts(db)
console.log("Migration completed")
},
}

0 comments on commit 639abc3

Please sign in to comment.