v3 migration: correctly migrate challenges tasks

This commit is contained in:
Matteo Pagliazzi
2016-05-03 23:27:24 +02:00
parent c218a2cbdf
commit e5e4bb5823
4 changed files with 58 additions and 5 deletions

View File

@@ -17,6 +17,7 @@ var mongoose = require('mongoose');
var _ = require('lodash');
var uuid = require('uuid');
var consoleStamp = require('console-stamp');
var fs = require('fs');
// Add timestamps to console messages
consoleStamp(console);
@@ -48,6 +49,8 @@ var BATCH_SIZE = 1000;
var processedChallenges = 0;
var totoalProcessedTasks = 0;
var newTasksIds = {}; // a map of old id -> [new id, challengeId]
// Only process challenges that fall in a interval ie -> up to 0000-4000-0000-0000
var AFTER_CHALLENGE_ID = nconf.get('AFTER_CHALLENGE_ID');
var BEFORE_CHALLENGE_ID = nconf.get('BEFORE_CHALLENGE_ID');
@@ -109,23 +112,33 @@ function processChallenges (afterId) {
if (!oldChallenge.group) throw new Error('challenge.group is required');
if (!oldChallenge.leader) throw new Error('challenge.leader is required');
delete oldChallenge.id;
var newChallenge = new NewChallenge(oldChallenge);
newChallenge.createdAt = createdAt;
oldTasks.forEach(function (oldTask) {
oldTask._id = uuid.v4(); // TODO keep the old uuid unless duplicated
oldTask._id = uuid.v4();
oldTask.legacyId = oldTask.id; // store the old task id
delete oldTask.id;
oldTask.challenge = oldTask.challenge || {};
oldTask.challenge.id = newChallenge._id;
if (newTasksIds[oldTask.legacyId + '-' + newChallenge._id]) {
throw new Error('duplicate :(');
} else {
newTasksIds[oldTask.legacyId + '-' + newChallenge._id] = oldTask._id;
}
oldTask.tags = _.map(oldTask.tags || {}, function (tagPresent, tagId) {
return tagPresent && tagId;
});
if (!oldTask.text) oldTask.text = 'task text'; // required
oldTask.challenge = oldTask.challenge || {};
oldTask.challenge.id = oldChallenge._id;
oldTask.createdAt = oldTask.dateCreated;
newChallenge.tasksOrder[`${oldTask.type}s`].push(oldTask._id);
if (oldTask.completed) oldTask.completed = false;
@@ -155,6 +168,8 @@ function processChallenges (afterId) {
if (lastChallenge) {
return processChallenges(lastChallenge);
} else {
console.log('Writing newTasksIds.json...')
fs.writeFileSync('newTasksIds.json', JSON.stringify(newTasksIds, null, 4), 'utf8');
return console.log('Done!');
}
});

View File

@@ -86,6 +86,13 @@ function processChallenges (afterId) {
lastChallenge = oldChallenges[oldChallenges.length - 1]._id;
}
// Tyler Renelle
oldChallenge.members.forEach(function (id, index) {
if (id === '9') {
oldChallenge.members[index] = '00000000-0000-4000-9000-000000000000';
}
});
oldChallenges.forEach(function (oldChallenge) {
promises.push(newUserCollection.updateMany({
_id: {$in: oldChallenge.members},

View File

@@ -144,6 +144,13 @@ function processGroups (afterId) {
}
if (oldGroup.members) {
// Tyler Renelle
oldGroup.members.forEach(function (id, index) {
if (id === '9') {
oldGroup.members[index] = '00000000-0000-4000-9000-000000000000';
}
});
promises.push(newUserCollection.updateMany({
_id: {$in: oldGroup.members},
}, updateMembers, {multi: true}));

View File

@@ -51,6 +51,12 @@ var BATCH_SIZE = 1000;
var processedUsers = 0;
var totoalProcessedTasks = 0;
var challengeTaskWithMatchingId = 0;
var challengeTaskNoMatchingId = 0;
// Load the new tasks ids for challenges tasks
var newTasksIds = require('./newTasksIds.json');
// Only process users that fall in a interval ie up to -> 0000-4000-0000-0000
var AFTER_USER_ID = nconf.get('AFTER_USER_ID');
var BEFORE_USER_ID = nconf.get('BEFORE_USER_ID');
@@ -110,6 +116,8 @@ function processUsers (afterId) {
delete oldUser.rewards;
delete oldUser.todos;
delete oldUser.id;
oldUser.tags = oldUser.tags.map(function (tag) {
return {
id: tag.id,
@@ -132,10 +140,24 @@ function processUsers (afterId) {
oldTask.challenge = oldTask.challenge || {};
if (oldTask.challenge.id) {
oldTask.challenge.taskId = oldTask.legacyId;
if (oldTask.challenge.broken) {
oldTask.challenge.taskId = oldTask.legacyId;
} else {
var newId = newTasksIds[oldTask.legacyId + '-' + oldTask.challenge.id];
// Challenges' tasks ids changed
if (!newId && !oldTask.challenge.broken) {
challengeTaskNoMatchingId++;
oldTask.challenge.taskId = oldTask.legacyId;
oldTask.challenge.broken = 'CHALLENGE_TASK_NOT_FOUND';
} else {
challengeTaskWithMatchingId++;
oldTask.challenge.taskId = newId;
}
}
}
oldTask.createdAt = old.dateCreated;
oldTask.createdAt = oldTask.dateCreated;
if (!oldTask.text) oldTask.text = 'task text'; // required
oldTask.tags = _.map(oldTask.tags, function (tagPresent, tagId) {
@@ -179,6 +201,8 @@ function processUsers (afterId) {
processedUsers += oldUsers.length;
console.log(`Saved ${oldUsers.length} users and their tasks.`);
console.log('Challenges\' tasks no matching id: ', challengeTaskNoMatchingId);
console.log('Challenges\' tasks with matching id: ', challengeTaskWithMatchingId);
if (lastUser) {
return processUsers(lastUser);