From 2dac6ab2816c58385e68767f665bc18a4149d140 Mon Sep 17 00:00:00 2001 From: zadam Date: Fri, 14 Jul 2023 18:24:15 +0200 Subject: [PATCH] DB migration should convert only "perfect candidates" to attachments --- .../0220__migrate_images_to_attachments.js | 2 +- src/becca/entities/bnote.js | 16 +++++++++------- src/routes/api/notes.js | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/db/migrations/0220__migrate_images_to_attachments.js b/db/migrations/0220__migrate_images_to_attachments.js index 76ba971da..512cb3c2f 100644 --- a/db/migrations/0220__migrate_images_to_attachments.js +++ b/db/migrations/0220__migrate_images_to_attachments.js @@ -9,7 +9,7 @@ module.exports = () => { for (const note of Object.values(becca.notes)) { try { - const attachment = note.convertToParentAttachment({force: false}); + const attachment = note.convertToParentAttachment({autoConversion: true}); if (attachment) { log.info(`Auto-converted note '${note.noteId}' into attachment '${attachment.attachmentId}'.`); diff --git a/src/becca/entities/bnote.js b/src/becca/entities/bnote.js index 4a36d2e4f..c21ecc843 100644 --- a/src/becca/entities/bnote.js +++ b/src/becca/entities/bnote.js @@ -1427,14 +1427,16 @@ class BNote extends AbstractBeccaEntity { return cloningService.cloneNoteToBranch(this.noteId, branch.branchId); } - isEligibleForConversionToAttachment() { + isEligibleForConversionToAttachment(opts = {autoConversion: false}) { if (this.type !== 'image' || !this.isContentAvailable() || this.hasChildren() || this.getParentBranches().length !== 1) { return false; } const targetRelations = this.getTargetRelations().filter(relation => relation.name === 'imageLink'); - if (targetRelations.length > 1) { + if (opts.autoConversion && targetRelations.length === 0) { + return false; + } else if (targetRelations.length > 1) { return false; } @@ -1461,16 +1463,16 @@ class BNote extends AbstractBeccaEntity { * * Currently, works only for image notes. * - * In future, this functionality might get more generic and some of the requirements relaxed. + * In the future, this functionality might get more generic and some of the requirements relaxed. * * @params {Object} [opts] - * @params {bolean} [opts.force=false} it is envisioned that user can force the conversion even if some conditions - * are not satisfied (e.g., relation to parent doesn't exist). + * @params {bolean} [opts.autoConversion=false} if true, the action is not triggered by user, but e.g. by migration, + * and only perfect candidates will be migrated * * @returns {BAttachment|null} - null if note is not eligible for conversion */ - convertToParentAttachment(opts = {force: false}) { - if (!this.isEligibleForConversionToAttachment()) { + convertToParentAttachment(opts = {autoConversion: false}) { + if (!this.isEligibleForConversionToAttachment(opts)) { return null; } diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js index 4239fb9fe..3fb27446b 100644 --- a/src/routes/api/notes.js +++ b/src/routes/api/notes.js @@ -232,7 +232,7 @@ function convertNoteToAttachment(req) { const note = becca.getNoteOrThrow(noteId); return { - attachment: note.convertToParentAttachment({ force: true }) + attachment: note.convertToParentAttachment() }; }