mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-29 18:36:34 -05:00
fix(db): skip 'adapter.delete' in deleteWithHooks when entity not found
This commit is contained in:
@@ -258,6 +258,33 @@ describe("internal adapter test", async () => {
|
||||
expect(hookVerificationDeleteAfter).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("should not call adapter.delete for missing verification record (prevents Prisma P2025)", async () => {
|
||||
const verification = await internalAdapter.createVerificationValue({
|
||||
identifier: "missing-entity-test",
|
||||
value: "test-value",
|
||||
expiresAt: new Date(Date.now() + 60000),
|
||||
});
|
||||
|
||||
// Remove the DB record so the entity no longer exists
|
||||
await authContext.adapter.deleteMany({
|
||||
model: "verification",
|
||||
where: [{ field: "identifier", value: verification.identifier }],
|
||||
});
|
||||
|
||||
const deleteSpy = vi.spyOn(authContext.adapter, "delete");
|
||||
|
||||
await internalAdapter.deleteVerificationByIdentifier("missing-entity-test");
|
||||
|
||||
// adapter.delete should NOT have been called because
|
||||
// deleteWithHooks skips deletion when the entity is not found
|
||||
const verificationDeleteCalls = deleteSpy.mock.calls.filter(
|
||||
(call) => call[0].model === "verification",
|
||||
);
|
||||
expect(verificationDeleteCalls.length).toBe(0);
|
||||
|
||||
deleteSpy.mockRestore();
|
||||
});
|
||||
|
||||
describe("verification token storage", () => {
|
||||
it("should hash identifier when storeIdentifier is 'hashed'", async () => {
|
||||
const hashedOpts = {
|
||||
|
||||
@@ -226,8 +226,10 @@ export function getWithHooks(
|
||||
? await customDeleteFn.fn(where)
|
||||
: null;
|
||||
|
||||
const shouldRunAdapterDelete =
|
||||
!customDeleteFn || customDeleteFn.executeMainFn;
|
||||
const deleted =
|
||||
!customDeleteFn || customDeleteFn.executeMainFn
|
||||
shouldRunAdapterDelete && entityToDelete
|
||||
? await (await getCurrentAdapter(adapter)).delete({
|
||||
model,
|
||||
where,
|
||||
|
||||
Reference in New Issue
Block a user