Skip to content

Commit

Permalink
feat: introduce GOOSE_MIGRATION_TABLE env variable overiden by `-ta…
Browse files Browse the repository at this point in the history
…ble` arg
  • Loading branch information
smoke committed Dec 26, 2023
1 parent d5ed18d commit bd7b53c
Show file tree
Hide file tree
Showing 2 changed files with 17 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 @@ -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")
Expand Down Expand Up @@ -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()
Expand All @@ -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 != "" {
Expand Down
8 changes: 6 additions & 2 deletions internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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},
}
}

Expand Down

0 comments on commit bd7b53c

Please sign in to comment.