Skip to content

Commit

Permalink
Add the ability to apply missing (out-of-order) migrations (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Oct 24, 2021
1 parent 9f88133 commit 8ed5f63
Show file tree
Hide file tree
Showing 9 changed files with 672 additions and 51 deletions.
9 changes: 0 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ dist:
GOOS=windows GOARCH=amd64 go build -o ./bin/goose-windows64.exe ./cmd/goose
GOOS=windows GOARCH=386 go build -o ./bin/goose-windows386.exe ./cmd/goose

.PHONY: vendor
vendor:
mv _go.mod go.mod
mv _go.sum go.sum
GO111MODULE=on go build -o ./bin/goose ./cmd/goose
GO111MODULE=on go mod vendor && GO111MODULE=on go mod tidy
mv go.mod _go.mod
mv go.sum _go.sum

test-packages:
go test -v $$(go list ./... | grep -v -e /tests -e /bin -e /cmd -e /examples)

Expand Down
29 changes: 20 additions & 9 deletions cmd/goose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ 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)")
sequential = flags.Bool("s", false, "use sequential numbering for new migrations")
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")
allowMissing = flags.Bool("allow-missing", false, "applies missing (out-of-order) migrations")
)

var (
Expand Down Expand Up @@ -86,7 +87,17 @@ func main() {
arguments = append(arguments, args[3:]...)
}

if err := goose.Run(command, db, *dir, arguments...); err != nil {
options := []goose.OptionsFunc{}
if *allowMissing {
options = append(options, goose.WithAllowMissing())
}
if err := goose.RunWithOptions(
command,
db,
*dir,
arguments,
options...,
); err != nil {
log.Fatalf("goose run: %v", err)
}
}
Expand Down
15 changes: 12 additions & 3 deletions goose.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,22 @@ func SetBaseFS(fsys fs.FS) {

// Run runs a goose command.
func Run(command string, db *sql.DB, dir string, args ...string) error {
return run(command, db, dir, args)
}

// Run runs a goose command with options.
func RunWithOptions(command string, db *sql.DB, dir string, args []string, options ...OptionsFunc) error {
return run(command, db, dir, args, options...)
}

func run(command string, db *sql.DB, dir string, args []string, options ...OptionsFunc) error {
switch command {
case "up":
if err := Up(db, dir); err != nil {
if err := Up(db, dir, options...); err != nil {
return err
}
case "up-by-one":
if err := UpByOne(db, dir); err != nil {
if err := UpByOne(db, dir, options...); err != nil {
return err
}
case "up-to":
Expand All @@ -56,7 +65,7 @@ func Run(command string, db *sql.DB, dir string, args ...string) error {
if err != nil {
return fmt.Errorf("version must be a number (got '%s')", args[0])
}
if err := UpTo(db, dir, version); err != nil {
if err := UpTo(db, dir, version, options...); err != nil {
return err
}
case "create":
Expand Down

0 comments on commit 8ed5f63

Please sign in to comment.