forked from mystiq/dex
pg: support host:port for backwards-compatibility
This commit is contained in:
parent
b1fd2fa8b1
commit
0b856d1a75
2 changed files with 24 additions and 4 deletions
|
@ -3,6 +3,7 @@ package sql
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -116,12 +117,24 @@ func (p *Postgres) createDataSourceName() string {
|
||||||
|
|
||||||
addParam("connect_timeout", strconv.Itoa(p.ConnectionTimeout))
|
addParam("connect_timeout", strconv.Itoa(p.ConnectionTimeout))
|
||||||
|
|
||||||
if p.Host != "" {
|
// detect host:port for backwards-compatibility
|
||||||
addParam("host", dataSourceStr(p.Host))
|
host, port, err := net.SplitHostPort(p.Host)
|
||||||
|
if err != nil {
|
||||||
|
// not host:port, probably unix socket or bare address
|
||||||
|
|
||||||
|
host = p.Host
|
||||||
|
|
||||||
|
if p.Port != 0 {
|
||||||
|
port = strconv.Itoa(int(p.Port))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Port != 0 {
|
if host != "" {
|
||||||
addParam("port", strconv.Itoa(int(p.Port)))
|
addParam("host", dataSourceStr(host))
|
||||||
|
}
|
||||||
|
|
||||||
|
if port != "" {
|
||||||
|
addParam("port", port)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.User != "" {
|
if p.User != "" {
|
||||||
|
|
|
@ -119,6 +119,13 @@ func TestCreateDataSourceName(t *testing.T) {
|
||||||
},
|
},
|
||||||
expected: "connect_timeout=0 host='coreos.com' sslmode='disable'",
|
expected: "connect_timeout=0 host='coreos.com' sslmode='disable'",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "with tcp host:port",
|
||||||
|
input: &Postgres{
|
||||||
|
Host: "coreos.com:6543",
|
||||||
|
},
|
||||||
|
expected: "connect_timeout=0 host='coreos.com' port=6543 sslmode='verify-full'",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
description: "with tcp host and port",
|
description: "with tcp host and port",
|
||||||
input: &Postgres{
|
input: &Postgres{
|
||||||
|
|
Loading…
Reference in a new issue