From cab54a458f84e5823d8d5bce24c701ced8a3ff71 Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 26 May 2018 23:25:09 -0400 Subject: [PATCH] unifying surrogate keys for event log and options, fixes #103 --- db/migrations/0096__unify_surrogate_keys.sql | 29 +++++++++++++++++++ .../services/note_detail_render.js | 10 +++---- src/routes/api/event_log.js | 10 ------- src/services/app_info.js | 2 +- src/services/event_log.js | 8 +++-- src/services/sync.js | 3 +- 6 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 db/migrations/0096__unify_surrogate_keys.sql diff --git a/db/migrations/0096__unify_surrogate_keys.sql b/db/migrations/0096__unify_surrogate_keys.sql new file mode 100644 index 000000000..a7e49ac7e --- /dev/null +++ b/db/migrations/0096__unify_surrogate_keys.sql @@ -0,0 +1,29 @@ +CREATE TABLE `event_log_mig` ( + `eventId` TEXT NOT NULL PRIMARY KEY, + `noteId` TEXT, + `comment` TEXT, + `dateCreated` TEXT NOT NULL +); + +INSERT INTO event_log_mig (eventId, noteId, comment, dateCreated) +SELECT id, noteId, comment, dateCreated FROM event_log; + +DROP TABLE event_log; +ALTER TABLE event_log_mig RENAME TO event_log; + +create table options_mig +( + optionId TEXT NOT NULL PRIMARY KEY, + name TEXT not null, + value TEXT, + dateModified INT, + isSynced INTEGER default 0 not null, + hash TEXT default "" not null, + dateCreated TEXT default '1970-01-01T00:00:00.000Z' not null +); + +INSERT INTO options_mig (optionId, name, value, dateModified, isSynced, hash, dateCreated) + SELECT name || "_key", name, value, dateModified, isSynced, hash, dateCreated FROM options; + +DROP TABLE options; +ALTER TABLE options_mig RENAME TO options; \ No newline at end of file diff --git a/src/public/javascripts/services/note_detail_render.js b/src/public/javascripts/services/note_detail_render.js index 4f212830c..24357e0af 100644 --- a/src/public/javascripts/services/note_detail_render.js +++ b/src/public/javascripts/services/note_detail_render.js @@ -13,11 +13,6 @@ let codeEditorInitialized; async function show() { codeEditorInitialized = false; - // if the note is empty, it doesn't make sense to do render-only since nothing will be rendered - if (!noteDetailService.getCurrentNote().content.trim()) { - toggleEdit(); - } - $noteDetailRender.show(); await render(); @@ -61,6 +56,11 @@ async function render() { $noteDetailRender.html(bundle.html); + // if the note is empty, it doesn't make sense to do render-only since nothing will be rendered + if (!bundle.html.trim()) { + toggleEdit(); + } + await bundleService.executeBundle(bundle); } diff --git a/src/routes/api/event_log.js b/src/routes/api/event_log.js index 3e8256a3c..d5c7c3f21 100644 --- a/src/routes/api/event_log.js +++ b/src/routes/api/event_log.js @@ -3,19 +3,9 @@ const sql = require('../../services/sql'); async function getEventLog() { - await deleteOld(); - return await sql.getRows("SELECT * FROM event_log ORDER BY dateCreated DESC"); } -async function deleteOld() { - const cutoffId = await sql.getValue("SELECT id FROM event_log ORDER BY id DESC LIMIT 1000, 1"); - - if (cutoffId) { - await sql.execute("DELETE FROM event_log WHERE id < ?", [cutoffId]); - } -} - module.exports = { getEventLog }; \ No newline at end of file diff --git a/src/services/app_info.js b/src/services/app_info.js index 33d9f74f5..4511c75fb 100644 --- a/src/services/app_info.js +++ b/src/services/app_info.js @@ -3,7 +3,7 @@ const build = require('./build'); const packageJson = require('../../package'); -const APP_DB_VERSION = 95; +const APP_DB_VERSION = 96; module.exports = { appVersion: packageJson.version, diff --git a/src/services/event_log.js b/src/services/event_log.js index bc6c3d01b..7a5f77b98 100644 --- a/src/services/event_log.js +++ b/src/services/event_log.js @@ -1,5 +1,6 @@ const sql = require('./sql'); const dateUtils = require('./date_utils'); +const utils = require('./utils'); const log = require('./log'); async function addEvent(comment) { @@ -8,9 +9,10 @@ async function addEvent(comment) { async function addNoteEvent(noteId, comment) { await sql.insert('event_log', { - noteId : noteId, - comment: comment, - dateCreated: dateUtils.nowDate() + eventId: utils.newEntityId(), + noteId : noteId, + comment: comment, + dateCreated: dateUtils.nowDate() }); log.info("Event log for " + noteId + ": " + comment); diff --git a/src/services/sync.js b/src/services/sync.js index 38bef6a85..ed254fc10 100644 --- a/src/services/sync.js +++ b/src/services/sync.js @@ -207,13 +207,12 @@ const primaryKeys = { "notes": "noteId", "branches": "branchId", "note_revisions": "noteRevisionId", - "option": "name", "recent_notes": "branchId", "images": "imageId", "note_images": "noteImageId", "labels": "labelId", "api_tokens": "apiTokenId", - "options": "name" + "options": "optionId" }; async function getEntityRow(entityName, entityId) {