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

This commit is contained in:
zadam 2022-12-23 20:40:58 +01:00
parent e5e0add1a2
commit 5c4db266a4
11 changed files with 29 additions and 35 deletions

View File

@ -0,0 +1 @@
-- NOOP

View File

@ -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';

View File

@ -1,7 +1,6 @@
UPDATE notes SET noteId = '_globalNoteMap', title = 'Note Map' WHERE noteId = 'globalnotemap'; UPDATE notes SET noteId = '_globalNoteMap', title = 'Note Map' WHERE noteId = 'globalnotemap';
UPDATE note_contents SET noteId = '_globalNoteMap' WHERE noteId = 'globalnotemap'; UPDATE note_contents SET noteId = '_globalNoteMap' WHERE noteId = 'globalnotemap';
UPDATE note_revisions 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 noteId = '_globalNoteMap' WHERE noteId = 'globalnotemap';
UPDATE branches SET parentNoteId = '_globalNoteMap' WHERE parentNoteId = 'globalnotemap'; UPDATE branches SET parentNoteId = '_globalNoteMap' WHERE parentNoteId = 'globalnotemap';
UPDATE attributes SET noteId = '_globalNoteMap' WHERE noteId = '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 notes SET noteId = '_bulkAction', title = 'Bulk Action' WHERE noteId = 'bulkaction';
UPDATE note_contents SET noteId = '_bulkAction' WHERE noteId = 'bulkaction'; UPDATE note_contents SET noteId = '_bulkAction' WHERE noteId = 'bulkaction';
UPDATE note_revisions 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 parentNoteId = '_bulkAction' WHERE parentNoteId = 'bulkaction';
UPDATE branches SET noteId = '_bulkAction' WHERE noteId = 'bulkaction'; UPDATE branches SET noteId = '_bulkAction' WHERE noteId = 'bulkaction';
UPDATE attributes 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_contents SET noteId = '_sqlConsole' WHERE noteId = 'sqlconsole';
UPDATE note_revisions 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 noteId = '_sqlConsole' WHERE noteId = 'sqlconsole';
UPDATE branches SET branchId = '_sqlConsole' WHERE branchId = 'sqlconsole';
UPDATE branches SET parentNoteId = '_sqlConsole' WHERE parentNoteId = 'sqlconsole'; UPDATE branches SET parentNoteId = '_sqlConsole' WHERE parentNoteId = 'sqlconsole';
UPDATE attributes SET noteId = '_sqlConsole' WHERE noteId = 'sqlconsole'; UPDATE attributes SET noteId = '_sqlConsole' WHERE noteId = 'sqlconsole';
UPDATE attributes SET value = '_sqlConsole' WHERE type = 'relation' AND value = '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 notes SET noteId = '_hidden', title = 'Hidden Notes' WHERE noteId = 'hidden';
UPDATE note_contents SET noteId = '_hidden' WHERE noteId = 'hidden'; UPDATE note_contents SET noteId = '_hidden' WHERE noteId = 'hidden';
UPDATE note_revisions 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 noteId = '_hidden', prefix = NULL WHERE noteId = 'hidden';
UPDATE branches SET branchId = '_hidden', prefix = NULL WHERE branchId = 'hidden';
UPDATE branches SET parentNoteId = '_hidden' WHERE parentNoteId = 'hidden'; UPDATE branches SET parentNoteId = '_hidden' WHERE parentNoteId = 'hidden';
UPDATE attributes SET noteId = '_hidden' WHERE noteId = 'hidden'; UPDATE attributes SET noteId = '_hidden' WHERE noteId = 'hidden';
UPDATE attributes SET value = '_hidden' WHERE type = 'relation' AND value = '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_contents SET noteId = '_search' WHERE noteId = 'search';
UPDATE note_revisions 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 noteId = '_search' WHERE noteId = 'search';
UPDATE branches SET branchId = '_search' WHERE branchId = 'search';
UPDATE branches SET parentNoteId = '_search' WHERE parentNoteId = 'search'; UPDATE branches SET parentNoteId = '_search' WHERE parentNoteId = 'search';
UPDATE attributes SET noteId = '_search' WHERE noteId = 'search'; UPDATE attributes SET noteId = '_search' WHERE noteId = 'search';
UPDATE attributes SET value = '_search' WHERE type = 'relation' AND value = 'search'; UPDATE attributes SET value = '_search' WHERE type = 'relation' AND value = 'search';

