-
Notifications
You must be signed in to change notification settings - Fork 272
/
main.go
327 lines (299 loc) · 8 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"errors"
"fmt"
"net/url"
"os"
"regexp"
"github.com/joho/godotenv"
"github.com/urfave/cli/v2"
"github.com/amacneil/dbmate/v2/pkg/dbmate"
_ "github.com/amacneil/dbmate/v2/pkg/driver/bigquery"
_ "github.com/amacneil/dbmate/v2/pkg/driver/clickhouse"
_ "github.com/amacneil/dbmate/v2/pkg/driver/mysql"
_ "github.com/amacneil/dbmate/v2/pkg/driver/postgres"
)
func main() {
err := loadEnvFiles(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(3)
}
app := NewApp()
err = app.Run(os.Args)
if err != nil {
errText := redactLogString(fmt.Sprintf("Error: %s\n", err))
_, _ = fmt.Fprint(os.Stderr, errText)
os.Exit(2)
}
}
// 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
defaultDB := dbmate.New(nil)
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Usage: "specify the database URL",
},
&cli.StringFlag{
Name: "env",
Aliases: []string{"e"},
Value: "DATABASE_URL",
Usage: "specify an environment variable containing the database URL",
},
&cli.StringSliceFlag{
Name: "env-file",
Value: cli.NewStringSlice(".env"),
Usage: "specify a file to load environment variables from",
},
&cli.StringSliceFlag{
Name: "migrations-dir",
Aliases: []string{"d"},
EnvVars: []string{"DBMATE_MIGRATIONS_DIR"},
Value: cli.NewStringSlice(defaultDB.MigrationsDir[0]),
Usage: "specify the directory containing migration files",
},
&cli.StringFlag{
Name: "migrations-table",
EnvVars: []string{"DBMATE_MIGRATIONS_TABLE"},
Value: defaultDB.MigrationsTableName,
Usage: "specify the database table to record migrations in",
},
&cli.StringFlag{
Name: "schema-file",
Aliases: []string{"s"},
EnvVars: []string{"DBMATE_SCHEMA_FILE"},
Value: defaultDB.SchemaFile,
Usage: "specify the schema file location",
},
&cli.BoolFlag{
Name: "no-dump-schema",
EnvVars: []string{"DBMATE_NO_DUMP_SCHEMA"},
Usage: "don't update the schema file on migrate/rollback",
},
&cli.BoolFlag{
Name: "wait",
EnvVars: []string{"DBMATE_WAIT"},
Usage: "wait for the db to become available before executing the subsequent command",
},
&cli.DurationFlag{
Name: "wait-timeout",
EnvVars: []string{"DBMATE_WAIT_TIMEOUT"},
Usage: "timeout for --wait flag",
Value: defaultDB.WaitTimeout,
},
}
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",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "strict",
EnvVars: []string{"DBMATE_STRICT"},
Usage: "fail if migrations would be applied out of order",
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
EnvVars: []string{"DBMATE_VERBOSE"},
Usage: "print the result of each statement execution",
},
},
Action: action(func(db *dbmate.DB, c *cli.Context) error {
db.Strict = c.Bool("strict")
db.Verbose = c.Bool("verbose")
return db.CreateAndMigrate()
}),
},
{
Name: "create",
Usage: "Create database",
Action: action(func(db *dbmate.DB, _ *cli.Context) error {
return db.Create()
}),
},
{
Name: "drop",
Usage: "Drop database (if it exists)",
Action: action(func(db *dbmate.DB, _ *cli.Context) error {
return db.Drop()
}),
},
{
Name: "migrate",
Usage: "Migrate to the latest version",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "strict",
EnvVars: []string{"DBMATE_STRICT"},
Usage: "fail if migrations would be applied out of order",
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
EnvVars: []string{"DBMATE_VERBOSE"},
Usage: "print the result of each statement execution",
},
},
Action: action(func(db *dbmate.DB, c *cli.Context) error {
db.Strict = c.Bool("strict")
db.Verbose = c.Bool("verbose")
return db.Migrate()
}),
},
{
Name: "rollback",
Aliases: []string{"down"},
Usage: "Rollback the most recent migration",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
EnvVars: []string{"DBMATE_VERBOSE"},
Usage: "print the result of each statement execution",
},
},
Action: action(func(db *dbmate.DB, c *cli.Context) error {
db.Verbose = c.Bool("verbose")
return db.Rollback()
}),
},
{
Name: "status",
Usage: "List applied and pending migrations",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "exit-code",
Usage: "return 1 if there are pending migrations",
},
&cli.BoolFlag{
Name: "quiet",
Usage: "don't output any text (implies --exit-code)",
},
},
Action: action(func(db *dbmate.DB, c *cli.Context) error {
db.Strict = c.Bool("strict")
setExitCode := c.Bool("exit-code")
quiet := c.Bool("quiet")
if quiet {
setExitCode = true
}
pending, err := db.Status(quiet)
if err != nil {
return err
}
if pending > 0 && setExitCode {
return cli.Exit("", 1)
}
return nil
}),
},
{
Name: "dump",
Usage: "Write the database schema to disk",
Action: action(func(db *dbmate.DB, _ *cli.Context) error {
return db.DumpSchema()
}),
},
{
Name: "load",
Usage: "Load schema file to the database",
Action: action(func(db *dbmate.DB, _ *cli.Context) error {
return db.LoadSchema()
}),
},
{
Name: "wait",
Usage: "Wait for the database to become available",
Action: action(func(db *dbmate.DB, _ *cli.Context) error {
return db.Wait()
}),
},
}
return app
}
// load environment variables from file(s)
func loadEnvFiles(args []string) error {
var envFiles []string
for i := 0; i < len(args); i++ {
if args[i] == "--env-file" {
if i+1 >= len(args) {
// returning nil here, even though it's an error
// because we want the caller to proceed anyway,
// and produce the actual arg parsing error response
return nil
}
envFiles = append(envFiles, args[i+1])
i++
}
}
if len(envFiles) == 0 {
envFiles = []string{".env"}
}
// try to load all files in sequential order,
// ignoring any that do not exist
for _, file := range envFiles {
err := godotenv.Load([]string{file}...)
if err == nil {
continue
}
var perr *os.PathError
if errors.As(err, &perr) && errors.Is(perr, os.ErrNotExist) {
// Ignoring file not found error
continue
}
return fmt.Errorf("loading env file(s) %v: %v", envFiles, err)
}
return nil
}
// 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.Bool("no-dump-schema")
db.MigrationsDir = c.StringSlice("migrations-dir")
db.MigrationsTableName = c.String("migrations-table")
db.SchemaFile = c.String("schema-file")
db.WaitBefore = c.Bool("wait")
waitTimeout := c.Duration("wait-timeout")
if waitTimeout != 0 {
db.WaitTimeout = waitTimeout
}
return f(db, c)
}
}
// getDatabaseURL returns the current database url from cli flag or environment variable
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")
value = os.Getenv(env)
}
return url.Parse(value)
}
// redactLogString attempts to redact passwords from errors
func redactLogString(in string) string {
re := regexp.MustCompile("([a-zA-Z]+://[^:]+:)[^@]+@")
return re.ReplaceAllString(in, "${1}********@")
}