File
Blob: test/util/d1Setup.ts
| 1 | import { applyD1Migrations } from "cloudflare:test"; |
| 2 | |
| 3 | import { readAppD1Migrations } from "./d1Migrations"; |
| 4 | |
| 5 | // Repo-serving route tests require D1 rows because the resolver returns null |
| 6 | // when D1 is empty. `applyD1Migrations` is itself idempotent on the journal, |
| 7 | // but the workerd cost is non-trivial; cache the "applied" state at the |
| 8 | // worker pool level so each test file only pays once. |
| 9 | let applied = false; |
| 10 | |
| 11 | export async function ensureD1Migrations(env: Env): Promise<void> { |
| 12 | if (applied) return; |
| 13 | await applyD1Migrations(env.DB, readAppD1Migrations()); |
| 14 | applied = true; |
| 15 | } |
| 16 | |
| 17 | // Test-only escape hatch when an isolated test wants to re-apply migrations |
| 18 | // (e.g. after dropping tables manually). Production code never calls this. |
| 19 | export function __resetD1MigrationCache(): void { |
| 20 | applied = false; |
| 21 | } |