challenges: migration script for TaskSchema subdocs, cleanup tags, remove old challenges

This commit is contained in:
Tyler Renelle
2013-10-28 12:03:20 -07:00
parent 35c4a62de0
commit 4ac0ef4ac2
4 changed files with 41 additions and 42 deletions

View File

@@ -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);
}
});

View File

@@ -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});

View File

@@ -17,7 +17,7 @@ var TaskSchema = new Schema({
_id:{type: String,'default': helpers.uuid}, _id:{type: String,'default': helpers.uuid},
text: String, text: String,
notes: {type: String, 'default': ''}, 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 type: {type:String, 'default': 'habit'}, // habit, daily
up: {type: Boolean, 'default': true}, up: {type: Boolean, 'default': true},
down: {type: Boolean, 'default': true}, down: {type: Boolean, 'default': true},

View File

@@ -64,10 +64,6 @@ var UserSchema = new Schema({
}, },
balance: Number, balance: Number,
habitIds: Array,
dailyIds: Array,
todoIds: Array,
rewardIds: Array,
filters: {type: Schema.Types.Mixed, 'default': {}}, filters: {type: Schema.Types.Mixed, 'default': {}},
purchased: { purchased: {
@@ -217,26 +213,6 @@ var UserSchema = new Schema({
minimize: false // So empty objects are returned 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() { UserSchema.methods.toJSON = function() {
var doc = this.toObject(); var doc = this.toObject();
doc.id = doc._id; doc.id = doc._id;