mirror of
https://github.com/TECHNOFAB11/dbmate.git
synced 2025-12-12 16:10:03 +01:00
Postgres defaults to unix socket (#230)
This commit is contained in:
parent
fb17e8eeca
commit
81fe01b34f
2 changed files with 25 additions and 5 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/amacneil/dbmate/pkg/dbmate"
|
||||
|
|
@ -48,7 +49,14 @@ func connectionString(u *url.URL) string {
|
|||
|
||||
// default 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
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"database/sql"
|
||||
"net/url"
|
||||
"os"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/amacneil/dbmate/pkg/dbmate"
|
||||
|
|
@ -50,13 +51,24 @@ func TestGetDriver(t *testing.T) {
|
|||
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) {
|
||||
cases := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
// defaults
|
||||
{"postgres:///foo", "postgres://localhost:5432/foo"},
|
||||
{"postgres:///foo", defaultConnString()},
|
||||
// support custom url params
|
||||
{"postgres://bob:secret@myhost:1234/foo?bar=baz", "postgres://bob:secret@myhost:1234/foo?bar=baz"},
|
||||
// support `host` and `port` via url params
|
||||
|
|
@ -85,11 +97,11 @@ func TestConnectionArgsForDump(t *testing.T) {
|
|||
expected []string
|
||||
}{
|
||||
// defaults
|
||||
{"postgres:///foo", []string{"postgres://localhost:5432/foo"}},
|
||||
{"postgres:///foo", []string{defaultConnString()}},
|
||||
// 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
|
||||
{"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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue