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 d21e8b5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
10 changes: 6 additions & 4 deletions connection_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,18 @@ func (cd *ConnectionDetails) withURL() error {
}
cd.Database = strings.TrimPrefix(u.Path, "/")

hp := strings.Split(u.Host, ":")
cd.Host = hp[0]
if len(hp) > 1 {
cd.Port = hp[1]
cd.Host = u.Hostname()
if u.Port() != "" {
cd.Port = u.Port()
}

if u.User != nil {
cd.User = u.User.Username()
cd.Password, _ = u.User.Password()
}
for k, v := range u.Query() {
cd.setOption(k, v[0])
}
cd.RawOptions = u.RawQuery

return nil
Expand Down
36 changes: 26 additions & 10 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 @@ -213,18 +214,31 @@ func (p *cockroach) DropDB() error {
}

func (p *cockroach) URL() string {
if p.ConnectionDetails.URL != "" {
return p.ConnectionDetails.URL
}
return p.url().String()
}

func (p *cockroach) url() *url.URL {
c := p.ConnectionDetails
if c.URL != "" {
return c.URL
q := url.Values{}
for k, v := range c.Options {
q.Set(k, v)
}
return &url.URL{
Scheme: "postgres",
User: url.UserPassword(c.User, c.Password),
Host: net.JoinHostPort(c.Host, c.Port),
Path: "/" + c.Database,
RawQuery: q.Encode(),
}
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(""))
}

func (p *cockroach) urlWithoutDb() string {
c := p.ConnectionDetails
s := "postgres://%s:%s@%s:%s/?%s"
return fmt.Sprintf(s, c.User, url.QueryEscape(c.Password), c.Host, c.Port, c.OptionsString(""))
u := p.url()
u.Path = "/"
return u.String()
}

func (p *cockroach) MigrationURL() string {
Expand All @@ -249,7 +263,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 Expand Up @@ -358,8 +372,10 @@ func newCockroach(deets *ConnectionDetails) (dialect, error) {
}

func finalizerCockroach(cd *ConnectionDetails) {
appName := filepath.Base(os.Args[0])
cd.setOptionWithDefault("application_name", cd.option("application_name"), appName)
if cd.URL == "" {
appName := filepath.Base(os.Args[0])
cd.setOptionWithDefault("application_name", cd.option("application_name"), appName)
}
cd.Port = defaults.String(cd.Port, portCockroach)
if cd.URL != "" {
cd.URL = "postgres://" + trimCockroachPrefix(cd.URL)
Expand Down

0 comments on commit d21e8b5

Please sign in to comment.