From 36c4e41131544944010f8709c079afc7f179db81 Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Mon, 23 Jun 2014 22:15:57 -0700 Subject: [PATCH] Add console command for deleting old channel tables --- index.js | 6 ++++++ lib/database/update.js | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 127be83f..03514bfb 100644 --- a/index.js +++ b/index.js @@ -47,5 +47,11 @@ function handleLine(line) { } else { Logger.syslog.log("Failed to invoke GC: node started without --expose-gc"); } + } else if (line === "/delete_old_tables") { + require("./lib/database/update").deleteOldChannelTables(function (err) { + if (!err) { + Logger.syslog.log("Deleted old channel tables"); + } + }); } } diff --git a/lib/database/update.js b/lib/database/update.js index ce2b4b76..fcae7fa8 100644 --- a/lib/database/update.js +++ b/lib/database/update.js @@ -26,8 +26,7 @@ module.exports.checkVersion = function () { var next = function () { hasUpdates.push(v); if (v < DB_VERSION) { - update(v++, next); - } else { + update(v++, next); } else { db.query("UPDATE `meta` SET `value`=? WHERE `key`='db_version'", [DB_VERSION]); } @@ -45,7 +44,11 @@ function update(version, cb) { Q.fcall(mergeChannelLibraries), Q.fcall(mergeChannelRanks), Q.fcall(mergeChannelBans) - ]).done(cb) + ]).done(function () { + Logger.syslog.log("Merged channel tables. Please verify that everything " + + "is working correctly, and then type '/delete_old_tables'" + + " into the CyTube process to remove the unused tables."); + }) } } @@ -182,3 +185,35 @@ function mergeChannelBans(cb) { } }).done(cb); } + +module.exports.deleteOldChannelTables = function (cb) { + Q.nfcall(db.query, "SHOW TABLES") + .then(function (rows) { + rows = rows.map(function (r) { + return r[Object.keys(r)[0]]; + }).filter(function (r) { + return r.match(/chan_(.*?)_(library|ranks|bans)$/); + }); + + var queue = []; + rows.forEach(function (table) { + queue.push(Q.nfcall(db.query, "DROP TABLE `" + table + "`") + .then(function () { + Logger.syslog.log("Deleted " + table); + }).catch(function (err) { + Logger.errlog.log("Deleting " + table + " failed: " + err); + if (err.stack) { + Logger.errlog.log(err.stack); + } + }) + ); + }); + + return Q.all(queue); + }).catch(function (err) { + Logger.errlog.log("Deleting old tables failed: " + err); + if (err.stack) { + Logger.errlog.log(err.stack); + } + }).done(cb); +};