Skip to content

Commit

Permalink
fix: delete existing steps before registering new ones (#1480)
Browse files Browse the repository at this point in the history
delete existing steps before registering new ones
  • Loading branch information
mathnogueira committed Nov 14, 2022
1 parent b3f7719 commit cc91a8b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion server/testdb/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ func (td *postgresDB) insertIntoTransactions(ctx context.Context, transaction mo
}

func (td *postgresDB) setTransactionSteps(ctx context.Context, tx *sql.Tx, transaction model.Transaction) (model.Transaction, error) {
// delete existing steps
stmt, err := tx.Prepare("DELETE FROM transaction_runs WHERE transaction_id = $1")
if err != nil {
tx.Rollback()
return model.Transaction{}, err
}

_, err = stmt.ExecContext(ctx, transaction.ID)
if err != nil {
tx.Rollback()
return model.Transaction{}, err
}

if len(transaction.Steps) == 0 {
return transaction, tx.Commit()
}
Expand All @@ -77,7 +90,7 @@ func (td *postgresDB) setTransactionSteps(ctx context.Context, tx *sql.Tx, trans
}

sql := "INSERT INTO transaction_steps VALUES " + strings.Join(values, ", ")
_, err := tx.ExecContext(ctx, sql)
_, err = tx.ExecContext(ctx, sql)
if err != nil {
tx.Rollback()
return model.Transaction{}, fmt.Errorf("cannot save transaction steps: %w", err)
Expand Down

0 comments on commit cc91a8b

Please sign in to comment.