mirror of
https://github.com/zadam/trilium.git
synced 2025-02-24 15:05:31 +08:00
erasing rows of deleted entities
This commit is contained in:
parent
248fa780e8
commit
6d7b9e0db3
2 changed files with 18 additions and 19 deletions
|
@ -268,24 +268,12 @@ async function syncRequest(syncContext, method, requestPath, body) {
|
|||
return await utils.timeLimit(request.exec(opts), timeout);
|
||||
}
|
||||
|
||||
const primaryKeys = {
|
||||
"notes": "noteId",
|
||||
"note_contents": "noteId",
|
||||
"branches": "branchId",
|
||||
"note_revisions": "noteRevisionId",
|
||||
"note_revision_contents": "noteRevisionId",
|
||||
"recent_notes": "noteId",
|
||||
"api_tokens": "apiTokenId",
|
||||
"options": "name",
|
||||
"attributes": "attributeId"
|
||||
};
|
||||
|
||||
function getEntityChangeRow(entityName, entityId) {
|
||||
if (entityName === 'note_reordering') {
|
||||
return sql.getMap("SELECT branchId, notePosition FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [entityId]);
|
||||
}
|
||||
else {
|
||||
const primaryKey = primaryKeys[entityName];
|
||||
const primaryKey = entityConstructor.getEntityFromEntityName(entityName).primaryKeyName;
|
||||
|
||||
if (!primaryKey) {
|
||||
throw new Error("Unknown entity " + entityName);
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
const sql = require('./sql');
|
||||
const entityChangesService = require('./entity_changes.js');
|
||||
const eventService = require('./events');
|
||||
const entityConstructor = require('../entities/entity_constructor');
|
||||
|
||||
function updateEntity(entityChange, entity, sourceId) {
|
||||
// can be undefined for options with isSynced=false
|
||||
if (!entity) {
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
|
||||
const updated = entityChange.entityName === 'note_reordering'
|
||||
|
@ -14,22 +15,32 @@ function updateEntity(entityChange, entity, sourceId) {
|
|||
|
||||
// currently making exception for protected notes and note revisions because here
|
||||
// the title and content are not available decrypted as listeners would expect
|
||||
if (updated && !entity.isProtected) {
|
||||
if (updated && !entity.isProtected && !entityChange.isErased) {
|
||||
eventService.emit(eventService.ENTITY_SYNCED, {
|
||||
entityName: entityChange.entityName,
|
||||
entity
|
||||
});
|
||||
}
|
||||
|
||||
return updated;
|
||||
}
|
||||
|
||||
function updateNormalEntity(entityChange, entity, sourceId) {
|
||||
const {utcDateChanged, hash} = sql.getRow(`
|
||||
SELECT utcDateChanged, hash
|
||||
const {utcDateChanged, hash, isErased} = sql.getRow(`
|
||||
SELECT utcDateChanged, hash, isErased
|
||||
FROM entity_changes
|
||||
WHERE entityName = ? AND entityId = ?`, [entityChange.entityName, entityChange.entityId]);
|
||||
|
||||
if (!isErased && entityChange.isErased) {
|
||||
sql.transactional(() => {
|
||||
const primaryKey = entityConstructor.getEntityFromEntityName(entityName).primaryKeyName;
|
||||
|
||||
sql.execute(`DELETE FROM ${entityChange.entityName} WHERE ${primaryKey} = ?`, entityChange.entityId);
|
||||
|
||||
entityChangesService.addEntityChange(entityChange, sourceId);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (utcDateChanged < entityChange.utcDateChanged
|
||||
|| hash !== entityChange.hash // sync error, we should still update
|
||||
) {
|
||||
|
|
Loading…
Reference in a new issue