"use strict"; var db = require('./database'), async = require('async'), winston = require('winston'), fs = require('fs'), path = require('path'), User = require('./user'), Topics = require('./topics'), Posts = require('./posts'), Categories = require('./categories'), Groups = require('./groups'), Meta = require('./meta'), Plugins = require('./plugins'), Utils = require('../public/src/utils'), Upgrade = {}, minSchemaDate = Date.UTC(2014, 9, 22), // This value gets updated every new MINOR version schemaDate, thisSchemaDate, // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema latestSchema = Date.UTC(2015, 1, 24, 1); Upgrade.check = function(callback) { db.get('schemaDate', function(err, value) { if(!value) { db.set('schemaDate', latestSchema, function(err) { callback(true); }); return; } if (parseInt(value, 10) >= latestSchema) { callback(true); } else { callback(false); } }); }; Upgrade.update = function(schemaDate, callback) { db.set('schemaDate', schemaDate, callback); }; Upgrade.upgrade = function(callback) { var updatesMade = false; winston.info('Beginning database schema update'); async.series([ function(next) { // Prepare for upgrade & check to make sure the upgrade is possible db.get('schemaDate', function(err, value) { if(!value) { db.set('schemaDate', latestSchema, function(err) { next(); }); schemaDate = latestSchema; } else { schemaDate = parseInt(value, 10); } if (schemaDate >= minSchemaDate) { next(); } else { next(new Error('upgrade-not-possible')); } }); }, function(next) { thisSchemaDate = Date.UTC(2014, 9, 31); if (schemaDate < thisSchemaDate) { updatesMade = true; winston.info('[2014/10/31] Applying newbiePostDelay values'); async.series([ async.apply(Meta.configs.setOnEmpty, 'newbiePostDelay', '120'), async.apply(Meta.configs.setOnEmpty, 'newbiePostDelayThreshold', '3') ], function(err) { if (err) { winston.error('[2014/10/31] Error encountered while Applying newbiePostDelay values'); return next(err); } winston.info('[2014/10/31] Applying newbiePostDelay values done'); Upgrade.update(thisSchemaDate, next); }); } else { winston.info('[2014/10/31] Applying newbiePostDelay values skipped'); next(); } }, function(next) { thisSchemaDate = Date.UTC(2014, 10, 6, 18, 30); if (schemaDate < thisSchemaDate) { updatesMade = true; winston.info('[2014/11/6] Updating topic authorship sorted set'); async.waterfall([ async.apply(db.getObjectField, 'global', 'nextTid'), function(nextTid, next) { var tids = []; for(var x=1,numTids=nextTid-1;x