Skip to content

Commit

Permalink
wip tasks collection: in progress users migration, complete challenge…
Browse files Browse the repository at this point in the history
… migration, take back old challenge model temporarily to access collection
  • Loading branch information
paglias committed Oct 21, 2015
1 parent 20af0bf commit 10674cb
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 49 deletions.
47 changes: 0 additions & 47 deletions migrations/20151019_tasks_collection.js

This file was deleted.

99 changes: 99 additions & 0 deletions migrations/20151019_tasks_collection_challenges_tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Migrate challenges' tasks to individual models

// Enable coffee-script
require('coffee-script');

// Load config
var nconf = require('nconf');
var utils = require('../website/src/utils');
utils.setupConfig();

// Load async
var async = require('async');

// Initialize mongoose
require('mongoose');

var mongooseOptions = {};
var db = mongoose.connect(nconf.get('NODE_DB_URI'), mongooseOptions, function(err) {
if (err) throw err;
logging.info('Connected with Mongoose');
});

// Load shared (for uuid)
var shared = require('../common/script');

// Load models
// oldChallenge will map to original challenge collection,
// newChallenge to the new collection
var OldChallengeModel = require('../website/src/models/challengeOld').model; // The originals challenge model
var NewChallengeModel = require('../website/src/models/challenge').model // The new challenge model
var TaskModel = require('../website/src/models/task').model;

// ... given a challenge

var processed = 0;
var batchSize = 50;

OldChallengeModel
.find()
.limit(100) // try with 100 to start
.lean() // Use plain JS objects as old challenge data won't match the new model
.batchSize(batchSize)
.exec(function(err, challenges) {
if(err) throw err;

console.log('Processing ' + batchSize + ' challenges.', 'Already processed: ' + processed);

async.each(challenges, function(challenge, cb) {
// add tasks order arrays
challenge.tasksOrder = {
habits: [],
rewards: [],
todos: [],
dailys: []
};

// ... convert tasks to individual models
async.each(
challenge.dailys
.concat(challenge.habits)
.concat(challenge.rewards)
.concat(challenge.todos),
function(task, cb1) {
task._id = task.id;
delete task.id;

task.challenge = task.challenge || {};
task.challenge.id = challenge._id;

task = new TaskModel(task); // this should also fix dailies that wen to the habits array or vice-versa

TaskModel.findOne({_id: task._id}, function(err, taskSameId){
if(err) return cb1(err);

// We already have a task with the same id, change this one
// MAKE SURE IT NEVER HAPPENS WITH CHALLENGES AS IT WILL BREAK USERS' TASKS THEN
// and will require special handling
if(taskSameId) {
return cb1(new Error('Duplicate challenge task id.'));
task._id = shared.uuid();
}

task.save(function(err, savedTask){
if(err) return cb1(err);

challenge.tasksOrder[savedTask.type + 's'].push(savedTask._id);

var newChallenge = new NewChallengeModel(challenge);
newChallenge.save(cb1);
});
});
}, cb);
}, function(err) {
if(err) throw err;

processed = processed + batchSize;
console.log('Processed ' + batchSize + ' challenges.', 'Total: ' + processed);
});
});
85 changes: 85 additions & 0 deletions migrations/20151019_tasks_collection_users_tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Migrate users' tasks to individual models
// This should run AFTER 20151019_tasks_collection_challenges_tasks.js

// Enable coffee-script
require('coffee-script');

// Load config
var nconf = require('nconf');
var utils = require('../website/src/utils');
utils.setupConfig();

// Initialize mongoose
require('mongoose');

var mongooseOptions = {
replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } },
server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }
};
var db = mongoose.connect(nconf.get('NODE_DB_URI'), mongooseOptions, function(err) {
if (err) throw err;
logging.info('Connected with Mongoose');
});

//...

var shared = require('../common/script');

// Load models
var UserModel = require('../website/src/models/user').model;
var TaskModel = require('../website/src/models/task').model;

// ... given an user

// add tasks order arrays

user.tasksOrder = {
habits: [],
rewards: [],
todos: [],
dailys: []
};

// ... convert tasks to individual models
var tasks = user.dailys
.concat(user.habits)
.concat(user.rewards)
.concat(user.todos)
.map(function(task) {
task._id = task.id;
delete task.id;

task.userId = user._id;
task = new TaskModel(task); // this should also fix dailies that wen to the habits array or vice-versa

// In case of a challenge task, assign a new id and link the original task
if(task.challenge && task.challenge.id) {
task.challenge.taskId = task._id;
task._id = shared.uuid();
}

TaskModel.findOne({_id: task._id}, function(err, taskSameId){
if(err) throw err;

// We already have a task with the same id, change this one
if(taskSameId) {
task._id = shared.uuid();
}

task.save(function(err, savedTask){
if(err) throw err;

user.tasksOrder[savedTask.type + 's'].push(savedTask._id);
});
});
});

delete user.habits;
delete user.dailys;
delete user.todos;
delete user.rewards;

UserModel.update({_id: user._id}, {
$set: {tasksOrder: user.tasksOrder},
$unset: {habits: 1, dailys: 1, rewards: 1, todos: 1}
});
4 changes: 2 additions & 2 deletions website/src/models/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var ChallengeSchema = new Schema({
// TODO filter just one or more types of tasks
ChallengeSchema.methods.getTasks = function(cb) {
Task.find({
// TODO $or should be avoided, revisit once discriminators are in place
// TODO $or for userId: null?
userId: {$exists: false},
'challenge.id': this._id
}, function(err, tasks){
Expand Down Expand Up @@ -206,4 +206,4 @@ ChallengeSchema.methods.syncToUser = function(user, tasks, cb) {


module.exports.schema = ChallengeSchema;
module.exports.model = mongoose.model("Challenge", ChallengeSchema);
module.exports.model = mongoose.model("NewChallenge", ChallengeSchema);
11 changes: 11 additions & 0 deletions website/src/models/challengeOld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var shared = require('../../../common');

// We don't care about the actual schema, just need to be able to load plain data
var ChallengeSchema = new Schema({
_id: {type: String, 'default': shared.uuid}
});

module.exports.schema = ChallengeSchema;
module.exports.model = mongoose.model("Challenge", ChallengeSchema);

0 comments on commit 10674cb

Please sign in to comment.