Skip to content

Commit

Permalink
refactor: Remove DumpSchema
Browse files Browse the repository at this point in the history
This feature adds too much complexity to the drivers.

Existing commands such as pg_dump and mysqldump do a better job of
capturing the myriad of input flags. So, removing all of this feels
pretty liberating right now.

Fun fact: All the mysql driver tests pass now.
  • Loading branch information
rafaelespinoza committed Apr 19, 2021
1 parent 1dc54d9 commit 69e9fc4
Show file tree
Hide file tree
Showing 8 changed files with 0 additions and 93 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ godfish rollback
# rollback and re-apply the last migration
godfish remigrate

godfish dump-schema > db/schema.sql

# show build metadata
godfish version
godfish version -json
Expand Down
2 changes: 0 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ type Driver interface {
// versions once they've been applied. The version should be a timestamp as
// a string, formatted as the TimeFormat variable in this package.
CreateSchemaMigrationsTable() error
// DumpSchema should output the database structure to stdout.
DumpSchema() error
// Execute runs the schema change and commits it to the database. The query
// parameter is a SQL string and may contain placeholders for the values in
// args. Input should be passed to conn so it could be sanitized, escaped.
Expand Down
22 changes: 0 additions & 22 deletions drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"database/sql"
"fmt"
"os"
"os/exec"
"regexp"
"strings"

Expand Down Expand Up @@ -115,27 +114,6 @@ func (d *driver) CreateSchemaMigrationsTable() (err error) {
return
}

func (d *driver) DumpSchema() (err error) {
params := d.dsn
cmd := exec.Command(
"mysqldump",
"--user", params.User, "--password="+params.Pass, // skip password prompt by a omitting space
"--host", params.Host, "--port", params.Port,
"--comments", "--no-data", "--routines", "--triggers", "--tz-utc",
"--skip-add-drop-table", "--add-locks", "--create-options", "--set-charset",
params.Name,
)

out, err := cmd.Output()
if val, ok := err.(*exec.ExitError); ok {
fmt.Println(string(val.Stderr))
err = val
return
}
fmt.Println(string(out))
return
}

func (d *driver) AppliedVersions() (out godfish.AppliedVersions, err error) {
rows, err := d.connection.Query(
`SELECT migration_id FROM schema_migrations ORDER BY migration_id ASC`,
Expand Down
18 changes: 0 additions & 18 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package postgres

import (
"bytes"
"database/sql"
"fmt"
"os"
"os/exec"

"github.com/lib/pq"
"github.com/rafaelespinoza/godfish"
Expand Down Expand Up @@ -90,21 +87,6 @@ func (d *driver) CreateSchemaMigrationsTable() (err error) {
return
}

func (d *driver) DumpSchema() (err error) {
var out bytes.Buffer
cmd := exec.Command(
"pg_dump",
"--schema-only", "--no-acl", "--no-owner", "--no-password",
d.dsn.Name,
)
cmd.Stdout = &out
if err = cmd.Run(); err != nil {
return
}
fmt.Println(out.String())
return
}

func (d *driver) AppliedVersions() (out godfish.AppliedVersions, err error) {
rows, err := d.connection.Query(
`SELECT migration_id FROM schema_migrations ORDER BY migration_id ASC`,
Expand Down
9 changes: 0 additions & 9 deletions godfish.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,6 @@ func CreateSchemaMigrationsTable(driver Driver) (err error) {
return driver.CreateSchemaMigrationsTable()
}

// DumpSchema describes the database structure and outputs to standard out.
func DumpSchema(driver Driver) (err error) {
if _, err = driver.Connect(); err != nil {
return err
}
defer driver.Close()
return driver.DumpSchema()
}

// Info displays the outputs of various helper functions.
func Info(driver Driver, directoryPath string, direction Direction, finishAtVersion string) (err error) {
if _, err = driver.Connect(); err != nil {
Expand Down
22 changes: 0 additions & 22 deletions internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,28 +200,6 @@ var subcommands = map[string]*subcommand{
return migration.GenerateFiles()
},
},
"dump-schema": &subcommand{
description: "generate a sql file describing the db schema",
setup: func(a *arguments) *flag.FlagSet {
flags := flag.NewFlagSet("dump-schema", flag.ExitOnError)
flags.Usage = func() {
fmt.Printf(`Usage: %s dump-schema
Print a database structure file to standard output.`,
bin)
printFlagDefaults(flags)
}
return flags
},
run: func(a arguments) error {
driver, err := bootDriver(a.DSN)
if err != nil {
return err
}
godfish.DumpSchema(driver)
return nil
},
},
"info": &subcommand{
description: "output current state of migrations",
setup: func(a *arguments) *flag.FlagSet {
Expand Down
1 change: 0 additions & 1 deletion internal/stub/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func (d *Driver) CreateSchemaMigrationsTable() error {
}
return d.err
}
func (d *Driver) DumpSchema() error { return d.err }
func (d *Driver) Execute(q string, a ...interface{}) error {
if strings.Contains(q, "invalid SQL") {
return fmt.Errorf(q)
Expand Down
17 changes: 0 additions & 17 deletions internal/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,6 @@ func RunDriverTests(t *testing.T, dsn godfish.DSN) {
})
})

t.Run("DumpSchema", func(t *testing.T) {
path, err := setup(driver, t.Name(), _DefaultTestDriverStubs, "34560102030405")
if err != nil {
t.Errorf("could not setup test; %v", err)
return
}
defer teardown(driver, path, "foos", "bars")

err = godfish.DumpSchema(driver)
if err != nil {
t.Errorf(
"test %q %q; could not dump schema; %v",
driver.Name(), t.Name(), err,
)
}
})

t.Run("ApplyMigration", func(t *testing.T) {
// testSetupState describes the state of the database before calling
// ApplyMigration.
Expand Down

0 comments on commit 69e9fc4

Please sign in to comment.