File size: 3,801 Bytes
5c0175e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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)`);
}