From 3d8b2c358b1b9170e8f19ee39019bcdf31ded6e8 Mon Sep 17 00:00:00 2001 From: Andrew Reed Date: Tue, 6 Jan 2026 15:14:17 -0600 Subject: [PATCH 1/2] Add PostCommit hooks to Tx --- sequel.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/sequel.go b/sequel.go index 92473d7..1775a25 100644 --- a/sequel.go +++ b/sequel.go @@ -412,6 +412,7 @@ type Tx struct { tx *sqlx.Tx clock clock.Clock doRebindModel bool + oncommit []func() } // Begin begins a transaction and returns a new Tx. @@ -441,7 +442,19 @@ func (t *Tx) rebindModel(query string) string { // Commit commits the transaction. func (t *Tx) Commit() error { - return t.tx.Commit() + err := t.tx.Commit() + if err != nil { + return err + } + for _, fn := range t.oncommit { + fn() + } + return nil +} + +// PostCommit registers a function to be called after a successful commit. +func (t *Tx) PostCommit(fn func()) { + t.oncommit = append(t.oncommit, fn) } // Rollback aborts the transaction. From 1b4461fc5b3adef5bd4b33551cd2db4890251236 Mon Sep 17 00:00:00 2001 From: Andrew Reed Date: Tue, 6 Jan 2026 17:12:33 -0600 Subject: [PATCH 2/2] Run commit hooks asynchronously --- sequel.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sequel.go b/sequel.go index 1775a25..4b1af59 100644 --- a/sequel.go +++ b/sequel.go @@ -447,12 +447,12 @@ func (t *Tx) Commit() error { return err } for _, fn := range t.oncommit { - fn() + go fn() } return nil } -// PostCommit registers a function to be called after a successful commit. +// PostCommit registers a function to be called asynchronously after a successful commit. func (t *Tx) PostCommit(fn func()) { t.oncommit = append(t.oncommit, fn) }