-
Notifications
You must be signed in to change notification settings - Fork 2
/
migrate.go
49 lines (44 loc) · 927 Bytes
/
migrate.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
package coresql
import (
"flag"
"log"
"os"
"github.com/golang-migrate/migrate/v4"
)
// Migrate represents functionality for
type Migrate interface {
Up() error
Down() error
}
// HandleMigrationArgs should be invoked to handle command line arguments for running migrations.
func HandleMigrationArgs(mig Migrate) {
flag.Parse()
switch flag.Arg(0) {
case "migrate":
switch flag.Arg(1) {
case "up":
if err := mig.Up(); err != nil {
log.Fatalln(err)
}
case "down":
if err := mig.Down(); err != nil {
log.Fatalln(err)
}
default:
log.Fatalln("migrate needs a sub-command")
}
os.Exit(0)
return
}
}
// MustMigrateUp will attempt to migrate your database up.
func MustMigrateUp(mig Migrate) {
if err := mig.Up(); err != nil {
switch err {
case migrate.ErrNoChange:
log.Println("database migration:", "no changes")
default:
log.Fatalln("database migration:", err)
}
}
}