Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Add GOOSE_MIGRATION_DIR env variable (pressly#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman authored Apr 28, 2022
1 parent c05d337 commit 1311a0e
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions cmd/goose/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

var (
flags = flag.NewFlagSet("goose", flag.ExitOnError)
dir = flags.String("dir", ".", "directory with migration files")
dir = flags.String("dir", defaultMigrationDir, "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")
Expand All @@ -27,7 +27,6 @@ var (
sslkey = flags.String("ssl-key", "", "file path to SSL key in pem format (only support on mysql)")
noVersioning = flags.Bool("no-versioning", false, "apply migration commands with no versioning, in file order, from directory pointed to")
)

var (
gooseVersion = ""
)
Expand Down Expand Up @@ -56,6 +55,11 @@ func main() {
flags.Usage()
return
}
// The -dir option has not been set, check whether the env variable is set
// before defaulting to ".".
if *dir == defaultMigrationDir && os.Getenv(envGooseMigrationDir) != "" {
*dir = os.Getenv(envGooseMigrationDir)
}

switch args[0] {
case "init":
Expand Down Expand Up @@ -123,8 +127,13 @@ func main() {
}

const (
envGooseDriver = "GOOSE_DRIVER"
envGooseDBString = "GOOSE_DBSTRING"
envGooseDriver = "GOOSE_DRIVER"
envGooseDBString = "GOOSE_DBSTRING"
envGooseMigrationDir = "GOOSE_MIGRATION_DIR"
)

const (
defaultMigrationDir = "."
)

func mergeArgs(args []string) []string {
Expand Down Expand Up @@ -234,7 +243,7 @@ SELECT 'down SQL query';

// initDir will create a directory with an empty SQL migration file.
func gooseInit(dir string) error {
if dir == "" || dir == "." {
if dir == "" || dir == defaultMigrationDir {
dir = "migrations"
}
_, err := os.Stat(dir)
Expand Down

0 comments on commit 1311a0e

Please sign in to comment.