(db dbx.Builder, fn func(txApp App) error, isForAuxDB bool)
| 23 | } |
| 24 | |
| 25 | func (app *BaseApp) runInTransaction(db dbx.Builder, fn func(txApp App) error, isForAuxDB bool) error { |
| 26 | switch txOrDB := db.(type) { |
| 27 | case *dbx.Tx: |
| 28 | // run as part of the already existing transaction |
| 29 | return fn(app) |
| 30 | case *dbx.DB: |
| 31 | var txApp *BaseApp |
| 32 | txErr := txOrDB.Transactional(func(tx *dbx.Tx) error { |
| 33 | txApp = app.createTxApp(tx, isForAuxDB) |
| 34 | return fn(txApp) |
| 35 | }) |
| 36 | |
| 37 | // execute all after event calls on transaction complete |
| 38 | if txApp != nil && txApp.txInfo != nil { |
| 39 | afterFuncErr := txApp.txInfo.runAfterFuncs(txErr) |
| 40 | if afterFuncErr != nil { |
| 41 | return errors.Join(txErr, afterFuncErr) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return txErr |
| 46 | default: |
| 47 | return errors.New("failed to start transaction (unknown db type)") |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // createTxApp shallow clones the current app and assigns a new tx state. |
| 52 | func (app *BaseApp) createTxApp(tx *dbx.Tx, isForAuxDB bool) *BaseApp { |
no test coverage detected