From 06be593fe4b72931a384c6744c7c632209e05874 Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 23 Apr 2019 21:27:45 +0200 Subject: [PATCH] crash if the DB has newer version than the application --- src/services/sql_init.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/services/sql_init.js b/src/services/sql_init.js index 3bed891a0..be1f782e9 100644 --- a/src/services/sql_init.js +++ b/src/services/sql_init.js @@ -6,6 +6,7 @@ const resourceDir = require('./resource_dir'); const appInfo = require('./app_info'); const sql = require('./sql'); const cls = require('./cls'); +const utils = require('./utils'); const optionService = require('./options'); const Option = require('../entities/option'); const ImportContext = require('../services/import_context'); @@ -58,6 +59,14 @@ async function initDbConnection() { await sql.execute("PRAGMA foreign_keys = ON"); + const currentDbVersion = await getDbVersion(); + + if (currentDbVersion > appInfo.dbVersion) { + 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(); + } + if (!await isDbUpToDate()) { // avoiding circular dependency const migrationService = require('./migration'); @@ -147,8 +156,12 @@ async function createDatabaseForSync(options, syncServerHost = '', syncProxy = ' log.info("Schema and not synced options generated."); } +async function getDbVersion() { + return parseInt(await sql.getValue("SELECT value FROM options WHERE name = 'dbVersion'")); +} + async function isDbUpToDate() { - const dbVersion = parseInt(await sql.getValue("SELECT value FROM options WHERE name = 'dbVersion'")); + const dbVersion = await getDbVersion(); const upToDate = dbVersion >= appInfo.dbVersion;