Skip to content

Commit 3759d1c

Browse files
authored
Add PostCommit hooks to Tx (#69)
1 parent f568b61 commit 3759d1c

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

sequel.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ type Tx struct {
412412
tx *sqlx.Tx
413413
clock clock.Clock
414414
doRebindModel bool
415+
oncommit []func()
415416
}
416417

417418
// Begin begins a transaction and returns a new Tx.
@@ -441,7 +442,19 @@ func (t *Tx) rebindModel(query string) string {
441442

442443
// Commit commits the transaction.
443444
func (t *Tx) Commit() error {
444-
return t.tx.Commit()
445+
err := t.tx.Commit()
446+
if err != nil {
447+
return err
448+
}
449+
for _, fn := range t.oncommit {
450+
go fn()
451+
}
452+
return nil
453+
}
454+
455+
// PostCommit registers a function to be called asynchronously after a successful commit.
456+
func (t *Tx) PostCommit(fn func()) {
457+
t.oncommit = append(t.oncommit, fn)
445458
}
446459

447460
// Rollback aborts the transaction.

0 commit comments

Comments
 (0)