Files
PaperClipAI/packages/db/src/test-postgres-admin.ts
T
devinfoleyandClaude Opus 5.5 e8afeb4ae7 Revert "perf(ci): stop printing Postgres TRUNCATE notices in server test shards (#21903)"
This reverts commit c938bdfee4.

The change made CI slower, so it is being undone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 22:51:51 +00:00

51 lines
1.9 KiB
TypeScript

import { randomUUID } from "node:crypto";
import postgres from "postgres";
import { applyPendingMigrations, closeRegisteredClients } from "./client.js";
export function testPostgresAdminUrl(): string | undefined {
const value = process.env.PAPERCLIP_TEST_POSTGRES_ADMIN_URL?.trim();
if (!value) return undefined;
const url = new URL(value);
if (url.protocol !== "postgres:" && url.protocol !== "postgresql:") {
throw new Error("PAPERCLIP_TEST_POSTGRES_ADMIN_URL must be a PostgreSQL URL");
}
return value;
}
export async function checkTestPostgresAdmin(url: string): Promise<void> {
const sql = postgres(url, { max: 1, connect_timeout: 5, onnotice() {} });
try { await sql`SELECT 1`; }
finally { await sql.end({ timeout: 1 }); }
}
export async function startTestPostgresDatabase(adminUrl: string): Promise<{
connectionString: string;
cleanup(): Promise<void>;
}> {
const name = `paperclip_test_${randomUUID().replaceAll("-", "")}`;
const setting = process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE;
const template = setting === "0" ? undefined : setting;
if (template && !/^paperclip_test_template_[a-f0-9]+$/.test(template)) {
throw new Error("Invalid disposable test template name");
}
const admin = postgres(adminUrl, { max: 1, connect_timeout: 5, onnotice() {} });
const url = new URL(adminUrl);
url.pathname = `/${name}`;
const connectionString = url.toString();
let cleaning: Promise<void> | null = null;
const cleanup = () => cleaning ??= (async () => {
try {
await closeRegisteredClients(connectionString, { scope: "database" });
await admin.unsafe(`DROP DATABASE IF EXISTS "${name}" WITH (FORCE)`);
} finally { await admin.end({ timeout: 1 }); }
})();
try {
await admin.unsafe(`CREATE DATABASE "${name}"${template ? ` TEMPLATE "${template}"` : ""}`);
await applyPendingMigrations(connectionString);
return { connectionString, cleanup };
} catch (error) {
await cleanup();
throw error;
}
}