Skip to content

Commit

Permalink
challenges: migration script for TaskSchema subdocs, cleanup tags, re…
Browse files Browse the repository at this point in the history
…move old challenges
  • Loading branch information
lefnire committed Oct 28, 2013
1 parent 35c4a62 commit 4ac0ef4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
17 changes: 0 additions & 17 deletions migrations/20131028_cleanup_deleted_tags.js

This file was deleted.

40 changes: 40 additions & 0 deletions migrations/20131028_task_subdocs_and_tags_cleanup.js
Original file line number Diff line number Diff line change
@@ -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});
2 changes: 1 addition & 1 deletion src/models/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
24 changes: 0 additions & 24 deletions src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4ac0ef4

Please sign in to comment.