From 06328929ecd3777e862de12f824d2aff29ad1257 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 22 Oct 2017 20:22:09 -0400 Subject: [PATCH] fix migration + cleanup unused Notecase tables in migration script --- migrations/0001__cleanup_tables.sql | 11 +++++ public/javascripts/note.js | 1 - routes/api/migration.js | 56 ++---------------------- routes/api/notes.js | 1 - routes/index.js | 13 +++++- services/backup.js | 2 +- services/migration.js | 66 +++++++++++++++++++++++++++++ services/sync.js | 6 ++- views/migration.ejs | 8 ++-- 9 files changed, 101 insertions(+), 63 deletions(-) create mode 100644 migrations/0001__cleanup_tables.sql create mode 100644 services/migration.js diff --git a/migrations/0001__cleanup_tables.sql b/migrations/0001__cleanup_tables.sql new file mode 100644 index 000000000..b78cf7626 --- /dev/null +++ b/migrations/0001__cleanup_tables.sql @@ -0,0 +1,11 @@ +DROP TABLE anchors; +DROP TABLE attachments; +DROP TABLE bookmarks; +DROP TABLE custom_properties; +DROP TABLE deletions; +DROP TABLE deletions_attachments; +DROP TABLE formatting; +DROP TABLE icons; +DROP TABLE tags; +DROP TABLE tags_notes; +DROP TABLE tasks; \ No newline at end of file diff --git a/public/javascripts/note.js b/public/javascripts/note.js index bb75994a7..3670ae498 100644 --- a/public/javascripts/note.js +++ b/public/javascripts/note.js @@ -59,7 +59,6 @@ $(document).ready(() => { }); function parseHtml(contents, note) { - note.formatting = []; note.links = []; note.images = []; diff --git a/routes/api/migration.js b/routes/api/migration.js index ee88f9a97..dc497fab9 100644 --- a/routes/api/migration.js +++ b/routes/api/migration.js @@ -3,68 +3,18 @@ const express = require('express'); const router = express.Router(); const auth = require('../../services/auth'); -const backup = require('../../services/backup'); const sql = require('../../services/sql'); -const fs = require('fs-extra'); - -const APP_DB_VERSION = 0; -const MIGRATIONS_DIR = "../trilium-data/migrations"; +const migration = require('../../services/migration'); router.get('', auth.checkApiAuth, async (req, res, next) => { res.send({ 'db_version': parseInt(await sql.getOption('db_version')), - 'app_db_version': APP_DB_VERSION + 'app_db_version': migration.APP_DB_VERSION }); }); router.post('', auth.checkApiAuth, async (req, res, next) => { - const migrations = []; - - await backup.backupNow(); - - const currentDbVersion = parseInt(await sql.getOption('db_version')); - - fs.readdirSync(MIGRATIONS_DIR).forEach(file => { - const match = file.match(/([0-9]{4})__([a-zA-Z0-9_ ]+)\.sql/); - - if (match) { - const dbVersion = parseInt(match.group(1)); - - if (dbVersion > currentDbVersion) { - const name = match.group(2); - - const migrationRecord = { - 'db_version': dbVersion, - 'name': name, - 'file': file - }; - - migrations.push(migrationRecord); - } - } - }); - - migrations.sort((a, b) => a.db_version - b.db_version); - - for (const mig of migrations) { - const migrationSql = fs.readFileSync(MIGRATIONS_DIR + "/" + mig.file); - - try { - await sql.beginTransaction(); - - await sql.executeScript(migrationSql); - - await sql.commit(); - - mig['success'] = true; - } - catch (e) { - mig['success'] = false; - mig['error'] = e.stack; - - break; - } - } + const migrations = await migration.migrate(); res.send({ 'migrations': migrations diff --git a/routes/api/notes.js b/routes/api/notes.js index 5e548c88e..9508488f5 100644 --- a/routes/api/notes.js +++ b/routes/api/notes.js @@ -21,7 +21,6 @@ router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => { res.send({ 'detail': detail, - 'formatting': await sql.getResults("select * from formatting where note_id = ? order by note_offset", [noteId]), 'links': await sql.getResults("select * from links where note_id = ? order by note_offset", [noteId]), 'images': await sql.getResults("select * from images where note_id = ? order by note_offset", [noteId]) }); diff --git a/routes/index.js b/routes/index.js index daab5ce07..b3ff65be3 100644 --- a/routes/index.js +++ b/routes/index.js @@ -3,9 +3,18 @@ const express = require('express'); const router = express.Router(); const auth = require('../services/auth'); +const migration = require('../services/migration'); +const sql = require('../services/sql'); -router.get('', auth.checkAuth, (req, res, next) => { - res.render('index', {}); +router.get('', auth.checkAuth, async (req, res, next) => { + const dbVersion = parseInt(await sql.getOption('db_version')) + + if (dbVersion < migration.APP_DB_VERSION) { + res.redirect("migration"); + } + else { + res.render('index', {}); + } }); module.exports = router; diff --git a/services/backup.js b/services/backup.js index 9a93c554b..8a26de3df 100644 --- a/services/backup.js +++ b/services/backup.js @@ -38,7 +38,7 @@ async function cleanupOldBackups() { const match = file.match(/backup-([0-9 -:]+)\.db/); if (match) { - const date_str = match.group(1); + const date_str = match[1]; const date = Date.parse(date_str); diff --git a/services/migration.js b/services/migration.js new file mode 100644 index 000000000..90111cf11 --- /dev/null +++ b/services/migration.js @@ -0,0 +1,66 @@ +const backup = require('./backup'); +const sql = require('./sql'); +const fs = require('fs-extra'); + +const APP_DB_VERSION = 1; +const MIGRATIONS_DIR = "./migrations"; + +async function migrate() { + const migrations = []; + + await backup.backupNow(); + + const currentDbVersion = parseInt(await sql.getOption('db_version')); + + fs.readdirSync(MIGRATIONS_DIR).forEach(file => { + const match = file.match(/([0-9]{4})__([a-zA-Z0-9_ ]+)\.sql/); + + if (match) { + const dbVersion = parseInt(match[1]); + + if (dbVersion > currentDbVersion) { + const name = match[2]; + + const migrationRecord = { + 'db_version': dbVersion, + 'name': name, + 'file': file + }; + + migrations.push(migrationRecord); + } + } + }); + + migrations.sort((a, b) => a.db_version - b.db_version); + + for (const mig of migrations) { + const migrationSql = fs.readFileSync(MIGRATIONS_DIR + "/" + mig.file).toString('utf8'); + + try { + console.log("Running script: ", migrationSql); + + await sql.beginTransaction(); + + await sql.executeScript(migrationSql); + + await sql.commit(); + + mig['success'] = true; + } + catch (e) { + mig['success'] = false; + mig['error'] = e.stack; + + console.log("error during migration: ", e); + + break; + } + } + return migrations; +} + +module.exports = { + migrate, + APP_DB_VERSION +}; \ No newline at end of file diff --git a/services/sync.js b/services/sync.js index 1ad726f86..4c32e0b01 100644 --- a/services/sync.js +++ b/services/sync.js @@ -1,3 +1,7 @@ "use strict"; -//setInterval(); \ No newline at end of file +function sync() { + +} + +setInterval(sync, 60000); \ No newline at end of file diff --git a/views/migration.ejs b/views/migration.ejs index 9137b334b..3c631e707 100644 --- a/views/migration.ejs +++ b/views/migration.ejs @@ -50,11 +50,11 @@ - + - - + + - + \ No newline at end of file