From 785108002e4165d79644ba7c5c0bf90d742115c0 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Tue, 14 Jul 2015 22:42:46 -0500 Subject: [PATCH] Support both postgresql:// and posgres:// url prefix --- pkg/client/client_test.go | 12 ++++++++++++ pkg/connection/connection_string.go | 2 +- pkg/connection/connection_string_test.go | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index f94ab0c..a4157ca 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -81,6 +81,18 @@ func test_NewClientFromUrl(t *testing.T) { 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) { assert.Equal(t, nil, testClient.Test()) } diff --git a/pkg/connection/connection_string.go b/pkg/connection/connection_string.go index 9d95412..091e93f 100644 --- a/pkg/connection/connection_string.go +++ b/pkg/connection/connection_string.go @@ -28,7 +28,7 @@ func FormatUrl(opts command.Options) (string, error) { url := opts.Url // 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") } diff --git a/pkg/connection/connection_string_test.go b/pkg/connection/connection_string_test.go index 885da7c..a13b35e 100644 --- a/pkg/connection/connection_string_test.go +++ b/pkg/connection/connection_string_test.go @@ -12,7 +12,7 @@ import ( func Test_Invalid_Url(t *testing.T) { opts := command.Options{} examples := []string{ - "postgresql://foobar", + "postgre://foobar", "foobar", }