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) { for (const attribute of attributes) {
if (attribute.type === type && attribute.name === name && (value === undefined || value === attribute.value)) { if (attribute.type === type && attribute.name === name && (value === undefined || value === attribute.value)) {
attribute.isDeleted = true; attribute.markAsDeleted();
attribute.save();
this.invalidateAttributeCache(); this.invalidateAttributeCache();
} }

View File

@ -24,7 +24,7 @@ const TPL = `
More complex example would be deleting all matched note's attributes: 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>
</div> </div>

View File

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

View File

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

View File

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

View File

@ -120,8 +120,7 @@ class Attribute extends AbstractEntity {
position: this.position, position: this.position,
value: this.value, value: this.value,
isInheritable: this.isInheritable, isInheritable: this.isInheritable,
utcDateModified: dateUtils.utcNowDateTime(), utcDateModified: dateUtils.utcNowDateTime()
isDeleted: false
}; };
} }
@ -143,10 +142,6 @@ class Attribute extends AbstractEntity {
this.isInheritable = false; this.isInheritable = false;
} }
if (!this.isDeleted) {
this.isDeleted = false;
}
super.beforeSaving(); super.beforeSaving();
} }
@ -158,13 +153,12 @@ class Attribute extends AbstractEntity {
value: value, value: value,
position: this.position, position: this.position,
isInheritable: isInheritable, isInheritable: isInheritable,
isDeleted: false,
utcDateModified: this.utcDateModified utcDateModified: this.utcDateModified
}); });
} }
markAttributeAsDeleted() { markAsDeleted(deleteId = null) {
sql.execute("UPDATE attributes SET isDeleted = 1 WHERE attributeId = ?", [this.attributeId]); 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) // 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() { beforeSaving() {
// TODO can be refactored into becca
if (this.notePosition === undefined || this.notePosition === null) { if (this.notePosition === undefined || this.notePosition === null) {
const maxNotePos = sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [this.parentNoteId]); const maxNotePos = sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [this.parentNoteId]);
this.notePosition = maxNotePos === null ? 0 : maxNotePos + 10; this.notePosition = maxNotePos === null ? 0 : maxNotePos + 10;
@ -110,6 +111,13 @@ class Branch extends AbstractEntity {
super.beforeSaving(); 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; module.exports = Branch;

View File

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

View File

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

View File

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

View File

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

View File

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