From 9f19cf80463bf5ae804120860302066d3cfb2353 Mon Sep 17 00:00:00 2001 From: zadam Date: Fri, 12 Feb 2021 22:39:38 +0100 Subject: [PATCH] don't sync recent_notes --- ...82__remove_recentChanges_from_entityChanges.sql | 14 ++++++++++++++ src/entities/entity.js | 2 +- src/entities/recent_note.js | 10 ++-------- src/routes/api/recent_notes.js | 9 +++++++++ src/services/app_info.js | 4 ++-- src/services/consistency_checks.js | 1 - src/services/note_cache/similarity.js | 3 ++- src/services/repository.js | 4 ++++ 8 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 db/migrations/0182__remove_recentChanges_from_entityChanges.sql diff --git a/db/migrations/0182__remove_recentChanges_from_entityChanges.sql b/db/migrations/0182__remove_recentChanges_from_entityChanges.sql new file mode 100644 index 000000000..c09b6191a --- /dev/null +++ b/db/migrations/0182__remove_recentChanges_from_entityChanges.sql @@ -0,0 +1,14 @@ +DELETE FROM entity_changes WHERE entityName = 'recent_notes'; + +CREATE TABLE IF NOT EXISTS "mig_recent_notes" +( + noteId TEXT not null primary key, + notePath TEXT not null, + utcDateCreated TEXT not null +); + +INSERT INTO mig_recent_notes (noteId, notePath, utcDateCreated) + SELECT noteId, notePath, utcDateCreated FROM recent_notes; + +DROP TABLE recent_notes; +ALTER TABLE mig_recent_notes RENAME TO recent_notes; diff --git a/src/entities/entity.js b/src/entities/entity.js index 37a0efb53..1bb652583 100644 --- a/src/entities/entity.js +++ b/src/entities/entity.js @@ -15,7 +15,7 @@ class Entity { } } - if ('isDeleted' in this) { + if ('isDeleted' in this && this.constructor.entityName !== 'recent_notes') { this.isDeleted = !!this.isDeleted; } } diff --git a/src/entities/recent_note.js b/src/entities/recent_note.js index 45012a731..f4ef899e6 100644 --- a/src/entities/recent_note.js +++ b/src/entities/recent_note.js @@ -8,21 +8,15 @@ const dateUtils = require('../services/date_utils'); * * @property {string} noteId * @property {string} notePath - * @property {boolean} isDeleted - * @property {string} utcDateModified + * @property {string} utcDateCreated * * @extends Entity */ class RecentNote extends Entity { static get entityName() { return "recent_notes"; } static get primaryKeyName() { return "noteId"; } - static get hashedProperties() { return ["noteId", "notePath", "utcDateCreated", "isDeleted"]; } beforeSaving() { - if (!this.isDeleted) { - this.isDeleted = false; - } - if (!this.utcDateCreated) { this.utcDateCreated = dateUtils.utcNowDateTime(); } @@ -31,4 +25,4 @@ class RecentNote extends Entity { } } -module.exports = RecentNote; \ No newline at end of file +module.exports = RecentNote; diff --git a/src/routes/api/recent_notes.js b/src/routes/api/recent_notes.js index 678c1de43..2b6594f4d 100644 --- a/src/routes/api/recent_notes.js +++ b/src/routes/api/recent_notes.js @@ -1,12 +1,21 @@ "use strict"; const RecentNote = require('../../entities/recent_note'); +const sql = require('../../services/sql'); +const dateUtils = require('../../services/date_utils'); function addRecentNote(req) { new RecentNote({ noteId: req.body.noteId, notePath: req.body.notePath }).save(); + + if (Math.random() < 0.05) { + // it's not necessary to run this everytime ... + const cutOffDate = dateUtils.utcDateStr(new Date(Date.now() - 24 * 3600 * 1000)); + + sql.execute(`DELETE FROM recent_notes WHERE utcDateCreated < ?`, [cutOffDate]); + } } module.exports = { diff --git a/src/services/app_info.js b/src/services/app_info.js index 070570165..b3fcd9a73 100644 --- a/src/services/app_info.js +++ b/src/services/app_info.js @@ -4,8 +4,8 @@ const build = require('./build'); const packageJson = require('../../package'); const {TRILIUM_DATA_DIR} = require('./data_dir'); -const APP_DB_VERSION = 181; -const SYNC_VERSION = 19; +const APP_DB_VERSION = 182; +const SYNC_VERSION = 20; const CLIPPER_PROTOCOL_VERSION = "1.0"; module.exports = { diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.js index 014772a12..f225f90a5 100644 --- a/src/services/consistency_checks.js +++ b/src/services/consistency_checks.js @@ -505,7 +505,6 @@ class ConsistencyChecks { this.runEntityChangeChecks("note_contents", "noteId"); this.runEntityChangeChecks("note_revisions", "noteRevisionId"); this.runEntityChangeChecks("branches", "branchId"); - this.runEntityChangeChecks("recent_notes", "noteId"); this.runEntityChangeChecks("attributes", "attributeId"); this.runEntityChangeChecks("api_tokens", "apiTokenId"); this.runEntityChangeChecks("options", "name"); diff --git a/src/services/note_cache/similarity.js b/src/services/note_cache/similarity.js index ddca8c306..116f51267 100644 --- a/src/services/note_cache/similarity.js +++ b/src/services/note_cache/similarity.js @@ -1,4 +1,5 @@ const noteCache = require('./note_cache'); +const log = require('../log'); const noteCacheService = require('./note_cache_service.js'); const dateUtils = require('../date_utils'); const repository = require('../repository'); @@ -333,7 +334,7 @@ async function findSimilarNotes(noteId) { let value = attr.value; let factor = 1; - if (!value) { + if (!value.startsWith) { log.info(`Unexpected falsy value for attribute ${JSON.stringify(attr.pojo)}`); continue; } diff --git a/src/services/repository.js b/src/services/repository.js index 898b5721f..9d8190876 100644 --- a/src/services/repository.js +++ b/src/services/repository.js @@ -111,6 +111,10 @@ function updateEntity(entity) { sql.transactional(() => { sql.upsert(entityName, primaryKeyName, clone); + if (entityName === 'recent_notes') { + return; + } + const entityId = entity[primaryKeyName]; const isSynced = entityName !== 'options' || entity.isSynced;