Allow setting flags via environment variables (#152)

This commit is contained in:
Adrian Macneil 2020-08-08 11:50:45 -07:00 committed by GitHub
parent a9aaaad1fb
commit 9e2d1b8c3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 14 deletions

View file

@ -24,7 +24,7 @@ For a comparison between dbmate and other popular database schema migration tool
## Installation ## Installation
**OSX** **macOS**
Install using Homebrew: Install using Homebrew:
@ -310,14 +310,15 @@ Please note that the `wait` command does not verify whether your specified datab
### Options ### Options
The following command line options are available with all commands. You must use command line arguments in the order `dbmate [global options] command [command options]`. The following command line options are available with all commands. You must use command line arguments in the order `dbmate [global options] command [command options]`. Most options can also be configured via environment variables (and loaded from your `.env` file, which is helpful to share configuration between team members).
* `--url, -u "protocol://host:port/dbname"` - specify the database url directly. * `--url, -u "protocol://host:port/dbname"` - specify the database url directly. _(env: `$DATABASE_URL`)_
* `--env, -e "DATABASE_URL"` - specify an environment variable to read the database connection URL from. * `--env, -e "DATABASE_URL"` - specify an environment variable to read the database connection URL from.
* `--migrations-dir, -d "./db/migrations"` - where to keep the migration files. * `--migrations-dir, -d "./db/migrations"` - where to keep the migration files. _(env: `$DBMATE_MIGRATIONS_DIR`)_
* `--schema-file, -s "./db/schema.sql"` - a path to keep the schema.sql file. * `--schema-file, -s "./db/schema.sql"` - a path to keep the schema.sql file. _(env: `$DBMATE_SCHEMA_FILE`)_
* `--no-dump-schema` - don't auto-update the schema.sql file on migrate/rollback * `--no-dump-schema` - don't auto-update the schema.sql file on migrate/rollback _(env: `$DBMATE_NO_DUMP_SCHEMA`)_
* `--wait` - wait for the db to become available before executing the subsequent command * `--wait` - wait for the db to become available before executing the subsequent command _(env: `$DBMATE_WAIT`)_
* `--wait-timeout 60s` - timeout for --wait flag _(env: `$DBMATE_WAIT_TIMEOUT`)_
For example, before running your test suite, you may wish to drop and recreate the test database. One easy way to do this is to store your test database connection URL in the `TEST_DATABASE_URL` environment variable: For example, before running your test suite, you may wish to drop and recreate the test database. One easy way to do this is to store your test database connection URL in the `TEST_DATABASE_URL` environment variable:

22
main.go
View file

@ -48,27 +48,32 @@ func NewApp() *cli.App {
&cli.StringFlag{ &cli.StringFlag{
Name: "migrations-dir", Name: "migrations-dir",
Aliases: []string{"d"}, Aliases: []string{"d"},
EnvVars: []string{"DBMATE_MIGRATIONS_DIR"},
Value: dbmate.DefaultMigrationsDir, Value: dbmate.DefaultMigrationsDir,
Usage: "specify the directory containing migration files", Usage: "specify the directory containing migration files",
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "schema-file", Name: "schema-file",
Aliases: []string{"s"}, Aliases: []string{"s"},
EnvVars: []string{"DBMATE_SCHEMA_FILE"},
Value: dbmate.DefaultSchemaFile, Value: dbmate.DefaultSchemaFile,
Usage: "specify the schema file location", Usage: "specify the schema file location",
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "no-dump-schema", Name: "no-dump-schema",
Usage: "don't update the schema file on migrate/rollback", EnvVars: []string{"DBMATE_NO_DUMP_SCHEMA"},
Usage: "don't update the schema file on migrate/rollback",
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "wait", Name: "wait",
Usage: "wait for the db to become available before executing the subsequent command", EnvVars: []string{"DBMATE_WAIT"},
Usage: "wait for the db to become available before executing the subsequent command",
}, },
&cli.DurationFlag{ &cli.DurationFlag{
Name: "wait-timeout", Name: "wait-timeout",
Usage: "timeout for --wait flag", EnvVars: []string{"DBMATE_WAIT_TIMEOUT"},
Value: dbmate.DefaultWaitTimeout, Usage: "timeout for --wait flag",
Value: dbmate.DefaultWaitTimeout,
}, },
} }
@ -89,6 +94,7 @@ func NewApp() *cli.App {
&cli.BoolFlag{ &cli.BoolFlag{
Name: "verbose", Name: "verbose",
Aliases: []string{"v"}, Aliases: []string{"v"},
EnvVars: []string{"DBMATE_VERBOSE"},
Usage: "print the result of each statement execution", Usage: "print the result of each statement execution",
}, },
}, },
@ -118,6 +124,7 @@ func NewApp() *cli.App {
&cli.BoolFlag{ &cli.BoolFlag{
Name: "verbose", Name: "verbose",
Aliases: []string{"v"}, Aliases: []string{"v"},
EnvVars: []string{"DBMATE_VERBOSE"},
Usage: "print the result of each statement execution", Usage: "print the result of each statement execution",
}, },
}, },
@ -134,6 +141,7 @@ func NewApp() *cli.App {
&cli.BoolFlag{ &cli.BoolFlag{
Name: "verbose", Name: "verbose",
Aliases: []string{"v"}, Aliases: []string{"v"},
EnvVars: []string{"DBMATE_VERBOSE"},
Usage: "print the result of each statement execution", Usage: "print the result of each statement execution",
}, },
}, },