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})`); }); forEachDb("package migrations run after core, in package-name order, once", async (db) => { const { mkdtempSync, writeFileSync, mkdirSync } = await import("node:fs"); const { join } = await import("node:path"); const { tmpdir } = await import("node:os"); const root = mkdtempSync(join(tmpdir(), "stc-mig-")); const a = join(root, "a"), z = join(root, "z"); mkdirSync(a); mkdirSync(z); // z's table is referenced by a's second file only through ordering by package name: "alpha" runs before "zeta". writeFileSync(join(z, "0001_zeta.sql"), "CREATE TABLE zeta_t (id TEXT PRIMARY KEY);"); writeFileSync(join(a, "0001_alpha.sql"), "CREATE TABLE alpha_t (id TEXT PRIMARY KEY);"); writeFileSync(join(a, "0002_alpha.sql"), "INSERT INTO alpha_t VALUES ('x');"); expect(await migrate(db, { zeta: [z], alpha: [a] })).toEqual(["0001_alpha.sql", "0002_alpha.sql", "0001_zeta.sql"]); expect(await migrate(db, { zeta: [z], alpha: [a] })).toEqual([]); expect((await db.sql`SELECT id FROM alpha_t`).length).toBe(1); // the same file name in two packages is refused rather than silently skipped writeFileSync(join(z, "0001_alpha.sql"), "SELECT 1;"); await expect(migrate(db, { zeta: [z], alpha: [a] })).rejects.toThrow(/exists in both/); });