| 1 |
import { expect, test } from "bun:test"; |
| 2 |
import { mkdtempSync } from "node:fs"; |
| 3 |
import { tmpdir } from "node:os"; |
| 4 |
import { join } from "node:path"; |
| 5 |
import { check, migrate, migrations, openDb } from "./db.ts"; |
| 6 |
|
| 7 |
test("migrations apply once, in order, and the file checks out", () => { |
| 8 |
const dir = mkdtempSync(join(tmpdir(), "app-db-")); |
| 9 |
const db = openDb(dir); |
| 10 |
expect(check(db).version).toBe(migrations.length); |
| 11 |
|
| 12 |
expect(migrate(db)).toBe(migrations.length); |
| 13 |
expect(() => openDb(dir)).not.toThrow(); |
| 14 |
expect(check(db)).toEqual({ version: migrations.length, integrity: "ok" }); |
| 15 |
expect( |
| 16 |
db.query<{ journal_mode: string }, []>("PRAGMA journal_mode").get() |
| 17 |
?.journal_mode, |
| 18 |
).toBe("wal"); |
| 19 |
expect( |
| 20 |
db.query<{ foreign_keys: number }, []>("PRAGMA foreign_keys").get() |
| 21 |
?.foreign_keys, |
| 22 |
).toBe(1); |
| 23 |
}); |