fix migration + cleanup unused Notecase tables in migration script

This commit is contained in:
azivner 2017-10-22 20:22:09 -04:00
parent dcdabe79d1
commit 06328929ec
9 changed files with 101 additions and 63 deletions

View File

@ -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;

View File

@ -59,7 +59,6 @@ $(document).ready(() => {
});
function parseHtml(contents, note) {
note.formatting = [];
note.links = [];
note.images = [];

View File

@ -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

View File

@ -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])
});

View File

@ -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;

View File

@ -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);

66
services/migration.js Normal file
View File

@ -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
};

View File

@ -1,3 +1,7 @@
"use strict";
//setInterval();
function sync() {
}
setInterval(sync, 60000);

View File

@ -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>