diff --git a/packages/db/src/test-embedded-postgres.test.ts b/packages/db/src/test-embedded-postgres.test.ts index 84856638701..5a7ea81a1e7 100644 --- a/packages/db/src/test-embedded-postgres.test.ts +++ b/packages/db/src/test-embedded-postgres.test.ts @@ -412,6 +412,31 @@ describeWithPostgres("startEmbeddedPostgresTestDatabase template path", () => { } }); + it.each([ + { mode: "template", setting: undefined }, + { mode: "fresh", setting: "0" }, + ])("hides NOTICE messages from new clients on the $mode path", { timeout: EMBEDDED_POSTGRES_TEST_TIMEOUT_MS }, async ({ mode, setting }) => { + if (setting === undefined) delete process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE; + else process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE = setting; + const database = await startEmbeddedPostgresTestDatabase("paperclip-quiet-notices-"); + try { + expect(lastStartMode()).toBe(mode); + const notices: unknown[] = []; + const sql = postgres(database.connectionString, { max: 1, onnotice: (notice) => notices.push(notice) }); + try { + expect((await sql`show client_min_messages`)[0]?.client_min_messages).toBe("warning"); + await sql`create table quiet_parent (id int primary key)`; + await sql`create table quiet_child (parent_id int references quiet_parent (id))`; + await sql`truncate quiet_parent cascade`; + expect(notices).toEqual([]); + } finally { + await sql.end(); + } + } finally { + await database.cleanup(); + } + }); + describeOnPosix("cache root safety", () => { it("uses fresh clusters when the cache root is not a private directory of this user", { timeout: EMBEDDED_POSTGRES_TEST_TIMEOUT_MS }, async () => { delete process.env.PAPERCLIP_TEST_POSTGRES_TEMPLATE; diff --git a/packages/db/src/test-embedded-postgres.ts b/packages/db/src/test-embedded-postgres.ts index c2a4b4fa984..0e2488523c6 100644 --- a/packages/db/src/test-embedded-postgres.ts +++ b/packages/db/src/test-embedded-postgres.ts @@ -15,7 +15,13 @@ import { import { prepareEmbeddedPostgresNativeRuntime } from "./embedded-postgres-native.js"; import { loadWithoutEmbeddedPostgresExitHooks } from "./embedded-postgres-lifecycle.js"; import { ownTestPostgres, TestPostgresShutdownError, type TestPostgresInstance } from "./test-postgres-owner.js"; -import { checkTestPostgresAdmin, startTestPostgresDatabase, testPostgresAdminUrl } from "./test-postgres-admin.js"; +import postgres from "postgres"; +import { + checkTestPostgresAdmin, + QUIET_TEST_DATABASE_SQL, + startTestPostgresDatabase, + testPostgresAdminUrl, +} from "./test-postgres-admin.js"; // Time budget (ms) for a vitest test in the embedded-Postgres cost class: a // test that starts an embedded Postgres cluster and runs migrations. Measured @@ -156,6 +162,12 @@ async function createEmbeddedPostgresTestInstanceAt(dataDir: string) { return { dataDir, port, instance, getRecentLogs: () => logBuffer.getRecentLogs() }; } +async function quietTestDatabase(adminUrl: string, databaseName: string): Promise { + const sql = postgres(adminUrl, { max: 1, onnotice() {} }); + try { await sql.unsafe(QUIET_TEST_DATABASE_SQL(databaseName)); } + finally { await sql.end({ timeout: 1 }); } +} + function adminConnectionStringFor(port: number): string { return `postgres://${EMBEDDED_POSTGRES_USER}:${EMBEDDED_POSTGRES_PASSWORD}@127.0.0.1:${port}/postgres`; } @@ -667,6 +679,7 @@ export async function startEmbeddedPostgresTestDatabase( // Both calls are no-ops on a template copy and keep the returned database // identical to the fresh path even if the template were ever stale. await ensurePostgresDatabase(adminConnectionStringFor(port), "paperclip"); + await quietTestDatabase(adminConnectionStringFor(port), "paperclip"); const connectionString = databaseConnectionStringFor(port); await applyPendingMigrations(connectionString); diff --git a/packages/db/src/test-postgres-admin.test.ts b/packages/db/src/test-postgres-admin.test.ts index 7498a4a467c..2b7b887a77a 100644 --- a/packages/db/src/test-postgres-admin.test.ts +++ b/packages/db/src/test-postgres-admin.test.ts @@ -1,4 +1,5 @@ import { randomUUID } from "node:crypto"; +import postgres from "postgres"; import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest"; import { createDb } from "./client.js"; import { @@ -83,4 +84,18 @@ describe.skipIf(!support.supported)("shared PostgreSQL fixtures", () => { } }, 90_000); + it("hides NOTICE messages from new clients of a disposable database", async () => { + __setEmbeddedPostgresCtorProviderForTests(async () => { throw new Error("Must not start another PostgreSQL server"); }); + const database = await startEmbeddedPostgresTestDatabase("ignored-"); + databases.push(database); + const notices: unknown[] = []; + const sql = postgres(database.connectionString, { max: 1, onnotice: (notice) => notices.push(notice) }); + try { + expect((await sql`SHOW client_min_messages`)[0]?.client_min_messages).toBe("warning"); + await sql`CREATE TABLE quiet_parent (id integer PRIMARY KEY)`; + await sql`CREATE TABLE quiet_child (parent_id integer REFERENCES quiet_parent (id))`; + await sql`TRUNCATE quiet_parent CASCADE`; + expect(notices).toEqual([]); + } finally { await sql.end(); } + }, 90_000); }); diff --git a/packages/db/src/test-postgres-admin.ts b/packages/db/src/test-postgres-admin.ts index a76848fcc05..95e91a447ab 100644 --- a/packages/db/src/test-postgres-admin.ts +++ b/packages/db/src/test-postgres-admin.ts @@ -2,6 +2,9 @@ import { randomUUID } from "node:crypto"; import postgres from "postgres"; import { applyPendingMigrations, closeRegisteredClients } from "./client.js"; +export const QUIET_TEST_DATABASE_SQL = (databaseName: string) => + `ALTER DATABASE "${databaseName}" SET client_min_messages = warning`; + export function testPostgresAdminUrl(): string | undefined { const value = process.env.PAPERCLIP_TEST_POSTGRES_ADMIN_URL?.trim(); if (!value) return undefined; @@ -41,6 +44,7 @@ export async function startTestPostgresDatabase(adminUrl: string): Promise<{ })(); try { await admin.unsafe(`CREATE DATABASE "${name}"${template ? ` TEMPLATE "${template}"` : ""}`); + await admin.unsafe(QUIET_TEST_DATABASE_SQL(name)); await applyPendingMigrations(connectionString); return { connectionString, cleanup }; } catch (error) {