diff --git a/apps/api/src/lib/pg.test.ts b/apps/api/src/lib/pg.test.ts index 76c2c30..daff231 100644 --- a/apps/api/src/lib/pg.test.ts +++ b/apps/api/src/lib/pg.test.ts @@ -55,6 +55,18 @@ describe("isPrismaServerUnreachable", () => { expect(isPrismaServerUnreachable(new Error("Can't reach database server at pr-db:5432"))).toBe(true); }); + it("matches a Postgres graceful-shutdown error surfaced as 57P03 (BIT-32470)", () => { + const err = new PrismaLike( + "P2039", + "Invalid `prisma.person.findFirst()` invocation:\n\nDatabase error. Code: `57P03`. Message: `the database system is shutting down`", + ); + expect(isPrismaServerUnreachable(err)).toBe(true); + }); + + it("matches the 57P03 shutdown message without the Prisma code", () => { + expect(isPrismaServerUnreachable(new Error("Database error. Code: `57P03`. Message: `the database system is shutting down`"))).toBe(true); + }); + it("does not match a server-closed (P1017) error", () => { expect(isPrismaServerUnreachable(new PrismaLike("P1017", "Server has closed the connection."))).toBe(false); }); diff --git a/apps/api/src/lib/pg.ts b/apps/api/src/lib/pg.ts index d464f93..cc4adc3 100644 --- a/apps/api/src/lib/pg.ts +++ b/apps/api/src/lib/pg.ts @@ -22,15 +22,21 @@ export function isPrismaConnectionClosed(err: unknown): boolean { /** * The database server is unreachable — Prisma never opened a connection (the server is down, restarting, or a - * network blip dropped the route). Prisma surfaces this as `PrismaClientKnownRequestError` with code `P1001` - * ("Can't reach database server at :"). The pool reconnects on the next attempt once the server is - * back, so this is transient and must not page the error loop: the caller should stop the current batch and let - * the next scheduled run retry (BIT-32462). + * network blip dropped the route), or Postgres is mid graceful shutdown. Prisma surfaces the former as + * `PrismaClientKnownRequestError` with code `P1001` ("Can't reach database server at :"), and a + * graceful shutdown (SQLSTATE `57P03`, "the database system is shutting down") as a `Database error` message + * wrapping the raw SQLSTATE. The pool reconnects on the next attempt once the server is back, so this is transient + * and must not page the error loop: the caller should stop the current batch and let the next scheduled run retry + * (BIT-32462, BIT-32470). */ export function isPrismaServerUnreachable(err: unknown): boolean { if (!(err instanceof Error)) return false; const code = (err as { code?: unknown }).code; - return code === "P1001" || /Can't reach database server/i.test(err.message); + return ( + code === "P1001" || + /Can't reach database server/i.test(err.message) || + /57P03|the database system is shutting down/i.test(err.message) + ); } /** diff --git a/apps/api/src/sports/service.test.ts b/apps/api/src/sports/service.test.ts index a8ca765..89f1f8c 100644 --- a/apps/api/src/sports/service.test.ts +++ b/apps/api/src/sports/service.test.ts @@ -18,6 +18,12 @@ const makeLog = (): { info: (obj: unknown, msg: string) => void; warn: (obj: unk const p1001 = () => Object.assign(new Error("Invalid `prisma.sportsPlayer.findMany()` invocation:\n\nCan't reach database server at 127.0.0.1:5432"), { code: "P1001" }); +const p57p03 = () => + Object.assign( + new Error("Invalid `prisma.person.findFirst()` invocation:\n\nDatabase error. Code: `57P03`. Message: `the database system is shutting down`"), + { code: "P2039" }, + ); + const ligaTeam = (id: string, externalId: string) => ({ id, provider: "LIGA", @@ -100,3 +106,34 @@ describe("syncEventSquads downgrades a P1001 unreachable database (BIT-32463)", expect(log.calls.some((c) => c.level === "info" && /database unreachable/.test(c.msg))).toBe(false); }); }); + +describe("syncEventSquads downgrades a 57P03 database-shutting-down error (BIT-32470)", () => { + it("logs at info and returns zeros when linkSquadPerson hits 57P03 on person.findFirst", async () => { + const fpaTeam = { + id: "t-fpa", + provider: "FPA", + externalId: "fpa-1", + name: "Team FPA", + abbreviation: "FPA", + logoUrl: null, + }; + const db = { + event: { findUnique: async () => makeEvent({ homeTeam: fpaTeam, awayTeam: null }) }, + sportsPlayer: { + findFirst: async () => ({ season: "2026" }), + findMany: async () => [{ id: "pl-1", name: "Player 1", externalId: "lic-1", number: "1", photoUrl: null, season: "2026" }], + findUnique: async () => null, + }, + person: { + findUnique: async () => null, + findFirst: async () => { throw p57p03(); }, + }, + }; + const service = new SportsService(makeDeps(db)); + const out = await service.syncEventSquads("evt-1"); + expect(out).toEqual({ created: 0, updated: 0, headshots: 0 }); + const log = (service as unknown as { deps: { log: { calls: LogCall[] } } }).deps.log; + expect(log.calls.some((c) => c.level === "info" && /database unreachable/.test(c.msg))).toBe(true); + expect(log.calls.some((c) => c.level === "warn")).toBe(false); + }); +});