Skip to content

Commit

Permalink
Merge pull request #215 from warrn/warrn/214
Browse files Browse the repository at this point in the history
Option '-s' causes CREATE to use sequential versioning
  • Loading branch information
mfridman authored Dec 8, 2020
2 parents 0fbe41d + 16a9d45 commit 7dd9c10
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 9 deletions.
18 changes: 11 additions & 7 deletions cmd/goose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
)

var (
flags = flag.NewFlagSet("goose", flag.ExitOnError)
dir = flags.String("dir", ".", "directory with migration files")
table = flags.String("table", "goose_db_version", "migrations table name")
verbose = flags.Bool("v", false, "enable verbose mode")
help = flags.Bool("h", false, "print help")
version = flags.Bool("version", false, "print version")
certfile = flags.String("certfile", "", "file path to root CA's certificates in pem format (only support on mysql)")
flags = flag.NewFlagSet("goose", flag.ExitOnError)
dir = flags.String("dir", ".", "directory with migration files")
table = flags.String("table", "goose_db_version", "migrations table name")
verbose = flags.Bool("v", false, "enable verbose mode")
help = flags.Bool("h", false, "print help")
version = flags.Bool("version", false, "print version")
certfile = flags.String("certfile", "", "file path to root CA's certificates in pem format (only support on mysql)")
sequential = flags.Bool("s", false, "use sequential numbering for new migrations")
)

func main() {
Expand All @@ -30,6 +31,9 @@ func main() {
if *verbose {
goose.SetVerbose(true)
}
if *sequential {
goose.SetSequential(true)
}
goose.SetTableName(*table)

args := flags.Args()
Expand Down
31 changes: 30 additions & 1 deletion create.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,38 @@ type tmplVars struct {
CamelName string
}

var (
sequential = false
)

// SetSequential set whether to use sequential versioning instead of timestamp based versioning
func SetSequential(s bool) {
sequential = s
}

// Create writes a new blank migration file.
func CreateWithTemplate(db *sql.DB, dir string, tmpl *template.Template, name, migrationType string) error {
version := time.Now().Format(timestampFormat)
var version string
if sequential {
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
if err != nil {
return err
}

vMigrations, err := migrations.versioned()
if err != nil {
return err
}

if last, err := vMigrations.Last(); err == nil {
version = fmt.Sprintf(seqVersionTemplate, last.Version+1)
} else {
version = fmt.Sprintf(seqVersionTemplate, int64(1))
}
} else {
version = time.Now().Format(timestampFormat)
}

filename := fmt.Sprintf("%v_%v.%v", version, snakeCase(name), migrationType)

if tmpl == nil {
Expand Down
55 changes: 55 additions & 0 deletions create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package goose

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"testing"
"time"
)

func TestSequential(t *testing.T) {
t.Parallel()

dir, err := ioutil.TempDir("", "tmptest")
if err != nil {
t.Fatal(err)
}

defer os.RemoveAll(dir) // clean up
defer os.Remove("./bin/create-goose") // clean up

commands := []string{
"go build -o ./bin/create-goose ./cmd/goose",
fmt.Sprintf("./bin/create-goose -s -dir=%s create create_table", dir),
fmt.Sprintf("./bin/create-goose -s -dir=%s create add_users", dir),
fmt.Sprintf("./bin/create-goose -s -dir=%s create add_indices", dir),
fmt.Sprintf("./bin/create-goose -s -dir=%s create update_users", dir),
}

for _, cmd := range commands {
args := strings.Split(cmd, " ")
time.Sleep(1 * time.Second)
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = os.Environ()
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("%s:\n%v\n\n%s", err, cmd, out)
}
}

files, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatal(err)
}

// check that the files are in order
for i, f := range files {
expected := fmt.Sprintf("%05v", i+1)
if !strings.HasPrefix(f.Name(), expected) {
t.Errorf("failed to find %s prefix in %s", expected, f.Name())
}
}
}
9 changes: 8 additions & 1 deletion fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

const seqVersionTemplate = "%05v"

func Fix(dir string) error {
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
if err != nil {
Expand All @@ -32,7 +34,12 @@ func Fix(dir string) error {
// fix filenames by replacing timestamps with sequential versions
for _, tsm := range tsMigrations {
oldPath := tsm.Source
newPath := strings.Replace(oldPath, fmt.Sprintf("%d", tsm.Version), fmt.Sprintf("%05v", version), 1)
newPath := strings.Replace(
oldPath,
fmt.Sprintf("%d", tsm.Version),
fmt.Sprintf(seqVersionTemplate, version),
1,
)

if err := os.Rename(oldPath, newPath); err != nil {
return err
Expand Down

0 comments on commit 7dd9c10

Please sign in to comment.