From 4b779d7512e351147f3f5a15ba480854b4e7d9b8 Mon Sep 17 00:00:00 2001 From: zadam Date: Tue, 27 Dec 2022 09:53:25 +0100 Subject: [PATCH] small refactors in sync --- src/services/entity_changes.js | 59 +++++++++++++++------------------- src/services/sync_update.js | 8 ++--- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/services/entity_changes.js b/src/services/entity_changes.js index 62eabc944..a1009cd6c 100644 --- a/src/services/entity_changes.js +++ b/src/services/entity_changes.js @@ -8,7 +8,7 @@ const becca = require("../becca/becca"); let maxEntityChangeId = 0; -function addEntityChangeWithinstanceId(origEntityChange, instanceId) { +function addEntityChangeWithInstanceId(origEntityChange, instanceId) { const ec = {...origEntityChange, instanceId}; return addEntityChange(ec); @@ -85,45 +85,38 @@ function cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey) { } function fillEntityChanges(entityName, entityPrimaryKey, condition = '') { - try { - cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey); + cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey); - sql.transactional(() => { - const entityIds = sql.getColumn(`SELECT ${entityPrimaryKey} FROM ${entityName}` - + (condition ? ` WHERE ${condition}` : '')); + sql.transactional(() => { + const entityIds = sql.getColumn(`SELECT ${entityPrimaryKey} FROM ${entityName}` + + (condition ? ` WHERE ${condition}` : '')); - let createdCount = 0; + let createdCount = 0; - for (const entityId of entityIds) { - const existingRows = sql.getValue("SELECT COUNT(1) FROM entity_changes WHERE entityName = ? AND entityId = ?", [entityName, entityId]); + for (const entityId of entityIds) { + const existingRows = sql.getValue("SELECT COUNT(1) FROM entity_changes WHERE entityName = ? AND entityId = ?", [entityName, entityId]); - // we don't want to replace existing entities (which would effectively cause full resync) - if (existingRows === 0) { - createdCount++; + // we don't want to replace existing entities (which would effectively cause full resync) + if (existingRows === 0) { + createdCount++; - const entity = becca.getEntity(entityName, entityId); + const entity = becca.getEntity(entityName, entityId); - addEntityChange({ - entityName, - entityId, - hash: entity.generateHash(), - isErased: false, - utcDateChanged: entity.getUtcDateChanged(), - isSynced: entityName !== 'options' || !!entity.isSynced - }); - } + addEntityChange({ + entityName, + entityId, + hash: entity.generateHash(), + isErased: false, + utcDateChanged: entity.getUtcDateChanged(), + isSynced: entityName !== 'options' || !!entity.isSynced + }); } + } - if (createdCount > 0) { - log.info(`Created ${createdCount} missing entity changes for ${entityName}.`); - } - }); - } - catch (e) { - // this is to fix migration from 0.30 to 0.32, can be removed later - // see https://github.com/zadam/trilium/issues/557 - log.error(`Filling entity changes failed for ${entityName} ${entityPrimaryKey} with error "${e.message}", continuing`); - } + if (createdCount > 0) { + log.info(`Created ${createdCount} missing entity changes for ${entityName}.`); + } + }); } function fillAllEntityChanges() { @@ -145,7 +138,7 @@ module.exports = { addNoteReorderingEntityChange, moveEntityChangeToTop, addEntityChange, - addEntityChangeWithinstanceId, + addEntityChangeWithInstanceId, fillAllEntityChanges, addEntityChangesForSector, getMaxEntityChangeId: () => maxEntityChangeId diff --git a/src/services/sync_update.js b/src/services/sync_update.js index 2492452e4..e46bb8eae 100644 --- a/src/services/sync_update.js +++ b/src/services/sync_update.js @@ -54,7 +54,7 @@ function updateNormalEntity(remoteEntityChange, entity, instanceId) { sql.execute(`DELETE FROM ${remoteEntityChange.entityName} WHERE ${primaryKey} = ?`, remoteEntityChange.entityId); - entityChangesService.addEntityChangeWithinstanceId(remoteEntityChange, instanceId); + entityChangesService.addEntityChangeWithInstanceId(remoteEntityChange, instanceId); }); return true; @@ -71,7 +71,7 @@ function updateNormalEntity(remoteEntityChange, entity, instanceId) { sql.transactional(() => { sql.replace(remoteEntityChange.entityName, entity); - entityChangesService.addEntityChangeWithinstanceId(remoteEntityChange, instanceId); + entityChangesService.addEntityChangeWithInstanceId(remoteEntityChange, instanceId); }); return true; @@ -86,7 +86,7 @@ function updateNoteReordering(entityChange, entity, instanceId) { sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [entity[key], key]); } - entityChangesService.addEntityChangeWithinstanceId(entityChange, instanceId); + entityChangesService.addEntityChangeWithInstanceId(entityChange, instanceId); }); return true; @@ -119,7 +119,7 @@ function eraseEntity(entityChange, instanceId) { eventService.emit(eventService.ENTITY_DELETE_SYNCED, { entityName, entityId }); - entityChangesService.addEntityChangeWithinstanceId(entityChange, instanceId); + entityChangesService.addEntityChangeWithInstanceId(entityChange, instanceId); } module.exports = {