From d5399903e4b6f96bda279b4f991ed148af7b69a7 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Wed, 14 Feb 2024 14:13:01 -0500 Subject: [PATCH] =?UTF-8?q?fix:=20defer=20certains=20appels=20=C3=A0=20`tx?= =?UTF-8?q?.Rollback`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pour `data.InsertMembres` et `data.InsertProgrammes` --- data/data.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/data/data.go b/data/data.go index c4cc026..fa8678c 100644 --- a/data/data.go +++ b/data/data.go @@ -78,24 +78,21 @@ func (d *DataClient) InsertMembres(membres []models.Membre) (int64, error) { var rowsInserted int64 tx, err := d.DB.Beginx() if err != nil { - tx.Rollback() return rowsInserted, err } + defer tx.Rollback() for _, membre := range membres { if membre.ID == "" { - tx.Rollback() return 0, errors.New("Cannot insert membre with no membre_id") } result, err := tx.NamedExec("INSERT INTO membres (id, last_name, first_name, prefered_name, programme_id) VALUES (:id, :last_name, :first_name, :prefered_name, :programme_id) ON CONFLICT (id) DO NOTHING;", &membre) if err != nil { - tx.Rollback() return 0, err } rows, err := result.RowsAffected() if err != nil { - tx.Rollback() return 0, err } @@ -114,25 +111,22 @@ func (d *DataClient) InsertProgrammes(programmes []models.Programme) (int64, err var rowsInserted int64 tx, err := d.DB.Beginx() if err != nil { - tx.Rollback() return rowsInserted, err } + defer tx.Rollback() for _, programme := range programmes { if programme.ID == "" { - tx.Rollback() return 0, errors.New("Cannot insert programme with no programme_id") } result, err := tx.NamedExec("INSERT INTO programmes (id, titre) VALUES (:id, :titre) ON CONFLICT DO NOTHING;", &programme) if err != nil { - tx.Rollback() return 0, err } rows, err := result.RowsAffected() if err != nil { - tx.Rollback() return 0, err } -- 2.45.2