migrating canvas etc.

This commit is contained in:
zadam 2023-01-23 16:57:28 +01:00
parent 339d8a7378
commit 8a33645360
6 changed files with 115 additions and 5 deletions

View File

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

View 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);
}
});
};

View File

@ -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();

View File

@ -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();

View File

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

View File

@ -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() {