2018-04-01 21:27:46 -04:00
const backupService = require ( './backup' ) ;
2017-10-22 20:22:09 -04:00
const sql = require ( './sql' ) ;
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' ) ;
2020-07-02 22:57:17 +02:00
const appInfo = require ( './app_info' ) ;
2017-10-22 20:22:09 -04:00
2021-01-30 22:12:38 +01:00
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 ) ;
}
} ) ;
}
2020-07-02 22:57:17 +02:00
async 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-07-02 22:57:17 +02:00
await backupService . backupNow ( "before-migration" ) ;
2017-10-22 20:22:09 -04:00
2021-09-23 22:08:23 +02:00
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 ;
}
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
2021-09-23 22:49:16 +02:00
executeMigration ( mig ) ;
2017-10-22 20:22:09 -04:00
2021-01-30 22:17:29 +01:00
sql . execute ( ` UPDATE options SET value = ? WHERE name = ? ` , [ mig . dbVersion . toString ( ) , "dbVersion" ] ) ;
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
}
}
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." ) ;
}
return upToDate ;
}
2017-10-24 22:17:48 -04:00
2020-07-02 22:57:17 +02:00
async function migrateIfNecessary ( ) {
const currentDbVersion = getDbVersion ( ) ;
2022-06-06 22:27:06 +02:00
if ( currentDbVersion > appInfo . dbVersion && process . env . TRILIUM _IGNORE _DB _VERSION !== 'true' ) {
2020-07-02 22:57:17 +02:00
log . error ( ` Current DB version ${ currentDbVersion } is newer than app db version ${ appInfo . dbVersion } which means that it was created by newer and incompatible version of Trilium. Upgrade to latest version of Trilium to resolve this issue. ` ) ;
utils . crash ( ) ;
}
2020-06-23 13:38:27 +02:00
2020-07-02 22:57:17 +02:00
if ( ! isDbUpToDate ( ) ) {
await migrate ( ) ;
2017-12-12 23:35:41 -05:00
}
2017-10-22 20:22:09 -04:00
}
module . exports = {
2020-07-02 22:57:17 +02:00
migrateIfNecessary
2020-06-20 12:31:38 +02:00
} ;