DB migration should convert only "perfect candidates" to attachments

This commit is contained in:
zadam 2023-07-14 18:24:15 +02:00
parent 4d00404f55
commit 2dac6ab281
3 changed files with 11 additions and 9 deletions

View File

@ -9,7 +9,7 @@ module.exports = () => {
for (const note of Object.values(becca.notes)) { for (const note of Object.values(becca.notes)) {
try { try {
const attachment = note.convertToParentAttachment({force: false}); const attachment = note.convertToParentAttachment({autoConversion: true});
if (attachment) { if (attachment) {
log.info(`Auto-converted note '${note.noteId}' into attachment '${attachment.attachmentId}'.`); log.info(`Auto-converted note '${note.noteId}' into attachment '${attachment.attachmentId}'.`);

View File

@ -1427,14 +1427,16 @@ class BNote extends AbstractBeccaEntity {
return cloningService.cloneNoteToBranch(this.noteId, branch.branchId); return cloningService.cloneNoteToBranch(this.noteId, branch.branchId);
} }
isEligibleForConversionToAttachment() { isEligibleForConversionToAttachment(opts = {autoConversion: false}) {
if (this.type !== 'image' || !this.isContentAvailable() || this.hasChildren() || this.getParentBranches().length !== 1) { if (this.type !== 'image' || !this.isContentAvailable() || this.hasChildren() || this.getParentBranches().length !== 1) {
return false; return false;
} }
const targetRelations = this.getTargetRelations().filter(relation => relation.name === 'imageLink'); 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; return false;
} }
@ -1461,16 +1463,16 @@ class BNote extends AbstractBeccaEntity {
* *
* Currently, works only for image notes. * 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 {Object} [opts]
* @params {bolean} [opts.force=false} it is envisioned that user can force the conversion even if some conditions * @params {bolean} [opts.autoConversion=false} if true, the action is not triggered by user, but e.g. by migration,
* are not satisfied (e.g., relation to parent doesn't exist). * and only perfect candidates will be migrated
* *
* @returns {BAttachment|null} - null if note is not eligible for conversion * @returns {BAttachment|null} - null if note is not eligible for conversion
*/ */
convertToParentAttachment(opts = {force: false}) { convertToParentAttachment(opts = {autoConversion: false}) {
if (!this.isEligibleForConversionToAttachment()) { if (!this.isEligibleForConversionToAttachment(opts)) {
return null; return null;
} }

View File

@ -232,7 +232,7 @@ function convertNoteToAttachment(req) {
const note = becca.getNoteOrThrow(noteId); const note = becca.getNoteOrThrow(noteId);
return { return {
attachment: note.convertToParentAttachment({ force: true }) attachment: note.convertToParentAttachment()
}; };
} }