-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip tasks collection: in progress users migration, complete challenge…
… migration, take back old challenge model temporarily to access collection
- Loading branch information
Showing
5 changed files
with
197 additions
and
49 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,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); | ||
}); | ||
}); |
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,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} | ||
}); |
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
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,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); |