From 89cd5adb0f6c8d6769d678d1b3b0f096e0ccab65 Mon Sep 17 00:00:00 2001 From: Miodec Date: Sat, 5 Jun 2021 20:56:55 +0100 Subject: [PATCH] more logs to see whats going on --- backend/migrate.js | 91 +++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/backend/migrate.js b/backend/migrate.js index b7e13940c..05a9d086e 100644 --- a/backend/migrate.js +++ b/backend/migrate.js @@ -24,53 +24,60 @@ mongoose.connect("mongodb://localhost:27017/monkeytype", { // Migrate users userCount = 1; db.collection("users") + // .where("name","==","mio") .get() .then((querySnapshot) => { + // console.log('start of foreach'); querySnapshot.forEach((userDoc) => { - let newUser = new User(userDoc.data()); - newUser.uid = userDoc.id; - newUser.globalStats = { - started: userDoc.data().startedTests, - completed: userDoc.data().completedTests, - time: userDoc.data().timeTyping, - }; - let tagIdDict = {}; - db.collection(`users/${userDoc.id}/tags`) - .get() - .then((tagsSnapshot) => { - tagsSnapshot.forEach((tagDoc) => { - let formattedTag = tagDoc.data(); - formattedTag._id = mongoose.Types.ObjectId(); //generate new objectId - tagIdDict[tagDoc.id] = formattedTag._id; //save pair of ids in memory to determine what to set new id as in result tags - newUser.tags.push(formattedTag); - }); - db.collection(`users/${userDoc.id}/results`) - .get() - .then((resultsSnapshot) => { - resultsSnapshot.forEach((result) => { - let formattedResult = result.data(); - formattedResult.tags.forEach((tag, index) => { - if (tagIdDict[tag]) - formattedResult.tags[index] = tagIdDict[tag]; - }); - newUser.results.push(formattedResult); - }); - newUser.results.sort((a, b) => { - return a.timestamp - b.timestamp; - }); - db.collection(`users/${userDoc.id}/presets`) - .get() - .then((presetsSnapshot) => { - presetsSnapshot.forEach((preset) => { - newUser.presets.push(preset.data()); - }); - newUser.save(); - console.log("User", userCount, "saved"); - userCount++; - }); + let newUser = new User(userDoc.data()); + newUser.uid = userDoc.id; + newUser.globalStats = { + started: userDoc.data().startedTests, + completed: userDoc.data().completedTests, + time: userDoc.data().timeTyping, + }; + let tagIdDict = {}; + db.collection(`users/${userDoc.id}/tags`) + .get() + .then((tagsSnapshot) => { + tagsSnapshot.forEach((tagDoc) => { + let formattedTag = tagDoc.data(); + formattedTag._id = mongoose.Types.ObjectId(); //generate new objectId + tagIdDict[tagDoc.id] = formattedTag._id; //save pair of ids in memory to determine what to set new id as in result tags + newUser.tags.push(formattedTag); + console.log(`Tag ${tagDoc.id} saved for user ${userCount}`); }); - }); + db.collection(`users/${userDoc.id}/results`) + .get() + .then((resultsSnapshot) => { + let resCount = 1; + resultsSnapshot.forEach((result) => { + let formattedResult = result.data(); + formattedResult.tags.forEach((tag, index) => { + if (tagIdDict[tag]) + formattedResult.tags[index] = tagIdDict[tag]; + }); + newUser.results.push(formattedResult); + console.log(`Result ${resCount} saved for user ${userCount}`); + resCount++; + }); + newUser.results.sort((a, b) => { + return a.timestamp - b.timestamp; + }); + db.collection(`users/${userDoc.id}/presets`) + .get() + .then((presetsSnapshot) => { + presetsSnapshot.forEach((preset) => { + newUser.presets.push(preset.data()); + }); + newUser.save(); + console.log(`User ${userCount} (${newUser.uid}) saved`); + userCount++; + }); + }); + }); }); + // console.log('end of foreach'); }); //not tested because I can't get leaderboards to work on my fork for some reason