Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/format/pgre/pgre.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"net/url"
"strings"

"github.com/go-pg/pg/v10"
"github.com/vmkteam/pgdesigner/pkg/pgd"
Expand Down Expand Up @@ -50,7 +51,7 @@ func connectDB(dsn string) (*pg.DB, error) {

// IsDSN returns true if the input looks like a PostgreSQL DSN.
func IsDSN(s string) bool {
return len(s) > 11 && (s[:11] == "postgres://" || s[:13] == "postgresql://")
return strings.HasPrefix(s, "postgres://") || strings.HasPrefix(s, "postgresql://")
}

func parseDSN(dsn string) (*pg.Options, error) {
Expand Down
19 changes: 19 additions & 0 deletions pkg/format/pgre/pgre_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ func skipIfNoPG(t *testing.T) {
}
}

func TestIsDSN(t *testing.T) {
tests := []struct {
name string
in string
want bool
}{
{name: "postgres scheme", in: "postgres://localhost/db", want: true},
{name: "postgresql scheme", in: "postgresql://localhost/db", want: true},
{name: "plain path", in: "docs/apisrv.xml", want: false},
{name: "trailing dot 12 chars", in: "docs/apisrv.", want: false},
{name: "empty", in: "", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, IsDSN(tt.in))
})
}
}

func TestParseIndexColumns(t *testing.T) {
tests := []struct {
name string
Expand Down