|
| 1 | +import { execSync } from "child_process"; |
| 2 | + |
| 3 | +const DIRECT_DB = process.env.DIRECT_DATABASE_URL || process.env.DATABASE_URL; |
| 4 | + |
| 5 | +function runPrisma(cmd: string) { |
| 6 | + const env = { ...process.env } as NodeJS.ProcessEnv; |
| 7 | + if (DIRECT_DB) { |
| 8 | + env.DIRECT_DATABASE_URL = DIRECT_DB; |
| 9 | + env.DATABASE_URL = DIRECT_DB; |
| 10 | + } |
| 11 | + return execSync(`npx prisma ${cmd}`, { env, encoding: "utf8" }).toString(); |
| 12 | +} |
| 13 | + |
| 14 | +describe("Migrations idempotency", () => { |
| 15 | + if (!DIRECT_DB) { |
| 16 | + test.skip("DIRECT_DATABASE_URL not set - skipping migration idempotency test", () => {}); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + test( |
| 21 | + "apply -> reset -> apply yields stable migration checksums", |
| 22 | + () => { |
| 23 | + jest.setTimeout(5 * 60 * 1000); |
| 24 | + |
| 25 | + // Apply all migrations (up) |
| 26 | + runPrisma("migrate deploy"); |
| 27 | + |
| 28 | + // Capture migration checksums from the migrations table |
| 29 | + const first = runPrisma( |
| 30 | + `db execute --command "SELECT migration_name, checksum FROM _prisma_migrations ORDER BY migration_name;" --url \"${DIRECT_DB}\"` |
| 31 | + ).trim(); |
| 32 | + |
| 33 | + // Reset will drop the DB and reapply migrations (down then up) |
| 34 | + runPrisma("migrate reset --force --skip-seed"); |
| 35 | + |
| 36 | + // Re-capture checksums after running reset |
| 37 | + const second = runPrisma( |
| 38 | + `db execute --command "SELECT migration_name, checksum FROM _prisma_migrations ORDER BY migration_name;" --url \"${DIRECT_DB}\"` |
| 39 | + ).trim(); |
| 40 | + |
| 41 | + expect(first).toBe(second); |
| 42 | + }, |
| 43 | + ); |
| 44 | +}); |
0 commit comments