Skip to content

Build your personal knowledge base with Trilium Notes

code should not rely on fixed branch IDs since they can change after move

zadam5c4db266a45c
diff --git a/db/migrations/0198__NOOP.sql b/db/migrations/0198__NOOP.sql
new file mode 100644
index 0000000000..e2f08fcea6
--- /dev/null
+++ b/db/migrations/0198__NOOP.sql
@@ -0,0 +1 @@
+-- NOOP
diff --git a/db/migrations/0198__set_branch_id_in_hidden_subtree.sql b/db/migrations/0198__set_branch_id_in_hidden_subtree.sql
deleted file mode 100644
index 409864fef2..0000000000
--- a/db/migrations/0198__set_branch_id_in_hidden_subtree.sql
+++ /dev/null
@@ -1,4 +0,0 @@
-UPDATE branches SET branchId = 'search' WHERE parentNoteId = 'hidden' AND noteId = 'search';
--- the IDs are renamed in the 0199 migration
-UPDATE branches SET branchId = 'globalnotemap' WHERE parentNoteId = 'singles' AND noteId = 'globalnotemap';
-UPDATE branches SET branchId = 'sqlconsole' WHERE parentNoteId = 'hidden' AND noteId = 'sqlconsole';
diff --git a/db/migrations/0199__rename_ids.sql b/db/migrations/0199__rename_ids.sql
index d0de0233d0..1de3324a9f 100644
--- a/db/migrations/0199__rename_ids.sql
+++ b/db/migrations/0199__rename_ids.sql
@@ -1,7 +1,6 @@
UPDATE notes SET noteId = '_globalNoteMap', title = 'Note Map' WHERE noteId = 'globalnotemap';
UPDATE note_contents SET noteId = '_globalNoteMap' WHERE noteId = 'globalnotemap';
UPDATE note_revisions SET noteId = '_globalNoteMap' WHERE noteId = 'globalnotemap';
-UPDATE branches SET branchId = '_globalNoteMap' WHERE branchId = 'globalnotemap';
UPDATE branches SET noteId = '_globalNoteMap' WHERE noteId = 'globalnotemap';
UPDATE branches SET parentNoteId = '_globalNoteMap' WHERE parentNoteId = 'globalnotemap';
UPDATE attributes SET noteId = '_globalNoteMap' WHERE noteId = 'globalnotemap';
@@ -11,7 +10,6 @@ UPDATE entity_changes SET entityId = '_globalNoteMap' WHERE entityId = 'globalno
UPDATE notes SET noteId = '_bulkAction', title = 'Bulk Action' WHERE noteId = 'bulkaction';
UPDATE note_contents SET noteId = '_bulkAction' WHERE noteId = 'bulkaction';
UPDATE note_revisions SET noteId = '_bulkAction' WHERE noteId = 'bulkaction';
-UPDATE branches SET branchId = '_bulkAction' WHERE branchId = 'bulkaction';
UPDATE branches SET parentNoteId = '_bulkAction' WHERE parentNoteId = 'bulkaction';
UPDATE branches SET noteId = '_bulkAction' WHERE noteId = 'bulkaction';
UPDATE attributes SET noteId = '_bulkAction' WHERE noteId = 'bulkaction';
@@ -22,7 +20,6 @@ UPDATE notes SET noteId = '_sqlConsole', title = 'SQL Console History' WHERE not
UPDATE note_contents SET noteId = '_sqlConsole' WHERE noteId = 'sqlconsole';
UPDATE note_revisions SET noteId = '_sqlConsole' WHERE noteId = 'sqlconsole';
UPDATE branches SET noteId = '_sqlConsole' WHERE noteId = 'sqlconsole';
-UPDATE branches SET branchId = '_sqlConsole' WHERE branchId = 'sqlconsole';
UPDATE branches SET parentNoteId = '_sqlConsole' WHERE parentNoteId = 'sqlconsole';
UPDATE attributes SET noteId = '_sqlConsole' WHERE noteId = 'sqlconsole';
UPDATE attributes SET value = '_sqlConsole' WHERE type = 'relation' AND value = 'sqlconsole';
@@ -31,8 +28,7 @@ UPDATE entity_changes SET entityId = '_sqlConsole' WHERE entityId = 'sqlconsole'
UPDATE notes SET noteId = '_hidden', title = 'Hidden Notes' WHERE noteId = 'hidden';
UPDATE note_contents SET noteId = '_hidden' WHERE noteId = 'hidden';
UPDATE note_revisions SET noteId = '_hidden' WHERE noteId = 'hidden';
-UPDATE branches SET noteId = '_hidden' WHERE noteId = 'hidden';
-UPDATE branches SET branchId = '_hidden', prefix = NULL WHERE branchId = 'hidden';
+UPDATE branches SET noteId = '_hidden', prefix = NULL WHERE noteId = 'hidden';
UPDATE branches SET parentNoteId = '_hidden' WHERE parentNoteId = 'hidden';
UPDATE attributes SET noteId = '_hidden' WHERE noteId = 'hidden';
UPDATE attributes SET value = '_hidden' WHERE type = 'relation' AND value = 'hidden';
@@ -42,7 +38,6 @@ UPDATE notes SET noteId = '_search', title = 'Search History' WHERE noteId = 'se
UPDATE note_contents SET noteId = '_search' WHERE noteId = 'search';
UPDATE note_revisions SET noteId = '_search' WHERE noteId = 'search';
UPDATE branches SET noteId = '_search' WHERE noteId = 'search';
-UPDATE branches SET branchId = '_search' WHERE branchId = 'search';
UPDATE branches SET parentNoteId = '_search' WHERE parentNoteId = 'search';
UPDATE attributes SET noteId = '_search' WHERE noteId = 'search';
UPDATE attributes SET value = '_search' WHERE type = 'relation' AND value = 'search';
diff --git a/src/becca/entities/branch.js b/src/becca/entities/branch.js
index f97fe0ec30..c1744c90dc 100644
--- a/src/becca/entities/branch.js
+++ b/src/becca/entities/branch.js
@@ -2,7 +2,6 @@
const Note = require('./note');
const AbstractEntity = require("./abstract_entity");
-const sql = require("../../services/sql");
const dateUtils = require("../../services/date_utils");
const utils = require("../../services/utils");
const TaskContext = require("../../services/task_context");
diff --git a/src/public/app/services/branches.js b/src/public/app/services/branches.js
index c45fff90dd..86eb96c08e 100644
--- a/src/public/app/services/branches.js
+++ b/src/public/app/services/branches.js
@@ -57,7 +57,9 @@ async function moveAfterBranch(branchIdsToMove, afterBranchId) {
}
async function moveToParentNote(branchIdsToMove, newParentBranchId) {
- if (newParentBranchId === '_lbRoot') {
+ const newParentBranch = await froca.getBranch(newParentBranchId);
+
+ if (newParentBranch.noteId === '_lbRoot') {
toastService.showError('Cannot move notes here.');
return;
}
diff --git a/src/services/backend_script_api.js b/src/services/backend_script_api.js
index 15b885a77b..cdeabe2123 100644
--- a/src/services/backend_script_api.js
+++ b/src/services/backend_script_api.js
@@ -493,12 +493,12 @@ function BackendScriptApi(currentNote, apiParams) {
if (opts.type === 'customWidget' && !opts.widgetNoteId) { throw new Error("widgetNoteId is mandatory for launchers of type 'customWidget'"); }
const parentNoteId = !!opts.isVisible ? '_lbVisibleLaunchers' : '_lbAvailableLaunchers';
- const actualId = 'al_' + opts.id;
+ const noteId = 'al_' + opts.id;
const launcherNote =
becca.getNote(opts.id) ||
specialNotesService.createLauncher({
- id: actualId,
+ noteId: noteId,
parentNoteId: parentNoteId,
launcherType: opts.type,
}).note;
diff --git a/src/services/cloning.js b/src/services/cloning.js
index 3fb56473c9..6a7dde1f0e 100644
--- a/src/services/cloning.js
+++ b/src/services/cloning.js
@@ -121,8 +121,16 @@ function toggleNoteInParent(present, noteId, parentNoteId, prefix) {
function cloneNoteAfter(noteId, afterBranchId) {
if (['_hidden', 'root'].includes(noteId)) {
- return { success: false, message: 'Cloning the given note is forbidden.' };
- } else if (afterBranchId === '_hidden') {
+ return { success: false, message: `Cloning the note '${noteId}' is forbidden.` };
+ }
+
+ const afterBranch = becca.getBranch(afterBranchId);
+
+ if (!afterBranch) {
+ return { success: false, message: `Branch '${afterBranchId}' does not exist.` };
+ }
+
+ if (afterBranch.noteId === '_hidden') {
return { success: false, message: 'Cannot clone after the hidden branch.' };
}
diff --git a/src/services/hidden_subtree.js b/src/services/hidden_subtree.js
index 98606355e9..4580a3e145 100644
--- a/src/services/hidden_subtree.js
+++ b/src/services/hidden_subtree.js
@@ -1,6 +1,5 @@
const becca = require("../becca/becca");
const noteService = require("./notes");
-const log = require("./log");
const LBTPL_ROOT = "_lbTplRoot";
const LBTPL_BASE = "_lbTplBase";
@@ -248,7 +247,6 @@ function checkHiddenSubtreeRecursively(parentNoteId, item) {
if (!note) {
({note, branch} = noteService.createNewNote({
- branchId: item.id,
noteId: item.id,
title: item.title,
type: item.type,
@@ -286,10 +284,9 @@ function checkHiddenSubtreeRecursively(parentNoteId, item) {
note.save();
}
- if (!branch) {
- // not sure if there's some better way to recover
- log.error(`Cannot find launcher branch id='${item.id}', ignoring...`);
- } else {
+ if (branch) {
+ // in case of launchers the branch ID is not preserved and should not be relied upon - launchers which move between
+ // visible and available will change branch since branch's parent-child relationship is immutable
if (item.notePosition !== undefined && branch.notePosition !== item.notePosition) {
branch.notePosition = item.notePosition;
branch.save();
diff --git a/src/services/notes.js b/src/services/notes.js
index 36ea273ddb..f17c7adce8 100644
--- a/src/services/notes.js
+++ b/src/services/notes.js
@@ -166,7 +166,6 @@ function createNewNote(params) {
note.setContent(params.content);
branch = new Branch({
- branchId: params.branchId,
noteId: note.noteId,
parentNoteId: params.parentNoteId,
notePosition: params.notePosition !== undefined ? params.notePosition : getNewNotePosition(params.parentNoteId),
diff --git a/src/services/special_notes.js b/src/services/special_notes.js
index ef6f639879..ee88a48eb4 100644
--- a/src/services/special_notes.js
+++ b/src/services/special_notes.js
@@ -139,10 +139,9 @@ function getMonthlyParentNoteId(rootNoteId, prefix) {
return monthNote.noteId;
}
-function createScriptLauncher(parentNoteId, forceId = null) {
+function createScriptLauncher(parentNoteId, forceNoteId = null) {
const note = noteService.createNewNote({
- noteId: forceId,
- branchId: forceId,
+ noteId: forceNoteId,
title: "Script Launcher",
type: 'launcher',
content: '',
@@ -153,13 +152,12 @@ function createScriptLauncher(parentNoteId, forceId = null) {
return note;
}
-function createLauncher({parentNoteId, launcherType, id}) {
+function createLauncher({parentNoteId, launcherType, noteId}) {
let note;
if (launcherType === 'note') {
note = noteService.createNewNote({
- noteId: id,
- branchId: id,
+ noteId: noteId,
title: "Note Launcher",
type: 'launcher',
content: '',
@@ -168,11 +166,10 @@ function createLauncher({parentNoteId, launcherType, id}) {
note.addRelation('template', LBTPL_NOTE_LAUNCHER);
} else if (launcherType === 'script') {
- note = createScriptLauncher(parentNoteId, id);
+ note = createScriptLauncher(parentNoteId, noteId);
} else if (launcherType === 'customWidget') {
note = noteService.createNewNote({
- noteId: id,
- branchId: id,
+ noteId: noteId,
title: "Widget Launcher",
type: 'launcher',
content: '',
@@ -182,8 +179,8 @@ function createLauncher({parentNoteId, launcherType, id}) {
note.addRelation('template', LBTPL_CUSTOM_WIDGET);
} else if (launcherType === 'spacer') {
note = noteService.createNewNote({
- noteId: id,
- branchId: id,
+ noteId: noteId,
+ branchId: noteId,
title: "Spacer",
type: 'launcher',
content: '',
diff --git a/src/services/tree.js b/src/services/tree.js
index a1665579ad..f24bdca09f 100644
--- a/src/services/tree.js
+++ b/src/services/tree.js
@@ -187,7 +187,7 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
for (const note of notes) {
const branch = note.getParentBranches().find(b => b.parentNoteId === parentNoteId);
- if (branch.branchId === '_hidden') {
+ if (branch.noteId === '_hidden') {
position = 999_999_999;
}