Roo-Code/apps/web/vitest.setup.server.ts
2025-06-20 16:17:35 -07:00

35 lines
918 B
TypeScript

import postgres from 'postgres';
import { drizzle } from 'drizzle-orm/postgres-js';
import { sql } from 'drizzle-orm';
let pgClient: ReturnType<typeof postgres> | undefined = undefined;
async function resetTestDatabase() {
pgClient = postgres('postgres://postgres:password@localhost:5432/test', {
prepare: false,
onnotice: () => {}, // Suppress NOTICE logs.
});
const db = drizzle({ client: pgClient });
const tables = await db.execute<{ table_name: string }>(sql`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_type = 'BASE TABLE';
`);
const tableNames = tables.map((t) => t.table_name);
for (const tableName of tableNames) {
await db.execute(sql`TRUNCATE TABLE "${sql.raw(tableName)}" CASCADE;`);
}
}
export default async function () {
await resetTestDatabase();
return async () => {
await pgClient?.end();
};
}