28 lines
1 KiB
TypeScript
28 lines
1 KiB
TypeScript
import "dotenv/config";
|
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
import { migrate } from "drizzle-orm/node-postgres/migrator";
|
|
import { Pool } from "pg";
|
|
|
|
/** Recreates the test schema from migrations before every vitest run. */
|
|
export default async function globalSetup() {
|
|
const url =
|
|
process.env.TEST_DATABASE_URL || "postgresql://blog:blog@localhost:5434/blog_test";
|
|
const pool = new Pool({ connectionString: url, max: 1 });
|
|
try {
|
|
await pool.query("select 1");
|
|
} catch (error) {
|
|
await pool.end();
|
|
throw new Error(
|
|
`Could not reach the test database at ${url}.\n` +
|
|
`Start it with: docker compose up -d\n(${String(error)})`,
|
|
);
|
|
}
|
|
// The drizzle schema holds the migration journal — drop it too, or the
|
|
// migrator will consider everything applied against the empty schema.
|
|
await pool.query(
|
|
"DROP SCHEMA public CASCADE; CREATE SCHEMA public; DROP SCHEMA IF EXISTS drizzle CASCADE;",
|
|
);
|
|
await migrate(drizzle(pool), { migrationsFolder: "./drizzle" });
|
|
await pool.end();
|
|
}
|