Last active
July 26, 2016 10:28
-
-
Save barisusakli/6a8704c6d929d8e2e9d3af8ab913c4d8 to your computer and use it in GitHub Desktop.
fix post counts in topic hash if they are out of sync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/*globals require, console, process */ | |
var nconf = require('nconf'); | |
var async = require('async'); | |
nconf.file({ | |
file: 'config.json' | |
}); | |
var db = require('./src/database'); | |
db.init(function(err) { | |
if (err) { | |
console.log('NodeBB could not connect to your database. Returned following error: ' + err.message); | |
process.exit(); | |
} | |
fixPostCounts(function(err) { | |
if (err) { | |
console.log(err.message); | |
process.exit(); | |
} | |
console.log('post counts fixed'); | |
process.exit(); | |
}); | |
}); | |
function fixPostCounts(callback) { | |
db.getSortedSetRange('topics:tid', 0, -1, function(err, tids) { | |
if (err) { | |
return callback(err); | |
} | |
async.eachSeries(tids, fixTopic, callback); | |
}); | |
} | |
function fixTopic(tid, callback) { | |
async.parallel({ | |
realCount: function(next) { | |
db.sortedSetCard('tid:' + tid + ':posts', next); | |
}, | |
postCount: function(next) { | |
db.getObjectField('topic:' + tid, 'postcount', next); | |
} | |
}, function(err, results) { | |
if (err) { | |
return callback(err); | |
} | |
var realCount = results.realCount + 1; | |
if (realCount === parseInt(results.postCount, 10)) { | |
return callback(); | |
} | |
console.log('fixing topic: ' + tid + ', postcount = ' + results.postCount + ', realcount = ' + realCount); | |
db.setObjectField('topic:' + tid, 'postcount', realCount, callback); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment