sync/src/database/update.js

131 lines
4.3 KiB
JavaScript
Raw Normal View History

2014-02-01 12:41:06 -06:00
var db = require("../database");
import Promise from 'bluebird';
2017-04-04 23:02:31 -07:00
2017-07-08 20:11:54 -07:00
const LOGGER = require('@calzoneman/jsli')('database/update');
2017-03-13 21:05:32 -07:00
const DB_VERSION = 11;
2014-06-11 08:56:06 -07:00
var hasUpdates = [];
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) {
2017-04-04 23:02:31 -07:00
LOGGER.warn("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);
if (v >= DB_VERSION) {
return;
}
var next = function () {
2014-06-11 08:56:06 -07:00
hasUpdates.push(v);
2017-04-04 23:02:31 -07:00
LOGGER.info("Updated database to version " + v);
if (v < DB_VERSION) {
2014-06-24 20:28:04 -07:00
update(v++, next);
} else {
db.query("UPDATE `meta` SET `value`=? WHERE `key`='db_version'",
[DB_VERSION]);
}
};
update(v++, next);
}
});
};
function update(version, cb) {
if (version < 7) {
LOGGER.error('Cannot auto-upgrade: db_version 4 is too old!');
process.exit(1);
} else if (version < 8) {
addUsernameDedupeColumn(cb);
} else if (version < 9) {
populateUsernameDedupeColumn(cb);
2017-03-13 20:55:06 -07:00
} else if (version < 10) {
addChannelLastLoadedColumn(cb);
2017-03-13 21:05:32 -07:00
} else if (version < 11) {
addChannelOwnerLastSeenColumn(cb);
2014-06-02 20:47:21 -07:00
}
}
function addUsernameDedupeColumn(cb) {
2017-04-04 23:02:31 -07:00
LOGGER.info("Adding name_dedupe column on the users table");
db.query("ALTER TABLE users ADD COLUMN name_dedupe VARCHAR(20) UNIQUE DEFAULT NULL", (error) => {
if (error) {
2017-04-04 23:02:31 -07:00
LOGGER.error(`Unable to add name_dedupe column: ${error}`);
} else {
cb();
}
});
}
function populateUsernameDedupeColumn(cb) {
const dbUsers = require("./accounts");
2017-04-04 23:02:31 -07:00
LOGGER.info("Populating name_dedupe column on the users table");
db.query("SELECT id, name FROM users WHERE name_dedupe IS NULL", (err, rows) => {
if (err) {
2017-04-04 23:02:31 -07:00
LOGGER.error("Unable to perform database upgrade to add dedupe column: " + err);
return;
}
Promise.map(rows, row => {
2017-05-28 22:39:27 -07:00
const dedupedName = dbUsers.dedupeUsername(row.name);
LOGGER.info(`Deduping [${row.name}] as [${dedupedName}]`);
return db.getDB().knex.raw("UPDATE users SET name_dedupe = ? WHERE id = ?", [dedupedName, row.id])
.catch(error => {
if (error.errno === 1062) {
LOGGER.info(`WARNING: could not set name_dedupe for [${row.name}] due to an existing row for [${dedupedName}]`);
} else {
throw error;
}
});
}, { concurrency: 10 }).then(() => {
cb();
}).catch(error => {
2017-04-04 23:02:31 -07:00
LOGGER.error("Unable to perform database upgrade to add dedupe column: " + (error.stack ? error.stack : error));
})
});
}
2017-03-13 20:55:06 -07:00
function addChannelLastLoadedColumn(cb) {
db.query("ALTER TABLE channels ADD COLUMN last_loaded TIMESTAMP NOT NULL DEFAULT 0", error => {
if (error) {
2017-04-04 23:02:31 -07:00
LOGGER.error(`Failed to add last_loaded column: ${error}`);
2017-03-13 20:55:06 -07:00
return;
}
db.query("ALTER TABLE channels ADD INDEX i_last_loaded (last_loaded)", error => {
if (error) {
2017-04-04 23:02:31 -07:00
LOGGER.error(`Failed to add index on last_loaded column: ${error}`);
2017-03-13 20:55:06 -07:00
return;
}
cb();
});
});
}
2017-03-13 21:05:32 -07:00
function addChannelOwnerLastSeenColumn(cb) {
db.query("ALTER TABLE channels ADD COLUMN owner_last_seen TIMESTAMP NOT NULL DEFAULT 0", error => {
if (error) {
2017-04-04 23:02:31 -07:00
LOGGER.error(`Failed to add owner_last_seen column: ${error}`);
2017-03-13 21:05:32 -07:00
return;
}
db.query("ALTER TABLE channels ADD INDEX i_owner_last_seen (owner_last_seen)", error => {
if (error) {
2017-04-04 23:02:31 -07:00
LOGGER.error(`Failed to add index on owner_last_seen column: ${error}`);
2017-03-13 21:05:32 -07:00
return;
}
cb();
});
});
}