diff --git a/src/database/mongo/main.js b/src/database/mongo/main.js index eff7459a69..278ae6c413 100644 --- a/src/database/mongo/main.js +++ b/src/database/mongo/main.js @@ -66,7 +66,7 @@ module.exports = function (db, module) { if (!key) { return callback(); } - module.getObjectField(key, 'value', callback); + module.getObjectField(key, 'data', callback); }; module.set = function (key, value, callback) { @@ -74,7 +74,7 @@ module.exports = function (db, module) { if (!key) { return callback(); } - var data = { value: value }; + var data = { data: value }; module.setObject(key, data, callback); }; @@ -115,7 +115,7 @@ module.exports = function (db, module) { return callback(null, 'set'); } else if (keys.length === 3 && data.hasOwnProperty('_key') && data.hasOwnProperty('array')) { return callback(null, 'list'); - } else if (keys.length === 3 && data.hasOwnProperty('_key') && data.hasOwnProperty('value')) { + } else if (keys.length === 3 && data.hasOwnProperty('_key') && data.hasOwnProperty('data')) { return callback(null, 'string'); } callback(null, 'hash'); diff --git a/src/upgrades/1.7.3/key_value_schema_change.js b/src/upgrades/1.7.3/key_value_schema_change.js new file mode 100644 index 0000000000..4e747f6846 --- /dev/null +++ b/src/upgrades/1.7.3/key_value_schema_change.js @@ -0,0 +1,67 @@ +'use strict'; + +var async = require('async'); + +var db = require('../../database'); + +module.exports = { + name: 'Change the schema of simple keys so they don\'t use value field (mongodb only)', + timestamp: Date.UTC(2017, 11, 18), + method: function (callback) { + var configJSON = require('../../../config.json'); + var isMongo = configJSON.hasOwnProperty('mongo') && configJSON.database === 'mongo'; + var progress = this.progress; + if (!isMongo) { + return callback(); + } + var client = db.client; + var cursor; + async.waterfall([ + function (next) { + client.collection('objects').count({ + _key: { $exists: true }, + value: { $exists: true }, + score: { $exists: false }, + }, next); + }, + function (count, next) { + progress.total = count; + cursor = client.collection('objects').find({ + _key: { $exists: true }, + value: { $exists: true }, + score: { $exists: false }, + }).batchSize(1000); + + var done = false; + async.whilst( + function () { + return !done; + }, + function (next) { + async.waterfall([ + function (next) { + cursor.next(next); + }, + function (item, next) { + progress.incr(); + if (item === null) { + done = true; + return next(); + } + + if (Object.keys(item).length === 3 && item.hasOwnProperty('_key') && item.hasOwnProperty('value')) { + client.collection('objects').update({ _key: item._key }, { $rename: { value: 'data' } }, next); + } else { + next(); + } + }, + ], function (err) { + next(err); + }); + }, + next + ); + }, + ], callback); + }, +};