mirror of https://github.com/calzoneman/sync.git
37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
|
var db = require("./database");
|
||
|
var Logger = require("./logger");
|
||
|
|
||
|
const DB_VERSION = 1;
|
||
|
|
||
|
module.exports.checkVersion = function () {
|
||
|
db.query("SELECT `key`,`value` FROM `meta` WHERE `key`=?", ["db_version"], function (err, rows) {
|
||
|
if (err) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (rows.length === 0) {
|
||
|
Logger.errlog.log("[Warning] db_version key missing from database. Setting " +
|
||
|
"db_version=" + DB_VERSION);
|
||
|
db.query("INSERT INTO `meta` (`key`, `value`) VALUES ('db_version', ?)",
|
||
|
[DB_VERSION],
|
||
|
function (err) {
|
||
|
});
|
||
|
} else {
|
||
|
var v = parseInt(rows[0].value);
|
||
|
var next = function () {
|
||
|
if (v < DB_VERSION) {
|
||
|
update(v++, next);
|
||
|
} else {
|
||
|
db.query("UPDATE `meta` SET `value`=? WHERE `key`='db_version'",
|
||
|
[DB_VERSION]);
|
||
|
}
|
||
|
};
|
||
|
update(v++, next);
|
||
|
}
|
||
|
});
|
||
|
};
|
||
|
|
||
|
function update(version, cb) {
|
||
|
setImmediate(cb);
|
||
|
}
|