Add update function to add meta column

This commit is contained in:
Calvin Montgomery 2014-06-02 20:47:21 -07:00
parent f2769e5062
commit ac7f0ac47a

View file

@ -1,7 +1,8 @@
var db = require("../database");
var Logger = require("../logger");
var Q = require("q");
const DB_VERSION = 1;
const DB_VERSION = 2;
module.exports.checkVersion = function () {
db.query("SELECT `key`,`value` FROM `meta` WHERE `key`=?", ["db_version"], function (err, rows) {
@ -32,5 +33,31 @@ module.exports.checkVersion = function () {
};
function update(version, cb) {
setImmediate(cb);
if (version === 1) {
addMetaColumnToLibraries(cb);
}
}
function addMetaColumnToLibraries(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(/_library$/);
});
var queue = [];
rows.forEach(function (table) {
queue.push(Q.nfcall(db.query, "ALTER TABLE `" + table + "` ADD meta TEXT")
.then(function () {
Logger.syslog.log("Added meta column to " + table);
})
);
});
return Q.all(queue);
}).catch(function (err) {
Logger.errlog.log("Adding meta column to library tables failed: " + err);
}).done(cb);
}