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!');
}
});