Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix DumpSchema for MySQL and CockroachDB #833

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions dialect_cockroach.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pop

import (
"bufio"
"bytes"
"database/sql"
"fmt"
Expand All @@ -9,7 +10,6 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"

Expand Down Expand Up @@ -249,7 +249,7 @@ func (p *cockroach) FizzTranslator() fizz.Translator {
}

func (p *cockroach) DumpSchema(w io.Writer) error {
cmd := exec.Command("cockroach", "sql", "-e", "SHOW CREATE ALL TABLES", "-d", p.Details().Database, "--format", "raw")
cmd := exec.Command("cockroach", "sql", "--url", p.Details().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 All @@ -271,15 +271,24 @@ func cockroachDumpSchema(deets *ConnectionDetails, cmd *exec.Cmd, w io.Writer) e
return err
}

// --format raw returns comments prefixed with # which is invalid, so we make it a valid SQL comment.
result := regexp.MustCompile("(?m)^#").ReplaceAll(bb.Bytes(), []byte("-- #"))

if _, err := w.Write(result); err != nil {
var written int
s := bufio.NewScanner(&bb)
for s.Scan() {
// --format raw returns lots of useless comments starting with #
if bytes.HasPrefix(s.Bytes(), []byte{'#'}) {
continue
}
if n, err := fmt.Fprintln(w, s.Text()); err != nil {
return err
} else {
written += n
}
}
if err := s.Err(); err != nil {
return err
}

x := bytes.TrimSpace(result)
if len(x) == 0 {
if written == 0 {
return fmt.Errorf("unable to dump schema for %s", deets.Database)
}

Expand Down
2 changes: 1 addition & 1 deletion dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (m *mysql) FizzTranslator() fizz.Translator {

func (m *mysql) DumpSchema(w io.Writer) error {
deets := m.Details()
cmd := exec.Command("mysqldump", "-d", "-h", deets.Host, "-P", deets.Port, "-u", deets.User, fmt.Sprintf("--password=%s", deets.Password), deets.Database)
cmd := exec.Command("mysqldump", "--protocol", "TCP", "-d", "-h", deets.Host, "-P", deets.Port, "-u", deets.User, fmt.Sprintf("--password=%s", deets.Password), deets.Database)
if deets.Port == "socket" {
cmd = exec.Command("mysqldump", "-d", "-S", deets.Host, "-u", deets.User, fmt.Sprintf("--password=%s", deets.Password), deets.Database)
}
Expand Down
Loading