fix(db): rollback camoufle erreurs dans InsertTransactions
Defer rollback au lieu de l'appeler manuellement à chaque fois
This commit is contained in:
parent
d8fbd798c2
commit
853aa8cc77
1 changed files with 1 additions and 4 deletions
|
@ -79,20 +79,18 @@ func (d *DataClient) InsertTransactions(transactions []models.Transaction) ([]mo
|
||||||
// Start transaction
|
// Start transaction
|
||||||
tx, err := d.DB.Beginx()
|
tx, err := d.DB.Beginx()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return rowsInserted, err
|
return rowsInserted, err
|
||||||
}
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
for _, transaction := range transactions {
|
for _, transaction := range transactions {
|
||||||
// Check values
|
// Check values
|
||||||
if transaction.MembreID == "" {
|
if transaction.MembreID == "" {
|
||||||
tx.Rollback()
|
|
||||||
return rowsInserted, errors.New("Impossible d'insérer une transaction sans membre_id")
|
return rowsInserted, errors.New("Impossible d'insérer une transaction sans membre_id")
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := tx.NamedQuery("INSERT INTO transactions (membre_id, given_at, is_perpetual) VALUES (:membre_id, current_timestamp, :is_perpetual) RETURNING id, membre_id, is_perpetual;", &transaction)
|
rows, err := tx.NamedQuery("INSERT INTO transactions (membre_id, given_at, is_perpetual) VALUES (:membre_id, current_timestamp, :is_perpetual) RETURNING id, membre_id, is_perpetual;", &transaction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return rowsInserted, err
|
return rowsInserted, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
@ -100,7 +98,6 @@ func (d *DataClient) InsertTransactions(transactions []models.Transaction) ([]mo
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var transactionRow models.Transaction
|
var transactionRow models.Transaction
|
||||||
if err := rows.Scan(&transactionRow.ID, &transactionRow.MembreID, &transactionRow.IsPerpetual); err != nil {
|
if err := rows.Scan(&transactionRow.ID, &transactionRow.MembreID, &transactionRow.IsPerpetual); err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return rowsInserted, err
|
return rowsInserted, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue