Skip to content

Commit ffd3f00

Browse files
authored
Avoid to remove connections when disposing (#54)
1 parent 40bec12 commit ffd3f00

File tree

4 files changed

+10
-39
lines changed

4 files changed

+10
-39
lines changed

datasource.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,8 @@ func NewDatasource(c Driver) *sqldatasource {
101101
}
102102

103103
// Dispose cleans up datasource instance resources.
104+
// Note: Called when testing and saving a datasource
104105
func (ds *sqldatasource) Dispose() {
105-
ds.dbConnections.Range(func(key, dbConn interface{}) bool {
106-
err := dbConn.(dbConnection).db.Close()
107-
if err != nil {
108-
backend.Logger.Error(err.Error())
109-
}
110-
ds.dbConnections.Delete(key)
111-
return true
112-
})
113106
}
114107

115108
// QueryData creates the Responses list and executes each query

datasource_test.go

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"testing"
88

9-
"github.com/DATA-DOG/go-sqlmock"
109
"github.com/grafana/grafana-plugin-sdk-go/backend"
1110
)
1211

@@ -99,36 +98,18 @@ func Test_getDBConnectionFromQuery(t *testing.T) {
9998
}
10099

101100
func Test_Dispose(t *testing.T) {
102-
t.Run("it should close all db connections", func(t *testing.T) {
103-
db1, mock1, err := sqlmock.New()
104-
if err != nil {
105-
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
106-
}
107-
db2, mock2, err := sqlmock.New()
108-
if err != nil {
109-
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
110-
}
111-
101+
t.Run("it should not delete connections", func(t *testing.T) {
112102
ds := &sqldatasource{}
113-
ds.dbConnections.Store(defaultKey("uid1"), dbConnection{db: db1})
114-
ds.dbConnections.Store("foo", dbConnection{db: db2})
115-
116-
mock1.ExpectClose()
117-
mock2.ExpectClose()
103+
ds.dbConnections.Store(defaultKey("uid1"), dbConnection{})
104+
ds.dbConnections.Store("foo", dbConnection{})
118105
ds.Dispose()
119-
120-
err = mock1.ExpectationsWereMet()
121-
if err != nil {
122-
t.Error(err)
123-
}
124-
err = mock2.ExpectationsWereMet()
125-
if err != nil {
126-
t.Error(err)
127-
}
128-
106+
count := 0
129107
ds.dbConnections.Range(func(key, value interface{}) bool {
130-
t.Errorf("db connections were not deleted")
131-
return false
108+
count++
109+
return true
132110
})
111+
if count != 2 {
112+
t.Errorf("missing connections")
113+
}
133114
})
134115
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/grafana/sqlds/v2
33
go 1.15
44

55
require (
6-
github.com/DATA-DOG/go-sqlmock v1.5.0
76
github.com/go-sql-driver/mysql v1.4.0
87
github.com/google/go-cmp v0.5.6
98
github.com/grafana/grafana-plugin-sdk-go v0.94.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
33
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
4-
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
5-
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
64
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
75
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
86
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=

0 commit comments

Comments
 (0)