File
Blob: test/util/d1Migrations.ts
| 1 | import { type D1Migration } from "cloudflare:test"; |
| 2 | |
| 3 | import journal from "../../drizzle/d1/meta/_journal.json"; |
| 4 | |
| 5 | // Drizzle's `meta/_journal.json` lists migrations in tag order; each `tag` |
| 6 | // matches a sibling `<tag>.sql` file. We load every SQL file at vitest |
| 7 | // import time (`eager: true`) so workerd does not need filesystem access. |
| 8 | const sqlFiles = import.meta.glob("../../drizzle/d1/*.sql", { |
| 9 | query: "?raw", |
| 10 | import: "default", |
| 11 | eager: true, |
| 12 | }) as Record<string, string>; |
| 13 | |
| 14 | interface JournalEntry { |
| 15 | tag: string; |
| 16 | } |
| 17 | |
| 18 | interface MigrationJournal { |
| 19 | entries: JournalEntry[]; |
| 20 | } |
| 21 | |
| 22 | function splitStatements(sql: string): string[] { |
| 23 | return sql |
| 24 | .split("--> statement-breakpoint") |
| 25 | .map((statement) => statement.trim()) |
| 26 | .filter((statement) => statement.length > 0); |
| 27 | } |
| 28 | |
| 29 | export function readAppD1Migrations(): D1Migration[] { |
| 30 | const typedJournal = journal as MigrationJournal; |
| 31 | return typedJournal.entries.map((entry) => { |
| 32 | const fileName = `${entry.tag}.sql`; |
| 33 | const sql = sqlFiles[`../../drizzle/d1/${fileName}`]; |
| 34 | if (!sql) { |
| 35 | throw new Error(`Missing D1 migration file for ${fileName}.`); |
| 36 | } |
| 37 | return { name: fileName, queries: splitStatements(sql) }; |
| 38 | }); |
| 39 | } |