From 4ac0ef4ac2551f9d583680b5d89fb03299251a9f Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Mon, 28 Oct 2013 12:03:20 -0700 Subject: [PATCH] challenges: migration script for TaskSchema subdocs, cleanup tags, remove old challenges --- migrations/20131028_cleanup_deleted_tags.js | 17 -------- .../20131028_task_subdocs_and_tags_cleanup.js | 40 +++++++++++++++++++ src/models/task.js | 2 +- src/models/user.js | 24 ----------- 4 files changed, 41 insertions(+), 42 deletions(-) delete mode 100644 migrations/20131028_cleanup_deleted_tags.js create mode 100644 migrations/20131028_task_subdocs_and_tags_cleanup.js diff --git a/migrations/20131028_cleanup_deleted_tags.js b/migrations/20131028_cleanup_deleted_tags.js deleted file mode 100644 index 165088dc6a8..00000000000 --- a/migrations/20131028_cleanup_deleted_tags.js +++ /dev/null @@ -1,17 +0,0 @@ -_.each(db.users.find(), function(user){ - var tags = user.tags; - - _.each(user.tasks, function(task){ - _.each(task.tags, function(val, key){ - _.each(tags, function(tag){ - if(key == tag.id) delete task.tags[key]; - }); - }; - }); - - try { - db.users.update({_id:user._id}, user); - } catch(e) { - print(e); - } -}); diff --git a/migrations/20131028_task_subdocs_and_tags_cleanup.js b/migrations/20131028_task_subdocs_and_tags_cleanup.js new file mode 100644 index 00000000000..f01cf967da8 --- /dev/null +++ b/migrations/20131028_task_subdocs_and_tags_cleanup.js @@ -0,0 +1,40 @@ +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){ + if(key == tag.id) delete task.tags[key]; + }); + }); + }); + + // Migrate to TaskSchema subdocs!! + if (!user.tasks) { + printjson(user.auth); + // FIXME before deploying! + } else { + _.each(['habit', 'daily', 'todo', 'reward'], function(type) { + // we use _.transform instead of a simple _.where in order to maintain sort-order + user[type + "s"] = _.reduce(user[type + "Ids"], function(m, tid) { + var task = user.tasks[tid]; + if (!task) return m; // remove null tasks + //if (!user.tasks[tid].tags) user.tasks[tid].tags = {}; // shouldn't be necessary, since TaskSchema.tags has default {} + task._id = task.id; + m.push(task); + return m; + }, []); + delete user[type + 'Ids']; + }); + delete user.tasks; + } + + try { + db.users.update({_id:user._id}, user); + } catch(e) { + print(e); + } +}); + +// Remove old groups.*.challenges, they're not compatible with the new system +db.groups.update({},{$pull:{challenges:1}},{multi:true}); diff --git a/src/models/task.js b/src/models/task.js index fd89af35a46..3935e0f232f 100644 --- a/src/models/task.js +++ b/src/models/task.js @@ -17,7 +17,7 @@ var TaskSchema = new Schema({ _id:{type: String,'default': helpers.uuid}, text: String, notes: {type: String, 'default': ''}, - tags: Schema.Types.Mixed, //{ "4ddf03d9-54bd-41a3-b011-ca1f1d2e9371" : true }, + tags: {type: Schema.Types.Mixed, 'default': {}}, //{ "4ddf03d9-54bd-41a3-b011-ca1f1d2e9371" : true }, type: {type:String, 'default': 'habit'}, // habit, daily up: {type: Boolean, 'default': true}, down: {type: Boolean, 'default': true}, diff --git a/src/models/user.js b/src/models/user.js index 6462af039e3..4f1438895bc 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -64,10 +64,6 @@ var UserSchema = new Schema({ }, balance: Number, - habitIds: Array, - dailyIds: Array, - todoIds: Array, - rewardIds: Array, filters: {type: Schema.Types.Mixed, 'default': {}}, purchased: { @@ -217,26 +213,6 @@ var UserSchema = new Schema({ minimize: false // So empty objects are returned }); -// Legacy Derby Function? -// ---------------------- -// Derby requires a strange storage format for somethign called "refLists". Here we hook into loading the data, so we -// can provide a more "expected" storage format for our various helper methods. Since the attributes are passed by reference, -// the underlying data will be modified too - so when we save back to the database, it saves it in the way Derby likes. -// This will go away after the rewrite is complete - -//FIXME use this in migration -/*function transformTaskLists(doc) { - _.each(['habit', 'daily', 'todo', 'reward'], function(type) { - // we use _.transform instead of a simple _.where in order to maintain sort-order - doc[type + "s"] = _.reduce(doc[type + "Ids"], function(m, tid) { - if (!doc.tasks[tid]) return m; // FIXME tmp hotfix, people still have null tasks? - if (!doc.tasks[tid].tags) doc.tasks[tid].tags = {}; // FIXME remove this when we switch tasks to subdocs and can define tags default in schema - m.push(doc.tasks[tid]); - return m; - }, []); - }); -}*/ - UserSchema.methods.toJSON = function() { var doc = this.toObject(); doc.id = doc._id;