forked from amacneil/dbmate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (138 loc) · 3.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"log"
"net/url"
"os"
"github.com/amacneil/dbmate/pkg/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: "env, e",
Value: "DATABASE_URL",
Usage: "specify an environment variable containing the database URL",
},
cli.StringFlag{
Name: "migrations-dir, d",
Value: dbmate.DefaultMigrationsDir,
Usage: "specify the directory containing migration files",
},
cli.StringFlag{
Name: "schema-file, s",
Value: dbmate.DefaultSchemaFile,
Usage: "specify the schema file location",
},
cli.BoolFlag{
Name: "no-dump-schema",
Usage: "don't update the schema file on migrate/rollback",
},
}
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.NewMigration(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.CreateAndMigrate()
}),
},
{
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()
}),
},
{
Name: "dump",
Usage: "Write the database schema to disk",
Action: action(func(db *dbmate.DB, c *cli.Context) error {
return db.DumpSchema()
}),
},
{
Name: "wait",
Usage: "Wait for the database to become available",
Action: action(func(db *dbmate.DB, c *cli.Context) error {
return db.Wait()
}),
},
}
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.Fatalf("Error loading .env file: %s", err.Error())
}
}
// 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.New(u)
db.AutoDumpSchema = !c.GlobalBool("no-dump-schema")
db.MigrationsDir = c.GlobalString("migrations-dir")
db.SchemaFile = c.GlobalString("schema-file")
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)
}