From 60d000b9cbc796c0f9bfed7ce8bddc3ec8ef08d1 Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 30 Jan 2021 22:12:38 +0100 Subject: [PATCH] VACUUM for migration --- ...5__rename_eraseNotesAfterTimeInSeconds.sql | 2 +- db/migrations/0179__VACUUM.sql | 1 + src/services/app_info.js | 2 +- src/services/migration.js | 50 +++++++++++-------- 4 files changed, 32 insertions(+), 23 deletions(-) create mode 100644 db/migrations/0179__VACUUM.sql diff --git a/db/migrations/0175__rename_eraseNotesAfterTimeInSeconds.sql b/db/migrations/0175__rename_eraseNotesAfterTimeInSeconds.sql index ebdbc314f..b84e9e31c 100644 --- a/db/migrations/0175__rename_eraseNotesAfterTimeInSeconds.sql +++ b/db/migrations/0175__rename_eraseNotesAfterTimeInSeconds.sql @@ -1,2 +1,2 @@ UPDATE options SET name = 'eraseEntitiesAfterTimeInSeconds' WHERE name = 'eraseNotesAfterTimeInSeconds'; -UPDATE entity_changes SET entityName = 'eraseEntitiesAfterTimeInSeconds' WHERE entityName = 'eraseNotesAfterTimeInSeconds'; +UPDATE entity_changes SET entityId = 'eraseEntitiesAfterTimeInSeconds' WHERE entityName = 'options' AND entityId = 'eraseNotesAfterTimeInSeconds'; diff --git a/db/migrations/0179__VACUUM.sql b/db/migrations/0179__VACUUM.sql new file mode 100644 index 000000000..78434d08a --- /dev/null +++ b/db/migrations/0179__VACUUM.sql @@ -0,0 +1 @@ +VACUUM diff --git a/src/services/app_info.js b/src/services/app_info.js index 23464fe6d..706e973f6 100644 --- a/src/services/app_info.js +++ b/src/services/app_info.js @@ -4,7 +4,7 @@ const build = require('./build'); const packageJson = require('../../package'); const {TRILIUM_DATA_DIR} = require('./data_dir'); -const APP_DB_VERSION = 178; +const APP_DB_VERSION = 179; const SYNC_VERSION = 19; const CLIPPER_PROTOCOL_VERSION = "1.0"; diff --git a/src/services/migration.js b/src/services/migration.js index 72e32b669..2b9afc860 100644 --- a/src/services/migration.js +++ b/src/services/migration.js @@ -7,6 +7,28 @@ const utils = require('./utils'); const resourceDir = require('./resource_dir'); 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); + } + + // not using repository because of changed utcDateModified column in migration 129 + sql.execute(`UPDATE options SET value = ? WHERE name = ?`, [mig.dbVersion.toString(), "dbVersion"]); + }); +} + async function migrate() { const migrations = []; @@ -43,27 +65,13 @@ async function migrate() { try { log.info("Attempting migration to version " + mig.dbVersion); - 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); - } - - // not using repository because of changed utcDateModified column in migration 129 - sql.execute(`UPDATE options SET value = ? WHERE name = ?`, [mig.dbVersion.toString(), "dbVersion"]); - }); + if (mig.name === 'VACUUM') { + // special case since VACUUM can't be executed in a transaction + sql.execute('VACUUM'); + } + else { + executeMigration(mig); + } log.info("Migration to version " + mig.dbVersion + " has been successful."); }