mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-02 02:21:14 +01:00
updated logic to make it a bit simpler to implement per upgrade script
This commit is contained in:
12
src/batch.js
12
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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
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.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);
|
||||
});
|
||||
progress.incr();
|
||||
db.sortedSetAdd('uid:' + uid + ':moderation:notes', note.timestamp, JSON.stringify(note), next);
|
||||
});
|
||||
}, next);
|
||||
}, {
|
||||
progress: this.progress,
|
||||
}, callback);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
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.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);
|
||||
});
|
||||
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);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user