deadhead-space-freight-tycoon/apps/server/test/migrations.test.ts

59 lines
1.7 KiB
TypeScript

import pg from "pg";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { runMigrations } from "../src/db/migrate";
import { testConfig } from "./helpers";
const url = testConfig().databaseUrl;
const client = new pg.Client({ connectionString: url });
const tables = async () =>
(
await client.query<{ table_name: string }>(
"select table_name from information_schema.tables where table_schema = 'public' order by table_name",
)
).rows.map((r) => r.table_name);
const appliedMigrations = async () =>
Number(
(await client.query<{ n: string }>("select count(*) as n from drizzle.__drizzle_migrations"))
.rows[0]!.n,
);
describe("database migrations", () => {
beforeAll(async () => {
await client.connect();
// Start from an empty database.
await client.query("drop schema if exists drizzle cascade");
await client.query("drop schema public cascade");
await client.query("create schema public");
});
afterAll(() => client.end());
it("creates every table on an empty database", async () => {
await runMigrations(url);
expect(await tables()).toEqual([
"accepted_contracts",
"account",
"bays",
"claimed_goals",
"companies",
"contract_boards",
"discovered_locations",
"dispatches",
"pilots",
"players",
"session",
"ship_upgrades",
"ships",
"user",
"verification",
]);
});
it("is a no-op when run a second time", async () => {
const before = { tables: await tables(), migrations: await appliedMigrations() };
await runMigrations(url);
expect({ tables: await tables(), migrations: await appliedMigrations() }).toEqual(before);
});
});