use .markAsDeleted() instead of setting .isDeleted manually

This commit is contained in:
zadam 2021-05-02 20:32:50 +02:00
parent 273d4e0052
commit c035627f0a
12 changed files with 42 additions and 68 deletions

View File

@ -543,8 +543,7 @@ class Note extends Entity {
for (const attribute of attributes) {
if (attribute.type === type && attribute.name === name && (value === undefined || value === attribute.value)) {
attribute.isDeleted = true;
attribute.save();
attribute.markAsDeleted();
this.invalidateAttributeCache();
}

View File

@ -24,7 +24,7 @@ const TPL = `
More complex example would be deleting all matched note's attributes:
<pre>for (const attr of note.getOwnedAttributes) { attr.isDeleted = true; attr.save(); }</pre>
<pre>for (const attr of note.getOwnedAttributes) { attr.markAsDeleted(); }</pre>
</div>
</div>

View File

@ -36,8 +36,7 @@ function updateNoteAttribute(req) {
newAttribute.save();
}
attribute.isDeleted = true;
attribute.save();
attribute.markAsDeleted();
return {
attributeId: newAttribute ? newAttribute.attributeId : null
@ -61,7 +60,7 @@ function updateNoteAttribute(req) {
}
else {
// relations should never have empty target
attribute.isDeleted = true;
attribute.markAsDeleted();
}
attribute.save();
@ -108,7 +107,7 @@ function deleteNoteAttribute(req) {
return [400, `Attribute ${attributeId} is not owned by ${noteId}`];
}
attribute.markAttributeAsDeleted();
attribute.markAsDeleted();
}
}
@ -174,8 +173,7 @@ function updateNoteAttributes(req) {
// all the remaining existing attributes are not defined anymore and should be deleted
for (const toDeleteAttr of existingAttrs) {
if (!toDeleteAttr.isAutoLink()) {
toDeleteAttr.isDeleted = true;
toDeleteAttr.save();
toDeleteAttr.markAsDeleted();
}
}
}
@ -221,8 +219,7 @@ function deleteRelation(req) {
let attribute = repository.getEntity(`SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [sourceNoteId, name, targetNoteId]);
if (attribute) {
attribute.isDeleted = true;
attribute.save();
attribute.markAsDeleted();
}
}

View File

