From eaab82551fb8d138786ab61f8d56710e787cbe81 Mon Sep 17 00:00:00 2001 From: zadam Date: Fri, 17 Feb 2023 23:23:53 +0100 Subject: [PATCH] API migration to remove deprecated calls --- db/migrations/0213__migrate_scripts.js | 48 ++++++++++++++++++++++++++ src/services/app_info.js | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 db/migrations/0213__migrate_scripts.js diff --git a/db/migrations/0213__migrate_scripts.js b/db/migrations/0213__migrate_scripts.js new file mode 100644 index 000000000..eed005c1c --- /dev/null +++ b/db/migrations/0213__migrate_scripts.js @@ -0,0 +1,48 @@ +module.exports = () => { + const beccaLoader = require("../../src/becca/becca_loader"); + const becca = require("../../src/becca/becca"); + const cls = require("../../src/services/cls"); + const log = require("../../src/services/log"); + + cls.init(() => { + beccaLoader.load(); + + for (const note of Object.values(becca.notes)) { + try { + if (!note.isJavaScript()) { + continue; + } + + if (!note.mime?.endsWith('env=frontend') && !note.mime?.endsWith('env=backend')) { + continue; + } + + const origContent = note.getContent().toString(); + const fixedContent = origContent + .replaceAll("runOnServer", "runOnBackend") + .replaceAll("api.refreshTree()", "") + .replaceAll("addTextToActiveTabEditor", "addTextToActiveContextEditor") + .replaceAll("getActiveTabNote", "getActiveContextNote") + .replaceAll("getActiveTabTextEditor", "getActiveContextTextEditor") + .replaceAll("getActiveTabNotePath", "getActiveContextNotePath") + .replaceAll("getDateNote", "getDayNote") + .replaceAll("utils.unescapeHtml", "unescapeHtml") + .replaceAll("sortNotesByTitle", "sortNotes") + .replaceAll("CollapsibleWidget", "RightPanelWidget") + .replaceAll("TabAwareWidget", "NoteContextAwareWidget") + .replaceAll("TabCachingWidget", "NoteContextAwareWidget") + .replaceAll("NoteContextCachingWidget", "NoteContextAwareWidget"); + + if (origContent !== fixedContent) { + log.info(`Replacing legacy API calls for note '${note.noteId}'`); + + note.saveNoteRevision(); + note.setContent(fixedContent); + } + } + catch (e) { + log.error(`Error during migration to 213 for note '${note.noteId}': ${e.message} ${e.stack}`); + } + } + }); +}; diff --git a/src/services/app_info.js b/src/services/app_info.js index 4a8f50158..3f5f02f3b 100644 --- a/src/services/app_info.js +++ b/src/services/app_info.js @@ -4,7 +4,7 @@ const build = require('./build'); const packageJson = require('../../package'); const {TRILIUM_DATA_DIR} = require('./data_dir'); -const APP_DB_VERSION = 212; +const APP_DB_VERSION = 213; const SYNC_VERSION = 29; const CLIPPER_PROTOCOL_VERSION = "1.0";