Add console command for deleting old channel tables

This commit is contained in:
Calvin Montgomery 2014-06-23 22:15:57 -07:00
parent c768d9595c
commit 36c4e41131
2 changed files with 44 additions and 3 deletions

View file

@ -47,5 +47,11 @@ function handleLine(line) {
} else { } else {
Logger.syslog.log("Failed to invoke GC: node started without --expose-gc"); 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");
}
});
} }
} }

View file

@ -26,8 +26,7 @@ module.exports.checkVersion = function () {
var next = function () { var next = function () {
hasUpdates.push(v); hasUpdates.push(v);
if (v < DB_VERSION) { if (v < DB_VERSION) {
update(v++, next); update(v++, next); } else {
} else {
db.query("UPDATE `meta` SET `value`=? WHERE `key`='db_version'", db.query("UPDATE `meta` SET `value`=? WHERE `key`='db_version'",
[DB_VERSION]); [DB_VERSION]);
} }
@ -45,7 +44,11 @@ function update(version, cb) {
Q.fcall(mergeChannelLibraries), Q.fcall(mergeChannelLibraries),
Q.fcall(mergeChannelRanks), Q.fcall(mergeChannelRanks),
Q.fcall(mergeChannelBans) 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); }).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);
};