-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate cmd package for easier importing
- Loading branch information
Showing
21 changed files
with
407 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
/dist | ||
.DS_Store | ||
dbmate | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/amacneil/dbmate" | ||
"github.com/joho/godotenv" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func main() { | ||
loadDotEnv() | ||
|
||
app := NewApp() | ||
err := app.Run(os.Args) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error: %s\n", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// 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." | ||
app.Version = dbmate.Version | ||
|
||
app.Flags = []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "migrations-dir, d", | ||
Value: dbmate.DefaultMigrationsDir, | ||
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{ | ||
{ | ||
Name: "new", | ||
Aliases: []string{"n"}, | ||
Usage: "Generate a new migration file", | ||
Action: action(func(db *dbmate.DB, c *cli.Context) error { | ||
name := c.Args().First() | ||
return db.New(name) | ||
}), | ||
}, | ||
{ | ||
Name: "up", | ||
Usage: "Create database (if necessary) and migrate to the latest version", | ||
Action: action(func(db *dbmate.DB, c *cli.Context) error { | ||
return db.Up() | ||
}), | ||
}, | ||
{ | ||
Name: "create", | ||
Usage: "Create database", | ||
Action: action(func(db *dbmate.DB, c *cli.Context) error { | ||
return db.Create() | ||
}), | ||
}, | ||
{ | ||
Name: "drop", | ||
Usage: "Drop database (if it exists)", | ||
Action: action(func(db *dbmate.DB, c *cli.Context) error { | ||
return db.Drop() | ||
}), | ||
}, | ||
{ | ||
Name: "migrate", | ||
Usage: "Migrate to the latest version", | ||
Action: action(func(db *dbmate.DB, c *cli.Context) error { | ||
return db.Migrate() | ||
}), | ||
}, | ||
{ | ||
Name: "rollback", | ||
Aliases: []string{"down"}, | ||
Usage: "Rollback the most recent migration", | ||
Action: action(func(db *dbmate.DB, c *cli.Context) error { | ||
return db.Rollback() | ||
}), | ||
}, | ||
} | ||
|
||
return app | ||
} | ||
|
||
// load environment variables from .env file | ||
func loadDotEnv() { | ||
if _, err := os.Stat(".env"); err != nil { | ||
return | ||
} | ||
|
||
if err := godotenv.Load(); err != nil { | ||
log.Fatal("Error loading .env file") | ||
} | ||
} | ||
|
||
// action wraps a cli.ActionFunc with dbmate initialization logic | ||
func action(f func(*dbmate.DB, *cli.Context) error) cli.ActionFunc { | ||
return func(c *cli.Context) error { | ||
u, err := getDatabaseURL(c) | ||
if err != nil { | ||
return err | ||
} | ||
db := dbmate.NewDB(u) | ||
db.MigrationsDir = c.GlobalString("migrations-dir") | ||
|
||
return f(db, c) | ||
} | ||
} | ||
|
||
// getDatabaseURL returns the current environment database url | ||
func getDatabaseURL(c *cli.Context) (u *url.URL, err error) { | ||
env := c.GlobalString("env") | ||
value := os.Getenv(env) | ||
|
||
return url.Parse(value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"net/url" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func testContext(t *testing.T, u *url.URL) *cli.Context { | ||
var err error | ||
|
||
err = os.Setenv("DATABASE_URL", u.String()) | ||
require.Nil(t, err) | ||
|
||
app := NewApp() | ||
flagset := flag.NewFlagSet(app.Name, flag.ContinueOnError) | ||
for _, f := range app.Flags { | ||
f.Apply(flagset) | ||
} | ||
|
||
return cli.NewContext(app, flagset, nil) | ||
} | ||
|
||
func TestGetDatabaseUrl(t *testing.T) { | ||
envURL, err := url.Parse("foo://example.org/db") | ||
require.Nil(t, err) | ||
ctx := testContext(t, envURL) | ||
|
||
u, err := getDatabaseURL(ctx) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, "foo", u.Scheme) | ||
require.Equal(t, "example.org", u.Host) | ||
require.Equal(t, "/db", u.Path) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.