Skip to content

Build your personal knowledge base with Trilium Notes

DB migration should convert only "perfect candidates" to attachments

zadam2dac6ab2816c
diff --git a/db/migrations/0220__migrate_images_to_attachments.js b/db/migrations/0220__migrate_images_to_attachments.js
index 76ba971dab..512cb3c2f2 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 4a36d2e4fa..c21ecc8436 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 4239fb9fe2..3fb27446bf 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()
};
}