Skip to content

Commit 2f9063b

Browse files
dmakushinDmitrii Makushin
authored and
Dmitrii Makushin
committed
Add RunInTx method for DB
1 parent 992acfb commit 2f9063b

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/stephenafamo/bob
33
go 1.23
44

55
require (
6+
github.com/DATA-DOG/go-sqlmock v1.5.2
67
github.com/DATA-DOG/go-txdb v0.1.6
78
github.com/Masterminds/sprig/v3 v3.2.2
89
github.com/aarondl/opt v0.0.0-20230114172057-b91f370c41f0
@@ -21,6 +22,7 @@ require (
2122
github.com/qdm12/reprint v0.0.0-20200326205758-722754a53494
2223
github.com/stephenafamo/scan v0.6.1
2324
github.com/stephenafamo/sqlparser v0.0.0-20241111104950-b04fa8a26c9c
25+
github.com/stretchr/testify v1.8.2
2426
github.com/urfave/cli/v2 v2.23.7
2527
github.com/volatiletech/strmangle v0.0.6
2628
github.com/wasilibs/go-pgquery v0.0.0-20240319230125-b9b2e95c69a7
@@ -37,6 +39,7 @@ require (
3739
github.com/Masterminds/semver/v3 v3.1.1 // indirect
3840
github.com/aarondl/json v0.0.0-20221020222930-8b0db17ef1bf // indirect
3941
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
42+
github.com/davecgh/go-spew v1.1.1 // indirect
4043
github.com/dustin/go-humanize v1.0.1 // indirect
4144
github.com/fsnotify/fsnotify v1.6.0 // indirect
4245
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
@@ -54,11 +57,11 @@ require (
5457
github.com/mitchellh/reflectwalk v1.0.2 // indirect
5558
github.com/ncruces/go-strftime v0.1.9 // indirect
5659
github.com/pganalyze/pg_query_go/v5 v5.1.0 // indirect
60+
github.com/pmezard/go-difflib v1.0.0 // indirect
5761
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
5862
github.com/russross/blackfriday/v2 v2.1.0 // indirect
5963
github.com/shopspring/decimal v1.3.1 // indirect
6064
github.com/spf13/cast v1.5.0 // indirect
61-
github.com/stretchr/testify v1.8.2 // indirect
6265
github.com/tetratelabs/wazero v1.7.0 // indirect
6366
github.com/volatiletech/inflect v0.0.1 // indirect
6467
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect

go.sum

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
22
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
3+
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
4+
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
35
github.com/DATA-DOG/go-txdb v0.1.6 h1:D1Ob/L79mCW6UCFL6vwM/9TWs/rshZujxTsvy7+gicw=
46
github.com/DATA-DOG/go-txdb v0.1.6/go.mod h1:DhAhxMXZpUJVGnT+p9IbzJoRKvlArO2pkHjnGX7o0n0=
57
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
@@ -57,6 +59,7 @@ github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk
5759
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
5860
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
5961
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
62+
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
6063
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
6164
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
6265
github.com/knadh/koanf/parsers/yaml v0.1.0 h1:ZZ8/iGfRLvKSaMEECEBPM1HQslrZADk8fP1XFUxVI5w=
@@ -109,8 +112,6 @@ github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
109112
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
110113
github.com/stephenafamo/fakedb v0.0.0-20221230081958-0b86f816ed97 h1:XItoZNmhOih06TC02jK7l3wlpZ0XT/sPQYutDcGOQjg=
111114
github.com/stephenafamo/fakedb v0.0.0-20221230081958-0b86f816ed97/go.mod h1:bM3Vmw1IakoaXocHmMIGgJFYob0vuK+CFWiJHQvz0jQ=
112-
github.com/stephenafamo/scan v0.6.0 h1:N0joyP/wriC9VvP6w9SDxHIuQGatW4c2YW7Z5L4m45s=
113-
github.com/stephenafamo/scan v0.6.0/go.mod h1:FhIUJ8pLNyex36xGFiazDJJ5Xry0UkAi+RkWRrEcRMg=
114115
github.com/stephenafamo/scan v0.6.1 h1:nXokGCQwYazMuyvdNAoK0T8Z76FWcpMvDdtengpz6PU=
115116
github.com/stephenafamo/scan v0.6.1/go.mod h1:FhIUJ8pLNyex36xGFiazDJJ5Xry0UkAi+RkWRrEcRMg=
116117
github.com/stephenafamo/sqlparser v0.0.0-20241111104950-b04fa8a26c9c h1:JFga++XBnZG2xlnvQyHJkeBWZ9G9mGdtgvLeSRbp/BA=

stdlib.go

+28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"database/sql"
66
"database/sql/driver"
7+
"errors"
8+
"fmt"
79

810
"github.com/stephenafamo/scan"
911
"github.com/stephenafamo/scan/stdscan"
@@ -96,6 +98,32 @@ func (d DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error) {
9698
return NewTx(tx), nil
9799
}
98100

101+
// RunInTx runs the provided function in a transaction.
102+
// If the function returns an error, the transaction is rolled back.
103+
// Otherwise, the transaction is committed.
104+
func (d DB) RunInTx(ctx context.Context, txOptions *sql.TxOptions, fn func(context.Context, Tx) error) error {
105+
tx, err := d.BeginTx(ctx, txOptions)
106+
if err != nil {
107+
return fmt.Errorf("begin transaction: %w", err)
108+
}
109+
110+
if err := fn(ctx, tx); err != nil {
111+
err = fmt.Errorf("call method in transaction: %w", err)
112+
113+
if rollbackErr := tx.Rollback(); rollbackErr != nil {
114+
return errors.Join(err, rollbackErr)
115+
}
116+
117+
return err
118+
}
119+
120+
if err := tx.Commit(); err != nil {
121+
return fmt.Errorf("commit transaction: %w", err)
122+
}
123+
124+
return nil
125+
}
126+
99127
// NewTx wraps an [*sql.Tx] and returns a type that implements [Queryer] but still
100128
// retains the expected methods used by *sql.Tx
101129
// This is useful when an existing *sql.Tx is used in other places in the codebase

0 commit comments

Comments
 (0)