mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
migrating canvas etc.
This commit is contained in:
parent
339d8a7378
commit
8a33645360
@ -15,5 +15,5 @@ CREATE TABLE IF NOT EXISTS "note_attachment_contents" (`noteAttachmentId` TEXT N
|
||||
|
||||
CREATE INDEX IDX_note_attachments_name
|
||||
on note_attachments (name);
|
||||
CREATE INDEX IDX_note_attachments_noteId
|
||||
on note_attachments (noteId);
|
||||
CREATE UNIQUE INDEX IDX_note_attachments_noteId_name
|
||||
on note_attachments (noteId, name);
|
||||
|
39
db/migrations/0214__migrate_canvas_note_to_attachment.js
Normal file
39
db/migrations/0214__migrate_canvas_note_to_attachment.js
Normal file
@ -0,0 +1,39 @@
|
||||
module.exports = async () => {
|
||||
const cls = require("../../src/services/cls");
|
||||
const beccaLoader = require("../../src/becca/becca_loader");
|
||||
const becca = require("../../src/becca/becca");
|
||||
const log = require("../../src/services/log");
|
||||
|
||||
await cls.init(async () => {
|
||||
beccaLoader.load();
|
||||
|
||||
for (const note of Object.values(becca.notes)) {
|
||||
if (note.type !== 'canvas') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (note.isProtected) {
|
||||
// can't migrate protected notes, but that's not critical.
|
||||
continue;
|
||||
}
|
||||
|
||||
const content = note.getContent(true);
|
||||
let svg;
|
||||
|
||||
try {
|
||||
const payload = JSON.parse(content);
|
||||
svg = payload?.svg;
|
||||
|
||||
if (!svg) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
log.info(`Could not create a note attachment for canvas "${note.noteId}" with error: ${e.message} ${e.stack}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
note.saveNoteAttachment('canvasSvg', 'image/svg+xml', svg);
|
||||
}
|
||||
});
|
||||
};
|
@ -1116,6 +1116,12 @@ class BNote extends AbstractBeccaEntity {
|
||||
.map(row => new BNoteAttachment(row));
|
||||
}
|
||||
|
||||
getNoteAttachmentByName(name) {
|
||||
return sql.getRows("SELECT * FROM note_attachments WHERE noteId = ? AND name = ? AND isDeleted = 0", [this.noteId, name])
|
||||
.map(row => new BNoteAttachment(row))
|
||||
[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {string[][]} - array of notePaths (each represented by array of noteIds constituting the particular note path)
|
||||
*/
|
||||
@ -1429,6 +1435,25 @@ class BNote extends AbstractBeccaEntity {
|
||||
return noteRevision;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {BNoteAttachment}
|
||||
*/
|
||||
saveNoteAttachment(name, mime, content) {
|
||||
this.getNoteAttachments()
|
||||
|
||||
const noteAttachment = new BNoteAttachment({
|
||||
name,
|
||||
mime,
|
||||
isProtected: this.isProtected
|
||||
});
|
||||
|
||||
noteAttachment.save();
|
||||
|
||||
noteAttachment.setContent(content);
|
||||
|
||||
return noteAttachment;
|
||||
}
|
||||
|
||||
beforeSaving() {
|
||||
super.beforeSaving();
|
||||
|
||||
|
@ -110,6 +110,12 @@ class BNoteAttachment extends AbstractBeccaEntity {
|
||||
}
|
||||
|
||||
beforeSaving() {
|
||||
if (!this.name.match(/^[a-z0-9]+$/i)) {
|
||||
throw new Error(`Name must be alphanumerical, "${this.name}" given.`);
|
||||
}
|
||||
|
||||
this.noteAttachmentId = `${this.noteId}_${this.name}`;
|
||||
|
||||
super.beforeSaving();
|
||||
|
||||
this.utcDateModified = dateUtils.utcNowDateTime();
|
||||
|
@ -4,7 +4,7 @@ const build = require('./build');
|
||||
const packageJson = require('../../package');
|
||||
const {TRILIUM_DATA_DIR} = require('./data_dir');
|
||||
|
||||
const APP_DB_VERSION = 213;
|
||||
const APP_DB_VERSION = 214;
|
||||
const SYNC_VERSION = 30;
|
||||
const CLIPPER_PROTOCOL_VERSION = "1.0";
|
||||
|
||||
|
@ -198,11 +198,31 @@ class ConsistencyChecks {
|
||||
logError(`Relation '${attributeId}' references missing note '${noteId}'`)
|
||||
}
|
||||
});
|
||||
|
||||
this.findAndFixIssues(`
|
||||
SELECT noteAttachmentId, note_attachments.noteId AS noteId
|
||||
FROM note_attachments
|
||||
LEFT JOIN notes USING (noteId)
|
||||
WHERE notes.noteId IS NULL
|
||||
AND note_attachments.isDeleted = 0`,
|
||||
({noteAttachmentId, noteId}) => {
|
||||
if (this.autoFix) {
|
||||
const noteAttachment = becca.getNoteAttachment(noteAttachmentId);
|
||||
noteAttachment.markAsDeleted();
|
||||
|
||||
this.reloadNeeded = false;
|
||||
|
||||
logFix(`Note attachment '${noteAttachmentId}' has been deleted since it references missing note '${noteId}'`);
|
||||
} else {
|
||||
logError(`Note attachment '${noteAttachmentId}' references missing note '${noteId}'`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
findExistencyIssues() {
|
||||
// principle for fixing inconsistencies is that if the note itself is deleted (isDeleted=true) then all related entities should be also deleted (branches, attributes)
|
||||
// but if note is not deleted, then at least one branch should exist.
|
||||
// principle for fixing inconsistencies is that if the note itself is deleted (isDeleted=true) then all related
|
||||
// entities should be also deleted (branches, attributes), but if note is not deleted,
|
||||
// then at least one branch should exist.
|
||||
|
||||
// the order here is important - first we might need to delete inconsistent branches and after that
|
||||
// another check might create missing branch
|
||||
@ -304,6 +324,26 @@ class ConsistencyChecks {
|
||||
logError(`Duplicate branches for note '${noteId}' and parent '${parentNoteId}'`);
|
||||
}
|
||||
});
|
||||
|
||||
this.findAndFixIssues(`
|
||||
SELECT noteAttachmentId,
|
||||
note_attachments.noteId AS noteId
|
||||
FROM note_attachments
|
||||
JOIN notes USING (noteId)
|
||||
WHERE notes.isDeleted = 1
|
||||
AND note_attachments.isDeleted = 0`,
|
||||
({noteAttachmentId, noteId}) => {
|
||||
if (this.autoFix) {
|
||||
const noteAttachment = becca.getNoteAttachment(noteAttachmentId);
|
||||
noteAttachment.markAsDeleted();
|
||||
|
||||
this.reloadNeeded = false;
|
||||
|
||||
logFix(`Note attachment '${noteAttachmentId}' has been deleted since associated note '${noteId}' is deleted.`);
|
||||
} else {
|
||||
logError(`Note attachment '${noteAttachmentId}' is not deleted even though associated note '${noteId}' is deleted.`)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
findLogicIssues() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user