Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix migration + cleanup unused Notecase tables in migration script
azivner06328929ecd3
diff --git a/migrations/0001__cleanup_tables.sql b/migrations/0001__cleanup_tables.sqlnew file mode 100644index 0000000000..b78cf76269--- /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 filediff --git a/public/javascripts/note.js b/public/javascripts/note.jsindex bb75994a7e..3670ae4987 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.jsindex ee88f9a975..dc497fab96 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': migrationsdiff --git a/routes/api/notes.js b/routes/api/notes.jsindex 5e548c88eb..9508488f56 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.jsindex daab5ce076..b3ff65be3e 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.jsindex 9a93c554b2..8a26de3dff 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.jsnew file mode 100644index 0000000000..90111cf11d--- /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 filediff --git a/services/sync.js b/services/sync.jsindex 1ad726f869..4c32e0b013 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 filediff --git a/views/migration.ejs b/views/migration.ejsindex 9137b334b0..3c631e7078 100644--- a/views/migration.ejs+++ b/views/migration.ejs@@ -50,11 +50,11 @@<!-- Required for correct loading of scripts in Electron --><script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>- <script src="stat/lib/jquery.min.js"></script>+ <script src="libraries/jquery.min.js"></script>- <link href="stat/lib/bootstrap/css/bootstrap.css" rel="stylesheet">- <script src="stat/lib/bootstrap/js/bootstrap.js"></script>+ <link href="libraries/bootstrap/css/bootstrap.css" rel="stylesheet">+ <script src="libraries/bootstrap/js/bootstrap.js"></script>- <script src="stat/js/migration.js"></script>+ <script src="javascripts/migration.js"></script></body></html>\ No newline at end of file