mirror of
https://github.com/TECHNOFAB11/dbmate.git
synced 2025-12-11 23:50:04 +01:00
Add --url flag (#150)
This commit is contained in:
parent
d1b3334ff7
commit
a9aaaad1fb
3 changed files with 41 additions and 19 deletions
|
|
@ -312,6 +312,7 @@ Please note that the `wait` command does not verify whether your specified datab
|
||||||
|
|
||||||
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]`.
|
||||||
|
|
||||||
|
* `--url, -u "protocol://host:port/dbname"` - specify the database url directly.
|
||||||
* `--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.
|
||||||
* `--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.
|
||||||
|
|
@ -335,6 +336,14 @@ Creating: myapp_test
|
||||||
Applying: 20151127184807_create_users_table.sql
|
Applying: 20151127184807_create_users_table.sql
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Alternatively, you can specify the url directly on the command line:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ dbmate -u "postgres://postgres@127.0.0.1:5432/myapp_test?sslmode=disable" up
|
||||||
|
```
|
||||||
|
|
||||||
|
The only advantage of using `dbmate -e TEST_DATABASE_URL` over `dbmate -u $TEST_DATABASE_URL` is that the former takes advantage of dbmate's automatic `.env` file loading.
|
||||||
|
|
||||||
## FAQ
|
## FAQ
|
||||||
|
|
||||||
**How do I use dbmate under Alpine linux?**
|
**How do I use dbmate under Alpine linux?**
|
||||||
|
|
|
||||||
14
main.go
14
main.go
|
|
@ -34,6 +34,11 @@ func NewApp() *cli.App {
|
||||||
app.Version = dbmate.Version
|
app.Version = dbmate.Version
|
||||||
|
|
||||||
app.Flags = []cli.Flag{
|
app.Flags = []cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "url",
|
||||||
|
Aliases: []string{"u"},
|
||||||
|
Usage: "specify the database URL",
|
||||||
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "env",
|
Name: "env",
|
||||||
Aliases: []string{"e"},
|
Aliases: []string{"e"},
|
||||||
|
|
@ -220,10 +225,15 @@ func action(f func(*dbmate.DB, *cli.Context) error) cli.ActionFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// getDatabaseURL returns the current environment database url
|
// getDatabaseURL returns the current database url from cli flag or environment variable
|
||||||
func getDatabaseURL(c *cli.Context) (u *url.URL, err error) {
|
func getDatabaseURL(c *cli.Context) (u *url.URL, err error) {
|
||||||
|
// check --url flag first
|
||||||
|
value := c.String("url")
|
||||||
|
if value == "" {
|
||||||
|
// if empty, default to --env or DATABASE_URL
|
||||||
env := c.String("env")
|
env := c.String("env")
|
||||||
value := os.Getenv(env)
|
value = os.Getenv(env)
|
||||||
|
}
|
||||||
|
|
||||||
return url.Parse(value)
|
return url.Parse(value)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
35
main_test.go
35
main_test.go
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
|
@ -10,30 +9,34 @@ import (
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testContext(t *testing.T, u *url.URL) *cli.Context {
|
func TestGetDatabaseUrl(t *testing.T) {
|
||||||
err := os.Setenv("DATABASE_URL", u.String())
|
// set environment variables
|
||||||
require.NoError(t, err)
|
require.NoError(t, os.Setenv("DATABASE_URL", "foo://example.org/one"))
|
||||||
|
require.NoError(t, os.Setenv("CUSTOM_URL", "foo://example.org/two"))
|
||||||
|
|
||||||
app := NewApp()
|
app := NewApp()
|
||||||
flagset := flag.NewFlagSet(app.Name, flag.ContinueOnError)
|
flagset := flag.NewFlagSet(app.Name, flag.ContinueOnError)
|
||||||
for _, f := range app.Flags {
|
for _, f := range app.Flags {
|
||||||
_ = f.Apply(flagset)
|
require.NoError(t, f.Apply(flagset))
|
||||||
}
|
}
|
||||||
|
ctx := cli.NewContext(app, flagset, nil)
|
||||||
|
|
||||||
return cli.NewContext(app, flagset, nil)
|
// no flags defaults to DATABASE_URL
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetDatabaseUrl(t *testing.T) {
|
|
||||||
envURL, err := url.Parse("foo://example.org/db")
|
|
||||||
require.NoError(t, err)
|
|
||||||
ctx := testContext(t, envURL)
|
|
||||||
|
|
||||||
u, err := getDatabaseURL(ctx)
|
u, err := getDatabaseURL(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "foo://example.org/one", u.String())
|
||||||
|
|
||||||
require.Equal(t, "foo", u.Scheme)
|
// --env overwrites DATABASE_URL
|
||||||
require.Equal(t, "example.org", u.Host)
|
require.NoError(t, ctx.Set("env", "CUSTOM_URL"))
|
||||||
require.Equal(t, "/db", u.Path)
|
u, err = getDatabaseURL(ctx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "foo://example.org/two", u.String())
|
||||||
|
|
||||||
|
// --url takes precedence over preceding two options
|
||||||
|
require.NoError(t, ctx.Set("url", "foo://example.org/three"))
|
||||||
|
u, err = getDatabaseURL(ctx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "foo://example.org/three", u.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRedactLogString(t *testing.T) {
|
func TestRedactLogString(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue