Skip to content

Build your personal knowledge base with Trilium Notes

17.2 KB
Raw Blame
/**
 * @module
 *
 * Contains all the migrations that are run on the database.
 */
export function getMaxMigrationVersion() {
    return MIGRATIONS[0].version;
}

// Migrations should be kept in descending order, so the latest migration is first.
export const MIGRATIONS: (SqlMigration | JsMigration)[] = [
    // The in-app help (`_help` subtree) became virtual — injected into becca from the bundled
    // help meta on every load, never persisted or synced (see services/virtual_notes.ts).
    // Remove the previously persisted rows, including their entity_changes so the deletions
    // don't churn through sync: every instance in a cluster runs this same migration (the
    // accompanying sync-version bump guarantees no un-migrated peers), so all copies converge.
    // User clones of help notes and user attributes on them are dropped by design — the help
    // structure is application-owned. Orphaned blobs are vacuumed by the regular erase job.
    // The rows for indirectly-keyed entities must be removed from entity_changes before their
    // base rows disappear from under the subselects.
    {
        version: 242,
        sql: /*sql*/`
            DELETE FROM entity_changes WHERE entityName = 'branches' AND entityId IN
                (SELECT branchId FROM branches WHERE noteId LIKE '\\_help%' ESCAPE '\\' OR parentNoteId LIKE '\\_help%' ESCAPE '\\');
            DELETE FROM entity_changes WHERE entityName = 'attributes' AND entityId IN
                (SELECT attributeId FROM attributes WHERE noteId LIKE '\\_help%' ESCAPE '\\');
            DELETE FROM entity_changes WHERE entityName = 'revisions' AND entityId IN
                (SELECT revisionId FROM revisions WHERE noteId LIKE '\\_help%' ESCAPE '\\');
            DELETE FROM entity_changes WHERE entityName = 'attachments' AND entityId IN
                (SELECT attachmentId FROM attachments WHERE ownerId LIKE '\\_help%' ESCAPE '\\');
            -- Blanket sweep by ID prefix: catches notes and note_reordering rows directly, plus
            -- tombstones of help branches/attributes deleted (and later erased) by past releases —
            -- their base rows are gone, so the subselects above cannot find them, but branch IDs
            -- ('parentNoteId_noteId') and attribute IDs ('noteId_...') carry the _help prefix.
            DELETE FROM entity_changes WHERE entityId LIKE '\\_help%' ESCAPE '\\';
            DELETE FROM branches WHERE noteId LIKE '\\_help%' ESCAPE '\\' OR parentNoteId LIKE '\\_help%' ESCAPE '\\';
            DELETE FROM attributes WHERE noteId LIKE '\\_help%' ESCAPE '\\';
            DELETE FROM revisions WHERE noteId LIKE '\\_help%' ESCAPE '\\';
            DELETE FROM attachments WHERE ownerId LIKE '\\_help%' ESCAPE '\\';
            DELETE FROM notes WHERE noteId LIKE '\\_help%' ESCAPE '\\';
        `
    },
    // Delete the globe icon the web clipper wrote onto clippings, so they take the icon of their clipType
    {
        version: 241,
        module: () => import("./0241__drop_clipper_globe_icon.js")
    },
    // Give every board its own select definition for the label it groups by, so the columns stop
    // living only in the board.json attachment
    {
        version: 240,
        module: () => import("./0240__migrate_board_status_to_select.js")
    },
    // Turn TOTP back off for installs that had disabled MFA before the enable flag was removed
    {
        version: 239,
        module: () => import("./0239__disable_totp_when_mfa_was_turned_off.js")
    },
    // Add description column to revisions table for manual revision comments
    {
        version: 238,
        sql: /*sql*/`
            ALTER TABLE revisions ADD COLUMN description TEXT DEFAULT '' NOT NULL;
            ALTER TABLE revisions ADD COLUMN source TEXT DEFAULT 'auto' NOT NULL;
        `,
        ignoreErrors: true
    },
    // Clean up obsolete keyboard shortcut options from renamed actions
    {
        version: 237,
        sql: /*sql*/`
            DELETE FROM options WHERE name = 'keyboardShortcutsShowNoteRevisions';
            DELETE FROM entity_changes WHERE entityName = 'options' AND entityId = 'keyboardShortcutsShowNoteRevisions';
            DELETE FROM options WHERE name = 'keyboardShortcutsForceSaveNoteRevision';
            DELETE FROM entity_changes WHERE entityName = 'options' AND entityId = 'keyboardShortcutsForceSaveNoteRevision';
        `
    },
    // Add text representation column to blobs table
    {
        version: 236,
        sql: /*sql*/`\
            ALTER TABLE blobs ADD COLUMN textRepresentation TEXT DEFAULT NULL;
        `,
        ignoreErrors: true
    },
    // Add missing database indices for query performance
    {
        version: 235,
        sql: /*sql*/`
            CREATE INDEX IF NOT EXISTS IDX_entity_changes_isSynced_id
                ON entity_changes (isSynced, id);
            CREATE INDEX IF NOT EXISTS IDX_entity_changes_isErased_entityName
                ON entity_changes (isErased, entityName);
            CREATE INDEX IF NOT EXISTS IDX_notes_isDeleted_utcDateModified
                ON notes (isDeleted, utcDateModified);
            CREATE INDEX IF NOT EXISTS IDX_branches_isDeleted_utcDateModified
                ON branches (isDeleted, utcDateModified);
            CREATE INDEX IF NOT EXISTS IDX_attributes_isDeleted_utcDateModified
                ON attributes (isDeleted, utcDateModified);
            CREATE INDEX IF NOT EXISTS IDX_attachments_isDeleted_utcDateModified
                ON attachments (isDeleted, utcDateModified);
            DROP INDEX IF EXISTS IDX_branches_parentNoteId;
            CREATE INDEX IF NOT EXISTS IDX_branches_parentNoteId_isDeleted_notePosition
                ON branches (parentNoteId, isDeleted, notePosition);
        `
    },
    // Migrate aiChat notes to code notes since LLM integration has been removed
    {
        version: 234,
        module: async () => import("./0234__migrate_ai_chat_to_code.js")
    },
    // Migrate geo map to collection
    {
        version: 233,
        module: async () => import("./0233__migrate_geo_map_to_collection.js")
    },
    // Remove embedding tables since LLM embedding functionality has been removed
    {
        version: 232,
        sql: /*sql*/`
            -- Remove LLM embedding tables and data
            DROP TABLE IF EXISTS "note_embeddings";
            DROP TABLE IF EXISTS "embedding_queue";
            DROP TABLE IF EXISTS "embedding_providers";

            -- Remove embedding-related entity changes
            DELETE FROM entity_changes WHERE entityName IN ('note_embeddings', 'embedding_queue', 'embedding_providers');
        `
    },
    // Session store
    {
        version: 231,
        sql: /*sql*/`\
            CREATE TABLE IF NOT EXISTS sessions (
                id TEXT PRIMARY KEY,
                data TEXT,
                expires INTEGER
            );
        `
    },
    // Add tables for vector embeddings storage and management
    // This migration adds embedding support to the main document.db database
    {
        version: 230,
        sql: /*sql*/`\
            -- Store embeddings for notes
            CREATE TABLE IF NOT EXISTS "note_embeddings" (
                "embedId" TEXT NOT NULL PRIMARY KEY,
                "noteId" TEXT NOT NULL,
                "providerId" TEXT NOT NULL,
                "modelId" TEXT NOT NULL,
                "dimension" INTEGER NOT NULL,
                "embedding" BLOB NOT NULL,
                "version" INTEGER NOT NULL DEFAULT 1,
                "dateCreated" TEXT NOT NULL,
                "utcDateCreated" TEXT NOT NULL,
                "dateModified" TEXT NOT NULL,
                "utcDateModified" TEXT NOT NULL
            );

            CREATE INDEX "IDX_note_embeddings_noteId" ON "note_embeddings" ("noteId");
            CREATE INDEX "IDX_note_embeddings_providerId_modelId" ON "note_embeddings" ("providerId", "modelId");

            -- Table to track which notes need embedding updates
            CREATE TABLE IF NOT EXISTS "embedding_queue" (
                "noteId" TEXT NOT NULL PRIMARY KEY,
                "operation" TEXT NOT NULL, -- CREATE, UPDATE, DELETE
                "dateQueued" TEXT NOT NULL,
                "utcDateQueued" TEXT NOT NULL,
                "priority" INTEGER NOT NULL DEFAULT 0,
                "attempts" INTEGER NOT NULL DEFAULT 0,
                "lastAttempt" TEXT NULL,
                "error" TEXT NULL,
                "failed" INTEGER NOT NULL DEFAULT 0,
                "isProcessing" INTEGER NOT NULL DEFAULT 0
            );

            -- Table to store embedding provider configurations
            CREATE TABLE IF NOT EXISTS "embedding_providers" (
                "providerId" TEXT NOT NULL PRIMARY KEY,
                "name" TEXT NOT NULL,
                "priority" INTEGER NOT NULL DEFAULT 0,
                "config" TEXT NOT NULL, -- JSON config object
                "dateCreated" TEXT NOT NULL,
                "utcDateCreated" TEXT NOT NULL,
                "dateModified" TEXT NOT NULL,
                "utcDateModified" TEXT NOT NULL
            );
        `
    },

    // add the oauth user data table
    {
        version: 229,
        sql: /*sql*/`\
            CREATE TABLE IF NOT EXISTS "user_data"
            (
                tmpID INT,
                username TEXT,
                email TEXT,
                userIDEncryptedDataKey TEXT,
                userIDVerificationHash TEXT,
                salt TEXT,
                derivedKey TEXT,
                isSetup TEXT DEFAULT "false",
                UNIQUE (tmpID),
                PRIMARY KEY (tmpID)
            );
        `
    },
    // fix blob IDs
    {
        version: 228,
        sql: /*sql*/`\
        -- + is normally replaced by X and / by Y, but this can temporarily cause UNIQUE key exception
        -- this might create blob duplicates, but cleanup will eventually take care of it

        UPDATE blobs SET blobId = REPLACE(blobId, '+', 'A');
        UPDATE blobs SET blobId = REPLACE(blobId, '/', 'B');

        UPDATE notes SET blobId = REPLACE(blobId, '+', 'A');
        UPDATE notes SET blobId = REPLACE(blobId, '/', 'B');

        UPDATE attachments SET blobId = REPLACE(blobId, '+', 'A');
        UPDATE attachments SET blobId = REPLACE(blobId, '/', 'B');

        UPDATE revisions SET blobId = REPLACE(blobId, '+', 'A');
        UPDATE revisions SET blobId = REPLACE(blobId, '/', 'B');

        UPDATE entity_changes SET entityId = REPLACE(entityId, '+', 'A') WHERE entityName = 'blobs';
        UPDATE entity_changes SET entityId = REPLACE(entityId, '/', 'B') WHERE entityName = 'blobs';
        `
    },
    // disable image compression
    {
        version: 227,
        sql: /*sql*/`\
            -- emergency disabling of image compression since it appears to make problems in migration to 0.61
            UPDATE options SET value = 'false' WHERE name = 'compressImages';
        `
    },
    // rename note size label
    {
        version: 226,
        sql: /*sql*/`\
            UPDATE attributes SET value = 'contentAndAttachmentsAndRevisionsSize' WHERE name = 'orderBy' AND value = 'noteSize';
        `
    },
    // create blob ID indices
    {
        version: 225,
        sql: /*sql*/`\
            CREATE INDEX IF NOT EXISTS IDX_notes_blobId on notes (blobId);
            CREATE INDEX IF NOT EXISTS IDX_revisions_blobId on revisions (blobId);
            CREATE INDEX IF NOT EXISTS IDX_attachments_blobId on attachments (blobId);
        `
    },
    // fix blob IDs
    {
        version: 224,
        sql: /*sql*/`\
            UPDATE blobs SET blobId = REPLACE(blobId, '+', 'X');
            UPDATE blobs SET blobId = REPLACE(blobId, '/', 'Y');

            UPDATE notes SET blobId = REPLACE(blobId, '+', 'X');
            UPDATE notes SET blobId = REPLACE(blobId, '/', 'Y');

            UPDATE attachments SET blobId = REPLACE(blobId, '+', 'X');
            UPDATE attachments SET blobId = REPLACE(blobId, '/', 'Y');

            UPDATE revisions SET blobId = REPLACE(blobId, '+', 'X');
            UPDATE revisions SET blobId = REPLACE(blobId, '/', 'Y');

            UPDATE entity_changes SET entityId = REPLACE(entityId, '+', 'X') WHERE entityName = 'blobs';
            UPDATE entity_changes SET entityId = REPLACE(entityId, '/', 'Y') WHERE entityName = 'blobs';
        `
    },
    // no operation
    {
        version: 223,
        sql: /*sql*/`\
            SELECT 1;
        `
    },
    // rename open tabs to open note contexts
    {
        version: 222,
        sql: /*sql*/`\
            UPDATE options SET name = 'openNoteContexts' WHERE name = 'openTabs';
            UPDATE entity_changes SET entityId = 'openNoteContexts' WHERE entityName = 'options' AND entityId = 'openTabs';
        `
    },
    // remove hide included images option
    {
        version: 221,
        sql: /*sql*/`\
            DELETE FROM options WHERE name = 'hideIncludedImages_main';
            DELETE FROM entity_changes WHERE entityName = 'options' AND entityId = 'hideIncludedImages_main';
        `
    },
    // migrate images to attachments
    {
        version: 220,
        module: () => import("./0220__migrate_images_to_attachments.js")
    },
    // attachments
    {
        version: 219,
        sql: /*sql*/`\
            CREATE TABLE IF NOT EXISTS "attachments"
            (
                attachmentId      TEXT not null primary key,
                ownerId       TEXT not null,
                role         TEXT not null,
                mime         TEXT not null,
                title         TEXT not null,
                isProtected    INT  not null DEFAULT 0,
                position     INT  default 0 not null,
                blobId    TEXT DEFAULT null,
                dateModified TEXT NOT NULL,
                utcDateModified TEXT not null,
                utcDateScheduledForErasureSince TEXT DEFAULT NULL,
                isDeleted    INT  not null,
                deleteId    TEXT DEFAULT NULL);

            CREATE INDEX IDX_attachments_ownerId_role
                on attachments (ownerId, role);

            CREATE INDEX IDX_attachments_utcDateScheduledForErasureSince
                on attachments (utcDateScheduledForErasureSince);

            CREATE INDEX IF NOT EXISTS IDX_attachments_blobId on attachments (blobId);
        `
    },
    // rename note revision to revision
    {
        version: 218,
        sql: /*sql*/`\
            CREATE TABLE IF NOT EXISTS "revisions" (
                revisionId	TEXT NOT NULL PRIMARY KEY,
                noteId	TEXT NOT NULL,
                type TEXT DEFAULT '' NOT NULL,
                mime TEXT DEFAULT '' NOT NULL,
                title	TEXT NOT NULL,
                isProtected	INT NOT NULL DEFAULT 0,
                blobId TEXT DEFAULT NULL,
                utcDateLastEdited TEXT NOT NULL,
                utcDateCreated TEXT NOT NULL,
                utcDateModified TEXT NOT NULL,
                dateLastEdited TEXT NOT NULL,
                dateCreated TEXT NOT NULL
            );

            INSERT INTO revisions (revisionId, noteId, type, mime, title, isProtected, utcDateLastEdited, utcDateCreated, utcDateModified, dateLastEdited, dateCreated, blobId)
            SELECT noteRevisionId, noteId, type, mime, title, isProtected, utcDateLastEdited, utcDateCreated, utcDateModified, dateLastEdited, dateCreated, blobId FROM note_revisions;

            DROP TABLE note_revisions;

            CREATE INDEX IDX_revisions_noteId ON revisions (noteId);
            CREATE INDEX IDX_revisions_utcDateCreated ON revisions (utcDateCreated);
            CREATE INDEX IDX_revisions_utcDateLastEdited ON revisions (utcDateLastEdited);
            CREATE INDEX IDX_revisions_dateCreated ON revisions (dateCreated);
            CREATE INDEX IDX_revisions_dateLastEdited ON revisions (dateLastEdited);
            CREATE INDEX IF NOT EXISTS IDX_revisions_blobId on revisions (blobId);

            UPDATE entity_changes SET entityName = 'revisions' WHERE entityName = 'note_revisions';
        `
    },
    // drop content tables
    {
        version: 217,
        sql: /*sql*/`\
            DROP TABLE note_contents;
            DROP TABLE note_revision_contents;

            DELETE FROM entity_changes WHERE entityName IN ('note_contents', 'note_revision_contents');
        `
    },
    {
        version: 216,
        module: async () => import("./0216__move_content_into_blobs.js")
    },
    // content structure
    {
        version: 215,
        sql: /*sql*/`\
            CREATE TABLE IF NOT EXISTS "blobs" (
                blobId	TEXT NOT NULL,
                content	TEXT NULL DEFAULT NULL,
                dateModified TEXT NOT NULL,
                utcDateModified TEXT NOT NULL,
                PRIMARY KEY (blobId)
            );

            ALTER TABLE notes ADD blobId TEXT DEFAULT NULL;
            ALTER TABLE note_revisions ADD blobId TEXT DEFAULT NULL;

            CREATE INDEX IF NOT EXISTS IDX_notes_blobId on notes (blobId);
            CREATE INDEX IF NOT EXISTS IDX_note_revisions_blobId on note_revisions (blobId);
        `
    }
];

interface Migration {
    version: number;
    /** If true, errors during this migration are logged but do not halt the migration process. Useful for migrations that may have already been applied (e.g. adding a column that already exists). */
    ignoreErrors?: boolean;
}

interface SqlMigration extends Migration {
    sql: string;
}

interface JsMigration extends Migration {
    module: () => Promise<{ default: () => void }>;
}