mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
challenges: migration script for TaskSchema subdocs, cleanup tags, remove old challenges
This commit is contained in:
@@ -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);
|
||||
}
|
||||
});
|
||||
40
migrations/20131028_task_subdocs_and_tags_cleanup.js
Normal file
40
migrations/20131028_task_subdocs_and_tags_cleanup.js
Normal 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});
|
||||
@@ -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},
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user