fix: defer certains appels à tx.Rollback
Pour `data.InsertMembres` et `data.InsertProgrammes`
This commit is contained in:
parent
585c626e1c
commit
d5399903e4
1 changed files with 2 additions and 8 deletions
10
data/data.go
10
data/data.go
|
@ -78,24 +78,21 @@ func (d *DataClient) InsertMembres(membres []models.Membre) (int64, error) {
|
||||||
var rowsInserted int64
|
var rowsInserted int64
|
||||||
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 _, membre := range membres {
|
for _, membre := range membres {
|
||||||
if membre.ID == "" {
|
if membre.ID == "" {
|
||||||
tx.Rollback()
|
|
||||||
return 0, errors.New("Cannot insert membre with no membre_id")
|
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)
|
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 {
|
if err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := result.RowsAffected()
|
rows, err := result.RowsAffected()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,25 +111,22 @@ func (d *DataClient) InsertProgrammes(programmes []models.Programme) (int64, err
|
||||||
var rowsInserted int64
|
var rowsInserted int64
|
||||||
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 _, programme := range programmes {
|
for _, programme := range programmes {
|
||||||
if programme.ID == "" {
|
if programme.ID == "" {
|
||||||
tx.Rollback()
|
|
||||||
return 0, errors.New("Cannot insert programme with no programme_id")
|
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)
|
result, err := tx.NamedExec("INSERT INTO programmes (id, titre) VALUES (:id, :titre) ON CONFLICT DO NOTHING;", &programme)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := result.RowsAffected()
|
rows, err := result.RowsAffected()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue