[#1702] much more thorough migration of tasks to new schema, providing defaults

where task attrs fail type checks
This commit is contained in:
Tyler Renelle 2013-11-01 10:42:40 -07:00
parent eccc9a7953
commit 2f83ac27ae
2 changed files with 30 additions and 5 deletions

View file

@ -68,11 +68,36 @@ db.users.find().forEach(function(user){
_.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];
var task = user.tasks[tid],
newTask = {};
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);
// Transform to proper schema
newTask._id = newTask.id = task.id;
newTask.text = (_.isString(task.text)) ? task.text : '';
if (_.isString(task.notes)) newTask.notes = task.notes;
newTask.tags = (_.isObject(task.tags)) ? task.tags : {};
newTask.type = (_.isString(task.type)) ? task.type : 'habit';
newTask.value = (_.isNumber(task.value)) ? task.value : 0;
newTask.priority = (_.isString(task.priority)) ? task.priority : '!';
switch (newTask.type) {
case 'habit':
newTask.up = (_.isBoolean(task.up)) ? task.up : true;
newTask.down = (_.isBoolean(task.down)) ? task.down : true;
newTask.history = (_.isArray(task.history)) ? task.history : [];
break;
case 'daily':
newTask.repeat = (_.isObject(task.repeat)) ? task.repeat : {m:1, t:1, w:1, th:1, f:1, s:1, su:1};
newTask.streak = (_.isNumber(task.streak)) ? task.streak : 0;
newTask.completed = (_.isBoolean(task.completed)) ? task.completed : false;
newTask.history = (_.isArray(task.history)) ? task.history : [];
break;
case 'todo':
newTask.completed = (_.isBoolean(task.completed)) ? task.completed : false;
break;
}
m.push(newTask);
return m;
}, []);
delete user[type + 'Ids'];

View file

@ -13,8 +13,8 @@ var _ = require('lodash');
// -----------
var TaskSchema = new Schema({
history: [{date:Date, value:Number}],
_id:{type: String,'default': helpers.uuid},
history: [{date:Date, value:Number}],
text: String,
notes: {type: String, 'default': ''},
tags: {type: Schema.Types.Mixed, 'default': {}}, //{ "4ddf03d9-54bd-41a3-b011-ca1f1d2e9371" : true },