feat(learner): package scaffold, auth tables migration, migrations runner
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0b117159cd
commit
5a215744e5
5 files changed
+116
No files matched your search
@@ -0,0 +1,35 @@
|
||||
import { test } from "bun:test";
|
||||
import { connect, migrate, type Db } from "@stc/db";
|
||||
import { runMigrations } from "../src/server/migrate";
|
||||
|
||||
const PG = process.env.STC_TEST_PG_URL;
|
||||
|
||||
/** Fresh migrated in-memory SQLite (and a fresh Postgres schema when STC_TEST_PG_URL is set), with learner migrations applied. */
|
||||
export function forEachDb(name: string, fn: (db: Db) => Promise<void>, timeout?: number): void {
|
||||
test(`${name} [sqlite]`, async () => {
|
||||
const db = await connect("sqlite://:memory:");
|
||||
try { await migrate(db); await runMigrations(db); await fn(db); } finally { await db.close(); }
|
||||
}, timeout);
|
||||
if (!PG) return;
|
||||
test(`${name} [postgres]`, async () => {
|
||||
const { db, drop } = await pgSchemaDb();
|
||||
try { await migrate(db); await runMigrations(db); await fn(db); } finally { await drop(); }
|
||||
}, timeout);
|
||||
}
|
||||
|
||||
export async function pgSchemaDb(): Promise<{ db: Db; drop(): Promise<void> }> {
|
||||
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());
|
||||
return {
|
||||
db,
|
||||
async drop() {
|
||||
await db.close();
|
||||
await admin.sql.unsafe(`DROP SCHEMA ${schema} CASCADE`);
|
||||
await admin.close();
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user