-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicolas Burtey
committed
Dec 21, 2023
1 parent
316627b
commit 639abc3
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
core/api/src/migrations/20231220170640-quiz-new-collection-1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}, | ||
} |