Notes/src/services/migration.ts

147 lines
4.3 KiB
TypeScript
Raw Normal View History

import backupService from "./backup.js";
import sql from "./sql.js";
import fs from "fs-extra";
import log from "./log.js";
import { crash } from "./utils.js";
import resourceDir from "./resource_dir.js";
import appInfo from "./app_info.js";
import cls from "./cls.js";
import { t } from "i18next";
2024-02-17 19:42:30 +02:00
interface MigrationInfo {
dbVersion: number;
name: string;
file: string;
type: string;
}
async function migrate() {
const currentDbVersion = getDbVersion();
if (currentDbVersion < 214) {
await crash(t("migration.old_version"));
2024-02-17 19:42:30 +02:00
return;
}
// backup before attempting migration
await backupService.backupNow(
// creating a special backup for version 0.60.4, the changes in 0.61 are major.
2025-01-09 18:07:02 +02:00
currentDbVersion === 214 ? `before-migration-v060` : "before-migration"
2024-02-17 19:42:30 +02:00
);
const migrationFiles = fs.readdirSync(resourceDir.MIGRATIONS_DIR);
if (migrationFiles == null) {
return;
}
2025-01-09 18:07:02 +02:00
const migrations = migrationFiles
.map((file) => {
const match = file.match(/^([0-9]{4})__([a-zA-Z0-9_ ]+)\.(sql|js)$/);
if (!match) {
return null;
}
2024-02-17 19:42:30 +02:00
2025-01-09 18:07:02 +02:00
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 is MigrationInfo => !!el);
2024-02-17 19:42:30 +02:00
migrations.sort((a, b) => a.dbVersion - b.dbVersion);
// all migrations are executed in one transaction - upgrade either succeeds, or the user can stay at the old version
// 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.
cls.setMigrationRunning(true);
sql.transactional(async () => {
2024-02-17 19:42:30 +02:00
for (const mig of migrations) {
try {
log.info(`Attempting migration to version ${mig.dbVersion}`);
await executeMigration(mig);
2024-02-17 19:42:30 +02:00
2025-01-09 18:07:02 +02:00
sql.execute(
`UPDATE options
SET value = ?
2025-01-09 18:07:02 +02:00
WHERE name = ?`,
[mig.dbVersion.toString(), "dbVersion"]
);
2024-02-17 19:42:30 +02:00
log.info(`Migration to version ${mig.dbVersion} has been successful.`);
} catch (e: any) {
crash(t("migration.error_message", { version: mig.dbVersion, stack: e.stack }));
2024-07-18 23:26:21 +03:00
break; // crash() is sometimes async
2024-02-17 19:42:30 +02:00
}
}
});
if (currentDbVersion === 214) {
// special VACUUM after the big migration
log.info("VACUUMing database, this might take a while ...");
sql.execute("VACUUM");
}
}
async function executeMigration(mig: MigrationInfo) {
2025-01-09 18:07:02 +02:00
if (mig.type === "sql") {
const migrationSql = fs.readFileSync(`${resourceDir.MIGRATIONS_DIR}/${mig.file}`).toString("utf8");
2024-02-17 19:42:30 +02:00
console.log(`Migration with SQL script: ${migrationSql}`);
sql.executeScript(migrationSql);
2025-01-09 18:07:02 +02:00
} else if (mig.type === "js") {
2024-02-17 19:42:30 +02:00
console.log("Migration with JS module");
const migrationModule = await import(`${resourceDir.MIGRATIONS_DIR}/${mig.file}`);
await migrationModule.default();
2024-02-17 19:42:30 +02:00
} else {
throw new Error(`Unknown migration type '${mig.type}'`);
}
}
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.`);
}
return upToDate;
}
async function migrateIfNecessary() {
const currentDbVersion = getDbVersion();
2025-01-09 18:07:02 +02:00
if (currentDbVersion > appInfo.dbVersion && process.env.TRILIUM_IGNORE_DB_VERSION !== "true") {
await crash(t("migration.wrong_db_version", { version: currentDbVersion, targetVersion: appInfo.dbVersion }));
2024-02-17 19:42:30 +02:00
}
if (!isDbUpToDate()) {
await migrate();
}
}
export default {
2024-02-17 19:42:30 +02:00
migrateIfNecessary,
isDbUpToDate
};