diff --git a/src/db-sql/migration.ts b/src/db-sql/migration.ts index c0358f20..eda0a7e8 100644 --- a/src/db-sql/migration.ts +++ b/src/db-sql/migration.ts @@ -117,6 +117,11 @@ const MIGRATIONS = [ id TEXT PRIMARY KEY, blobB64 TEXT ); + + CREATE TABLE IF NOT EXISTS migrations ( + name TEXT PRIMARY KEY, + applied_at TEXT NOT NULL DEFAULT (datetime('now')) + ); `, }, { @@ -126,8 +131,12 @@ const MIGRATIONS = [ `, }, { - name: "003_active_did_separate_table", + name: "003_active_did_separation", sql: ` + -- CONSOLIDATED MIGRATION: Combines original migrations 003, 004, 005, and 006 + -- This migration handles the complete separation of activeDid from settings + -- and establishes proper data integrity constraints + -- Create new active_identity table with proper constraints CREATE TABLE IF NOT EXISTS active_identity ( id INTEGER PRIMARY KEY CHECK (id = 1), @@ -150,40 +159,22 @@ const MIGRATIONS = [ lastUpdated = datetime('now') WHERE id = 1 AND EXISTS (SELECT 1 FROM settings WHERE id = 1 AND activeDid IS NOT NULL AND activeDid != ''); - `, - }, - { - name: "004_remove_activeDid_from_settings", - sql: ` + -- Remove activeDid column from settings table (moved to active_identity) -- Note: SQLite doesn't support DROP COLUMN in older versions -- This migration will be skipped if DROP COLUMN is not supported -- The activeDid column will remain but won't be used by the application - - -- Try to drop the activeDid column (works in SQLite 3.35.0+) ALTER TABLE settings DROP COLUMN activeDid; - `, - }, - { - name: "005_eliminate_master_settings_key", - sql: ` + -- Eliminate MASTER_SETTINGS_KEY concept - remove confusing id=1 row -- This creates clean separation: active_identity for current identity, settings for identity config - - -- Delete the confusing MASTER_SETTINGS_KEY row (id=1 with accountDid=NULL) DELETE FROM settings WHERE id = 1 AND accountDid IS NULL; -- Reset auto-increment to start from 1 again DELETE FROM sqlite_sequence WHERE name = 'settings'; - `, - }, - { - name: "006_add_unique_constraint_accountDid", - sql: ` + -- Add unique constraint to prevent duplicate accountDid values -- This ensures data integrity: each identity can only have one settings record - - -- First, remove any duplicate accountDid entries (keep the most recent one) DELETE FROM settings WHERE id NOT IN ( SELECT MAX(id) @@ -192,7 +183,6 @@ const MIGRATIONS = [ GROUP BY accountDid ) AND accountDid IS NOT NULL; - -- Add unique constraint on accountDid CREATE UNIQUE INDEX IF NOT EXISTS idx_settings_accountDid_unique ON settings(accountDid); `, },