Skip to content

Commit

Permalink
add redoall command
Browse files Browse the repository at this point in the history
  • Loading branch information
nullbio committed May 28, 2017
1 parent 93a2494 commit 9551e8a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
37 changes: 35 additions & 2 deletions cmd/mig/redo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ import (

var redoCmd = &cobra.Command{
Use: "redo",
Short: "Re-run the latest migration",
Long: "Re-run the latest migration",
Short: "Down then up the latest migration",
Long: "Down then up the latest migration",
Example: `mig redo postgres "user=postgres dbname=postgres sslmode=disable"`,
RunE: redoRunE,
}

var redoAllCmd = &cobra.Command{
Use: "redo",
Short: "Down then up all migrations",
Long: "Down then up all migrations",
Example: `mig redoall postgres "user=postgres dbname=postgres sslmode=disable"`,
RunE: redoAllRunE,
}

func init() {
redoCmd.Flags().StringP("dir", "d", ".", "directory with migration files")

Expand All @@ -42,3 +50,28 @@ func redoRunE(cmd *cobra.Command, args []string) error {
fmt.Printf("Success %v\n", name)
return nil
}

func redoAllRunE(cmd *cobra.Command, args []string) error {
driver, conn, err := getConnArgs(args)
if err != nil {
return err
}

_, err = mig.DownAll(driver, conn, viper.GetString("dir"))
if err != nil {
return err
}

count, err := mig.Up(driver, conn, viper.GetString("dir"))
if err != nil {
return err
}

if count == 0 {
fmt.Printf("No migrations to run")
} else {
fmt.Printf("Success %d migrations\n", count)
}

return nil
}
26 changes: 13 additions & 13 deletions migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"testing"
)

func newMigration(v int64, src string) *Migration {
return &Migration{Version: v, Previous: -1, Next: -1, Source: src}
func newMigration(v int64, src string) *migration {
return &migration{version: v, previous: -1, next: -1, source: src}
}

func TestMigrationSort(t *testing.T) {

ms := Migrations{}
ms := migrations{}

// insert in any order
ms = append(ms, newMigration(20120000, "test"))
Expand All @@ -25,32 +25,32 @@ func TestMigrationSort(t *testing.T) {
validateMigrationSort(t, ms, sorted)
}

func validateMigrationSort(t *testing.T, ms Migrations, sorted []int64) {
func validateMigrationSort(t *testing.T, ms migrations, sorted []int64) {

for i, m := range ms {
if sorted[i] != m.Version {
if sorted[i] != m.version {
t.Error("incorrect sorted version")
}

var next, prev int64

if i == 0 {
prev = -1
next = ms[i+1].Version
next = ms[i+1].version
} else if i == len(ms)-1 {
prev = ms[i-1].Version
prev = ms[i-1].version
next = -1
} else {
prev = ms[i-1].Version
next = ms[i+1].Version
prev = ms[i-1].version
next = ms[i+1].version
}

if m.Next != next {
t.Errorf("mismatched Next. v: %v, got %v, wanted %v\n", m, m.Next, next)
if m.next != next {
t.Errorf("mismatched next. v: %v, got %v, wanted %v\n", m, m.next, next)
}

if m.Previous != prev {
t.Errorf("mismatched Previous v: %v, got %v, wanted %v\n", m, m.Previous, prev)
if m.previous != prev {
t.Errorf("mismatched previous v: %v, got %v, wanted %v\n", m, m.previous, prev)
}
}

Expand Down
5 changes: 4 additions & 1 deletion migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ func TestSplitStatements(t *testing.T) {
}

for _, test := range tests {
stmts := splitSQLStatements(strings.NewReader(test.sql), test.direction)
stmts, err := splitSQLStatements(strings.NewReader(test.sql), test.direction)
if err != nil {
t.Error(err)
}
if len(stmts) != test.count {
t.Errorf("incorrect number of stmts. got %v, want %v", len(stmts), test.count)
}
Expand Down

0 comments on commit 9551e8a

Please sign in to comment.