From 0b856d1a75915c56420b88ced5a6ebe3b1589b5a Mon Sep 17 00:00:00 2001 From: Alex Suraci Date: Tue, 20 Nov 2018 10:22:39 -0500 Subject: [PATCH] pg: support host:port for backwards-compatibility --- storage/sql/config.go | 21 +++++++++++++++++---- storage/sql/config_test.go | 7 +++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/storage/sql/config.go b/storage/sql/config.go index 3be63b30..701df926 100644 --- a/storage/sql/config.go +++ b/storage/sql/config.go @@ -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 { + port = strconv.Itoa(int(p.Port)) + } } - if p.Port != 0 { - addParam("port", strconv.Itoa(int(p.Port))) + if host != "" { + addParam("host", dataSourceStr(host)) + } + + if port != "" { + addParam("port", port) } if p.User != "" { diff --git a/storage/sql/config_test.go b/storage/sql/config_test.go index 972fe973..d823e099 100644 --- a/storage/sql/config_test.go +++ b/storage/sql/config_test.go @@ -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{