@ -44,7 +44,7 @@ function moveBranchToParent(req) {
const newBranch = branchToMove.createClone(parentBranch.noteId, newNotePos);
newBranch.save();
branchToMove.isDeleted = true;
branchToMove.markAsDeleted();
branchToMove.save();
return { success: true };
@ -85,8 +85,7 @@ function moveBranchBeforeNote(req) {
const newBranch = branchToMove.createClone(beforeBranch.parentNoteId, beforeBranch.notePosition);
newBranch.save();
branchToMove.isDeleted = true;
branchToMove.save();
branchToMove.markAsDeleted();
}
return { success: true };
@ -121,8 +120,7 @@ function moveBranchAfterNote(req) {
const newBranch = branchToMove.createClone(afterNote.parentNoteId, movedNotePosition);
newBranch.save();
branchToMove.isDeleted = true;
branchToMove.save();
branchToMove.markAsDeleted();
}
return { success: true };

View File

@ -59,22 +59,19 @@ async function searchFromNote(req) {
const ACTION_HANDLERS = {
deleteNote: (action, note) => {
note.isDeleted = true;
note.save();
note.markAsDeleted();
},
deleteNoteRevisions: (action, note) => {
noteRevisionService.eraseNoteRevisions(note.getNoteRevisions().map(rev => rev.noteRevisionId));
},
deleteLabel: (action, note) => {
for (const label of note.getOwnedLabels(action.labelName)) {
label.isDeleted = true;
label.save();
label.markAsDeleted();
}
},
deleteRelation: (action, note) => {
for (const relation of note.getOwnedRelations(action.relationName)) {
relation.isDeleted = true;
relation.save();
relation.markAsDeleted();
}
},
renameLabel: (action, note) => {

View File

@ -120,8 +120,7 @@ class Attribute extends AbstractEntity {
position: this.position,
value: this.value,
isInheritable: this.isInheritable,
utcDateModified: dateUtils.utcNowDateTime(),
isDeleted: false
utcDateModified: dateUtils.utcNowDateTime()
};
}
@ -143,10 +142,6 @@ class Attribute extends AbstractEntity {
this.isInheritable = false;
}
if (!this.isDeleted) {
this.isDeleted = false;
}
super.beforeSaving();
}
@ -158,13 +153,12 @@ class Attribute extends AbstractEntity {
value: value,
position: this.position,
isInheritable: isInheritable,
isDeleted: false,
utcDateModified: this.utcDateModified
});
}
markAttributeAsDeleted() {
sql.execute("UPDATE attributes SET isDeleted = 1 WHERE attributeId = ?", [this.attributeId]);
markAsDeleted(deleteId = null) {
sql.execute("UPDATE attributes SET isDeleted = 1, deleteId = ? WHERE attributeId = ?", [deleteId, this.attributeId]);
// FIXME: this needs to be published into entity_changes (for sync and becca cleanup)
}

View File

@ -99,6 +99,7 @@ class Branch extends AbstractEntity {
}
beforeSaving() {
// TODO can be refactored into becca
if (this.notePosition === undefined || this.notePosition === null) {
const maxNotePos = sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [this.parentNoteId]);
this.notePosition = maxNotePos === null ? 0 : maxNotePos + 10;
@ -110,6 +111,13 @@ class Branch extends AbstractEntity {
super.beforeSaving();
}
markAsDeleted(deleteId = null) {
sql.execute("UPDATE branches SET isDeleted = 1, deleteId = ? WHERE branchId = ?",
[deleteId, this.branchId]);
// FIXME: this needs to be published into entity_changes (for sync and becca cleanup)
}
}
module.exports = Branch;

View File

@ -864,8 +864,8 @@ class Note extends AbstractEntity {
this.utcDateModified = dateUtils.utcNowDateTime();
}
markAsDeleted() {
sql.execute("UPDATE notes SET isDeleted = 1 WHERE noteId = ?", [this.noteId]);
markAsDeleted(deleteId = null) {
sql.execute("UPDATE notes SET isDeleted = 1, deleteId = ? WHERE noteId = ?", [deleteId, this.noteId]);
// FIXME: this needs to be published into entity_changes (for sync and becca cleanup)
}

View File

@ -103,8 +103,7 @@ class ConsistencyChecks {
({branchId, noteId}) => {
if (this.autoFix) {
const branch = becca.getBranch(branchId);
branch.isDeleted = true;
branch.save();
branch.markAsDeleted();
logFix(`Branch ${branchId} has been deleted since it references missing note ${noteId}`);
} else {
@ -140,8 +139,7 @@ class ConsistencyChecks {
({attributeId, noteId}) => {
if (this.autoFix) {
const attribute = becca.getAttribute(attributeId);
attribute.isDeleted = true;
attribute.save();
attribute.markAsDeleted();
logFix(`Attribute ${attributeId} has been deleted since it references missing source note ${noteId}`);
} else {
@ -159,8 +157,7 @@ class ConsistencyChecks {
({attributeId, noteId}) => {
if (this.autoFix) {
const attribute = becca.getAttribute(attributeId);
attribute.isDeleted = true;
attribute.save();
attribute.markAsDeleted();
logFix(`Relation ${attributeId} has been deleted since it references missing note ${noteId}`)
} else {
@ -185,8 +182,7 @@ class ConsistencyChecks {
({branchId, noteId}) => {
if (this.autoFix) {
const branch = becca.getBranch(branchId);
branch.isDeleted = true;
branch.save();
branch.markAsDeleted();
logFix(`Branch ${branchId} has been deleted since associated note ${noteId} is deleted.`);
} else {
@ -204,8 +200,7 @@ class ConsistencyChecks {
`, ({branchId, parentNoteId}) => {
if (this.autoFix) {
const branch = becca.getBranch(branchId);
branch.isDeleted = true;
branch.save();
branch.markAsDeleted();
logFix(`Branch ${branchId} has been deleted since associated parent note ${parentNoteId} is deleted.`);
} else {
@ -258,8 +253,7 @@ class ConsistencyChecks {
// delete all but the first branch
for (const branch of branches.slice(1)) {
branch.isDeleted = true;
branch.save();
branch.markAsDeleted();
logFix(`Removing branch ${branch.branchId} since it's parent-child duplicate of branch ${origBranch.branchId}`);
}
@ -388,8 +382,7 @@ class ConsistencyChecks {
({attributeId}) => {
if (this.autoFix) {
const relation = becca.getAttribute(attributeId);
relation.isDeleted = true;
relation.save();
relation.markAsDeleted();
logFix(`Removed relation ${relation.attributeId} of name "${relation.name} with empty target.`);
} else {
@ -426,8 +419,7 @@ class ConsistencyChecks {
({attributeId, noteId}) => {
if (this.autoFix) {
const attribute = becca.getAttribute(attributeId);
attribute.isDeleted = true;
attribute.save();
attribute.markAsDeleted();
logFix(`Removed attribute ${attributeId} because owning note ${noteId} is also deleted.`);
} else {
@ -446,8 +438,7 @@ class ConsistencyChecks {
({attributeId, targetNoteId}) => {
if (this.autoFix) {
const attribute = becca.getAttribute(attributeId);
attribute.isDeleted = true;
attribute.save();
attribute.markAsDeleted();
logFix(`Removed attribute ${attributeId} because target note ${targetNoteId} is also deleted.`);
} else {

View File

@ -154,8 +154,7 @@ eventService.subscribe(eventService.ENTITY_DELETED, ({ entityName, entity }) =>
note.invalidateAttributeCache();
targetNote.invalidateAttributeCache();
relation.isDeleted = true;
relation.save();
relation.markAsDeleted();
}
}
});

View File

@ -532,9 +532,7 @@ function deleteBranch(branch, deleteId, taskContext) {
throw new Error("Can't delete root branch/note");
}
branch.isDeleted = true;
branch.deleteId = deleteId;
branch.save();
branch.markAsDeleted(deleteId);
const note = branch.getNote();
const notDeletedBranches = note.getBranches();
@ -546,22 +544,16 @@ function deleteBranch(branch, deleteId, taskContext) {
// first delete children and then parent - this will show up better in recent changes
note.isDeleted = true;
note.deleteId = deleteId;
note.save();
note.markAsDeleted(deleteId);
log.info("Deleting note " + note.noteId);
for (const attribute of note.getOwnedAttributes()) {
attribute.isDeleted = true;
attribute.deleteId = deleteId;
attribute.save();
attribute.markAsDeleted(deleteId);
}
for (const relation of note.getTargetRelations()) {
relation.isDeleted = true;
relation.deleteId = deleteId;
relation.save();
relation.markAsDeleted(deleteId);
}
return true;

View File

@ -196,14 +196,13 @@ function setNoteToParent(noteId, prefix, parentNoteId) {
if (branch) {
if (!parentNoteId) {
branch.isDeleted = true;
branch.markAsDeleted();
}
else {
branch.parentNoteId = parentNoteId;
branch.prefix = prefix;
branch.save();
}
branch.save();
}
else if (parentNoteId) {
const note = becca.getNote(noteId);