feat(transaction-recurring): #100 fix database lock bug #137
@@ -200,7 +200,7 @@ func (h TransactionImpl) handleUpdateTransaction() http.HandlerFunc {
|
|||||||
|
|
||||||
var transaction *types.Transaction
|
var transaction *types.Transaction
|
||||||
if idStr == "new" {
|
if idStr == "new" {
|
||||||
transaction, err = h.s.Add(user, input)
|
transaction, err = h.s.Add(nil, user, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
handleError(w, r, err)
|
handleError(w, r, err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Transaction interface {
|
type Transaction interface {
|
||||||
Add(user *types.User, transaction types.Transaction) (*types.Transaction, error)
|
Add(tx *sqlx.Tx, user *types.User, transaction types.Transaction) (*types.Transaction, error)
|
||||||
Update(user *types.User, transaction types.Transaction) (*types.Transaction, error)
|
Update(user *types.User, transaction types.Transaction) (*types.Transaction, error)
|
||||||
Get(user *types.User, id string) (*types.Transaction, error)
|
Get(user *types.User, id string) (*types.Transaction, error)
|
||||||
GetAll(user *types.User, filter types.TransactionItemsFilter) ([]*types.Transaction, error)
|
GetAll(user *types.User, filter types.TransactionItemsFilter) ([]*types.Transaction, error)
|
||||||
@@ -48,21 +48,24 @@ func NewTransaction(db *sqlx.DB, random Random, clock Clock) Transaction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s TransactionImpl) Add(user *types.User, transactionInput types.Transaction) (*types.Transaction, error) {
|
func (s TransactionImpl) Add(tx *sqlx.Tx, user *types.User, transactionInput types.Transaction) (*types.Transaction, error) {
|
||||||
transactionMetric.WithLabelValues("add").Inc()
|
transactionMetric.WithLabelValues("add").Inc()
|
||||||
|
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return nil, ErrUnauthorized
|
return nil, ErrUnauthorized
|
||||||
}
|
}
|
||||||
|
|
||||||
tx, err := s.db.Beginx()
|
var err error
|
||||||
err = db.TransformAndLogDbError("transaction Add", nil, err)
|
if tx == nil {
|
||||||
if err != nil {
|
tx, err = s.db.Beginx()
|
||||||
return nil, err
|
err = db.TransformAndLogDbError("transaction Add", nil, err)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
_ = tx.Rollback()
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
defer func() {
|
|
||||||
_ = tx.Rollback()
|
|
||||||
}()
|
|
||||||
|
|
||||||
transaction, err := s.validateAndEnrichTransaction(tx, nil, user.Id, transactionInput)
|
transaction, err := s.validateAndEnrichTransaction(tx, nil, user.Id, transactionInput)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -364,7 +364,7 @@ func (s TransactionRecurringImpl) GenerateTransactions(user *types.User) error {
|
|||||||
Value: transactionRecurring.Value,
|
Value: transactionRecurring.Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = s.transaction.Add(user, transaction)
|
_, err = s.transaction.Add(tx, user, transaction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user