/
transaction.go
37 lines (30 loc) · 932 Bytes
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package code
import (
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
var errRollback = errors.New("rollback")
// transaction wraps the given function in a transaction. In case the given
// functions returns an error, the transaction will be rolled back.
// Note: this is a copy of storage.Transaction, but since the storage package
// uses the code migrations, using it from there would cause an import cycle.
func transaction(db *sqlx.DB, f func(tx sqlx.Ext) error) error {
tx, err := db.Beginx()
if err != nil {
return errors.Wrap(err, "migration/code: begin transaction error")
}
err = f(tx)
if err != nil {
if rbErr := tx.Rollback(); rbErr != nil {
return errors.Wrap(rbErr, "migration/code: transaction rollback error")
}
if err == errRollback {
return nil
}
return err
}
if err := tx.Commit(); err != nil {
return errors.Wrap(err, "storage: transaction commit error")
}
return nil
}