Commit 0f524fc9 by Marcus Efraimsson Committed by GitHub

MSSQL: Revert usage of new connectionstring format (#19203)

This reverts commit 25142090 from #18384. Reason is that it doesn't 
work due to xorm 0.7.1 which doesn't support this new connectionstring 
format.

Fixes #19189
Ref #18384
Ref #17665
parent 043bb595
...@@ -3,7 +3,6 @@ package mssql ...@@ -3,7 +3,6 @@ package mssql
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"net/url"
"strconv" "strconv"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
...@@ -24,7 +23,10 @@ func init() { ...@@ -24,7 +23,10 @@ func init() {
func newMssqlQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) { func newMssqlQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) {
logger := log.New("tsdb.mssql") logger := log.New("tsdb.mssql")
cnnstr := generateConnectionString(datasource) cnnstr, err := generateConnectionString(datasource)
if err != nil {
return nil, err
}
if setting.Env == setting.DEV { if setting.Env == setting.DEV {
logger.Debug("getEngine", "connection", cnnstr) logger.Debug("getEngine", "connection", cnnstr)
} }
...@@ -43,21 +45,21 @@ func newMssqlQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndpoin ...@@ -43,21 +45,21 @@ func newMssqlQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndpoin
return sqleng.NewSqlQueryEndpoint(&config, &rowTransformer, newMssqlMacroEngine(), logger) 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") server, port := util.SplitHostPortDefault(datasource.Url, "localhost", "1433")
encrypt := datasource.JsonData.Get("encrypt").MustString("false")
query := url.Values{} encrypt := datasource.JsonData.Get("encrypt").MustString("false")
query.Add("database", datasource.Database) connStr := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s;",
query.Add("encrypt", encrypt) server,
port,
u := &url.URL{ datasource.Database,
Scheme: "sqlserver", datasource.User,
User: url.UserPassword(datasource.User, datasource.DecryptedPassword()), datasource.DecryptedPassword(),
Host: fmt.Sprintf("%s:%s", server, port), )
RawQuery: query.Encode(), if encrypt != "false" {
connStr += fmt.Sprintf("encrypt=%s;", encrypt)
} }
return u.String() return connStr, nil
} }
type mssqlRowTransformer struct { type mssqlRowTransformer struct {
......
...@@ -28,33 +28,6 @@ import ( ...@@ -28,33 +28,6 @@ import (
// If needed, change the variable below to the IP address of the database. // If needed, change the variable below to the IP address of the database.
var serverIP = "localhost" 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) { func TestMSSQL(t *testing.T) {
SkipConvey("MSSQL", t, func() { SkipConvey("MSSQL", t, func() {
x := InitMSSQLTestDB(t) x := InitMSSQLTestDB(t)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment