mirror of
https://github.com/TECHNOFAB11/dbmate.git
synced 2025-12-12 08:00:04 +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"
|
"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
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue