Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update url connection-string handling to work with modern pq #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ func (t timeoutDriver) Open(connection string) (_ driver.Conn, err error) {
for _, setting := range strings.Fields(connection) {
s := strings.Split(setting, "=")
if s[0] == "read_timeout" {
val, err := strconv.Atoi(s[1])
trimmed := strings.Trim(s[1], "'")
val, err := strconv.Atoi(trimmed)
if err != nil {
return nil, fmt.Errorf("Error interpreting value for read_timeout")
return nil, fmt.Errorf("Error interpreting value %#v for read_timeout", trimmed)
}
readTimeout = time.Duration(val) * time.Millisecond // timeout is in milliseconds
} else if s[0] == "write_timeout" {
val, err := strconv.Atoi(s[1])
trimmed := strings.Trim(s[1], "'")
val, err := strconv.Atoi(trimmed)
if err != nil {
return nil, fmt.Errorf("Error interpreting value for write_timeout")
return nil, fmt.Errorf("Error interpreting value %#v for write_timeout", trimmed)
}
writeTimeout = time.Duration(val) * time.Millisecond // timeout is in milliseconds
} else {
Expand Down
12 changes: 8 additions & 4 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestOpenTimeoutsAddedWriteError(t *testing.T) {
t.Error("An error was expected")
}

if err.Error() != "Error interpreting value for write_timeout" {
if err.Error() != "Error interpreting value \"seven\" for write_timeout" {
t.Errorf("The error is unexpected: %q", err.Error())
}

Expand All @@ -148,7 +148,7 @@ func TestOpenTimeoutsAddedReadError(t *testing.T) {
t.Error("An error was expected")
}

if err.Error() != "Error interpreting value for read_timeout" {
if err.Error() != "Error interpreting value \"\" for read_timeout" {
t.Errorf("The error is unexpected: %q", err.Error())
}

Expand All @@ -175,7 +175,9 @@ func TestPostgresURL(t *testing.T) {
t.Error("Unexpected error")
}

if connection != "dbname=pqtest host=localhost password=password sslmode=verify-full user=pqtest" {
// may be quoted depending on pq version
if connection != "dbname=pqtest host=localhost password=password sslmode=verify-full user=pqtest" &&
connection != "dbname='pqtest' host='localhost' password='password' sslmode='verify-full' user='pqtest'" {
t.Errorf("The connection string was not as expected: %q", connection)
}

Expand Down Expand Up @@ -209,7 +211,9 @@ func TestPostgresqlURLError(t *testing.T) {
t.Error("An error was expected")
}

if err.Error() != "parse postgresql://pqtest\\\\/:password@localhost/pqtest?read_timeout=500&sslmode=verify-full&write_timeout=100: invalid character \"\\\\\" in host name" {
// may be quoted depending on pq version
if err.Error() != "parse postgresql://pqtest\\\\/:password@localhost/pqtest?read_timeout=500&sslmode=verify-full&write_timeout=100: invalid character \"\\\\\" in host name" &&
err.Error() != "parse \"postgresql://pqtest\\\\\\\\/:password@localhost/pqtest?read_timeout=500&sslmode=verify-full&write_timeout=100\": invalid character \"\\\\\" in host name" {
t.Errorf("The error was not as expected: %q", err.Error())
}

Expand Down