2018-04-01 21:27:46 -04:00
|
|
|
const backupService = require('./backup');
|
2017-10-22 20:22:09 -04:00
|
|
|
const sql = require('./sql');
|
2018-04-02 21:25:20 -04:00
|
|
|
const sqlInit = require('./sql_init');
|
2018-04-01 21:27:46 -04:00
|
|
|
const optionService = require('./options');
|
2017-10-22 20:22:09 -04:00
|
|
|
const fs = require('fs-extra');
|
2017-10-24 22:17:48 -04:00
|
|
|
const log = require('./log');
|
2018-11-18 09:05:50 +01:00
|
|
|
const utils = require('./utils');
|
2018-04-01 21:27:46 -04:00
|
|
|
const resourceDir = require('./resource_dir');
|
2017-10-22 20:22:09 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function migrate() {
|
2017-10-22 20:22:09 -04:00
|
|
|
const migrations = [];
|
|
|
|
|
2017-10-24 22:17:48 -04:00
|
|
|
// backup before attempting migration
|
2020-06-20 12:31:38 +02:00
|
|
|
backupService.backupNow("before-migration");
|
2017-10-22 20:22:09 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const currentDbVersion = parseInt(optionService.getOption('dbVersion'));
|
2017-10-22 20:22:09 -04:00
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
fs.readdirSync(resourceDir.MIGRATIONS_DIR).forEach(file => {
|
2017-11-15 22:13:45 -05:00
|
|
|
const match = file.match(/([0-9]{4})__([a-zA-Z0-9_ ]+)\.(sql|js)/);
|
2017-10-22 20:22:09 -04:00
|
|
|
|
|
|
|
if (match) {
|
|
|
|
const dbVersion = parseInt(match[1]);
|
|
|
|
|
|
|
|
if (dbVersion > currentDbVersion) {
|
|
|
|
const name = match[2];
|
2017-11-15 22:13:45 -05:00
|
|
|
const type = match[3];
|
2017-10-22 20:22:09 -04:00
|
|
|
|
|
|
|
const migrationRecord = {
|
2017-10-22 20:29:31 -04:00
|
|
|
dbVersion: dbVersion,
|
|
|
|
name: name,
|
2017-11-15 22:13:45 -05:00
|
|
|
file: file,
|
|
|
|
type: type
|
2017-10-22 20:22:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
migrations.push(migrationRecord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-11-05 18:59:58 -05:00
|
|
|
migrations.sort((a, b) => a.dbVersion - b.dbVersion);
|
2017-10-22 20:22:09 -04:00
|
|
|
|
|
|
|
for (const mig of migrations) {
|
|
|
|
try {
|
2017-11-15 22:13:45 -05:00
|
|
|
log.info("Attempting migration to version " + mig.dbVersion);
|
2017-10-22 20:22:09 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
sql.transactional(() => {
|
2017-11-15 22:13:45 -05:00
|
|
|
if (mig.type === 'sql') {
|
2018-04-01 21:27:46 -04:00
|
|
|
const migrationSql = fs.readFileSync(resourceDir.MIGRATIONS_DIR + "/" + mig.file).toString('utf8');
|
2017-11-15 22:13:45 -05:00
|
|
|
|
|
|
|
console.log("Migration with SQL script: " + migrationSql);
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
sql.executeScript(migrationSql);
|
2017-11-15 22:13:45 -05:00
|
|
|
}
|
|
|
|
else if (mig.type === 'js') {
|
|
|
|
console.log("Migration with JS module");
|
|
|
|
|
2018-07-24 20:35:03 +02:00
|
|
|
const migrationModule = require(resourceDir.MIGRATIONS_DIR + "/" + mig.file);
|
2020-06-20 12:31:38 +02:00
|
|
|
migrationModule();
|
2017-11-15 22:13:45 -05:00
|
|
|
}
|
|
|
|
else {
|
2017-12-06 20:58:59 -05:00
|
|
|
throw new Error("Unknown migration type " + mig.type);
|
2017-11-15 22:13:45 -05:00
|
|
|
}
|
2017-10-22 20:22:09 -04:00
|
|
|
|
2019-03-13 22:43:59 +01:00
|
|
|
// not using repository because of changed utcDateModified column in migration 129
|
2020-06-20 12:31:38 +02:00
|
|
|
sql.execute(`UPDATE options SET value = ? WHERE name = ?`, [mig.dbVersion, "dbVersion"]);
|
2017-10-29 18:50:28 -04:00
|
|
|
});
|
2017-10-22 20:22:09 -04:00
|
|
|
|
2017-10-24 22:17:48 -04:00
|
|
|
log.info("Migration to version " + mig.dbVersion + " has been successful.");
|
2017-10-22 20:22:09 -04:00
|
|
|
}
|
|
|
|
catch (e) {
|
2017-10-24 22:17:48 -04:00
|
|
|
log.error("error during migration to version " + mig.dbVersion + ": " + e.stack);
|
2018-07-24 20:35:03 +02:00
|
|
|
log.error("migration failed, crashing hard"); // this is not very user friendly :-/
|
2017-10-22 20:22:09 -04:00
|
|
|
|
2018-11-18 09:05:50 +01:00
|
|
|
utils.crash();
|
2017-10-22 20:22:09 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 22:17:48 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
if (sqlInit.isDbUpToDate()) {
|
|
|
|
sqlInit.initDbConnection();
|
2017-12-12 23:35:41 -05:00
|
|
|
}
|
2017-10-22 20:22:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2017-12-10 12:56:59 -05:00
|
|
|
migrate
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|