Notes/src/services/migration.js

128 lines
4.1 KiB
JavaScript
Raw Normal View History

const backupService = require('./backup');
const sql = require('./sql');
const fs = require('fs-extra');
2017-10-24 22:17:48 -04:00
const log = require('./log');
const utils = require('./utils');
const resourceDir = require('./resource_dir');
2020-07-02 22:57:17 +02:00
const appInfo = require('./app_info');
2020-07-02 22:57:17 +02:00
async function migrate() {
const currentDbVersion = getDbVersion();
if (currentDbVersion < 183) {
log.error("Direct migration from your current version is not supported. Please upgrade to the latest v0.47.X first and only then to this version.");
utils.crash();
return;
}
// backup before attempting migration
await backupService.backupNow(
// special name for the pre-0.60 migration to prevent later overwrite
currentDbVersion < 214
? `before-migration-v${currentDbVersion}`
: 'before-migration'
);
const migrations = fs.readdirSync(resourceDir.MIGRATIONS_DIR).map(file => {
2022-12-27 21:17:40 +01:00
const match = file.match(/^([0-9]{4})__([a-zA-Z0-9_ ]+)\.(sql|js)$/);
if (!match) {
return null;
}
const dbVersion = parseInt(match[1]);
if (dbVersion > currentDbVersion) {
const name = match[2];
const type = match[3];
return {
dbVersion: dbVersion,
name: name,
file: file,
type: type
};
} else {
return null;
}
}).filter(el => !!el);
migrations.sort((a, b) => a.dbVersion - b.dbVersion);
2023-05-05 23:41:11 +02:00
// all migrations are executed in one transaction - upgrade either succeeds, or the user can stay at the old version
2022-12-22 19:02:41 +01:00
// otherwise if half of the migrations succeed, user can't use any version - DB is too "new" for the old app,
// and too old for the new app version.
sql.transactional(() => {
for (const mig of migrations) {
try {
log.info(`Attempting migration to version ${mig.dbVersion}`);
executeMigration(mig);
sql.execute(`UPDATE options
SET value = ?
WHERE name = ?`, [mig.dbVersion.toString(), "dbVersion"]);
2021-01-30 22:17:29 +01:00
log.info(`Migration to version ${mig.dbVersion} has been successful.`);
} catch (e) {
log.error(`error during migration to version ${mig.dbVersion}: ${e.stack}`);
2022-12-27 14:52:32 +01:00
log.error("migration failed, crashing hard"); // this is not very user-friendly :-/
utils.crash();
2022-12-27 14:52:32 +01:00
break; // crash() above does not seem to work right away
}
}
});
}
function executeMigration(mig) {
if (mig.type === 'sql') {
const migrationSql = fs.readFileSync(`${resourceDir.MIGRATIONS_DIR}/${mig.file}`).toString('utf8');
console.log(`Migration with SQL script: ${migrationSql}`);
sql.executeScript(migrationSql);
} else if (mig.type === 'js') {
console.log("Migration with JS module");
const migrationModule = require(`${resourceDir.MIGRATIONS_DIR}/${mig.file}`);
migrationModule();
} else {
2023-06-29 22:10:13 +02:00
throw new Error(`Unknown migration type '${mig.type}'`);
}
2020-07-02 22:57:17 +02:00
}
function getDbVersion() {
return parseInt(sql.getValue("SELECT value FROM options WHERE name = 'dbVersion'"));
}
function isDbUpToDate() {
const dbVersion = getDbVersion();
const upToDate = dbVersion >= appInfo.dbVersion;
if (!upToDate) {
log.info(`App db version is ${appInfo.dbVersion}, while db version is ${dbVersion}. Migration needed.`);
2020-07-02 22:57:17 +02:00
}
return upToDate;
}
2017-10-24 22:17:48 -04:00
2020-07-02 22:57:17 +02:00
async function migrateIfNecessary() {
const currentDbVersion = getDbVersion();
if (currentDbVersion > appInfo.dbVersion && process.env.TRILIUM_IGNORE_DB_VERSION !== 'true') {
2023-06-29 22:10:13 +02:00
log.error(`Current DB version ${currentDbVersion} is newer than the current DB version ${appInfo.dbVersion}, which means that it was created by a newer and incompatible version of Trilium. Upgrade to the latest version of Trilium to resolve this issue.`);
2020-07-02 22:57:17 +02:00
utils.crash();
}
2020-07-02 22:57:17 +02:00
if (!isDbUpToDate()) {
await migrate();
}
}
module.exports = {
migrateIfNecessary,
isDbUpToDate
2020-06-20 12:31:38 +02:00
};