diff --git a/apps/api/src/archive/service.test.ts b/apps/api/src/archive/service.test.ts new file mode 100644 index 0000000..fb784da --- /dev/null +++ b/apps/api/src/archive/service.test.ts @@ -0,0 +1,50 @@ +import { describe, expect, it } from "vitest"; +import { ArchiveService } from "./service.js"; + +describe("dropIfEmpty retries a deadlock (BIT-32041)", () => { + it("retries once on P2034 and succeeds", async () => { + let calls = 0; + const db = { + event: { + deleteMany: async () => { + calls++; + if (calls === 1) throw Object.assign(new Error("Transaction failed due to a write conflict or a deadlock. Please retry your transaction"), { code: "P2034" }); + return { count: 1 }; + }, + }, + }; + const service = new ArchiveService({ db, log: { info() {}, debug() {}, error() {}, warn() {} } } as never); + await (service as unknown as { dropIfEmpty: (id: string) => Promise }).dropIfEmpty("event-1"); + expect(calls).toBe(2); + }); + + it("gives up and rethrows after repeated deadlocks", async () => { + let calls = 0; + const db = { + event: { + deleteMany: async () => { + calls++; + throw Object.assign(new Error("deadlock"), { code: "P2034" }); + }, + }, + }; + const service = new ArchiveService({ db, log: { info() {}, debug() {}, error() {}, warn() {} } } as never); + await expect((service as unknown as { dropIfEmpty: (id: string) => Promise }).dropIfEmpty("event-1")).rejects.toThrow("deadlock"); + expect(calls).toBe(3); + }); + + it("does not retry other errors", async () => { + let calls = 0; + const db = { + event: { + deleteMany: async () => { + calls++; + throw Object.assign(new Error("boom"), { code: "P2025" }); + }, + }, + }; + const service = new ArchiveService({ db, log: { info() {}, debug() {}, error() {}, warn() {} } } as never); + await expect((service as unknown as { dropIfEmpty: (id: string) => Promise }).dropIfEmpty("event-1")).rejects.toThrow("boom"); + expect(calls).toBe(1); + }); +}); diff --git a/apps/api/src/archive/service.ts b/apps/api/src/archive/service.ts index dbc9cd6..bfe7857 100644 --- a/apps/api/src/archive/service.ts +++ b/apps/api/src/archive/service.ts @@ -998,11 +998,19 @@ export class ArchiveService { * then fail with a foreign key error moving its photos in here — the "empty" check now lives in the delete's own * `WHERE`, so Postgres re-reads it against whatever just committed (BIT-31531). */ - private async dropIfEmpty(eventId: string): Promise { + private async dropIfEmpty(eventId: string, attempt = 0): Promise { const { db } = this.deps; - await db.event.deleteMany({ - where: { id: eventId, archivedAt: { not: null }, photos: { none: {} }, cloudFolders: { none: {} }, galleries: { none: {} }, deliveries: { none: {} } }, - }); + try { + await db.event.deleteMany({ + where: { id: eventId, archivedAt: { not: null }, photos: { none: {} }, cloudFolders: { none: {} }, galleries: { none: {} }, deliveries: { none: {} } }, + }); + } catch (err) { + // Two folders settling on the same old event at once can deadlock Postgres (BIT-32041): the loser is told to + // retry, and immediately doing so (the delete is conditional on the where, so it is safe to repeat) finishes + // the cleanup instead of leaving an orphaned empty event behind. + if ((err as { code?: string }).code === "P2034" && attempt < 2) return this.dropIfEmpty(eventId, attempt + 1); + throw err; + } } // --- indexing --------------------------------------------------------------------------------------------------