Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(migration): JavaScript-based migrations not working
Elian Dorana80c3e32f1d4
diff --git a/db/migrations/0216__move_content_into_blobs.js b/db/migrations/0216__move_content_into_blobs.tssimilarity index 71%rename from db/migrations/0216__move_content_into_blobs.jsrename to db/migrations/0216__move_content_into_blobs.tsindex 53586d0e3d..e43bd25e34 100644--- a/db/migrations/0216__move_content_into_blobs.js+++ b/db/migrations/0216__move_content_into_blobs.ts@@ -1,11 +1,24 @@-module.exports = () => {- const sql = require("../../src/services/sql");- const utils = require("../../src/services/utils");+interface NoteContentsRow {+ noteId: string;+ content: string | Buffer;+ dateModified: string;+ utcDateModified: string;+}++interface NoteRevisionContents {+ noteRevisionId: string;+ content: string | Buffer;+ utcDateModified: string;+}++export default async () => {+ const sql = (await import("../../src/services/sql")).default;+ const utils = (await import("../../src/services/utils")).default;const existingBlobIds = new Set();- for (const noteId of sql.getColumn(`SELECT noteId FROM note_contents`)) {- const row = sql.getRow(`SELECT noteId, content, dateModified, utcDateModified FROM note_contents WHERE noteId = ?`, [noteId]);+ for (const noteId of sql.getColumn<string>(`SELECT noteId FROM note_contents`)) {+ const row = sql.getRow<NoteContentsRow>(`SELECT noteId, content, dateModified, utcDateModified FROM note_contents WHERE noteId = ?`, [noteId]);const blobId = utils.hashedBlobId(row.content);if (!existingBlobIds.has(blobId)) {@@ -28,7 +41,7 @@ module.exports = () => {}for (const noteRevisionId of sql.getColumn(`SELECT noteRevisionId FROM note_revision_contents`)) {- const row = sql.getRow(`SELECT noteRevisionId, content, utcDateModified FROM note_revision_contents WHERE noteRevisionId = ?`, [noteRevisionId]);+ const row = sql.getRow<NoteRevisionContents>(`SELECT noteRevisionId, content, utcDateModified FROM note_revision_contents WHERE noteRevisionId = ?`, [noteRevisionId]);const blobId = utils.hashedBlobId(row.content);if (!existingBlobIds.has(blobId)) {@@ -44,7 +57,7 @@ module.exports = () => {sql.execute("UPDATE entity_changes SET entityName = 'blobs', entityId = ? WHERE entityName = 'note_revision_contents' AND entityId = ?", [blobId, row.noteRevisionId]);} else {// duplicates- sql.execute("DELETE FROM entity_changes WHERE entityName = 'note_revision_contents' AND entityId = ?", [row.noteId]);+ sql.execute("DELETE FROM entity_changes WHERE entityName = 'note_revision_contents' AND entityId = ?", [row.noteRevisionId]);}sql.execute("UPDATE note_revisions SET blobId = ? WHERE noteRevisionId = ?", [blobId, row.noteRevisionId]);diff --git a/db/migrations/0220__migrate_images_to_attachments.js b/db/migrations/0220__migrate_images_to_attachments.tssimilarity index 66%rename from db/migrations/0220__migrate_images_to_attachments.jsrename to db/migrations/0220__migrate_images_to_attachments.tsindex a93e2028d7..60cfd337ff 100644--- a/db/migrations/0220__migrate_images_to_attachments.js+++ b/db/migrations/0220__migrate_images_to_attachments.ts@@ -1,9 +1,9 @@-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");- const sql = require("../../src/services/sql");+export default async () => {+ const beccaLoader = (await import("../../src/becca/becca_loader")).default;+ const becca = (await import("../../src/becca/becca")).default;+ const cls = (await import("../../src/services/cls")).default;+ const log = (await import("../../src/services/log")).default;+ const sql = (await import("../../src/services/sql")).default;cls.init(() => {// emergency disabling of image compression since it appears to make problems in migration to 0.61diff --git a/src/services/migration.spec.ts b/src/services/migration.spec.tsindex e69de29bb2..0cd6c28392 100644--- a/src/services/migration.spec.ts+++ b/src/services/migration.spec.ts@@ -0,0 +1,17 @@+import { describe, it } from "vitest";+import becca from "../becca/becca.js";+import sql from "./sql.js";+import migration from "./migration.js";+import cls from "./cls.js";++describe("Migration", () => {+ it("migrates from v214", async () => {+ return new Promise<void>((resolve) => {+ cls.init(async () => {+ sql.rebuildIntegrationTestDatabase("test/db/document_v214.db");+ await migration.migrateIfNecessary();+ resolve();+ });+ });+ });+});diff --git a/src/services/migration.ts b/src/services/migration.tsindex f8cbfbc223..e0fc2a6ed9 100644--- a/src/services/migration.ts+++ b/src/services/migration.ts@@ -106,7 +106,7 @@ async function executeMigration(mig: MigrationInfo) {console.log("Migration with JS module");const migrationModule = await import(`${resourceDir.MIGRATIONS_DIR}/${mig.file}`);- migrationModule();+ await migrationModule.default();} else {throw new Error(`Unknown migration type '${mig.type}'`);}diff --git a/src/services/sql.ts b/src/services/sql.tsindex ab98d3a04a..87d3df936c 100644--- a/src/services/sql.ts+++ b/src/services/sql.ts@@ -25,14 +25,18 @@ function buildDatabase() {return new Database(dataDir.DOCUMENT_PATH);}-function buildIntegrationTestDatabase() {- const dbBuffer = fs.readFileSync(dataDir.DOCUMENT_PATH);+function buildIntegrationTestDatabase(dbPath?: string) {+ const dbBuffer = fs.readFileSync(dbPath ?? dataDir.DOCUMENT_PATH);return new Database(dbBuffer);}-function rebuildIntegrationTestDatabase() {+function rebuildIntegrationTestDatabase(dbPath: string) {+ if (dbConnection) {+ dbConnection.close();+ }+// This allows a database that is read normally but is kept in memory and discards all modifications.- dbConnection = buildIntegrationTestDatabase();+ dbConnection = buildIntegrationTestDatabase(dbPath);statementCache = {};}diff --git a/test/db/document_v214.db b/test/db/document_v214.dbindex 482024b745..928561d9d8 100644Binary files a/test/db/document_v214.db and b/test/db/document_v214.db differ