Download src/lib/migrate.ts from Zero124/VR-MEDIA: direct link, hf CLI and curl.
- Browser
- Download file 3.8 kB
-
https://huggingface.co/spaces/Zero124/VR-MEDIA/resolve/main/src/lib/migrate.ts
- Command line
-
hf download hf://spaces/Zero124/VR-MEDIA/src/lib/migrate.ts
-
curl -L -o migrate.ts https://huggingface.co/spaces/Zero124/VR-MEDIA/resolve/main/src/lib/migrate.ts
3.8 kB
| // Lightweight self-migration for the persistent SQLite volume. | |
| // | |
| // The Docker image seeds /data/vrms.db only on first boot, so an existing | |
| // deployment keeps its old table shape after an upgrade. Rather than shipping | |
| // the Prisma CLI into the runtime image, we apply the few additive columns this | |
| // version needs with idempotent ALTER TABLE statements. | |
| // | |
| // Safe to run on every boot: "duplicate column name" errors are swallowed. | |
| import { db } from "./db"; | |
| type Statement = { sql: string; label: string }; | |
| const STATEMENTS: Statement[] = [ | |
| // Incremental-sync bookkeeping on Video. | |
| { | |
| label: "Video.availability", | |
| sql: `ALTER TABLE "Video" ADD COLUMN "availability" TEXT NOT NULL DEFAULT 'available'`, | |
| }, | |
| { | |
| label: "Video.lastSeenAt", | |
| sql: `ALTER TABLE "Video" ADD COLUMN "lastSeenAt" DATETIME`, | |
| }, | |
| { | |
| label: "Video.missingSince", | |
| sql: `ALTER TABLE "Video" ADD COLUMN "missingSince" DATETIME`, | |
| }, | |
| { | |
| label: "Video.contentKey", | |
| sql: `ALTER TABLE "Video" ADD COLUMN "contentKey" TEXT`, | |
| }, | |
| { | |
| label: "Video.contentHash", | |
| sql: `ALTER TABLE "Video" ADD COLUMN "contentHash" TEXT`, | |
| }, | |
| // Custom thumbnails matched from cloud-drive images (Blomp first). | |
| { | |
| label: "Video.customThumbSource", | |
| sql: `ALTER TABLE "Video" ADD COLUMN "customThumbSource" TEXT`, | |
| }, | |
| { | |
| label: "Video.customThumbPath", | |
| sql: `ALTER TABLE "Video" ADD COLUMN "customThumbPath" TEXT`, | |
| }, | |
| { | |
| label: "Video.customThumbHash", | |
| sql: `ALTER TABLE "Video" ADD COLUMN "customThumbHash" TEXT`, | |
| }, | |
| { | |
| label: "Video.customThumbMime", | |
| sql: `ALTER TABLE "Video" ADD COLUMN "customThumbMime" TEXT`, | |
| }, | |
| { | |
| label: "Video.customThumbAt", | |
| sql: `ALTER TABLE "Video" ADD COLUMN "customThumbAt" DATETIME`, | |
| }, | |
| // Auto thumbnail upload: web images found by video title and pushed to | |
| // the connected Blomp drive (src/lib/thumb-uploader.ts). | |
| { | |
| label: "Video.autoThumbPath", | |
| sql: `ALTER TABLE "Video" ADD COLUMN "autoThumbPath" TEXT`, | |
| }, | |
| { | |
| label: "Video.autoThumbAt", | |
| sql: `ALTER TABLE "Video" ADD COLUMN "autoThumbAt" DATETIME`, | |
| }, | |
| { | |
| label: "Video.availability index", | |
| sql: `CREATE INDEX IF NOT EXISTS "Video_availability_idx" ON "Video" ("availability")`, | |
| }, | |
| { | |
| label: "Video.contentKey index", | |
| sql: `CREATE INDEX IF NOT EXISTS "Video_contentKey_idx" ON "Video" ("contentKey")`, | |
| }, | |
| ]; | |
| let done = false; | |
| function isBenign(message: string): boolean { | |
| const m = message.toLowerCase(); | |
| return ( | |
| m.includes("duplicate column") || | |
| m.includes("already exists") || | |
| m.includes("duplicate column name") | |
| ); | |
| } | |
| /** Apply pending additive migrations. Runs at most once per process. */ | |
| export async function ensureSchema(): Promise<void> { | |
| if (done) return; | |
| done = true; | |
| let applied = 0; | |
| for (const stmt of STATEMENTS) { | |
| try { | |
| await db.$executeRawUnsafe(stmt.sql); | |
| applied++; | |
| console.log(`[migrate] applied ${stmt.label}`); | |
| } catch (err) { | |
| const msg = err instanceof Error ? err.message : String(err); | |
| if (!isBenign(msg)) { | |
| console.warn(`[migrate] ${stmt.label} failed: ${msg}`); | |
| } | |
| } | |
| } | |
| // Backfill the content key for rows created before this version. | |
| try { | |
| await db.$executeRawUnsafe( | |
| `UPDATE "Video" SET "contentKey" = "offcloudId" || '::' || "name" WHERE "contentKey" IS NULL` | |
| ); | |
| await db.$executeRawUnsafe( | |
| `UPDATE "Video" SET "availability" = 'available' WHERE "availability" IS NULL` | |
| ); | |
| } catch (err) { | |
| console.warn("[migrate] backfill skipped:", err); | |
| } | |
| if (applied > 0) console.log(`[migrate] schema up to date (${applied} statements)`); | |
| } | |