Chore: Propagate context for data source provisioning (#40235)

* context all the things

* apply feedback

* rollback some alerting changes

* rollback some alerting changes #2

* more rollbacks

* more rollbacks #2

* more rollbacks #3

* more rollbacks #4

* fix integration test

* add missing context

* add missing and remove incorrect dispatch
This commit is contained in:
Will Browne
2021-10-18 17:06:19 +02:00
committed by GitHub
parent ec0fcbbf4b
commit e4297006f6
22 changed files with 129 additions and 112 deletions
+25 -20
View File
@@ -1,6 +1,7 @@
package sqlstore
import (
"context"
"fmt"
"strings"
"time"
@@ -15,24 +16,28 @@ import (
// GetDataSource adds a datasource to the query model by querying by org_id as well as
// either uid (preferred), id, or name and is added to the bus.
func (ss *SQLStore) GetDataSource(query *models.GetDataSourceQuery) error {
func (ss *SQLStore) GetDataSource(ctx context.Context, query *models.GetDataSourceQuery) error {
metrics.MDBDataSourceQueryByID.Inc()
if query.OrgId == 0 || (query.Id == 0 && len(query.Name) == 0 && len(query.Uid) == 0) {
return models.ErrDataSourceIdentifierNotSet
}
datasource := models.DataSource{Name: query.Name, OrgId: query.OrgId, Id: query.Id, Uid: query.Uid}
has, err := x.Get(&datasource)
return ss.WithDbSession(ctx, func(sess *DBSession) error {
if query.OrgId == 0 || (query.Id == 0 && len(query.Name) == 0 && len(query.Uid) == 0) {
return models.ErrDataSourceIdentifierNotSet
}
if err != nil {
sqlog.Error("Failed getting data source", "err", err, "uid", query.Uid, "id", query.Id, "name", query.Name, "orgId", query.OrgId)
return err
} else if !has {
return models.ErrDataSourceNotFound
}
datasource := &models.DataSource{Name: query.Name, OrgId: query.OrgId, Id: query.Id, Uid: query.Uid}
has, err := sess.Get(datasource)
query.Result = &datasource
return nil
if err != nil {
sqlog.Error("Failed getting data source", "err", err, "uid", query.Uid, "id", query.Id, "name", query.Name, "orgId", query.OrgId)
return err
} else if !has {
return models.ErrDataSourceNotFound
}
query.Result = datasource
return nil
})
}
func (ss *SQLStore) GetDataSources(query *models.GetDataSourcesQuery) error {
@@ -73,7 +78,7 @@ func (ss *SQLStore) GetDefaultDataSource(query *models.GetDefaultDataSourceQuery
// DeleteDataSource removes a datasource by org_id as well as either uid (preferred), id, or name
// and is added to the bus.
func (ss *SQLStore) DeleteDataSource(cmd *models.DeleteDataSourceCommand) error {
func (ss *SQLStore) DeleteDataSource(ctx context.Context, cmd *models.DeleteDataSourceCommand) error {
params := make([]interface{}, 0)
makeQuery := func(sql string, p ...interface{}) {
@@ -94,7 +99,7 @@ func (ss *SQLStore) DeleteDataSource(cmd *models.DeleteDataSourceCommand) error
return models.ErrDataSourceIdentifierNotSet
}
return inTransaction(func(sess *DBSession) error {
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
result, err := sess.Exec(params...)
cmd.DeletedDatasourcesCount, _ = result.RowsAffected()
@@ -110,8 +115,8 @@ func (ss *SQLStore) DeleteDataSource(cmd *models.DeleteDataSourceCommand) error
})
}
func (ss *SQLStore) AddDataSource(cmd *models.AddDataSourceCommand) error {
return inTransaction(func(sess *DBSession) error {
func (ss *SQLStore) AddDataSource(ctx context.Context, cmd *models.AddDataSourceCommand) error {
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
existing := models.DataSource{OrgId: cmd.OrgId, Name: cmd.Name}
has, _ := sess.Get(&existing)
@@ -188,8 +193,8 @@ func updateIsDefaultFlag(ds *models.DataSource, sess *DBSession) error {
return nil
}
func (ss *SQLStore) UpdateDataSource(cmd *models.UpdateDataSourceCommand) error {
return inTransaction(func(sess *DBSession) error {
func (ss *SQLStore) UpdateDataSource(ctx context.Context, cmd *models.UpdateDataSourceCommand) error {
return inTransactionCtx(ctx, func(sess *DBSession) error {
if cmd.JsonData == nil {
cmd.JsonData = simplejson.New()
}