2015-11-25 10:57:58 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
loadDotEnv()
|
|
|
|
|
|
2015-11-25 11:32:13 -08:00
|
|
|
app := NewApp()
|
2016-05-21 22:33:42 -07:00
|
|
|
err := app.Run(os.Args)
|
2016-08-07 22:40:59 -07:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2015-11-25 11:32:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewApp creates a new command line app
|
|
|
|
|
func NewApp() *cli.App {
|
2015-11-25 10:57:58 -08:00
|
|
|
app := cli.NewApp()
|
|
|
|
|
app.Name = "dbmate"
|
|
|
|
|
app.Usage = "A lightweight, framework-independent database migration tool."
|
2015-12-01 18:17:38 -08:00
|
|
|
app.Version = Version
|
2015-11-25 10:57:58 -08:00
|
|
|
|
|
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
|
cli.StringFlag{
|
|
|
|
|
Name: "migrations-dir, d",
|
|
|
|
|
Value: "./db/migrations",
|
|
|
|
|
Usage: "specify the directory containing migration files",
|
|
|
|
|
},
|
2015-11-25 11:32:13 -08:00
|
|
|
cli.StringFlag{
|
|
|
|
|
Name: "env, e",
|
|
|
|
|
Value: "DATABASE_URL",
|
|
|
|
|
Usage: "specify an environment variable containing the database URL",
|
|
|
|
|
},
|
2015-11-25 10:57:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.Commands = []cli.Command{
|
|
|
|
|
{
|
2016-08-07 23:25:35 -07:00
|
|
|
Name: "new",
|
|
|
|
|
Aliases: []string{"n"},
|
|
|
|
|
Usage: "Generate a new migration file",
|
|
|
|
|
Action: NewCommand,
|
2015-11-25 12:26:57 -08:00
|
|
|
},
|
2015-11-25 10:57:58 -08:00
|
|
|
{
|
2016-08-07 22:40:59 -07:00
|
|
|
Name: "up",
|
|
|
|
|
Usage: "Create database (if necessary) and migrate to the latest version",
|
|
|
|
|
Action: UpCommand,
|
2015-11-25 10:57:58 -08:00
|
|
|
},
|
|
|
|
|
{
|
2016-08-07 22:40:59 -07:00
|
|
|
Name: "create",
|
|
|
|
|
Usage: "Create database",
|
|
|
|
|
Action: CreateCommand,
|
2015-11-25 10:57:58 -08:00
|
|
|
},
|
|
|
|
|
{
|
2016-08-07 22:40:59 -07:00
|
|
|
Name: "drop",
|
|
|
|
|
Usage: "Drop database (if it exists)",
|
|
|
|
|
Action: DropCommand,
|
2015-11-25 10:57:58 -08:00
|
|
|
},
|
2015-11-27 10:34:42 -08:00
|
|
|
{
|
2016-08-07 22:40:59 -07:00
|
|
|
Name: "migrate",
|
|
|
|
|
Usage: "Migrate to the latest version",
|
|
|
|
|
Action: MigrateCommand,
|
2015-11-27 10:34:42 -08:00
|
|
|
},
|
|
|
|
|
{
|
2016-07-27 08:44:26 -07:00
|
|
|
Name: "rollback",
|
|
|
|
|
Aliases: []string{"down"},
|
|
|
|
|
Usage: "Rollback the most recent migration",
|
|
|
|
|
Action: RollbackCommand,
|
2015-11-27 10:34:42 -08:00
|
|
|
},
|
2015-11-25 10:57:58 -08:00
|
|
|
}
|
|
|
|
|
|
2015-11-25 11:32:13 -08:00
|
|
|
return app
|
2015-11-25 10:57:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type command func(*cli.Context) error
|
|
|
|
|
|
|
|
|
|
func loadDotEnv() {
|
|
|
|
|
if _, err := os.Stat(".env"); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := godotenv.Load(); err != nil {
|
|
|
|
|
log.Fatal("Error loading .env file")
|
|
|
|
|
}
|
|
|
|
|
}
|