mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
execute all DB migrations in one transaction
This commit is contained in:
parent
186e06bc01
commit
af654a171f
@ -6,25 +6,6 @@ const utils = require('./utils');
|
|||||||
const resourceDir = require('./resource_dir');
|
const resourceDir = require('./resource_dir');
|
||||||
const appInfo = require('./app_info');
|
const appInfo = require('./app_info');
|
||||||
|
|
||||||
function executeMigration(mig) {
|
|
||||||
sql.transactional(() => {
|
|
||||||
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 {
|
|
||||||
throw new Error(`Unknown migration type ${mig.type}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function migrate() {
|
async function migrate() {
|
||||||
const migrations = [];
|
const migrations = [];
|
||||||
|
|
||||||
@ -64,22 +45,43 @@ async function migrate() {
|
|||||||
|
|
||||||
migrations.sort((a, b) => a.dbVersion - b.dbVersion);
|
migrations.sort((a, b) => a.dbVersion - b.dbVersion);
|
||||||
|
|
||||||
for (const mig of migrations) {
|
// all migrations are executed in one transaction - upgrade either succeeds or the user can stay at the old version
|
||||||
try {
|
sql.transactional(() => {
|
||||||
log.info(`Attempting migration to version ${mig.dbVersion}`);
|
for (const mig of migrations) {
|
||||||
|
try {
|
||||||
|
log.info(`Attempting migration to version ${mig.dbVersion}`);
|
||||||
|
|
||||||
executeMigration(mig);
|
executeMigration(mig);
|
||||||
|
|
||||||
sql.execute(`UPDATE options SET value = ? WHERE name = ?`, [mig.dbVersion.toString(), "dbVersion"]);
|
sql.execute(`UPDATE options
|
||||||
|
SET value = ?
|
||||||
|
WHERE name = ?`, [mig.dbVersion.toString(), "dbVersion"]);
|
||||||
|
|
||||||
log.info(`Migration to version ${mig.dbVersion} has been successful.`);
|
log.info(`Migration to version ${mig.dbVersion} has been successful.`);
|
||||||
|
} catch (e) {
|
||||||
|
log.error(`error during migration to version ${mig.dbVersion}: ${e.stack}`);
|
||||||
|
log.error("migration failed, crashing hard"); // this is not very user friendly :-/
|
||||||
|
|
||||||
|
utils.crash();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (e) {
|
});
|
||||||
log.error(`error during migration to version ${mig.dbVersion}: ${e.stack}`);
|
}
|
||||||
log.error("migration failed, crashing hard"); // this is not very user friendly :-/
|
|
||||||
|
|
||||||
utils.crash();
|
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 {
|
||||||
|
throw new Error(`Unknown migration type ${mig.type}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user