import { test } from "bun:test"; import { connect, type Db } from "../src/connect"; import { migrate } from "../src/migrate"; const PG = process.env.STC_TEST_PG_URL; /** Runs `fn` against a fresh migrated in-memory SQLite, and a fresh Postgres schema when STC_TEST_PG_URL is set. */ export function forEachDb(name: string, fn: (db: Db) => Promise): void { test(`${name} [sqlite]`, async () => { const db = await connect("sqlite://:memory:"); try { await migrate(db); await fn(db); } finally { await db.close(); } }); if (!PG) return; test(`${name} [postgres]`, async () => { const schema = `t_${Bun.randomUUIDv7().replaceAll("-", "")}`; const admin = await connect(PG); await admin.sql.unsafe(`CREATE SCHEMA ${schema}`); const u = new URL(PG); u.searchParams.set("options", `-c search_path=${schema}`); const db = await connect(u.toString()); try { await migrate(db); await fn(db); } finally { await db.close(); await admin.sql.unsafe(`DROP SCHEMA ${schema} CASCADE`); await admin.close(); } }); }