fix(lagoon): warn on after-commit work registered after lagoon:after_commit; correct the OnDatabase example and pivot read guidance (quick-261028-r16)
- flushStatementAfterCommit marks the statement; AfterCommit logs a warning when work is buffered on a flushed statement that no longer holds GORM's transaction. Log-only: the work still never runs, the write succeeds - subtests pin the late callback warning, a callback between commit and flush, and a reused chained statement staying silent - README OnDatabase example registers After(gorm:create).Before(commit), matching docs/database/transactions.md - RegisterJoinTable: pivot-ordered reads join the pivot table explicitly; a Preload cannot be ordered by a pivot column
This commit is contained in:
@@ -14,6 +14,9 @@ const AfterCommitCallback = "lagoon:after_commit"
|
||||
|
||||
const statementBufferKey = "lagoon:after_commit"
|
||||
|
||||
// statementFlushedKey marks a statement whose AfterCommitCallback has run.
|
||||
const statementFlushedKey = "lagoon:after_commit_flushed"
|
||||
|
||||
type afterCommitKey struct{}
|
||||
|
||||
// afterCommitBuffer holds the callbacks registered inside one transaction.
|
||||
@@ -95,6 +98,14 @@ func Transaction(ctx context.Context, gdb *gorm.DB, fn func(ctx context.Context,
|
||||
// run, because Lagoon cannot know whether that transaction will commit.
|
||||
// Anywhere else, fn runs immediately on db's connection.
|
||||
//
|
||||
// Called from a GORM callback that runs after AfterCommitCallback on the
|
||||
// same write, fn is registered too late and never runs; AfterCommit logs a
|
||||
// warning for it. Register such callbacks
|
||||
// Before("gorm:commit_or_rollback_transaction"). The warning also requires
|
||||
// that the statement no longer holds GORM's transaction, so a statement
|
||||
// reused across writes does not warn for a correctly ordered callback in a
|
||||
// later write.
|
||||
//
|
||||
// The handle fn receives always has an empty statement on the connection
|
||||
// the work belongs to, whatever handle AfterCommit was called with. Queries
|
||||
// through it never continue from the written model's
|
||||
@@ -115,6 +126,10 @@ func AfterCommit(ctx context.Context, db *gorm.DB, fn func(ctx context.Context,
|
||||
}
|
||||
if db != nil && db.Statement != nil {
|
||||
if _, started := db.InstanceGet("gorm:started_transaction"); started {
|
||||
if _, flushed := db.InstanceGet(statementFlushedKey); flushed && !transactionalHandle(db) {
|
||||
moduleLog.Warn("lagoon: after-commit callback registered after lagoon:after_commit ran; it will not run",
|
||||
"hint", "register the GORM callback before gorm:commit_or_rollback_transaction")
|
||||
}
|
||||
buf := &afterCommitBuffer{}
|
||||
if existing, ok := db.InstanceGet(statementBufferKey); ok {
|
||||
if b, ok := existing.(*afterCommitBuffer); ok && b != nil {
|
||||
@@ -155,8 +170,11 @@ func bufferFrom(ctx context.Context, db *gorm.DB) *afterCommitBuffer {
|
||||
|
||||
// flushStatementAfterCommit is the AfterCommitCallback GORM callback: it
|
||||
// runs the statement's buffered callbacks once GORM's own transaction has
|
||||
// committed, on a fresh session over the pool.
|
||||
// committed, on a fresh session over the pool. It first marks the
|
||||
// statement as flushed, on every write that passes through it, so that
|
||||
// AfterCommit can warn about work registered after this point.
|
||||
func flushStatementAfterCommit(db *gorm.DB) {
|
||||
db.InstanceSet(statementFlushedKey, true)
|
||||
raw, ok := db.InstanceGet(statementBufferKey)
|
||||
if !ok {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user