From 03bf33630e2a45b57d0f50019d229a3c337794fc Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 26 May 2018 12:38:25 -0400 Subject: [PATCH] unify audit fields, fixes #102 --- db/migrations/0094__unify_auditing_fields.sql | 30 +++++++++++++++++++ src/entities/branch.js | 6 +++- src/entities/recent_note.js | 15 +++++++++- src/public/javascripts/dialogs/event_log.js | 2 +- src/routes/api/event_log.js | 2 +- src/routes/api/recent_notes.js | 6 ++-- src/services/app_info.js | 2 +- src/services/event_log.js | 2 +- src/services/sync_update.js | 2 +- 9 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 db/migrations/0094__unify_auditing_fields.sql diff --git a/db/migrations/0094__unify_auditing_fields.sql b/db/migrations/0094__unify_auditing_fields.sql new file mode 100644 index 000000000..387528071 --- /dev/null +++ b/db/migrations/0094__unify_auditing_fields.sql @@ -0,0 +1,30 @@ +ALTER TABLE branches ADD dateCreated TEXT NOT NULL DEFAULT '1970-01-01T00:00:00.000Z'; + +CREATE TABLE `event_log_mig` ( + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + `noteId` TEXT, + `comment` TEXT, + `dateCreated` TEXT NOT NULL +); + +INSERT INTO event_log_mig (id, noteId, comment, dateCreated) +SELECT id, noteId, comment, dateAdded FROM event_log; + +DROP TABLE event_log; +ALTER TABLE event_log_mig RENAME TO event_log; + +ALTER TABLE options ADD dateCreated TEXT NOT NULL DEFAULT '1970-01-01T00:00:00.000Z'; + +CREATE TABLE `recent_notes_mig` ( + `branchId` TEXT NOT NULL PRIMARY KEY, + `notePath` TEXT NOT NULL, + hash TEXT DEFAULT "" NOT NULL, + `dateCreated` TEXT NOT NULL, + isDeleted INT +); + +INSERT INTO recent_notes_mig (branchId, notePath, hash, dateCreated, isDeleted) +SELECT branchId, notePath, hash, dateAccessed, isDeleted FROM recent_notes; + +DROP TABLE recent_notes; +ALTER TABLE recent_notes_mig RENAME TO recent_notes; \ No newline at end of file diff --git a/src/entities/branch.js b/src/entities/branch.js index 4d3dde86b..36d237df1 100644 --- a/src/entities/branch.js +++ b/src/entities/branch.js @@ -27,7 +27,11 @@ class Branch extends Entity { this.isDeleted = false; } - this.dateModified = dateUtils.nowDate() + if (!this.dateCreated) { + this.dateCreated = dateUtils.nowDate(); + } + + this.dateModified = dateUtils.nowDate(); } } diff --git a/src/entities/recent_note.js b/src/entities/recent_note.js index 2e4e1a2dc..42f6a343a 100644 --- a/src/entities/recent_note.js +++ b/src/entities/recent_note.js @@ -1,11 +1,24 @@ "use strict"; const Entity = require('./entity'); +const dateUtils = require('../services/date_utils'); class RecentNote extends Entity { static get tableName() { return "recent_notes"; } static get primaryKeyName() { return "branchId"; } - static get hashedProperties() { return ["branchId", "notePath", "dateAccessed", "isDeleted"]; } + static get hashedProperties() { return ["branchId", "notePath", "dateCreated", "isDeleted"]; } + + beforeSaving() { + super.beforeSaving(); + + if (!this.isDeleted) { + this.isDeleted = false; + } + + if (!this.dateCreated) { + this.dateCreated = dateUtils.nowDate(); + } + } } module.exports = RecentNote; \ No newline at end of file diff --git a/src/public/javascripts/dialogs/event_log.js b/src/public/javascripts/dialogs/event_log.js index fada33f19..9130bae3f 100644 --- a/src/public/javascripts/dialogs/event_log.js +++ b/src/public/javascripts/dialogs/event_log.js @@ -19,7 +19,7 @@ async function showDialog() { $list.html(''); for (const event of result) { - const dateTime = utils.formatDateTime(utils.parseDate(event.dateAdded)); + const dateTime = utils.formatDateTime(utils.parseDate(event.dateCreated)); if (event.noteId) { const noteLink = await linkService.createNoteLink(event.noteId).prop('outerHTML'); diff --git a/src/routes/api/event_log.js b/src/routes/api/event_log.js index 42b14752d..3e8256a3c 100644 --- a/src/routes/api/event_log.js +++ b/src/routes/api/event_log.js @@ -5,7 +5,7 @@ const sql = require('../../services/sql'); async function getEventLog() { await deleteOld(); - return await sql.getRows("SELECT * FROM event_log ORDER BY dateAdded DESC"); + return await sql.getRows("SELECT * FROM event_log ORDER BY dateCreated DESC"); } async function deleteOld() { diff --git a/src/routes/api/recent_notes.js b/src/routes/api/recent_notes.js index a5b0c0cf3..14d779f5b 100644 --- a/src/routes/api/recent_notes.js +++ b/src/routes/api/recent_notes.js @@ -16,7 +16,7 @@ async function getRecentNotes() { recent_notes.isDeleted = 0 AND branches.isDeleted = 0 ORDER BY - dateAccessed DESC + dateCreated DESC LIMIT 200`); } @@ -26,9 +26,7 @@ async function addRecentNote(req) { await new RecentNote({ branchId: branchId, - notePath: notePath, - dateAccessed: dateUtils.nowDate(), - isDeleted: 0 + notePath: notePath }).save(); await optionService.setOption('startNotePath', notePath); diff --git a/src/services/app_info.js b/src/services/app_info.js index f6976a77f..d0692660e 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 = 93; +const APP_DB_VERSION = 94; module.exports = { appVersion: packageJson.version, diff --git a/src/services/event_log.js b/src/services/event_log.js index 914a65b97..bc6c3d01b 100644 --- a/src/services/event_log.js +++ b/src/services/event_log.js @@ -10,7 +10,7 @@ async function addNoteEvent(noteId, comment) { await sql.insert('event_log', { noteId : noteId, comment: comment, - dateAdded: dateUtils.nowDate() + dateCreated: dateUtils.nowDate() }); log.info("Event log for " + noteId + ": " + comment); diff --git a/src/services/sync_update.js b/src/services/sync_update.js index d7b2dd70a..8c0abd9e1 100644 --- a/src/services/sync_update.js +++ b/src/services/sync_update.js @@ -127,7 +127,7 @@ async function updateOptions(entity, sourceId) { async function updateRecentNotes(entity, sourceId) { const orig = await sql.getRowOrNull("SELECT * FROM recent_notes WHERE branchId = ?", [entity.branchId]); - if (orig === null || orig.dateAccessed < entity.dateAccessed) { + if (orig === null || orig.dateCreated < entity.dateCreated) { await sql.transactional(async () => { await sql.replace('recent_notes', entity);