trilium/services/migration.js

96 lines
2.7 KiB
JavaScript
Raw Normal View History

const backup = require('./backup');
const sql = require('./sql');
2017-11-03 08:48:02 +08:00
const options = require('./options');
const fs = require('fs-extra');
2017-10-25 10:17:48 +08:00
const log = require('./log');
2017-12-04 11:29:23 +08:00
const app_info = require('./app_info');
const path = require('path');
const MIGRATIONS_DIR = path.resolve(__dirname, "..", "migrations");
2017-12-07 09:58:59 +08:00
if (!fs.existsSync(MIGRATIONS_DIR)) {
log.error("Could not find migration directory: " + MIGRATIONS_DIR);
process.exit(1);
}
async function migrate() {
const migrations = [];
2017-10-25 10:17:48 +08:00
// backup before attempting migration
await backup.backupNow();
2017-11-03 08:48:02 +08:00
const currentDbVersion = parseInt(await options.getOption('db_version'));
fs.readdirSync(MIGRATIONS_DIR).forEach(file => {
const match = file.match(/([0-9]{4})__([a-zA-Z0-9_ ]+)\.(sql|js)/);
if (match) {
const dbVersion = parseInt(match[1]);
if (dbVersion > currentDbVersion) {
const name = match[2];
const type = match[3];
const migrationRecord = {
2017-10-23 08:29:31 +08:00
dbVersion: dbVersion,
name: name,
file: file,
type: type
};
migrations.push(migrationRecord);
}
}
});
migrations.sort((a, b) => a.dbVersion - b.dbVersion);
for (const mig of migrations) {
try {
log.info("Attempting migration to version " + mig.dbVersion);
await sql.doInTransaction(async () => {
if (mig.type === 'sql') {
const migrationSql = fs.readFileSync(MIGRATIONS_DIR + "/" + mig.file).toString('utf8');
console.log("Migration with SQL script: " + migrationSql);
await sql.executeScript(migrationSql);
}
else if (mig.type === 'js') {
console.log("Migration with JS module");
const migrationModule = require("../" + MIGRATIONS_DIR + "/" + mig.file);
await migrationModule(db);
}
else {
2017-12-07 09:58:59 +08:00
throw new Error("Unknown migration type " + mig.type);
}
await options.setOption("db_version", mig.dbVersion);
});
2017-10-25 10:17:48 +08:00
log.info("Migration to version " + mig.dbVersion + " has been successful.");
mig['success'] = true;
}
catch (e) {
mig['success'] = false;
mig['error'] = e.stack;
2017-10-25 10:17:48 +08:00
log.error("error during migration to version " + mig.dbVersion + ": " + e.stack);
break;
}
}
2017-10-25 10:17:48 +08:00
if (sql.isDbUpToDate()) {
sql.setDbReadyAsResolved();
}
return migrations;
}
module.exports = {
migrate
};