From 33082d90cc53b95aaf45d653967f497bc3f3b787 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sun, 16 Apr 2017 13:25:01 -0400 Subject: [PATCH] updated logic to make it a bit simpler to implement per upgrade script --- src/batch.js | 12 +++++ src/upgrade.js | 19 +++++--- .../1.5.0/moderation_history_refactor.js | 44 ++++++++----------- src/upgrades/1.5.0/post_votes_zset.js | 36 +++++++-------- 4 files changed, 58 insertions(+), 53 deletions(-) diff --git a/src/batch.js b/src/batch.js index 475c4aa7ca..f15d06a453 100644 --- a/src/batch.js +++ b/src/batch.js @@ -21,6 +21,17 @@ exports.processSortedSet = function (setKey, process, options, callback) { return callback(new Error('[[error:process-not-a-function]]')); } + // Progress bar handling (upgrade scripts) + if (options.progress) { + db.sortedSetCard(setKey, function (err, total) { + if (err) { + // Unable to get total, do nothing. + } else { + options.progress.total = total; + } + }); + } + // use the fast path if possible if (db.processSortedSet && typeof options.doneIf !== 'function' && !utils.isNumber(options.alwaysStartAt)) { return db.processSortedSet(setKey, process, options.batch || DEFAULT_BATCH_SIZE, callback); @@ -53,6 +64,7 @@ exports.processSortedSet = function (setKey, process, options, callback) { } start += utils.isNumber(options.alwaysStartAt) ? options.alwaysStartAt : batch + 1; stop = start + batch; + next(); }); }); diff --git a/src/upgrade.js b/src/upgrade.js index 9b2bf8b29e..5352a091f9 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -7,7 +7,7 @@ var path = require('path'); var semver = require('semver'); var db = require('./database'); -var utils = require('../public/src/utils'); +var file = require('../src/file'); /* * Need to write an upgrade script for NodeBB? Cool. @@ -58,7 +58,7 @@ var Upgrade = { Upgrade.getAll = function (callback) { async.waterfall([ - async.apply(utils.walk, path.join(__dirname, './upgrades')), + async.apply(file.walk, path.join(__dirname, './upgrades')), function (files, next) { // Sort the upgrade scripts based on version var versionA; @@ -128,7 +128,7 @@ Upgrade.runSingle = function (query, callback) { process.stdout.write('\nParsing upgrade scripts... '); async.waterfall([ - async.apply(utils.walk, path.join(__dirname, './upgrades')), + async.apply(file.walk, path.join(__dirname, './upgrades')), function (files, next) { next(null, files.filter(function (file) { return path.basename(file, '.js') === query; @@ -207,14 +207,19 @@ Upgrade.incrementProgress = function () { this.current += 1; // Redraw the progress bar - var percentage = Math.floor((this.current / this.total) * 100) + '%'; - var filled = Math.floor((this.current / this.total) * 15); - var unfilled = 15 - filled; + var percentage = 0; + var filled = 0; + var unfilled = 15; + if (this.total) { + percentage = Math.floor((this.current / this.total) * 100) + '%'; + filled = Math.floor((this.current / this.total) * 15); + unfilled = 15 - filled; + } process.stdout.clearLine(); process.stdout.cursorTo(0); process.stdout.write(' → '.white + String('[' + [this.date.getUTCFullYear(), this.date.getUTCMonth() + 1, this.date.getUTCDate()].join('/') + '] ').gray + String(this.script.name).reset + '... '); - process.stdout.write('[' + (filled ? new Array(filled).join('#') : '') + new Array(unfilled).join(' ') + '] (' + this.current + '/' + this.total + ') ' + percentage); + process.stdout.write('[' + (filled ? new Array(filled).join('#') : '') + new Array(unfilled).join(' ') + '] (' + this.current + '/' + (this.total || '??') + ') ' + percentage); }; module.exports = Upgrade; diff --git a/src/upgrades/1.5.0/moderation_history_refactor.js b/src/upgrades/1.5.0/moderation_history_refactor.js index b167246e5f..f98e1c62db 100644 --- a/src/upgrades/1.5.0/moderation_history_refactor.js +++ b/src/upgrades/1.5.0/moderation_history_refactor.js @@ -13,30 +13,24 @@ module.exports = { method: function (callback) { var progress = this.progress; - db.sortedSetCard('users:joindate', function (err, numPosts) { - if (err) { - return callback(err); - } - - progress.total = numPosts; - - batch.processSortedSet('users:joindate', function (ids, next) { - async.each(ids, function (uid, next) { - db.getObjectField('user:' + uid, 'moderationNote', function (err, moderationNote) { - if (err || !moderationNote) { - return next(err); - } - var note = { - uid: 1, - note: moderationNote, - timestamp: Date.now(), - }; - - progress.incr(); - db.sortedSetAdd('uid:' + uid + ':moderation:notes', note.timestamp, JSON.stringify(note), next); - }); - }, next); - }, callback); - }); + batch.processSortedSet('users:joindate', function (ids, next) { + async.each(ids, function (uid, next) { + db.getObjectField('user:' + uid, 'moderationNote', function (err, moderationNote) { + if (err || !moderationNote) { + return next(err); + } + var note = { + uid: 1, + note: moderationNote, + timestamp: Date.now(), + }; + + progress.incr(); + db.sortedSetAdd('uid:' + uid + ':moderation:notes', note.timestamp, JSON.stringify(note), next); + }); + }, next); + }, { + progress: this.progress, + }, callback); }, }; diff --git a/src/upgrades/1.5.0/post_votes_zset.js b/src/upgrades/1.5.0/post_votes_zset.js index df5a4c522f..b1d5b0cae4 100644 --- a/src/upgrades/1.5.0/post_votes_zset.js +++ b/src/upgrades/1.5.0/post_votes_zset.js @@ -12,26 +12,20 @@ module.exports = { method: function (callback) { var progress = this.progress; - db.sortedSetCard('posts:pid', function (err, numPosts) { - if (err) { - return callback(err); - } - - progress.total = numPosts; - - require('../../batch').processSortedSet('posts:pid', function (pids, next) { - async.eachSeries(pids, function (pid, next) { - db.getObjectFields('post:' + pid, ['upvotes', 'downvotes'], function (err, postData) { - if (err || !postData) { - return next(err); - } - - var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10); - progress.incr(); - db.sortedSetAdd('posts:votes', votes, pid, next); - }); - }, next); - }, {}, callback); - }); + require('../../batch').processSortedSet('posts:pid', function (pids, next) { + async.eachSeries(pids, function (pid, next) { + db.getObjectFields('post:' + pid, ['upvotes', 'downvotes'], function (err, postData) { + if (err || !postData) { + return next(err); + } + + progress.incr(); + var votes = parseInt(postData.upvotes || 0, 10) - parseInt(postData.downvotes || 0, 10); + db.sortedSetAdd('posts:votes', votes, pid, next); + }); + }, next); + }, { + progress: this.progress, + }, callback); }, };