Skip to content

Commit

Permalink
fix: dump schema for CockroachDB when not specifying DSN as URL
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Jun 26, 2024
1 parent 34676f8 commit ee915ed
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions dialect_cockroach.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"database/sql"
"fmt"
"io"
"net"
"net/url"
"os"
"os/exec"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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") {
Expand Down

0 comments on commit ee915ed

Please sign in to comment.