View File

@ -2,7 +2,6 @@
const Note = require('./note'); const Note = require('./note');
const AbstractEntity = require("./abstract_entity"); const AbstractEntity = require("./abstract_entity");
const sql = require("../../services/sql");
const dateUtils = require("../../services/date_utils"); const dateUtils = require("../../services/date_utils");
const utils = require("../../services/utils"); const utils = require("../../services/utils");
const TaskContext = require("../../services/task_context"); const TaskContext = require("../../services/task_context");

View File

@ -57,7 +57,9 @@ async function moveAfterBranch(branchIdsToMove, afterBranchId) {
} }
async function moveToParentNote(branchIdsToMove, newParentBranchId) { async function moveToParentNote(branchIdsToMove, newParentBranchId) {
if (newParentBranchId === '_lbRoot') { const newParentBranch = await froca.getBranch(newParentBranchId);
if (newParentBranch.noteId === '_lbRoot') {
toastService.showError('Cannot move notes here.'); toastService.showError('Cannot move notes here.');
return; return;
} }

View File

@ -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'"); } if (opts.type === 'customWidget' && !opts.widgetNoteId) { throw new Error("widgetNoteId is mandatory for launchers of type 'customWidget'"); }
const parentNoteId = !!opts.isVisible ? '_lbVisibleLaunchers' : '_lbAvailableLaunchers'; const parentNoteId = !!opts.isVisible ? '_lbVisibleLaunchers' : '_lbAvailableLaunchers';
const actualId = 'al_' + opts.id; const noteId = 'al_' + opts.id;
const launcherNote = const launcherNote =
becca.getNote(opts.id) || becca.getNote(opts.id) ||
specialNotesService.createLauncher({ specialNotesService.createLauncher({
id: actualId, noteId: noteId,
parentNoteId: parentNoteId, parentNoteId: parentNoteId,
launcherType: opts.type, launcherType: opts.type,
}).note; }).note;

View File

@ -121,8 +121,16 @@ function toggleNoteInParent(present, noteId, parentNoteId, prefix) {
function cloneNoteAfter(noteId, afterBranchId) { function cloneNoteAfter(noteId, afterBranchId) {
if (['_hidden', 'root'].includes(noteId)) { if (['_hidden', 'root'].includes(noteId)) {
return { success: false, message: 'Cloning the given note is forbidden.' }; return { success: false, message: `Cloning the note '${noteId}' is forbidden.` };
} else if (afterBranchId === '_hidden') { }
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.' }; return { success: false, message: 'Cannot clone after the hidden branch.' };
} }

View File

@ -1,6 +1,5 @@
const becca = require("../becca/becca"); const becca = require("../becca/becca");
const noteService = require("./notes"); const noteService = require("./notes");
const log = require("./log");
const LBTPL_ROOT = "_lbTplRoot"; const LBTPL_ROOT = "_lbTplRoot";
const LBTPL_BASE = "_lbTplBase"; const LBTPL_BASE = "_lbTplBase";
@ -248,7 +247,6 @@ function checkHiddenSubtreeRecursively(parentNoteId, item) {
if (!note) { if (!note) {
({note, branch} = noteService.createNewNote({ ({note, branch} = noteService.createNewNote({
branchId: item.id,
noteId: item.id, noteId: item.id,
title: item.title, title: item.title,
type: item.type, type: item.type,
@ -286,10 +284,9 @@ function checkHiddenSubtreeRecursively(parentNoteId, item) {
note.save(); note.save();
} }
if (!branch) { if (branch) {
// not sure if there's some better way to recover // in case of launchers the branch ID is not preserved and should not be relied upon - launchers which move between
log.error(`Cannot find launcher branch id='${item.id}', ignoring...`); // visible and available will change branch since branch's parent-child relationship is immutable
} else {
if (item.notePosition !== undefined && branch.notePosition !== item.notePosition) { if (item.notePosition !== undefined && branch.notePosition !== item.notePosition) {
branch.notePosition = item.notePosition; branch.notePosition = item.notePosition;
branch.save(); branch.save();

View File

@ -166,7 +166,6 @@ function createNewNote(params) {
note.setContent(params.content); note.setContent(params.content);
branch = new Branch({ branch = new Branch({
branchId: params.branchId,
noteId: note.noteId, noteId: note.noteId,
parentNoteId: params.parentNoteId, parentNoteId: params.parentNoteId,
notePosition: params.notePosition !== undefined ? params.notePosition : getNewNotePosition(params.parentNoteId), notePosition: params.notePosition !== undefined ? params.notePosition : getNewNotePosition(params.parentNoteId),

View File

@ -139,10 +139,9 @@ function getMonthlyParentNoteId(rootNoteId, prefix) {
return monthNote.noteId; return monthNote.noteId;
} }
function createScriptLauncher(parentNoteId, forceId = null) { function createScriptLauncher(parentNoteId, forceNoteId = null) {
const note = noteService.createNewNote({ const note = noteService.createNewNote({
noteId: forceId, noteId: forceNoteId,
branchId: forceId,
title: "Script Launcher", title: "Script Launcher",
type: 'launcher', type: 'launcher',
content: '', content: '',
@ -153,13 +152,12 @@ function createScriptLauncher(parentNoteId, forceId = null) {
return note; return note;
} }
function createLauncher({parentNoteId, launcherType, id}) { function createLauncher({parentNoteId, launcherType, noteId}) {
let note; let note;
if (launcherType === 'note') { if (launcherType === 'note') {
note = noteService.createNewNote({ note = noteService.createNewNote({
noteId: id, noteId: noteId,
branchId: id,
title: "Note Launcher", title: "Note Launcher",
type: 'launcher', type: 'launcher',
content: '', content: '',
@ -168,11 +166,10 @@ function createLauncher({parentNoteId, launcherType, id}) {
note.addRelation('template', LBTPL_NOTE_LAUNCHER); note.addRelation('template', LBTPL_NOTE_LAUNCHER);
} else if (launcherType === 'script') { } else if (launcherType === 'script') {
note = createScriptLauncher(parentNoteId, id); note = createScriptLauncher(parentNoteId, noteId);
} else if (launcherType === 'customWidget') { } else if (launcherType === 'customWidget') {
note = noteService.createNewNote({ note = noteService.createNewNote({
noteId: id, noteId: noteId,
branchId: id,
title: "Widget Launcher", title: "Widget Launcher",
type: 'launcher', type: 'launcher',
content: '', content: '',
@ -182,8 +179,8 @@ function createLauncher({parentNoteId, launcherType, id}) {
note.addRelation('template', LBTPL_CUSTOM_WIDGET); note.addRelation('template', LBTPL_CUSTOM_WIDGET);
} else if (launcherType === 'spacer') { } else if (launcherType === 'spacer') {
note = noteService.createNewNote({ note = noteService.createNewNote({
noteId: id, noteId: noteId,
branchId: id, branchId: noteId,
title: "Spacer", title: "Spacer",
type: 'launcher', type: 'launcher',
content: '', content: '',

View File

@ -187,7 +187,7 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
for (const note of notes) { for (const note of notes) {
const branch = note.getParentBranches().find(b => b.parentNoteId === parentNoteId); const branch = note.getParentBranches().find(b => b.parentNoteId === parentNoteId);
if (branch.branchId === '_hidden') { if (branch.noteId === '_hidden') {
position = 999_999_999; position = 999_999_999;
} }