From dc6daf3f542ab622afb21848fda628c57cc45788 Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Sun, 7 Jun 2026 22:01:57 +0200 Subject: [PATCH] refactor(migrations): extract applyMigrations as callable function --- src/migrations/apply.ts | 83 ++++++++++++++++++++++++++++++++++++++++ src/migrations/init.ts | 85 ++--------------------------------------- 2 files changed, 87 insertions(+), 81 deletions(-) create mode 100644 src/migrations/apply.ts diff --git a/src/migrations/apply.ts b/src/migrations/apply.ts new file mode 100644 index 0000000..6ded012 --- /dev/null +++ b/src/migrations/apply.ts @@ -0,0 +1,83 @@ +import type { Database } from "@db/sqlite"; + +export const applyMigrations = (db: Database): void => { + db.exec(` + CREATE TABLE IF NOT EXISTS note ( + title TEXT NOT NULL, + publishedAt DATETIME, + createdAt DATETIME NOT NULL, + did TEXT NOT NULL, + rkey TEXT NOT NULL, + discoverable INTEGER NOT NULL DEFAULT 1, + PRIMARY KEY (did, rkey) + ); + `); + + try { + db.exec( + `ALTER TABLE note ADD COLUMN listed INTEGER NOT NULL DEFAULT 1;`, + ); + } catch { + // Column already exists — no-op + } + + try { + db.exec( + `ALTER TABLE note ADD COLUMN language STRING;`, + ); + } catch { + // Column already exists — no-op + } + + try { + db.exec(`ALTER TABLE note RENAME COLUMN listed TO discoverable;`); + } catch { + // Column already renamed or doesn't exist — no-op + } + + db.exec(` + CREATE TABLE IF NOT EXISTS state ( + key TEXT PRIMARY KEY, + value TEXT NOT NULL + ); + `); + + db.exec(` + CREATE TABLE IF NOT EXISTS webhook_subscription ( + id INTEGER PRIMARY KEY, + did TEXT NOT NULL, + method TEXT NOT NULL, + url TEXT NOT NULL + ); + `); + + db.exec(` + CREATE INDEX IF NOT EXISTS idx_webhook_subscription_did + ON webhook_subscription(did); + `); + + try { + db.exec(`ALTER TABLE webhook_subscription ADD COLUMN token TEXT;`); + } catch { + // Column already exists — no-op + } + + try { + db.exec( + `ALTER TABLE webhook_subscription ADD COLUMN verb TEXT NOT NULL DEFAULT 'create';`, + ); + db.exec(` + INSERT INTO webhook_subscription (did, method, url, token, verb) + SELECT did, method, url, token, 'delete' + FROM webhook_subscription + WHERE verb = 'create'; + `); + } catch { + // Column already exists — backfill already happened on a previous run. + } + + db.exec(` + CREATE INDEX IF NOT EXISTS idx_webhook_subscription_did_verb + ON webhook_subscription(did, verb); + `); +}; diff --git a/src/migrations/init.ts b/src/migrations/init.ts index 21a540d..4999220 100644 --- a/src/migrations/init.ts +++ b/src/migrations/init.ts @@ -1,83 +1,6 @@ -import { db } from "../data/db.ts"; - -db.exec(` - CREATE TABLE IF NOT EXISTS note ( - title TEXT NOT NULL, - publishedAt DATETIME, - createdAt DATETIME NOT NULL, - did TEXT NOT NULL, - rkey TEXT NOT NULL, - discoverable INTEGER NOT NULL DEFAULT 1, - PRIMARY KEY (did, rkey) - ); -`); - -try { - db.exec( - `ALTER TABLE note ADD COLUMN listed INTEGER NOT NULL DEFAULT 1;`, - ); -} catch { - // Column already exists — no-op -} - -try { - db.exec( - `ALTER TABLE note ADD COLUMN language STRING;`, - ); -} catch { - // Column already exists — no-op -} - -try { - db.exec(`ALTER TABLE note RENAME COLUMN listed TO discoverable;`); -} catch { - // Column already renamed or doesn't exist — no-op -} - -db.exec(` - CREATE TABLE IF NOT EXISTS state ( - key TEXT PRIMARY KEY, - value TEXT NOT NULL - ); -`); - -db.exec(` - CREATE TABLE IF NOT EXISTS webhook_subscription ( - id INTEGER PRIMARY KEY, - did TEXT NOT NULL, - method TEXT NOT NULL, - url TEXT NOT NULL - ); -`); - -db.exec(` - CREATE INDEX IF NOT EXISTS idx_webhook_subscription_did - ON webhook_subscription(did); -`); - -try { - db.exec(`ALTER TABLE webhook_subscription ADD COLUMN token TEXT;`); -} catch { - // Column already exists — no-op -} - -try { - db.exec( - `ALTER TABLE webhook_subscription ADD COLUMN verb TEXT NOT NULL DEFAULT 'create';`, - ); - db.exec(` - INSERT INTO webhook_subscription (did, method, url, token, verb) - SELECT did, method, url, token, 'delete' - FROM webhook_subscription - WHERE verb = 'create'; - `); -} catch { - // Column already exists — backfill already happened on a previous run. -} - -db.exec(` - CREATE INDEX IF NOT EXISTS idx_webhook_subscription_did_verb - ON webhook_subscription(did, verb); -`); +import { Database } from "@db/sqlite"; +import { applyMigrations } from "./apply.ts"; +const db = new Database(Deno.env.get("SQLITE_PATH") ?? "notes.db"); +applyMigrations(db); db.close();