Support both postgresql:// and posgres:// url prefix

This commit is contained in:
Dan Sosedoff 2015-07-14 22:42:46 -05:00
parent c12853d0b0
commit 785108002e
3 changed files with 14 additions and 2 deletions

View File

@ -81,6 +81,18 @@ func test_NewClientFromUrl(t *testing.T) {
assert.Equal(t, url, client.ConnectionString) assert.Equal(t, url, client.ConnectionString)
} }
func test_NewClientFromUrl2(t *testing.T) {
url := "postgresql://postgres@localhost/booktown?sslmode=disable"
client, err := NewFromUrl(url)
if err != nil {
defer client.Close()
}
assert.Equal(t, nil, err)
assert.Equal(t, url, client.ConnectionString)
}
func test_Test(t *testing.T) { func test_Test(t *testing.T) {
assert.Equal(t, nil, testClient.Test()) assert.Equal(t, nil, testClient.Test())
} }

View File

@ -28,7 +28,7 @@ func FormatUrl(opts command.Options) (string, error) {
url := opts.Url url := opts.Url
// Make sure to only accept urls in a standard format // Make sure to only accept urls in a standard format
if !strings.Contains(url, "postgres://") { if !strings.HasPrefix(url, "postgres://") && !strings.HasPrefix(url, "postgresql://") {
return "", errors.New("Invalid URL. Valid format: postgres://user:password@host:port/db?sslmode=mode") return "", errors.New("Invalid URL. Valid format: postgres://user:password@host:port/db?sslmode=mode")
} }

View File

@ -12,7 +12,7 @@ import (
func Test_Invalid_Url(t *testing.T) { func Test_Invalid_Url(t *testing.T) {
opts := command.Options{} opts := command.Options{}
examples := []string{ examples := []string{
"postgresql://foobar", "postgre://foobar",
"foobar", "foobar",
} }