Run queries inside one transaction

This commit is contained in:
Denis Makogon
2017-09-21 01:21:41 +03:00
parent 7a9591fd45
commit a09159308c

View File

@@ -221,30 +221,32 @@ func (ds *sqlStore) UpdateApp(ctx context.Context, newapp *models.App) (*models.
} }
func (ds *sqlStore) RemoveApp(ctx context.Context, appName string) error { func (ds *sqlStore) RemoveApp(ctx context.Context, appName string) error {
res, err := ds.db.ExecContext(ctx, ds.db.Rebind( return ds.Tx(func(tx *sqlx.Tx) error {
`DELETE FROM apps WHERE name = ?`), appName) res, err := ds.db.ExecContext(ctx, ds.db.Rebind(
if err != nil { `DELETE FROM apps WHERE name = ?`), appName)
return err
}
_, err = res.RowsAffected()
if err == sql.ErrNoRows {
return models.ErrAppsNotFound
}
deletes := []string{
`DELETE FROM logs WHERE app_name=?`,
`DELETE FROM calls WHERE app_name=?`,
`DELETE FROM routes WHERE app_name=?`,
}
for _, stmt := range deletes {
_, err = ds.db.ExecContext(ctx, ds.db.Rebind(stmt), appName)
if err != nil { if err != nil {
return err return err
} }
} _, err = res.RowsAffected()
if err == sql.ErrNoRows {
return models.ErrAppsNotFound
}
return nil deletes := []string{
`DELETE FROM logs WHERE app_name=?`,
`DELETE FROM calls WHERE app_name=?`,
`DELETE FROM routes WHERE app_name=?`,
}
for _, stmt := range deletes {
_, err = ds.db.ExecContext(ctx, ds.db.Rebind(stmt), appName)
if err != nil {
return err
}
}
return nil
})
} }
func (ds *sqlStore) GetApp(ctx context.Context, name string) (*models.App, error) { func (ds *sqlStore) GetApp(ctx context.Context, name string) (*models.App, error) {