Skip to content

Commit

Permalink
working loading/dumping schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Mar 17, 2017
1 parent f77b588 commit c162f95
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pop
import (
"encoding/gob"
"fmt"
"io"

. "github.com/markbates/pop/columns"
"github.com/markbates/pop/fizz"
Expand All @@ -26,6 +27,8 @@ type dialect interface {
SelectMany(store, *Model, Query) error
CreateDB() error
DropDB() error
DumpSchema(io.Writer) error
LoadSchema(io.Reader) error
FizzTranslator() fizz.Translator
Lock(func() error) error
}
Expand Down
31 changes: 31 additions & 0 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package pop

import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"

_ "github.com/go-sql-driver/mysql"
Expand All @@ -12,6 +15,8 @@ import (
"github.com/pkg/errors"
)

var _ dialect = &mysql{}

type mysql struct {
ConnectionDetails *ConnectionDetails
}
Expand Down Expand Up @@ -85,6 +90,32 @@ func (m *mysql) Lock(fn func() error) error {
return fn()
}

func (m *mysql) DumpSchema(w io.Writer) error {
// mysqldump -d -h localhost -P 3306 -u root --password=root coke_development
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.Stdout = w
cmd.Stderr = os.Stderr
return cmd.Run()
}

func (m *mysql) LoadSchema(r io.Reader) error {
// mysql -u root --password=root -h localhost -P 3306 -D coke_test < schema.sql
tmp, err := ioutil.TempFile("", "mysql-dump")
if err != nil {
return err
}
defer os.Remove(tmp.Name())
_, err = io.Copy(tmp, r)
if err != nil {
return err
}

deets := m.Details()
cmd := exec.Command("mysql", "-d", "-h", deets.Host, "-P", deets.Port, "-u", deets.User, fmt.Sprintf("--password=%s", deets.Password), "-D", deets.Database, "<", tmp.Name())
return cmd.Run()
}

func newMySQL(deets *ConnectionDetails) dialect {
cd := &mysql{
ConnectionDetails: deets,
Expand Down
28 changes: 28 additions & 0 deletions postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package pop

import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strconv"
"sync"
Expand All @@ -14,6 +17,8 @@ import (
"github.com/pkg/errors"
)

var _ dialect = &postgresql{}

type postgresql struct {
translateCache map[string]string
mu sync.Mutex
Expand Down Expand Up @@ -133,6 +138,29 @@ func (p *postgresql) Lock(fn func() error) error {
return fn()
}

func (p *postgresql) DumpSchema(w io.Writer) error {
// pg_dump -s --dbname=postgresql://postgres:postgres@127.0.0.1:5432/papercall_development > schema.sql
cmd := exec.Command("pg_dump", "-s", fmt.Sprintf("--dbname=%s", p.URL()))
cmd.Stdout = w
cmd.Stderr = os.Stderr
return cmd.Run()
}

func (p *postgresql) LoadSchema(r io.Reader) error {
// psql postgresql://postgres:postgres@127.0.0.1:5432/papercall_test < schema.sql
tmp, err := ioutil.TempFile("", "postgres-dump")
if err != nil {
return err
}
defer os.Remove(tmp.Name())
_, err = io.Copy(tmp, r)
if err != nil {
return err
}
cmd := exec.Command("psql", p.URL(), "<", tmp.Name())
return cmd.Run()
}

func newPostgreSQL(deets *ConnectionDetails) dialect {
cd := &postgresql{
ConnectionDetails: deets,
Expand Down
24 changes: 24 additions & 0 deletions soda/cmd/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"fmt"

"github.com/markbates/pop/soda/cmd/schema"
"github.com/spf13/cobra"
)

// schemaCmd represents the schema command
var schemaCmd = &cobra.Command{
Use: "schema",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, args []string) {
// TODO: Work your own magic here
fmt.Println("schema called")
},
}

func init() {
schemaCmd.AddCommand(schema.LoadCmd)
schemaCmd.AddCommand(schema.DumpCmd)
RootCmd.AddCommand(schemaCmd)
}
29 changes: 29 additions & 0 deletions soda/cmd/schema/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package schema

import (
"os"

"github.com/markbates/pop"
"github.com/spf13/cobra"
)

var env string
var DumpCmd = &cobra.Command{
Use: "dump",
Short: "A brief description of your command",
RunE: func(cmd *cobra.Command, args []string) error {
c, err := pop.Connect(env)
if err != nil {
return err
}
err = c.Dialect.DumpSchema(os.Stdout)
if err != nil {
return err
}
return nil
},
}

func init() {
DumpCmd.Flags().StringVarP(&env, "env", "e", "development", "The environment you want to run schema against. Will use $GO_ENV if set.")
}
17 changes: 17 additions & 0 deletions soda/cmd/schema/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package schema

import (
"fmt"

"github.com/spf13/cobra"
)

// schema/loadCmd represents the schema/load command
var LoadCmd = &cobra.Command{
Use: "load",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, args []string) {
// TODO: Work your own magic here
fmt.Println("schema/load called")
},
}
29 changes: 29 additions & 0 deletions sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package pop

import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
Expand All @@ -14,6 +17,8 @@ import (
"github.com/pkg/errors"
)

var _ dialect = &sqlite{}

type sqlite struct {
gil *sync.Mutex
smGil *sync.Mutex
Expand Down Expand Up @@ -98,6 +103,30 @@ func (m *sqlite) FizzTranslator() fizz.Translator {
return translators.NewSQLite(m.Details().Database)
}

func (m *sqlite) DumpSchema(w io.Writer) error {
// sqlite3 development.sqlite .schema > schema.sql
cmd := exec.Command("sqlite3", m.URL(), ".schema")
cmd.Stdout = w
cmd.Stderr = os.Stdout
return cmd.Run()
}

func (m *sqlite) LoadSchema(r io.Reader) error {
// sqlite3 development.sqlite < schema.sql
tmp, err := ioutil.TempFile("", "sqlite-dump")
if err != nil {
return err
}
defer os.Remove(tmp.Name())
_, err = io.Copy(tmp, r)
if err != nil {
return err
}

cmd := exec.Command("sqlite3", m.URL(), "<", tmp.Name())
return cmd.Run()
}

func newSQLite(deets *ConnectionDetails) dialect {
deets.URL = fmt.Sprintf("sqlite3://%s", deets.Database)
cd := &sqlite{
Expand Down

0 comments on commit c162f95

Please sign in to comment.