import { copyFileSync, readFileSync } from "node:fs"; import path from "node:path"; import { spawnSync } from "node:child_process"; import Database from "better-sqlite3"; import { afterEach, describe, expect, it } from "vitest"; import { applyCommittedMigrations, createDisposableDatabase, disposeDisposableDatabase, type DisposableDatabase, } from "./helpers/testDatabase"; const BEFORE_GROUP = "20260714010000_add_arcade"; const GROUP_MIGRATION = "20260807090000_add_material_groups"; const prismaCli = path.join(process.cwd(), "node_modules", "prisma", "build", "index.js"); const databases: DisposableDatabase[] = []; function createDatabase() { const database = createDisposableDatabase(); databases.push(database); return database; } function absoluteDatabaseUrl(database: DisposableDatabase) { return `file:${database.databasePath.replaceAll("\\", "/")}`; } function prisma(database: DisposableDatabase, ...args: string[]) { return spawnSync(process.execPath, [prismaCli, ...args], { cwd: process.cwd(), env: { ...process.env, DATABASE_URL: absoluteDatabaseUrl(database) }, encoding: "utf8", }); } afterEach(() => { for (const database of databases.splice(0)) disposeDisposableDatabase(database); }); describe.skipIf(process.platform === "win32")("native Prisma migration deployment", () => { it("deploys the committed chain to a fresh database with no schema drift", () => { const database = createDatabase(); const deploy = prisma(database, "migrate", "deploy"); expect(deploy.status, `${deploy.stdout}\n${deploy.stderr}`).toBe(0); const diff = prisma( database, "migrate", "diff", "--from-config-datasource", "--to-schema", "prisma/schema.prisma", "--exit-code" ); expect(diff.status, `${diff.stdout}\n${diff.stderr}`).toBe(0); expect(diff.stdout).toContain("No difference detected"); }, 60_000); it("preserves populated pre-group data while deploying later migrations", () => { const database = createDatabase(); applyCommittedMigrations(database, { through: BEFORE_GROUP }); const sqlite = new Database(database.databasePath); try { sqlite.pragma("foreign_keys = ON"); sqlite.exec(` INSERT INTO "Class" ("id", "slug", "name", "sortOrder") VALUES ('class-before', 'class-before', 'Before', 0); INSERT INTO "Deck" ("id", "classId", "name", "sortOrder") VALUES ('deck-before', 'class-before', 'Preserved Deck', 0); INSERT INTO "Flashcard" ("id", "deckId", "front", "back", "sortOrder") VALUES ('card-before', 'deck-before', 'Front', 'Back', 0); `); } finally { sqlite.close(); } const deploy = prisma(database, "migrate", "deploy"); expect(deploy.status, `${deploy.stdout}\n${deploy.stderr}`).toBe(0); const restored = new Database(database.databasePath, { readonly: true }); try { expect( restored .prepare(` SELECT d.name AS deckName, f.front AS front FROM "Deck" d JOIN "Flashcard" f ON f."deckId" = d.id WHERE d.id = 'deck-before' `) .get() ).toEqual({ deckName: "Preserved Deck", front: "Front" }); expect(restored.pragma("integrity_check", { simple: true })).toBe("ok"); expect(restored.pragma("foreign_key_check")).toEqual([]); } finally { restored.close(); } }, 60_000); it("adopts an exact schema-pushed group database only with a verified backup", () => { const database = createDatabase(); applyCommittedMigrations(database, { through: BEFORE_GROUP }); const sqlite = new Database(database.databasePath); try { sqlite.exec( readFileSync( path.join("prisma", "migrations", GROUP_MIGRATION, "migration.sql"), "utf8" ) ); sqlite.prepare( `INSERT INTO "Class" ("id", "slug", "name", "sortOrder") VALUES ('class-adopted', 'class-adopted', 'Adopted Class', 0)` ).run(); sqlite.prepare( `INSERT INTO "MaterialGroup" ("id", "classId", "type", "name", "sortOrder") VALUES ('group-adopted', 'class-adopted', 'FLASHCARD', 'Adopted', 0)` ).run(); } finally { sqlite.close(); } const backupPath = path.join(database.directory, "verified-backup.test.db"); copyFileSync(database.databasePath, backupPath); const preflight = spawnSync( process.execPath, ["scripts/migration-preflight.mjs", "--resolve", "--backup", backupPath], { cwd: process.cwd(), env: { ...process.env, DATABASE_URL: absoluteDatabaseUrl(database) }, encoding: "utf8", } ); expect(preflight.status, `${preflight.stdout}\n${preflight.stderr}`).toBe(0); expect(preflight.stdout).toContain('"classification": "ADOPT"'); const deploy = prisma(database, "migrate", "deploy"); expect(deploy.status, `${deploy.stdout}\n${deploy.stderr}`).toBe(0); const restored = new Database(database.databasePath, { readonly: true }); try { expect( restored.prepare( `SELECT name FROM "MaterialGroup" WHERE id = 'group-adopted'` ).get() ).toEqual({ name: "Adopted" }); } finally { restored.close(); } }, 60_000); it("refuses a partial group schema instead of repairing it", () => { const database = createDatabase(); applyCommittedMigrations(database, { through: BEFORE_GROUP }); const sqlite = new Database(database.databasePath); try { sqlite.exec('CREATE TABLE "MaterialGroup" ("id" TEXT NOT NULL PRIMARY KEY);'); } finally { sqlite.close(); } const preflight = spawnSync(process.execPath, ["scripts/migration-preflight.mjs"], { cwd: process.cwd(), env: { ...process.env, DATABASE_URL: absoluteDatabaseUrl(database) }, encoding: "utf8", }); expect(preflight.status).toBe(2); expect(preflight.stdout).toContain('"classification": "CONFLICT"'); const check = new Database(database.databasePath, { readonly: true }); try { const columns = check.pragma("table_info(MaterialGroup)") as Array<{ name: string; }>; expect(columns.map((column) => column.name)).toEqual(["id"]); } finally { check.close(); } }, 60_000); });