Merge pull request #1352 from concourse/pr/support-pq-host-port

pg: support host:port for backwards-compatibility
This commit is contained in:
Stephan Renatus 2018-11-20 16:38:56 +01:00 committed by GitHub
commit efb15205e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View file

@ -3,6 +3,7 @@ package sql
import (
"database/sql"
"fmt"
"net"
"regexp"
"strconv"
"strings"
@ -116,12 +117,24 @@ func (p *Postgres) createDataSourceName() string {
addParam("connect_timeout", strconv.Itoa(p.ConnectionTimeout))
if p.Host != "" {
addParam("host", dataSourceStr(p.Host))
}
// detect host:port for backwards-compatibility
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 {
addParam("port", strconv.Itoa(int(p.Port)))
port = strconv.Itoa(int(p.Port))
}
}
if host != "" {
addParam("host", dataSourceStr(host))
}
if port != "" {
addParam("port", port)
}
if p.User != "" {

View file

@ -119,6 +119,13 @@ func TestCreateDataSourceName(t *testing.T) {
},
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",
input: &Postgres{