erasing rows of deleted entities

This commit is contained in:
zadam 2020-12-14 13:58:02 +01:00
parent 248fa780e8
commit 6d7b9e0db3
2 changed files with 18 additions and 19 deletions

View File

@ -268,24 +268,12 @@ async function syncRequest(syncContext, method, requestPath, body) {
return await utils.timeLimit(request.exec(opts), timeout); 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) { function getEntityChangeRow(entityName, entityId) {
if (entityName === 'note_reordering') { if (entityName === 'note_reordering') {
return sql.getMap("SELECT branchId, notePosition FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [entityId]); return sql.getMap("SELECT branchId, notePosition FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [entityId]);
} }
else { else {
const primaryKey = primaryKeys[entityName]; const primaryKey = entityConstructor.getEntityFromEntityName(entityName).primaryKeyName;
if (!primaryKey) { if (!primaryKey) {
throw new Error("Unknown entity " + entityName); throw new Error("Unknown entity " + entityName);

View File

@ -1,11 +1,12 @@
const sql = require('./sql'); const sql = require('./sql');
const entityChangesService = require('./entity_changes.js'); const entityChangesService = require('./entity_changes.js');
const eventService = require('./events'); const eventService = require('./events');
const entityConstructor = require('../entities/entity_constructor');
function updateEntity(entityChange, entity, sourceId) { function updateEntity(entityChange, entity, sourceId) {
// can be undefined for options with isSynced=false // can be undefined for options with isSynced=false
if (!entity) { if (!entity) {
return false; return;
} }
const updated = entityChange.entityName === 'note_reordering' 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 // currently making exception for protected notes and note revisions because here
// the title and content are not available decrypted as listeners would expect // 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, { eventService.emit(eventService.ENTITY_SYNCED, {
entityName: entityChange.entityName, entityName: entityChange.entityName,
entity entity
}); });
} }
return updated;
} }
function updateNormalEntity(entityChange, entity, sourceId) { function updateNormalEntity(entityChange, entity, sourceId) {
const {utcDateChanged, hash} = sql.getRow(` const {utcDateChanged, hash, isErased} = sql.getRow(`
SELECT utcDateChanged, hash SELECT utcDateChanged, hash, isErased
FROM entity_changes FROM entity_changes
WHERE entityName = ? AND entityId = ?`, [entityChange.entityName, entityChange.entityId]); 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 if (utcDateChanged < entityChange.utcDateChanged
|| hash !== entityChange.hash // sync error, we should still update || hash !== entityChange.hash // sync error, we should still update
) { ) {