Skip to content

Commit

Permalink
feat: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Sep 13, 2022
1 parent 2a1cedf commit fe3ba2d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
3 changes: 0 additions & 3 deletions pkg/api/middlewares/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/numary/ledger/pkg/api/apierrors"
"github.com/numary/ledger/pkg/storage"
"github.com/pkg/errors"
)

type bufferedResponseWriter struct {
Expand Down Expand Up @@ -65,8 +64,6 @@ func Transaction() func(c *gin.Context) {
apierrors.ResponseError(c, err)
return
}
} else {
c.Error(errors.New("transaction will be rollbacked as "))
}

if err := bufferedWriter.WriteResponse(); err != nil {
Expand Down
35 changes: 18 additions & 17 deletions pkg/storage/sqlstorage/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,25 @@ func init() {
// defaultExecutorProvider use the context to register and manage a sql transaction (if the context is mark as transactional)
func defaultExecutorProvider(schema Schema) func(ctx context.Context) (executor, error) {
return func(ctx context.Context) (executor, error) {
var ret executor = schema
if storage.IsTransactional(ctx) {
if !storage.IsTransactionRegistered(ctx) {
sqlTx, err := schema.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
return nil, err
}
ret = sqlTx
storage.RegisterTransaction(ctx, sqlTx, func(ctx context.Context) error {
return sqlTx.Commit()
}, func(ctx context.Context) error {
return sqlTx.Rollback()
})
} else {
ret = storage.RegisteredTransaction(ctx).(*sql.Tx)
}
if !storage.IsTransactional(ctx) {
return schema, nil
}
return ret, nil

if storage.IsTransactionRegistered(ctx) {
return storage.RegisteredTransaction(ctx).(*sql.Tx), nil
}

sqlTx, err := schema.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
return nil, err
}

storage.RegisterTransaction(ctx, sqlTx, func(ctx context.Context) error {
return sqlTx.Commit()
}, func(ctx context.Context) error {
return sqlTx.Rollback()
})
return sqlTx, nil
}
}

Expand Down

0 comments on commit fe3ba2d

Please sign in to comment.