mirror of
https://github.com/zadam/trilium.git
synced 2025-11-17 10:40:41 +01:00
script to generate large documents, closes #55
This commit is contained in:
56
src/services/cloning.js
Normal file
56
src/services/cloning.js
Normal file
@@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
|
||||
const sql = require('./sql');
|
||||
const syncTable = require('./sync_table');
|
||||
const tree = require('./tree');
|
||||
const Branch = require('../entities/branch');
|
||||
|
||||
async function cloneNoteToParent(noteId, parentNoteId, prefix) {
|
||||
const validationResult = await tree.validateParentChild(parentNoteId, noteId);
|
||||
|
||||
if (!validationResult.success) {
|
||||
return validationResult;
|
||||
}
|
||||
|
||||
await new Branch({
|
||||
noteId: noteId,
|
||||
parentNoteId: parentNoteId,
|
||||
prefix: prefix,
|
||||
isExpanded: 0
|
||||
}).save();
|
||||
|
||||
await sql.execute("UPDATE branches SET isExpanded = 1 WHERE noteId = ?", [parentNoteId]);
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
async function cloneNoteAfter(noteId, afterBranchId) {
|
||||
const afterNote = await tree.getBranch(afterBranchId);
|
||||
|
||||
const validationResult = await tree.validateParentChild(afterNote.parentNoteId, noteId);
|
||||
|
||||
if (!validationResult.result) {
|
||||
return validationResult;
|
||||
}
|
||||
|
||||
// we don't change dateModified so other changes are prioritized in case of conflict
|
||||
// also we would have to sync all those modified branches otherwise hash checks would fail
|
||||
await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
|
||||
[afterNote.parentNoteId, afterNote.notePosition]);
|
||||
|
||||
await syncTable.addNoteReorderingSync(afterNote.parentNoteId);
|
||||
|
||||
await new Branch({
|
||||
noteId: noteId,
|
||||
parentNoteId: afterNote.parentNoteId,
|
||||
notePosition: afterNote.notePosition + 1,
|
||||
isExpanded: 0
|
||||
}).save();
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
cloneNoteToParent,
|
||||
cloneNoteAfter
|
||||
};
|
||||
@@ -105,7 +105,7 @@ async function runAllChecks() {
|
||||
LEFT JOIN notes USING(noteId)
|
||||
WHERE
|
||||
notes.noteId IS NULL`,
|
||||
"Missing notes records for following note tree ID > note ID", errorList);
|
||||
"Missing notes records for following branch ID > note ID", errorList);
|
||||
|
||||
await runCheck(`
|
||||
SELECT
|
||||
@@ -116,7 +116,7 @@ async function runAllChecks() {
|
||||
WHERE
|
||||
notes.isDeleted = 1
|
||||
AND branches.isDeleted = 0`,
|
||||
"Note tree is not deleted even though main note is deleted for following note tree IDs", errorList);
|
||||
"Note tree is not deleted even though main note is deleted for following branch IDs", errorList);
|
||||
|
||||
await runCheck(`
|
||||
SELECT
|
||||
@@ -128,7 +128,7 @@ async function runAllChecks() {
|
||||
AND child.parentNoteId != 'root'
|
||||
AND (SELECT COUNT(*) FROM branches AS parent WHERE parent.noteId = child.parentNoteId
|
||||
AND parent.isDeleted = 0) = 0`,
|
||||
"All parent note trees are deleted but child note tree is not for these child note tree IDs", errorList);
|
||||
"All parent branches are deleted but child note tree is not for these child note tree IDs", errorList);
|
||||
|
||||
// we do extra JOIN to eliminate orphan notes without note tree (which are reported separately)
|
||||
await runCheck(`
|
||||
@@ -140,7 +140,7 @@ async function runAllChecks() {
|
||||
WHERE
|
||||
(SELECT COUNT(*) FROM branches WHERE notes.noteId = branches.noteId AND branches.isDeleted = 0) = 0
|
||||
AND notes.isDeleted = 0
|
||||
`, 'No undeleted note trees for note IDs', errorList);
|
||||
`, 'No undeleted branches for note IDs', errorList);
|
||||
|
||||
await runCheck(`
|
||||
SELECT
|
||||
|
||||
@@ -83,7 +83,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
|
||||
noteData.mime = "application/json";
|
||||
}
|
||||
|
||||
const {note} = await createNewNote(parentNoteId, noteData);
|
||||
const {note, branch} = await createNewNote(parentNoteId, noteData);
|
||||
|
||||
if (extraOptions.labels) {
|
||||
for (const labelName in extraOptions.labels) {
|
||||
@@ -91,7 +91,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
|
||||
}
|
||||
}
|
||||
|
||||
return note.noteId;
|
||||
return {note, branch};
|
||||
}
|
||||
|
||||
async function protectNoteRecursively(note, protect) {
|
||||
|
||||
@@ -11,6 +11,9 @@ async function createConnection() {
|
||||
return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise});
|
||||
}
|
||||
|
||||
let schemaReadyResolve = null;
|
||||
const schemaReady = new Promise((resolve, reject) => schemaReadyResolve = resolve);
|
||||
|
||||
let dbReadyResolve = null;
|
||||
const dbReady = new Promise((resolve, reject) => {
|
||||
cls.init(async () => {
|
||||
@@ -29,19 +32,20 @@ const dbReady = new Promise((resolve, reject) => {
|
||||
if (tableResults.length !== 1) {
|
||||
await createInitialDatabase();
|
||||
}
|
||||
else {
|
||||
if (!await isUserInitialized()) {
|
||||
log.info("Login/password not initialized. DB not ready.");
|
||||
|
||||
return;
|
||||
}
|
||||
schemaReadyResolve();
|
||||
|
||||
if (!await isDbUpToDate()) {
|
||||
return;
|
||||
}
|
||||
if (!await isUserInitialized()) {
|
||||
log.info("Login/password not initialized. DB not ready.");
|
||||
|
||||
resolve(db);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!await isDbUpToDate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
resolve(db);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -97,6 +101,7 @@ async function isUserInitialized() {
|
||||
|
||||
module.exports = {
|
||||
dbReady,
|
||||
schemaReady,
|
||||
isUserInitialized,
|
||||
setDbReadyAsResolved,
|
||||
isDbUpToDate
|
||||
|
||||
@@ -37,7 +37,7 @@ async function updateBranch(entity, sourceId) {
|
||||
|
||||
await syncTableService.addBranchSync(entity.branchId, sourceId);
|
||||
|
||||
log.info("Update/sync note tree " + entity.branchId);
|
||||
log.info("Update/sync branch " + entity.branchId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user