| 62 | } |
| 63 | |
| 64 | async function check() { |
| 65 | const temporary = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-core-migration-check-")) |
| 66 | const incremental = path.join(temporary, "incremental") |
| 67 | const full = path.join(temporary, "full") |
| 68 | try { |
| 69 | await fs.mkdir(incremental) |
| 70 | await fs.mkdir(path.join(incremental, "baseline")) |
| 71 | await fs.copyFile(snapshot, path.join(incremental, "baseline/snapshot.json")) |
| 72 | await drizzle(temporary, incremental) |
| 73 | if ((await generatedMigrations(incremental)).length > 0) { |
| 74 | throw new Error( |
| 75 | "Core schema has ungenerated database migrations. Run `bun script/migration.ts` from packages/core.", |
| 76 | ) |
| 77 | } |
| 78 | |
| 79 | await fs.mkdir(full) |
| 80 | await drizzle(temporary, full, "schema") |
| 81 | if ((await Bun.file(schema).text()) !== (await formatTypescript(renderSchema(await generatedSql(full))))) { |
| 82 | throw new Error("Current database schema is stale. Run `bun script/migration.ts` from packages/core.") |
| 83 | } |
| 84 | |
| 85 | const migrations = await typescriptMigrations() |
| 86 | if ((await Bun.file(registry).text()) !== (await formatTypescript(renderRegistry(migrations)))) { |
| 87 | throw new Error("Database migration registry is stale. Run `bun script/migration.ts` from packages/core.") |
| 88 | } |
| 89 | } finally { |
| 90 | await fs.rm(temporary, { recursive: true, force: true }) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | async function drizzle(temporary: string, output: string, name?: string) { |
| 95 | const config = path.join(temporary, `${path.basename(output)}.config.ts`) |