From bd7b53c0d33a31f6ffe05d64c74d6efa66c4acfd Mon Sep 17 00:00:00 2001 From: Radoslav Kirilov Date: Wed, 11 Oct 2023 00:35:08 +0300 Subject: [PATCH] feat: introduce `GOOSE_MIGRATION_TABLE` env variable overiden by `-table` arg --- cmd/goose/main.go | 18 +++++++++++------- internal/cfg/cfg.go | 8 ++++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd/goose/main.go b/cmd/goose/main.go index c2518dfee..d2284cbcd 100644 --- a/cmd/goose/main.go +++ b/cmd/goose/main.go @@ -26,7 +26,7 @@ import ( var ( flags = flag.NewFlagSet("goose", flag.ExitOnError) dir = flags.String("dir", cfg.DefaultMigrationDir, "directory with migration files") - table = flags.String("table", "goose_db_version", "migrations table name") + table = flags.String("table", cfg.DefaultMigrationTable, "migrations table name") verbose = flags.Bool("v", false, "enable verbose mode") help = flags.Bool("h", false, "print help") versionFlag = flags.Bool("version", false, "print version") @@ -66,6 +66,16 @@ func main() { if *sequential { goose.SetSequential(true) } + + // read the `.env` or whichever file is pointed, skipping any error + _ = godotenv.Load(*envFile) + + // load the cfg from the environment variables + cfg.Load() + + if *table == cfg.DefaultMigrationTable || *table == "" { + *table = cfg.GOOSEMIGRATIONTABLE + } goose.SetTableName(*table) args := flags.Args() @@ -80,12 +90,6 @@ func main() { os.Exit(1) } - // read the `.env` or whichever file is pointed, skipping any error - _ = godotenv.Load(*envFile) - - // load the cfg from the environment variables - cfg.Load() - // The -dir option has not been set, check whether the env variable is set // before defaulting to ".". if *dir == cfg.DefaultMigrationDir && cfg.GOOSEMIGRATIONDIR != "" { diff --git a/internal/cfg/cfg.go b/internal/cfg/cfg.go index e0fe8fc0f..c28753e89 100644 --- a/internal/cfg/cfg.go +++ b/internal/cfg/cfg.go @@ -7,11 +7,13 @@ var ( GOOSEDBSTRING = "" GOOSEMIGRATIONDIR = DefaultMigrationDir // https://no-color.org/ - GOOSENOCOLOR = "false" + GOOSENOCOLOR = "false" + GOOSEMIGRATIONTABLE = DefaultMigrationTable ) var ( - DefaultMigrationDir = "." + DefaultMigrationDir = "." + DefaultMigrationTable = "goose_db_version" ) // Load reads the config values from environment, @@ -22,6 +24,7 @@ func Load() { GOOSEMIGRATIONDIR = envOr("GOOSE_MIGRATION_DIR", GOOSEMIGRATIONDIR) // https://no-color.org/ GOOSENOCOLOR = envOr("NO_COLOR", GOOSENOCOLOR) + GOOSEMIGRATIONTABLE = envOr("GOOSE_MIGRATION_TABLE", GOOSEMIGRATIONTABLE) } // An EnvVar is an environment variable Name=Value. @@ -36,6 +39,7 @@ func List() []EnvVar { {Name: "GOOSE_DBSTRING", Value: GOOSEDBSTRING}, {Name: "GOOSE_MIGRATION_DIR", Value: GOOSEMIGRATIONDIR}, {Name: "NO_COLOR", Value: GOOSENOCOLOR}, + {Name: "GOOSE_MIGRATION_TABLE", Value: GOOSEMIGRATIONTABLE}, } }