Skip to content

Commit 40bec12

Browse files
authored
Restore support for 7.5 (#52)
1 parent eb258df commit 40bec12

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

datasource.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,23 @@ func (ds *sqldatasource) storeDBConnection(key string, dbConn dbConnection) {
6262
ds.dbConnections.Store(key, dbConn)
6363
}
6464

65+
func getDatasourceUID(settings backend.DataSourceInstanceSettings) string {
66+
datasourceUID := settings.UID
67+
// Grafana < 8.0 won't include the UID yet
68+
if datasourceUID == "" {
69+
datasourceUID = fmt.Sprintf("%d", settings.ID)
70+
}
71+
return datasourceUID
72+
}
73+
6574
// NewDatasource creates a new `sqldatasource`.
6675
// It uses the provided settings argument to call the ds.Driver to connect to the SQL server
6776
func (ds *sqldatasource) NewDatasource(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
6877
db, err := ds.c.Connect(settings, nil)
6978
if err != nil {
7079
return nil, err
7180
}
72-
key := defaultKey(settings.UID)
81+
key := defaultKey(getDatasourceUID(settings))
7382
ds.storeDBConnection(key, dbConnection{db, settings})
7483

7584
mux := http.NewServeMux()
@@ -115,7 +124,7 @@ func (ds *sqldatasource) QueryData(ctx context.Context, req *backend.QueryDataRe
115124
// Execute each query and store the results by query RefID
116125
for _, q := range req.Queries {
117126
go func(query backend.DataQuery) {
118-
frames, err := ds.handleQuery(ctx, query, req.PluginContext.DataSourceInstanceSettings.UID)
127+
frames, err := ds.handleQuery(ctx, query, getDatasourceUID(*req.PluginContext.DataSourceInstanceSettings))
119128

120129
response.Set(query.RefID, backend.DataResponse{
121130
Frames: frames,

0 commit comments

Comments
 (0)