Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to apply missing (out-of-order) migrations #280

Merged
merged 16 commits into from
Oct 24, 2021
Merged
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")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep flip flopping on the name, I've narrowed it down to:

  • out-of-order
  • allow-missing

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going with "allow-missing" terminology, which is analogous to "out-of-order"

)

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