mirror of
https://github.com/TECHNOFAB11/dbmate.git
synced 2025-12-11 23:50:04 +01:00
Allow database URL to be specified via command line
This commit is contained in:
parent
99a4f266e6
commit
ece5d3cf0e
4 changed files with 36 additions and 9 deletions
13
commands.go
13
commands.go
|
|
@ -16,7 +16,7 @@ import (
|
|||
|
||||
// CreateCommand creates the current database
|
||||
func CreateCommand(ctx *cli.Context) error {
|
||||
u, err := GetDatabaseURL()
|
||||
u, err := GetDatabaseURL(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -31,7 +31,7 @@ func CreateCommand(ctx *cli.Context) error {
|
|||
|
||||
// DropCommand drops the current database (if it exists)
|
||||
func DropCommand(ctx *cli.Context) error {
|
||||
u, err := GetDatabaseURL()
|
||||
u, err := GetDatabaseURL(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -86,8 +86,11 @@ func NewCommand(ctx *cli.Context) error {
|
|||
}
|
||||
|
||||
// GetDatabaseURL returns the current environment database url
|
||||
func GetDatabaseURL() (u *url.URL, err error) {
|
||||
return url.Parse(os.Getenv("DATABASE_URL"))
|
||||
func GetDatabaseURL(ctx *cli.Context) (u *url.URL, err error) {
|
||||
env := ctx.GlobalString("env")
|
||||
value := os.Getenv(env)
|
||||
|
||||
return url.Parse(value)
|
||||
}
|
||||
|
||||
func doTransaction(db *sql.DB, txFunc func(shared.Transaction) error) error {
|
||||
|
|
@ -116,7 +119,7 @@ func MigrateCommand(ctx *cli.Context) error {
|
|||
return fmt.Errorf("No migration files found.")
|
||||
}
|
||||
|
||||
u, err := GetDatabaseURL()
|
||||
u, err := GetDatabaseURL(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,29 @@
|
|||
package main_test
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/adrianmacneil/dbmate"
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/stretchr/testify/require"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetDatabaseUrl(t *testing.T) {
|
||||
func newContext() *cli.Context {
|
||||
app := main.NewApp()
|
||||
flagset := flag.NewFlagSet(app.Name, flag.ContinueOnError)
|
||||
for _, f := range app.Flags {
|
||||
f.Apply(flagset)
|
||||
}
|
||||
|
||||
return cli.NewContext(app, flagset, nil)
|
||||
}
|
||||
|
||||
func TestGetDatabaseUrl_Default(t *testing.T) {
|
||||
os.Setenv("DATABASE_URL", "postgres://example.org/db")
|
||||
|
||||
u, err := main.GetDatabaseURL()
|
||||
ctx := newContext()
|
||||
u, err := main.GetDatabaseURL(ctx)
|
||||
require.Nil(t, err)
|
||||
|
||||
require.Equal(t, "postgres", u.Scheme)
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import (
|
|||
|
||||
func testURL(t *testing.T) *url.URL {
|
||||
str := os.Getenv("POSTGRES_PORT")
|
||||
require.NotEmpty(t, str)
|
||||
require.NotEmpty(t, str, "missing POSTGRES_PORT environment variable")
|
||||
|
||||
u, err := url.Parse(str)
|
||||
require.Nil(t, err)
|
||||
|
|
|
|||
13
main.go
13
main.go
|
|
@ -11,6 +11,12 @@ import (
|
|||
func main() {
|
||||
loadDotEnv()
|
||||
|
||||
app := NewApp()
|
||||
app.Run(os.Args)
|
||||
}
|
||||
|
||||
// NewApp creates a new command line app
|
||||
func NewApp() *cli.App {
|
||||
app := cli.NewApp()
|
||||
app.Name = "dbmate"
|
||||
app.Usage = "A lightweight, framework-independent database migration tool."
|
||||
|
|
@ -21,6 +27,11 @@ func main() {
|
|||
Value: "./db/migrations",
|
||||
Usage: "specify the directory containing migration files",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "env, e",
|
||||
Value: "DATABASE_URL",
|
||||
Usage: "specify an environment variable containing the database URL",
|
||||
},
|
||||
}
|
||||
|
||||
app.Commands = []cli.Command{
|
||||
|
|
@ -54,7 +65,7 @@ func main() {
|
|||
},
|
||||
}
|
||||
|
||||
app.Run(os.Args)
|
||||
return app
|
||||
}
|
||||
|
||||
type command func(*cli.Context) error
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue