From df9acd050493ada15b6d917f8c5ddf18f6180b08 Mon Sep 17 00:00:00 2001 From: azivner Date: Wed, 15 Aug 2018 18:32:06 +0200 Subject: [PATCH] relation target noteIds need to be translated into local noteIds --- src/routes/api/import.js | 26 ++++++++++++++++++++------ src/services/attributes.js | 20 -------------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/routes/api/import.js b/src/routes/api/import.js index 5911ab45e..3909ea2a0 100644 --- a/src/routes/api/import.js +++ b/src/routes/api/import.js @@ -77,11 +77,25 @@ async function importTar(file, parentNoteId) { // maps from original noteId (in tar file) to newly generated noteId const noteIdMap = {}; + const attributes = []; - await importNotes(files, parentNoteId, noteIdMap); + await importNotes(files, parentNoteId, noteIdMap, attributes); - // import might contain relations targeting notes which are not in the import - await attributeService.removeInvalidRelations(); + // we save attributes after importing notes because we need to have all the relation + // targets already existing + for (const attr of attributes) { + if (attr.type === 'relation') { + // map to local noteId + attr.value = noteIdMap[attr.value]; + + if (!attr.value) { + // relation is targeting note not present in the import + continue; + } + } + + await attributeService.createAttribute(attr); + } } function getFileName(name) { @@ -162,7 +176,7 @@ async function parseImportFile(file) { }); } -async function importNotes(files, parentNoteId, noteIdMap) { +async function importNotes(files, parentNoteId, noteIdMap, attributes) { for (const file of files) { if (file.meta.version !== 1) { throw new Error("Can't read meta data version " + file.meta.version); @@ -191,7 +205,7 @@ async function importNotes(files, parentNoteId, noteIdMap) { noteIdMap[file.meta.noteId] = note.noteId; for (const attribute of file.meta.attributes) { - await attributeService.createAttribute({ + attributes.push({ noteId: note.noteId, type: attribute.type, name: attribute.name, @@ -202,7 +216,7 @@ async function importNotes(files, parentNoteId, noteIdMap) { } if (file.children.length > 0) { - await importNotes(file.children, note.noteId, noteIdMap); + await importNotes(file.children, note.noteId, noteIdMap, attributes); } } } diff --git a/src/services/attributes.js b/src/services/attributes.js index bb4281773..cb676b7e9 100644 --- a/src/services/attributes.js +++ b/src/services/attributes.js @@ -80,31 +80,11 @@ async function getAttributeNames(type, nameLike) { return names; } -async function removeInvalidRelations() { - const relations = await repository.getEntities(` - SELECT attributes.* - FROM attributes - LEFT JOIN notes AS sourceNote ON attributes.noteId = sourceNote.noteId - LEFT JOIN notes AS targetNote ON attributes.value = targetNote.noteId - WHERE - attributes.isDeleted = 0 - AND attributes.type = 'relation' - AND (sourceNote.noteId IS NULL OR sourceNote.isDeleted - OR targetNote.noteId IS NULL OR targetNote.isDeleted)`); - - for (const relation of relations) { - relation.isDeleted = true; - - await relation.save(); - } -} - module.exports = { getNotesWithLabel, getNoteWithLabel, createLabel, createAttribute, getAttributeNames, - removeInvalidRelations, BUILTIN_ATTRIBUTES }; \ No newline at end of file