slingdata-io / sling-cli

Sling is a CLI tool that extracts data from a source storage/database and loads it in a target storage/database.
https://docs.slingdata.io
GNU General Public License v3.0
299 stars 16 forks source link

MSSQL connection uri with instance name #310

Closed Santhin closed 1 month ago

Santhin commented 1 month ago

Issue Description

While trying to test the env connection for the mssql db with the instance name I cannot connect due to adding the default port number. Based on the docs from Microsoft:

For optimal connection performance, you should set the portNumber when you connect to a named instance. This will avoid a round trip to the server to determine the port number. If both a portNumber and instanceName are used, the portNumber will take precedence and the instanceName will be ignored.

While providing the url

sqlserver://myuser:mypass@host.ip/my_instance?database=master

The code is adding port number which is creating the connection issue

sqlserver://myuser:mypass@host.ip:1433/my_instance?database=master

I've tested uri with go-mssqldb and it worked properly

import (
    "database/sql"
    "log"
    "fmt"
    _ "[github.com/microsoft/go-mssqldb](http://github.com/microsoft/go-mssqldb)"
)
func main() {
    conn, err := sql.Open("sqlserver", "sqlserver://myuser:mypass@host.ip/my_instance?database=master")
    if err != nil {
        log.Fatal("Open connection failed:", err.Error())
    }
    defer conn.Close()
    err = conn.Ping()
    if err != nil {
        log.Fatal("Cannot connect: ", err.Error())
    }
    fmt.Println("Connected!")
}

For time being the workaround for that is to locally compile without this line https://github.com/slingdata-io/sling-cli/blob/main/core/dbio/database/database_sqlserver.go#L40

flarco commented 1 month ago

Thanks for raising this. Interesting, didn't know about the port number vs named instance logic for MSSQL.

flarco commented 1 month ago

Done, see https://github.com/slingdata-io/sling-cli/pull/303/commits/fe03d9cc5b5f09d8b55c27f1ca40970bca975e28 Feel free to test and reporting any issues. Closing.