This commit is contained in:
parent
13774eec7d
commit
45fcecdc04
29 changed files with 2865 additions and 3867 deletions
18
internal/repository/helpers.go
Normal file
18
internal/repository/helpers.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package repository
|
||||
|
||||
import "github.com/jmoiron/sqlx"
|
||||
|
||||
// WithTx runs fn inside a transaction. If fn returns an error, the transaction
|
||||
// is rolled back; otherwise it is committed.
|
||||
func WithTx(db *sqlx.DB, fn func(*sqlx.Tx) error) error {
|
||||
tx, err := db.Beginx()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
if err := fn(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue