Skip to content

Build your personal knowledge base with Trilium Notes

unify audit fields, fixes #102

azivner03bf33630e2a
diff --git a/db/migrations/0094__unify_auditing_fields.sql b/db/migrations/0094__unify_auditing_fields.sql
new file mode 100644
index 0000000000..3875280716
--- /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 4d3dde86b5..36d237df1b 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 2e4e1a2dc8..42f6a343ab 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 fada33f19d..9130bae3fa 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 42b14752d2..3e8256a3c7 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 a5b0c0cf37..14d779f5bf 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 f6976a77f7..d0692660e1 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 914a65b971..bc6c3d01b5 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 d7b2dd70a3..8c0abd9e19 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);