// Runtime schema migration for existing /data/vrms.db databases. // // The Prisma CLI is intentionally NOT shipped in the runtime image, so we use // the generated Prisma client's raw SQL escape hatch to add the multi-source // columns that were introduced with the TeraBox integration. Safe to run on // every container start (idempotent). import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient(); async function main() { const tables = await prisma.$queryRawUnsafe( `SELECT name FROM sqlite_master WHERE type='table' AND name='Video'` ); if (!Array.isArray(tables) || tables.length === 0) { console.log("[schema] Video table not found — skipping migration"); return; } const columns = await prisma.$queryRawUnsafe(`PRAGMA table_info("Video")`); const names = new Set(columns.map((column) => column.name)); const additions = [ { name: "source", sql: `ALTER TABLE "Video" ADD COLUMN "source" TEXT NOT NULL DEFAULT 'offcloud'`, }, { name: "sourceId", sql: `ALTER TABLE "Video" ADD COLUMN "sourceId" TEXT`, }, { name: "sourcePath", sql: `ALTER TABLE "Video" ADD COLUMN "sourcePath" TEXT`, }, // Custom thumbnails matched from cloud-drive images (Blomp first). { name: "customThumbSource", sql: `ALTER TABLE "Video" ADD COLUMN "customThumbSource" TEXT`, }, { name: "customThumbPath", sql: `ALTER TABLE "Video" ADD COLUMN "customThumbPath" TEXT`, }, { name: "customThumbHash", sql: `ALTER TABLE "Video" ADD COLUMN "customThumbHash" TEXT`, }, { name: "customThumbMime", sql: `ALTER TABLE "Video" ADD COLUMN "customThumbMime" TEXT`, }, { name: "customThumbAt", sql: `ALTER TABLE "Video" ADD COLUMN "customThumbAt" DATETIME`, }, // Auto thumbnail upload (web images pushed to the Blomp drive). { name: "autoThumbPath", sql: `ALTER TABLE "Video" ADD COLUMN "autoThumbPath" TEXT`, }, { name: "autoThumbAt", sql: `ALTER TABLE "Video" ADD COLUMN "autoThumbAt" DATETIME`, }, ]; for (const addition of additions) { if (names.has(addition.name)) { console.log(`[schema] Column Video.${addition.name} already exists`); continue; } await prisma.$executeRawUnsafe(addition.sql); console.log(`[schema] Added column Video.${addition.name}`); } // Backfill existing rows (created before multi-source support). await prisma.$executeRawUnsafe( `UPDATE "Video" SET "source" = 'offcloud' WHERE "source" IS NULL OR "source" = ''` ); await prisma.$executeRawUnsafe( `UPDATE "Video" SET "sourceId" = "offcloudId" WHERE ("sourceId" IS NULL OR "sourceId" = '') AND "offcloudId" IS NOT NULL` ); await prisma.$executeRawUnsafe( `CREATE INDEX IF NOT EXISTS "Video_source_idx" ON "Video"("source")` ); await prisma.$executeRawUnsafe( `CREATE INDEX IF NOT EXISTS "Video_source_sourceId_idx" ON "Video"("source", "sourceId")` ); console.log("[schema] Multi-source schema is up to date"); } main() .catch((err) => { console.error("[schema] Migration failed:", err); process.exitCode = 1; }) .finally(async () => { await prisma.$disconnect(); });