diff --git a/package-lock.json b/package-lock.json index 3e737316c..72c9ddf43 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "trilium", - "version": "0.48.4", + "version": "0.48.6", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2850,9 +2850,9 @@ } }, "electron": { - "version": "13.6.1", - "resolved": "https://registry.npmjs.org/electron/-/electron-13.6.1.tgz", - "integrity": "sha512-rZ6Y7RberigruefQpWOiI4bA9ppyT88GQF8htY6N1MrAgal5RrBc+Mh92CcGU7zT9QO+XO3DarSgZafNTepffQ==", + "version": "13.6.2", + "resolved": "https://registry.npmjs.org/electron/-/electron-13.6.2.tgz", + "integrity": "sha512-ZXx9t68yXftvNZVnQ7v2XHcnH+MPUF6LNStoz4MMXuWpkF9gq3qwjcYSqnbM4wiVkvWVHIyYvt1yemmStza9dQ==", "dev": true, "requires": { "@electron/get": "^1.0.1", @@ -2861,9 +2861,9 @@ }, "dependencies": { "@types/node": { - "version": "14.17.32", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.32.tgz", - "integrity": "sha512-JcII3D5/OapPGx+eJ+Ik1SQGyt6WvuqdRfh9jUwL6/iHGjmyOriBDciBUu7lEIBTL2ijxwrR70WUnw5AEDmFvQ==", + "version": "14.17.33", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.33.tgz", + "integrity": "sha512-noEeJ06zbn3lOh4gqe2v7NMGS33jrulfNqYFDjjEbhpDEHR5VTxgYNQSBqBlJIsBJW3uEYDgD6kvMnrrhGzq8g==", "dev": true } } diff --git a/src/routes/api/sync.js b/src/routes/api/sync.js index c927103e3..a928540d2 100644 --- a/src/routes/api/sync.js +++ b/src/routes/api/sync.js @@ -204,6 +204,11 @@ function queueSector(req) { entityChangesService.addEntityChangesForSector(entityName, sector); } +function checkEntityChanges() { + const consistencyChecks = require("../../services/consistency_checks"); + consistencyChecks.runEntityChangesChecks(); +} + module.exports = { testSync, checkSync, @@ -215,5 +220,6 @@ module.exports = { update, getStats, syncFinished, - queueSector + queueSector, + checkEntityChanges }; diff --git a/src/routes/routes.js b/src/routes/routes.js index 5785e0c60..bacc34601 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -294,6 +294,7 @@ function register(app) { route(GET, '/api/sync/changed', [auth.checkApiAuth], syncApiRoute.getChanged, apiResultHandler); route(PUT, '/api/sync/update', [auth.checkApiAuth], syncApiRoute.update, apiResultHandler); route(POST, '/api/sync/finished', [auth.checkApiAuth], syncApiRoute.syncFinished, apiResultHandler); + route(POST, '/api/sync/check-entity-changes', [auth.checkApiAuth], syncApiRoute.checkEntityChanges, apiResultHandler); route(POST, '/api/sync/queue-sector/:entityName/:sector', [auth.checkApiAuth], syncApiRoute.queueSector, apiResultHandler); route(GET, '/api/sync/stats', [], syncApiRoute.getStats, apiResultHandler); diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.js index 8f15f8188..f12f930e3 100644 --- a/src/services/consistency_checks.js +++ b/src/services/consistency_checks.js @@ -701,6 +701,11 @@ function runOnDemandChecks(autoFix) { consistencyChecks.runChecks(); } +function runEntityChangesChecks() { + const consistencyChecks = new ConsistencyChecks(true); + consistencyChecks.findEntityChangeIssues(); +} + sqlInit.dbReady.then(() => { setInterval(cls.wrap(runPeriodicChecks), 60 * 60 * 1000); @@ -709,5 +714,6 @@ sqlInit.dbReady.then(() => { }); module.exports = { - runOnDemandChecks + runOnDemandChecks, + runEntityChangesChecks }; diff --git a/src/services/sync.js b/src/services/sync.js index cd14cc2c0..1dd2b6937 100644 --- a/src/services/sync.js +++ b/src/services/sync.js @@ -149,7 +149,10 @@ async function pullChanges(syncContext) { sql.transactional(() => { for (const {entityChange, entity} of entityChanges) { - if (!sourceIdService.isLocalSourceId(entityChange.sourceId)) { + // FIXME: temporary fix + const existsAlready = !!sql.getValue("SELECT id FROM entity_changes WHERE entityName = ? AND entityId = ? AND utcDateChanged = ? AND hash = ?", [entityChange.entityName, entityChange.entityId, entityChange.utcDateChanged, entityChange.hash]); + + if (!existsAlready && !sourceIdService.isLocalSourceId(entityChange.sourceId)) { if (!atLeastOnePullApplied) { // send only for first ws.syncPullInProgress(); @@ -249,6 +252,14 @@ async function checkContentHash(syncContext) { const failedChecks = contentHashService.checkContentHashes(resp.entityHashes); + if (failedChecks.length > 0) { + // before requeuing sectors make sure the entity changes are correct + const consistencyChecks = require("./consistency_checks"); + consistencyChecks.runEntityChangesChecks(); + + await syncRequest(syncContext, 'POST', `/api/sync/check-entity-changes`); + } + for (const {entityName, sector} of failedChecks) { entityChangesService.addEntityChangesForSector(entityName, sector);