From 349fdada1fe807f1a9ce0949af4ee6a0b943b05e Mon Sep 17 00:00:00 2001 From: Alice Harris Date: Wed, 27 Aug 2014 21:50:48 +1000 Subject: [PATCH] updated migrations script to use a node.js stream-based migration https://github.com/HabitRPG/habitrpg/pull/3907#issuecomment-53522251 --- ...emove_undefined_and_false_notifications.js | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/migrations/20140823_remove_undefined_and_false_notifications.js b/migrations/20140823_remove_undefined_and_false_notifications.js index 77882c4be7..27ee15f924 100644 --- a/migrations/20140823_remove_undefined_and_false_notifications.js +++ b/migrations/20140823_remove_undefined_and_false_notifications.js @@ -1,13 +1,45 @@ -db.users.find({}).forEach(function(user){ - var newNewMessages = {}; +// node .migrations/20140823_remove_undefined_and_false_notifications.js - for(var key in user.newMessages) { - var val = user.newMessages[key]; - // print("\n" + key + " " + val); +var migrationName = '20140823_remove_undefined_and_false_notifications'; + +var authorName = 'Alys'; // in case script author needs to know when their ... +var authorUuid = 'd904bd62-da08-416b-a816-ba797c9ee265'; //... own data is done + +/** + * https://github.com/HabitRPG/habitrpg/pull/3907 + */ + +var mongo = require('mongoskin'); +var _ = require('lodash'); + +// XXX @lefnire, choose wisely: +// var liveUsers = mongo.db('lefnire:mAdn3s5s@charlotte.mongohq.com:10015/habitrpg_large?auto_reconnect').collection('users'); +// var liveUsers = mongo.db('localhost:27017/habitrpg_old?auto_reconnect').collection('users'); + +// For local testing by script author: +// var liveUsers = mongo.db('localhost:27017/habitrpg?auto_reconnect').collection('users'); + + +var fields = {migration:1,newMessages:1}; +var progressCount = 1000; +// var progressCount = 1; +var count = 0; +liveUsers.findEach({migration: {$ne:migrationName}}, fields, {batchSize:250}, function(err, user){ + count++; + if (!user) err = '!user'; + if (err) {return console.error(err);} + + var newNewMessages = {}; + _.each(user.newMessages,function(val,key){ + // console.log(key + " " + val.name); if(key != "undefined" && val['value']){ newNewMessages[key] = val; } - } + }) - db.users.update({_id: user._id}, {$set: {'newMessages': newNewMessages}}); + liveUsers.update({_id:user._id}, {$set:{newMessages:newNewMessages, migration:migrationName}, $inc:{_v:1}}); + + if (count%progressCount == 0) console.log(count + ' ' + user._id); + if (user._id == '9') console.log('lefnire processed'); + if (user._id == authorUuid) console.log(authorName + ' processed'); });