From ee915ed0c3b4db1572deaa52ddba5b00acef8b66 Mon Sep 17 00:00:00 2001 From: Arne Luenser Date: Wed, 26 Jun 2024 21:14:55 +0200 Subject: [PATCH] fix: dump schema for CockroachDB when not specifying DSN as URL --- dialect_cockroach.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/dialect_cockroach.go b/dialect_cockroach.go index 77a080b8..db98f3d2 100644 --- a/dialect_cockroach.go +++ b/dialect_cockroach.go @@ -6,6 +6,7 @@ import ( "database/sql" "fmt" "io" + "net" "net/url" "os" "os/exec" @@ -217,8 +218,18 @@ func (p *cockroach) URL() string { if c.URL != "" { return c.URL } - s := "postgres://%s:%s@%s:%s/%s?%s" - return fmt.Sprintf(s, c.User, url.QueryEscape(c.Password), c.Host, c.Port, c.Database, c.OptionsString("")) + s := url.URL{ + Scheme: "postgres", + User: url.UserPassword(c.User, c.Password), + Host: net.JoinHostPort(c.Host, c.Port), + Path: "/" + c.Database, + } + q := url.Values{} + for k, v := range c.Options { + q.Set(k, v) + } + s.RawQuery = q.Encode() + return s.String() } func (p *cockroach) urlWithoutDb() string { @@ -249,7 +260,7 @@ func (p *cockroach) FizzTranslator() fizz.Translator { } func (p *cockroach) DumpSchema(w io.Writer) error { - cmd := exec.Command("cockroach", "sql", "--url", p.Details().URL, "-e", "SHOW CREATE ALL TABLES", "--format", "raw") + cmd := exec.Command("cockroach", "sql", "--url", p.URL(), "-e", "SHOW CREATE ALL TABLES", "--format", "raw") c := p.ConnectionDetails if defaults.String(c.option("sslmode"), "disable") == "disable" || strings.Contains(c.RawOptions, "sslmode=disable") {