Permalink
Browse files

data upgrade (v2): multiply `users.money` and `market.orders.price` b…

…y 1000
  • Loading branch information...
artch committed Sep 21, 2017
1 parent bbd256d commit 277cbb1faecd7ba59fff0122ef039b0a99eb4ab1
Showing with 26 additions and 1 deletion.
  1. +26 −1 lib/db.js
View
@@ -23,10 +23,35 @@ Object.assign(config.storage, {
},
loadDb() {
db = config.storage.getDb();
return q.ninvoke(db, 'loadDatabase', {});
return q.ninvoke(db, 'loadDatabase', {})
.then(upgradeDb);
}
});
function upgradeDb() {
if(!db.databaseVersion) {
return;
}
if(db.databaseVersion < 2) {
console.log("Upgrading database to version 2");
var users = db.getCollection('users').find({money: {$gt: 0}});
users.forEach(user => {
user.money = Math.round(user.money * 1000);
db.getCollection('users').update(user);
});
var orders = db.getCollection('market.orders').find({});
orders.forEach(order => {
console.log(order._id, order.price);
order.price = Math.round(order.price * 1000);
return db.getCollection('market.orders').update(order);
});
}
db.databaseVersion = 2;
}
function updateDocument(doc, update) {
if (update.$set) {

0 comments on commit 277cbb1

Please sign in to comment.