chore(migration): update Bailey to use monk

This commit is contained in:
Sabe Jones 2017-01-10 20:47:58 +00:00
parent 023e433a5c
commit f304d4fe52

View file

@ -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 * 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 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): function processUsers(lastId) {
var query = { // specify a query to limit the affected users (empty for all users):
'flags.newStuff':{$ne:true} var query = {
}; 'flags.newStuff': {$ne:true},
};
// specify fields we are interested in to limit retrieved data (empty if we're not reading data): if (lastId) {
var fields = { 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 progressCount = 1000;
var count = 0; var count = 0;
dbUsers.findEach(query, fields, {batchSize:250}, function(err, user) {
if (err) { return exiting(1, 'ERROR! ' + err); } function updateUsers (users) {
if (!user) { if (!users || users.length === 0) {
console.warn('All appropriate users found and modified.'); console.warn('All appropriate users found and modified.');
setTimeout(displayData, 300000); displayData();
return; 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++; 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'); if (user._id == authorUuid) console.warn(authorName + ' processed');
}); }
function displayData() { function displayData() {
console.warn('\n' + count + ' users processed\n'); console.warn('\n' + count + ' users processed\n');
return exiting(0); return exiting(0);
} }
function exiting(code, msg) { function exiting(code, msg) {
code = code || 0; // 0 = success code = code || 0; // 0 = success
if (code && !msg) { msg = 'ERROR!'; } if (code && !msg) { msg = 'ERROR!'; }
@ -58,3 +79,5 @@ function exiting(code, msg) {
} }
process.exit(code); process.exit(code);
} }
processUsers()