added transactional wrapper, and support for it
This commit is contained in:
parent
f88f00e78c
commit
6aa7788fd0
@ -7,7 +7,7 @@ import (
|
|||||||
"git.sdf.org/jchenry/x"
|
"git.sdf.org/jchenry/x"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Func func(db *sql.DB)
|
type Func func(ctx context.Context, db *sql.DB) error
|
||||||
|
|
||||||
type Actor struct {
|
type Actor struct {
|
||||||
DB *sql.DB
|
DB *sql.DB
|
||||||
@ -19,7 +19,9 @@ func (a *Actor) Run(ctx context.Context) error {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case f := <-a.ActionChan:
|
case f := <-a.ActionChan:
|
||||||
f(a.DB)
|
if err:= f(ctx, a.DB); err != nil{
|
||||||
|
return err
|
||||||
|
}
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
}
|
}
|
||||||
|
29
database/transactor.go
Normal file
29
database/transactor.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WithinTransaction is a functional equivalent of the Transactor interface created by Thibaut Rousseau's
|
||||||
|
// https://blog.thibaut-rousseau.com/blog/sql-transactions-in-go-the-good-way/
|
||||||
|
func WithinTransaction(f Func) Func {
|
||||||
|
return func(ctx context.Context, db *sql.DB) error {
|
||||||
|
tx, err := db.BeginTx(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to begin transaction: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := f(ctx, db); err != nil {
|
||||||
|
_ = tx.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Commit(); err != nil {
|
||||||
|
return fmt.Errorf("failed to commit transaction: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user