mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	parent-child relationship is now stored in notes_parent table
This commit is contained in:
		
							
								
								
									
										28
									
								
								migrations/0040__notes_parent.sql
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								migrations/0040__notes_parent.sql
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | |||||||
|  | CREATE TABLE [notes_parent] ( | ||||||
|  |     [parent_id] VARCHAR(30) NOT NULL, | ||||||
|  |     [child_id] VARCHAR(30) NOT NULL, | ||||||
|  |     date_created INTEGER NOT NULL DEFAULT 0, | ||||||
|  |     PRIMARY KEY (parent_id, child_id) | ||||||
|  | ); | ||||||
|  |  | ||||||
|  | INSERT INTO notes_parent (parent_id, child_id, date_created) | ||||||
|  |     SELECT note_pid, note_tree_id, date_modified FROM notes_tree; | ||||||
|  |  | ||||||
|  | CREATE TABLE [notes_tree_mig] ( | ||||||
|  |     [note_tree_id] VARCHAR(30) PRIMARY KEY NOT NULL, | ||||||
|  |     [note_id] VARCHAR(30) UNIQUE NOT NULL, | ||||||
|  |     [note_pos] INTEGER NOT NULL, | ||||||
|  |     [is_expanded] BOOLEAN NULL , | ||||||
|  |     date_modified INTEGER NOT NULL DEFAULT 0, | ||||||
|  |     is_deleted INTEGER NOT NULL DEFAULT 0 | ||||||
|  | ); | ||||||
|  |  | ||||||
|  | INSERT INTO notes_tree_mig (note_tree_id, note_id, note_pos, is_expanded, date_modified, is_deleted) | ||||||
|  |     SELECT note_tree_id, note_id, note_pos, is_expanded, date_modified, is_deleted FROM notes_tree; | ||||||
|  |  | ||||||
|  | DROP TABLE notes_tree; | ||||||
|  | ALTER TABLE notes_tree_mig RENAME TO notes_tree; | ||||||
|  |  | ||||||
|  | CREATE INDEX `IDX_notes_tree_note_id` ON `notes_tree` ( | ||||||
|  | 	`note_id` | ||||||
|  | ); | ||||||
| @@ -33,17 +33,20 @@ const noteTree = (function() { | |||||||
|         clipboardNoteTreeId = cbNoteId; |         clipboardNoteTreeId = cbNoteId; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     function prepareNoteTree(notes) { showAppIfHidden(); |     function prepareNoteTree(notes, notesParent) { | ||||||
|         parentToNotes = {}; |         parentToNotes = {}; | ||||||
|         notesMap = {}; |         notesMap = {}; | ||||||
|  |  | ||||||
|         for (const note of notes) { |         for (const note of notes) { | ||||||
|             if (!parentToNotes[note.note_pid]) { |             notesMap[note.note_tree_id] = note; | ||||||
|                 parentToNotes[note.note_pid] = []; |  | ||||||
|         } |         } | ||||||
|  |  | ||||||
|             notesMap[note.note_tree_id] = note; |         for (const np of notesParent) { | ||||||
|             parentToNotes[note.note_pid].push(note.note_tree_id); |             if (!parentToNotes[np.parent_id]) { | ||||||
|  |                 parentToNotes[np.parent_id] = []; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             parentToNotes[np.parent_id].push(np.child_id); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         glob.allNoteIds = Object.keys(notesMap); |         glob.allNoteIds = Object.keys(notesMap); | ||||||
| @@ -160,9 +163,13 @@ const noteTree = (function() { | |||||||
|                 setExpandedToServer(getNoteTreeIdFromKey(data.node.key), false); |                 setExpandedToServer(getNoteTreeIdFromKey(data.node.key), false); | ||||||
|             }, |             }, | ||||||
|             init: (event, data) => { |             init: (event, data) => { | ||||||
|                 if (startNoteTreeId) { |                 // if (startNoteTreeId) { | ||||||
|                     treeUtils.activateNode(startNoteTreeId); |                 //     treeUtils.activateNode(startNoteTreeId); | ||||||
|                 } |                 // } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |                 showAppIfHidden(); | ||||||
|             }, |             }, | ||||||
|             hotkeys: { |             hotkeys: { | ||||||
|                 keydown: keybindings |                 keydown: keybindings | ||||||
| @@ -258,7 +265,7 @@ const noteTree = (function() { | |||||||
|                 startNoteTreeId = document.location.hash.substr(1); // strip initial # |                 startNoteTreeId = document.location.hash.substr(1); // strip initial # | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             return prepareNoteTree(resp.notes); |             return prepareNoteTree(resp.notes, resp.notes_parent); | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -19,7 +19,14 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => { | |||||||
|         + "from notes_tree " |         + "from notes_tree " | ||||||
|         + "join notes on notes.note_id = notes_tree.note_id " |         + "join notes on notes.note_id = notes_tree.note_id " | ||||||
|         + "where notes.is_deleted = 0 and notes_tree.is_deleted = 0 " |         + "where notes.is_deleted = 0 and notes_tree.is_deleted = 0 " | ||||||
|         + "order by note_pid, note_pos"); |         + "order by note_pos"); | ||||||
|  |  | ||||||
|  |     const notes_parent = await sql.getResults("" + | ||||||
|  |         "select parent_id, child_id " + | ||||||
|  |         "from notes_parent " + | ||||||
|  |         "join notes_tree as child on child.note_tree_id = notes_parent.child_id " + | ||||||
|  |         "left join notes_tree as parent on parent.note_tree_id = notes_parent.parent_id " + | ||||||
|  |         "where child.is_deleted = 0 and (parent.is_deleted = 0 or notes_parent.parent_id = 'root')"); | ||||||
|  |  | ||||||
|     const dataKey = protected_session.getDataKey(req); |     const dataKey = protected_session.getDataKey(req); | ||||||
|  |  | ||||||
| @@ -31,6 +38,7 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => { | |||||||
|  |  | ||||||
|     res.send({ |     res.send({ | ||||||
|         notes: notes, |         notes: notes, | ||||||
|  |         notes_parent: notes_parent, | ||||||
|         start_note_tree_id: await options.getOption('start_note_tree_id'), |         start_note_tree_id: await options.getOption('start_note_tree_id'), | ||||||
|         tree_load_time: utils.nowTimestamp() |         tree_load_time: utils.nowTimestamp() | ||||||
|     }); |     }); | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ const options = require('./options'); | |||||||
| const fs = require('fs-extra'); | const fs = require('fs-extra'); | ||||||
| const log = require('./log'); | const log = require('./log'); | ||||||
|  |  | ||||||
| const APP_DB_VERSION = 39; | const APP_DB_VERSION = 40; | ||||||
| const MIGRATIONS_DIR = "migrations"; | const MIGRATIONS_DIR = "migrations"; | ||||||
|  |  | ||||||
| async function migrate() { | async function migrate() { | ||||||
|   | |||||||
| @@ -128,9 +128,11 @@ async function wrap(func) { | |||||||
|         return await func(db); |         return await func(db); | ||||||
|     } |     } | ||||||
|     catch (e) { |     catch (e) { | ||||||
|         log.error("Error executing query. Inner exception: " + e.stack + error.stack); |         const thisError = new Error(); | ||||||
|  |  | ||||||
|         throw new Error(); |         log.error("Error executing query. Inner exception: " + e.stack + thisError.stack); | ||||||
|  |  | ||||||
|  |         throw thisError; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user