lilith-platform.live/codebase/@features/api/scripts/drop-test-dbs.ts

31 lines
1.2 KiB
TypeScript

#!/usr/bin/env bun
/**
* Drop ALL `quinn_test_*` throwaway databases. Standalone admin connection —
* deliberately does NOT import the test harness, whose module load would
* create yet another test DB as a side effect. The harness now also sweeps
* orphans at startup and drops its own DB at exit (src/__tests__/test-db.ts);
* this script is the manual full-sweep for backlogs.
*/
import postgres from 'postgres';
const adminUrl =
process.env['QUINN_TEST_ADMIN_DB_URL'] ?? 'postgresql://quinn:devpassword@black.lan:25435/postgres';
const admin = postgres(adminUrl, { max: 1, connect_timeout: 10, onnotice: () => { /* suppress */ } });
const rows = await admin<Array<{ datname: string }>>`
SELECT datname FROM pg_database WHERE datname LIKE 'quinn_test\\_%' ORDER BY datname
`;
let dropped = 0;
for (const { datname } of rows) {
try {
await admin.unsafe(`DROP DATABASE IF EXISTS "${datname}" WITH (FORCE)`);
dropped += 1;
process.stdout.write(`dropped ${datname} (${dropped}/${rows.length})\n`);
} catch (err) {
process.stderr.write(`skip ${datname}: ${String(err)}\n`);
}
}
await admin.end();
process.stdout.write(`dropped ${dropped}/${rows.length} throwaway test db(s)\n`);