This repository was archived by the owner on Jul 14, 2022. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -178,6 +178,7 @@ type DBTx struct {
178178 err error
179179 rowsAffected int64
180180 wrappers []database.Wrapper
181+ afterCommit func (err error )
181182}
182183
183184func (store * DBStore ) BeginTx () (* DBTx , error ) {
@@ -202,7 +203,9 @@ func (tx *DBTx) Close() error {
202203 if tx .err != nil {
203204 return tx .tx .Rollback ()
204205 }
205- return tx .tx .Commit ()
206+ err := tx .tx .Commit ()
207+ tx .afterCommit (err )
208+ return err
206209}
207210
208211func (tx * DBTx ) Query (sql string , args ... interface {}) (* sql.Rows , error ) {
@@ -279,6 +282,10 @@ func (tx *DBTx) SetError(err error) {
279282 tx .err = err
280283}
281284
285+ func (tx * DBTx ) AfterCommit (afterCommit func (err error )) {
286+ tx .afterCommit = afterCommit
287+ }
288+
282289func TransactFunc (db * DBStore , txFunc func (* DBTx ) error ) (err error ) {
283290 tx , err := db .BeginTx ()
284291 if err != nil {
@@ -338,3 +345,14 @@ type TransactorWithContext interface {
338345func TransactContext (ctx context.Context , db * DBStore , t TransactorWithContext ) error {
339346 return TransactFuncContext (ctx , db , t .TransactContext )
340347}
348+
349+ func BeginTx (ctx context.Context , db * sql.DB ) (* DBTx , error ) {
350+ tx , err := db .BeginTx (ctx , nil )
351+ if err != nil {
352+ return nil , err
353+ }
354+
355+ return & DBTx {
356+ tx : tx ,
357+ }, nil
358+ }
You can’t perform that action at this time.
0 commit comments