Skip to content

Commit

Permalink
feat: add sql options on WithTX
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 23, 2024
1 parent 119dec6 commit cdda09d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions internal/controller/ledger/controller_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (ctrl *DefaultController) GetMigrationsInfo(ctx context.Context) ([]migrati

func (ctrl *DefaultController) runTx(ctx context.Context, parameters Parameters, fn func(sqlTX TX) (*ledger.Log, error)) (*ledger.Log, error) {
var log *ledger.Log
err := ctrl.store.WithTX(ctx, func(tx TX) (commit bool, err error) {
err := ctrl.store.WithTX(ctx, nil, func(tx TX) (commit bool, err error) {
log, err = fn(tx)
if err != nil {
return false, err
Expand Down Expand Up @@ -188,7 +188,7 @@ func (ctrl *DefaultController) Import(ctx context.Context, stream chan ledger.Lo
return newErrInvalidState(ledger.StateInitializing, ctrl.ledger.State)
}

return ctrl.store.WithTX(ctx, func(sqlTx TX) (bool, error) {
return ctrl.store.WithTX(ctx, nil, func(sqlTx TX) (bool, error) {
for log := range stream {
switch payload := log.Data.(type) {
case ledger.NewTransactionLogPayload:
Expand Down
17 changes: 9 additions & 8 deletions internal/controller/ledger/controller_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ledger

import (
"context"
"database/sql"
"math/big"
"testing"

Expand Down Expand Up @@ -33,8 +34,8 @@ func TestCreateTransaction(t *testing.T) {
Return(machine, nil)

store.EXPECT().
WithTX(gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, fn func(tx TX) (bool, error)) error {
WithTX(gomock.Any(), nil, gomock.Any()).
DoAndReturn(func(ctx context.Context, _ *sql.TxOptions, fn func(tx TX) (bool, error)) error {
_, err := fn(sqlTX)
return err
})
Expand Down Expand Up @@ -77,8 +78,8 @@ func TestRevertTransaction(t *testing.T) {
l := NewDefaultController(ledger.Ledger{}, store, listener, machineFactory)

store.EXPECT().
WithTX(gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, fn func(tx TX) (bool, error)) error {
WithTX(gomock.Any(), nil, gomock.Any()).
DoAndReturn(func(ctx context.Context, _ *sql.TxOptions, fn func(tx TX) (bool, error)) error {
_, err := fn(sqlTX)
return err
})
Expand Down Expand Up @@ -122,8 +123,8 @@ func TestSaveTransactionMetadata(t *testing.T) {
l := NewDefaultController(ledger.Ledger{}, store, listener, machineFactory)

store.EXPECT().
WithTX(gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, fn func(tx TX) (bool, error)) error {
WithTX(gomock.Any(), nil, gomock.Any()).
DoAndReturn(func(ctx context.Context, _ *sql.TxOptions, fn func(tx TX) (bool, error)) error {
_, err := fn(sqlTX)
return err
})
Expand Down Expand Up @@ -160,8 +161,8 @@ func TestDeleteTransactionMetadata(t *testing.T) {
l := NewDefaultController(ledger.Ledger{}, store, listener, machineFactory)

store.EXPECT().
WithTX(gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, fn func(tx TX) (bool, error)) error {
WithTX(gomock.Any(), nil, gomock.Any()).
DoAndReturn(func(ctx context.Context, _ *sql.TxOptions, fn func(tx TX) (bool, error)) error {
_, err := fn(sqlTX)
return err
})
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/ledger/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ledger

import (
"context"
"database/sql"
"encoding/json"
"math/big"

Expand Down Expand Up @@ -47,7 +48,7 @@ type TX interface {
}

type Store interface {
WithTX(context.Context, func(TX) (bool, error)) error
WithTX(context.Context, *sql.TxOptions, func(TX) (bool, error)) error
GetDB() bun.IDB
ListLogs(ctx context.Context, q GetLogsQuery) (*bunpaginate.Cursor[ledger.Log], error)
ReadLogWithIdempotencyKey(ctx context.Context, ik string) (*ledger.Log, error)
Expand Down
9 changes: 5 additions & 4 deletions internal/controller/ledger/store_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions internal/storage/ledger/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ type DefaultStoreAdapter struct {
*Store
}

func (d *DefaultStoreAdapter) WithTX(ctx context.Context, f func(ledgercontroller.TX) (bool, error)) error {
tx, err := d.GetDB().BeginTx(ctx, &sql.TxOptions{})
func (d *DefaultStoreAdapter) WithTX(ctx context.Context, opts *sql.TxOptions, f func(ledgercontroller.TX) (bool, error)) error {
if opts == nil {
opts = &sql.TxOptions{}
}

tx, err := d.GetDB().BeginTx(ctx, opts)
if err != nil {
return err
}
Expand Down

0 comments on commit cdda09d

Please sign in to comment.