From f304d4fe52c1d0e712a28dcedb49118dceaa3c6d Mon Sep 17 00:00:00 2001 From: Sabe Jones Date: Tue, 10 Jan 2017 20:47:58 +0000 Subject: [PATCH] chore(migration): update Bailey to use monk --- migrations/new_stuff.js | 67 +++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/migrations/new_stuff.js b/migrations/new_stuff.js index 518df71056..612b80c38d 100644 --- a/migrations/new_stuff.js +++ b/migrations/new_stuff.js @@ -6,49 +6,70 @@ var authorUuid = '7f14ed62-5408-4e1b-be83-ada62d504931'; //... own data is done * set the newStuff flag in all user accounts so they see a Bailey message */ -var mongo = require('mongoskin'); - +var monk = require('monk'); var connectionString = 'mongodb://localhost:27017/habitrpg?auto_reconnect=true'; // FOR TEST DATABASE +var dbUsers = monk(connectionString).get('users', { castIds: false }); -var dbUsers = mongo.db(connectionString).collection('users'); -// specify a query to limit the affected users (empty for all users): -var query = { - 'flags.newStuff':{$ne:true} -}; +function processUsers(lastId) { + // specify a query to limit the affected users (empty for all users): + var query = { + 'flags.newStuff': {$ne:true}, + }; -// specify fields we are interested in to limit retrieved data (empty if we're not reading data): -var fields = { -}; + if (lastId) { + query._id = { + $gt: lastId + } + } + + dbUsers.find(query, { + sort: {_id: 1}, + limit: 250, + fields: [] // specify fields we are interested in to limit retrieved data (empty if we're not reading data): + }) + .then(updateUsers) + .catch(function (err) { + console.log(err); + return exiting(1, 'ERROR! ' + err); + }); +} -console.warn('Updating users...'); var progressCount = 1000; var count = 0; -dbUsers.findEach(query, fields, {batchSize:250}, function(err, user) { - if (err) { return exiting(1, 'ERROR! ' + err); } - if (!user) { + +function updateUsers (users) { + if (!users || users.length === 0) { console.warn('All appropriate users found and modified.'); - setTimeout(displayData, 300000); + displayData(); return; } + + var userPaymentPromises = users.map(updateUser); + var lastUser = users[users.length - 1]; + + return Promise.all(userPaymentPromises) + .then(function () { + processUsers(lastUser._id); + }); +} + +function updateUser (user) { count++; - // specify user data to change: - var set = {'flags.newStuff':true}; + var set = {'flags.newStuff': true}; - dbUsers.update({_id:user._id}, {$set:set}); + dbUsers.update({_id: user._id}, {$set:set}); - if (count%progressCount == 0) console.warn(count + ' ' + user._id); + if (count % progressCount == 0) console.warn(count + ' ' + user._id); if (user._id == authorUuid) console.warn(authorName + ' processed'); -}); - +} function displayData() { console.warn('\n' + count + ' users processed\n'); return exiting(0); } - function exiting(code, msg) { code = code || 0; // 0 = success if (code && !msg) { msg = 'ERROR!'; } @@ -58,3 +79,5 @@ function exiting(code, msg) { } process.exit(code); } + +processUsers()