Postgres defaults to unix socket (#230)

This commit is contained in:
Matthew Wraith 2021-12-17 16:44:14 -08:00 committed by GitHub
parent fb17e8eeca
commit 81fe01b34f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 5 deletions

View file

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/url" "net/url"
"runtime"
"strings" "strings"
"github.com/amacneil/dbmate/pkg/dbmate" "github.com/amacneil/dbmate/pkg/dbmate"
@ -48,7 +49,14 @@ func connectionString(u *url.URL) string {
// default hostname // default hostname
if hostname == "" { if hostname == "" {
hostname = "localhost" switch runtime.GOOS {
case "linux":
query.Set("host", "/var/run/postgresql")
case "darwin", "freebsd", "dragonfly", "openbsd", "netbsd":
query.Set("host", "/tmp")
default:
hostname = "localhost"
}
} }
// host param overrides url hostname // host param overrides url hostname

View file

@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
"net/url" "net/url"
"os" "os"
"runtime"
"testing" "testing"
"github.com/amacneil/dbmate/pkg/dbmate" "github.com/amacneil/dbmate/pkg/dbmate"
@ -50,13 +51,24 @@ func TestGetDriver(t *testing.T) {
require.Equal(t, "schema_migrations", drv.migrationsTableName) require.Equal(t, "schema_migrations", drv.migrationsTableName)
} }
func defaultConnString() string {
switch runtime.GOOS {
case "linux":
return "postgres://:5432/foo?host=%2Fvar%2Frun%2Fpostgresql"
case "darwin", "freebsd", "dragonfly", "openbsd", "netbsd":
return "postgres://:5432/foo?host=%2Ftmp"
default:
return "postgres://localhost:5432/foo"
}
}
func TestConnectionString(t *testing.T) { func TestConnectionString(t *testing.T) {
cases := []struct { cases := []struct {
input string input string
expected string expected string
}{ }{
// defaults // defaults
{"postgres:///foo", "postgres://localhost:5432/foo"}, {"postgres:///foo", defaultConnString()},
// support custom url params // support custom url params
{"postgres://bob:secret@myhost:1234/foo?bar=baz", "postgres://bob:secret@myhost:1234/foo?bar=baz"}, {"postgres://bob:secret@myhost:1234/foo?bar=baz", "postgres://bob:secret@myhost:1234/foo?bar=baz"},
// support `host` and `port` via url params // support `host` and `port` via url params
@ -85,11 +97,11 @@ func TestConnectionArgsForDump(t *testing.T) {
expected []string expected []string
}{ }{
// defaults // defaults
{"postgres:///foo", []string{"postgres://localhost:5432/foo"}}, {"postgres:///foo", []string{defaultConnString()}},
// support single schema // support single schema
{"postgres:///foo?search_path=foo", []string{"--schema", "foo", "postgres://localhost:5432/foo"}}, {"postgres:///foo?search_path=foo", []string{"--schema", "foo", defaultConnString()}},
// support multiple schemas // support multiple schemas
{"postgres:///foo?search_path=foo,public", []string{"--schema", "foo", "--schema", "public", "postgres://localhost:5432/foo"}}, {"postgres:///foo?search_path=foo,public", []string{"--schema", "foo", "--schema", "public", defaultConnString()}},
} }
for _, c := range cases { for _, c := range cases {