import { expect } from "bun:test"; import { forEachDb } from "./helpers"; import { migrate } from "../src/migrate"; forEachDb("applies 0001 once", async (db) => { // helpers already migrated once; a second run applies nothing expect(await migrate(db)).toEqual([]); const rows = await db.sql`SELECT name FROM schema_migrations`; expect(rows.map((r: { name: string }) => r.name)).toEqual(["0001_init.sql"]); }); forEachDb("all tables exist", async (db) => { const s = db.sql; const t = "2026-01-01T00:00:00.000Z"; const one = async (table: string, rows: Promise) => { await rows; expect((await s.unsafe(`SELECT COUNT(*) AS n FROM ${table}`))[0].n.toString()).toBe("1"); }; await one("user_roles", s`INSERT INTO user_roles VALUES ('u','owner')`); await one("groups", s`INSERT INTO groups VALUES ('g','G')`); await one("group_members", s`INSERT INTO group_members VALUES ('g','u')`); await one("course_authors", s`INSERT INTO course_authors VALUES ('c','u')`); await one("course_acl", s`INSERT INTO course_acl VALUES ('c','user','u')`); await one("progress", s`INSERT INTO progress VALUES ('u','c','l','done',1.5,${t})`); await one("notes", s`INSERT INTO notes VALUES ('n','u','c','l','none',NULL,NULL,'b',${t},${t})`); await one("exercise_attempts", s`INSERT INTO exercise_attempts VALUES ('u','c','l','py','x',NULL,1,${t})`); await one("user_api_keys", s`INSERT INTO user_api_keys VALUES ('u','p','ct',${t})`); await one("ai_settings", s`INSERT INTO ai_settings VALUES ('server','',0,'{}',${t})`); await one("ai_conversations", s`INSERT INTO ai_conversations VALUES ('a','u','c',NULL,${t})`); await one("ai_messages", s`INSERT INTO ai_messages VALUES ('m','a','user','hi',${t})`); await one("library_settings", s`INSERT INTO library_settings VALUES ('k','v')`); await one("collections", s`INSERT INTO collections VALUES ('co','T',0)`); await one("collection_courses", s`INSERT INTO collection_courses VALUES ('co','c',0)`); await one("jobs", s`INSERT INTO jobs VALUES ('j','k','queued',0,NULL,'{}',NULL,NULL,${t},${t})`); });