[PR #7345] [MERGED] fix: delay database hooks execution until after transaction commits #24148

Closed
opened 2026-04-15 22:12:03 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7345
Author: @himself65
Created: 1/14/2026
Status: Merged
Merged: 1/20/2026
Merged by: @himself65

Base: canaryHead: claude/issue-7260-20260113-2226


📝 Commits (2)

  • ef25104 fix: delay database hooks execution until after transaction commits
  • 92c7aa9 feat: update code

📊 Changes

4 files changed (+104 additions, -27 deletions)

View changed files

📝 packages/better-auth/src/db/with-hooks.ts (+22 -10)
📝 packages/better-auth/src/plugins/last-login-method/index.ts (+9 -8)
📝 packages/core/src/context/index.ts (+1 -0)
📝 packages/core/src/context/transaction.ts (+72 -9)

📄 Description

Summary

This PR fixes issue #7260 where databaseHooks.user.create.after causes foreign key constraint violations during social login with database transactions enabled.

Problem

The issue occurred because after hooks executed INSIDE the transaction before commit, so external Prisma clients couldn't see uncommitted rows. This caused FK constraint violations when hooks tried to create related records that reference the new user.

Solution

  • Modified runWithTransaction to queue after hooks and execute them post-commit
  • Added queueAfterTransactionHook function to queue hooks in transaction context
  • Updated all after hooks (create, update, delete) to use the queue mechanism
  • Hooks execute immediately if not in a transaction (backward compatible)
  • If transaction fails, queued hooks never execute

Benefits

  • External DB clients can now see committed rows in after hooks
  • Maintains transactional integrity - hooks only run on successful commits
  • Prevents FK constraint violations
  • Fully backward compatible

Testing

Existing tests should continue to pass. The change is backward compatible because:

  • Hooks still execute after DB writes complete
  • Hooks that use the same adapter context will work as before
  • Hooks that use external clients will now work correctly

Fixes #7260


Generated with Claude Code


Summary by cubic

Run database after hooks only after the transaction commits to prevent FK constraint errors during social login with transactions. Fixes #7260.

  • Bug Fixes
    • Queue after hooks in transaction/adapter context and execute them after the operation (post-commit for transactions).
    • Added queueAfterTransactionHook; updated create/update/delete hooks to use it.
    • Improved last-login method detection for OAuth callbacks and email sign-in/sign-up.

Written for commit 92c7aa999c. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7345 **Author:** [@himself65](https://github.com/himself65) **Created:** 1/14/2026 **Status:** ✅ Merged **Merged:** 1/20/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `claude/issue-7260-20260113-2226` --- ### 📝 Commits (2) - [`ef25104`](https://github.com/better-auth/better-auth/commit/ef25104f7ac1cfa24b114538271b9904f1878c80) fix: delay database hooks execution until after transaction commits - [`92c7aa9`](https://github.com/better-auth/better-auth/commit/92c7aa999c60b8dbb22e54ebdaaf8ecd5595a37e) feat: update code ### 📊 Changes **4 files changed** (+104 additions, -27 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/with-hooks.ts` (+22 -10) 📝 `packages/better-auth/src/plugins/last-login-method/index.ts` (+9 -8) 📝 `packages/core/src/context/index.ts` (+1 -0) 📝 `packages/core/src/context/transaction.ts` (+72 -9) </details> ### 📄 Description ### Summary This PR fixes issue #7260 where `databaseHooks.user.create.after` causes foreign key constraint violations during social login with database transactions enabled. ### Problem The issue occurred because after hooks executed INSIDE the transaction before commit, so external Prisma clients couldn't see uncommitted rows. This caused FK constraint violations when hooks tried to create related records that reference the new user. ### Solution - Modified `runWithTransaction` to queue after hooks and execute them post-commit - Added `queueAfterTransactionHook` function to queue hooks in transaction context - Updated all after hooks (create, update, delete) to use the queue mechanism - Hooks execute immediately if not in a transaction (backward compatible) - If transaction fails, queued hooks never execute ### Benefits - External DB clients can now see committed rows in after hooks - Maintains transactional integrity - hooks only run on successful commits - Prevents FK constraint violations - Fully backward compatible ### Testing Existing tests should continue to pass. The change is backward compatible because: - Hooks still execute after DB writes complete - Hooks that use the same adapter context will work as before - Hooks that use external clients will now work correctly Fixes #7260 --- Generated with [Claude Code](https://claude.ai/code) <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Run database after hooks only after the transaction commits to prevent FK constraint errors during social login with transactions. Fixes #7260. - **Bug Fixes** - Queue after hooks in transaction/adapter context and execute them after the operation (post-commit for transactions). - Added queueAfterTransactionHook; updated create/update/delete hooks to use it. - Improved last-login method detection for OAuth callbacks and email sign-in/sign-up. <sup>Written for commit 92c7aa999c60b8dbb22e54ebdaaf8ecd5595a37e. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 22:12:03 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#24148