Skip to content

Commit

Permalink
MSSQL: Revert usage of new connectionstring format (grafana#19203)
Browse files Browse the repository at this point in the history
This reverts commit 2514209 from grafana#18384. Reason is that it doesn't 
work due to xorm 0.7.1 which doesn't support this new connectionstring 
format.

Fixes grafana#19189
Ref grafana#18384
Ref grafana#17665
  • Loading branch information
marefr authored Sep 18, 2019
1 parent 043bb59 commit 0f524fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 41 deletions.
30 changes: 16 additions & 14 deletions pkg/tsdb/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mssql
import (
"database/sql"
"fmt"
"net/url"
"strconv"

"github.com/grafana/grafana/pkg/setting"
Expand All @@ -24,7 +23,10 @@ func init() {
func newMssqlQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
logger := log.New("tsdb.mssql")

cnnstr := generateConnectionString(datasource)
cnnstr, err := generateConnectionString(datasource)
if err != nil {
return nil, err
}
if setting.Env == setting.DEV {
logger.Debug("getEngine", "connection", cnnstr)
}
Expand All @@ -43,21 +45,21 @@ func newMssqlQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndpoin
return sqleng.NewSqlQueryEndpoint(&config, &rowTransformer, newMssqlMacroEngine(), logger)
}

func generateConnectionString(datasource *models.DataSource) string {
func generateConnectionString(datasource *models.DataSource) (string, error) {
server, port := util.SplitHostPortDefault(datasource.Url, "localhost", "1433")
encrypt := datasource.JsonData.Get("encrypt").MustString("false")

query := url.Values{}
query.Add("database", datasource.Database)
query.Add("encrypt", encrypt)

u := &url.URL{
Scheme: "sqlserver",
User: url.UserPassword(datasource.User, datasource.DecryptedPassword()),
Host: fmt.Sprintf("%s:%s", server, port),
RawQuery: query.Encode(),
encrypt := datasource.JsonData.Get("encrypt").MustString("false")
connStr := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s;",
server,
port,
datasource.Database,
datasource.User,
datasource.DecryptedPassword(),
)
if encrypt != "false" {
connStr += fmt.Sprintf("encrypt=%s;", encrypt)
}
return u.String()
return connStr, nil
}

type mssqlRowTransformer struct {
Expand Down
27 changes: 0 additions & 27 deletions pkg/tsdb/mssql/mssql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,6 @@ import (
// If needed, change the variable below to the IP address of the database.
var serverIP = "localhost"

func TestGenerateConnectionString(t *testing.T) {
encrypted, _ := simplejson.NewJson([]byte(`{"encrypt":"false"}`))
testSet := []struct {
ds *models.DataSource
expected string
}{
{
&models.DataSource{
User: "user",
Database: "db",
Url: "localhost:1433",
SecureJsonData: securejsondata.GetEncryptedJsonData(map[string]string{
"password": "pass;word",
}),
JsonData: encrypted,
},
"sqlserver://user:pass;word@localhost:1433?database=db&encrypt=false",
},
}
for i := range testSet {
got := generateConnectionString(testSet[i].ds)
if got != testSet[i].expected {
t.Errorf("mssql connString error for testCase %d got: %s expected: %s", i, got, testSet[i].expected)
}
}
}

func TestMSSQL(t *testing.T) {
SkipConvey("MSSQL", t, func() {
x := InitMSSQLTestDB(t)
Expand Down

0 comments on commit 0f524fc

Please sign in to comment.