Download scripts/ensure-schema.mjs from Zero124/VR-MEDIA: direct link, hf CLI and curl.
- Browser
- Download file 3.38 kB
-
https://huggingface.co/spaces/Zero124/VR-MEDIA/resolve/main/scripts/ensure-schema.mjs
- Command line
-
hf download hf://spaces/Zero124/VR-MEDIA/scripts/ensure-schema.mjs
-
curl -L -o ensure-schema.mjs https://huggingface.co/spaces/Zero124/VR-MEDIA/resolve/main/scripts/ensure-schema.mjs
3.38 kB
| // 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(); | |
| }); | |