diff --git a/migrations/20131028_task_subdocs_tags_invites.js b/migrations/20131028_task_subdocs_tags_invites.js index 55913562ea..63470036ec 100644 --- a/migrations/20131028_task_subdocs_tags_invites.js +++ b/migrations/20131028_task_subdocs_tags_invites.js @@ -1,8 +1,14 @@ -var groups = {}; +// mongo habitrpg ./node_modules/lodash/lodash.js ./migrations/20131028_task_subdocs_tags_invites.js + +// TODO it might be better we just find() and save() all user objects using mongoose, and rely on our defined pre('save') +// and default values to "migrate" users. This way we can make sure those parts are working properly too +// @see http://stackoverflow.com/questions/14867697/mongoose-full-collection-scan +//Also, what do we think of a Mongoose Migration module? something like https://github.com/madhums/mongoose-migrate db.users.find().forEach(function(user){ - // add_invites_to_groups + // Add invites to groups + // ------------------------- if(user.invitations){ if(user.invitations.party){ db.groups.update({_id: user.invitations.party.id}, {$addToSet:{invites:user._id}}); @@ -16,6 +22,7 @@ db.users.find().forEach(function(user){ } // Cleanup broken tags + // ------------------------- _.each(user.tasks, function(task){ _.each(task.tags, function(val, key){ _.each(user.tags, function(tag){ @@ -24,8 +31,19 @@ db.users.find().forEach(function(user){ }); }); + // Fix corrupt dates + // ------------------------- + user.lastCron = new Date(user.lastCron); + if (user.lastCron == 'Invalid Date') user.lastCron = new Date(); + if (user.auth) { // what to do with !auth? + _.defaults(user.auth, {timestamps: {created:undefined, loggedin: undefined}}); + _.defaults(user.auth.timestamps, {created: new Date(user.lastCron), loggedin: new Date(user.lastCron)}); + } + // Add username - if (_.isEmpty(user.profile.name)) { + // ------------------------- + if (!user.profile) user.profile = {name:undefined}; + if (_.isEmpty(user.profile.name) && user.auth) { var fb = user.auth.facebook; user.profile.name = (user.auth.local && user.auth.local.username) || @@ -33,10 +51,14 @@ db.users.find().forEach(function(user){ 'Anonymous'; } - // Migrate to TaskSchema subdocs!! + // Migrate to TaskSchema Sub-Docs! + // ------------------------- if (!user.tasks) { - // FIXME before deploying! - print(user._id); + // So evidentaly users before 02/2013 were ALREADY setup based on habits[], dailys[], etcs... I don't remember our schema + // ever being that way... Anyway, print busted users here (they don't have tasks, but also don't have the right schema) + if (!user.habits || !user.dailys || !user.todos || !user.rewards) { + print(user._id); + } } else { _.each(['habit', 'daily', 'todo', 'reward'], function(type) { // we use _.transform instead of a simple _.where in order to maintain sort-order @@ -60,8 +82,8 @@ db.users.find().forEach(function(user){ } }); -// Remove old groups.*.challenges, they're not compatible with the new system -// set member counts +// Remove old groups.*.challenges, they're not compatible with the new system, set member counts +// ------------------------- db.groups.find().forEach(function(group){ db.groups.update({_id:group._id}, { $set:{memberCount: _.size(group.members)}, @@ -70,4 +92,5 @@ db.groups.find().forEach(function(group){ }); // HabitRPG => Tavern +// ------------------------- db.groups.update({_id:'habitrpg'}, {$set:{name:'Tavern'}}); \ No newline at end of file diff --git a/src/models/user.js b/src/models/user.js index 4f1438895b..728748d676 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -47,11 +47,8 @@ var UserSchema = new Schema({ username: String }, timestamps: { - created: { - type: Date, - 'default': Date.now - }, - loggedin: Date + created: {type: Date,'default': Date.now}, + loggedin: {type: Date,'default': Date.now} } },