-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathmigrate_cmd.go
114 lines (99 loc) · 2.81 KB
/
migrate_cmd.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
package cmd
import (
"fmt"
"net/url"
"os"
"github.com/gobuffalo/pop/v5"
"github.com/gobuffalo/pop/v5/logging"
"github.com/netlify/gotrue/conf"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var migrateCmd = cobra.Command{
Use: "migrate",
Long: "Migrate database strucutures. This will create new tables and add missing columns and indexes.",
Run: migrate,
}
func migrate(cmd *cobra.Command, args []string) {
globalConfig, err := conf.LoadGlobal(configFile)
if err != nil {
logrus.Fatalf("Failed to load configuration: %+v", err)
}
if globalConfig.DB.Driver == "" && globalConfig.DB.URL != "" {
u, err := url.Parse(globalConfig.DB.URL)
if err != nil {
logrus.Fatalf("%+v", errors.Wrap(err, "parsing db connection url"))
}
globalConfig.DB.Driver = u.Scheme
}
log := logrus.New()
pop.Debug = false
if globalConfig.Logging.Level != "" {
level, err := logrus.ParseLevel(globalConfig.Logging.Level)
if err != nil {
log.Fatalf("Failed to parse log level: %+v", err)
}
log.SetLevel(level)
if level == logrus.DebugLevel {
// Set to true to display query info
pop.Debug = true
}
if level != logrus.DebugLevel {
var noopLogger = func(lvl logging.Level, s string, args ...interface{}) {
return
}
// Hide pop migration logging
pop.SetLogger(noopLogger)
}
}
u, err := url.Parse(globalConfig.DB.URL)
processedUrl := globalConfig.DB.URL
if len(u.Query()) != 0 {
processedUrl = fmt.Sprintf("%s&application_name=gotrue_migrations", processedUrl)
} else {
processedUrl = fmt.Sprintf("%s?application_name=gotrue_migrations", processedUrl)
}
deets := &pop.ConnectionDetails{
Dialect: globalConfig.DB.Driver,
URL: processedUrl,
}
deets.Options = map[string]string{
"migration_table_name": "schema_migrations",
}
db, err := pop.NewConnection(deets)
if err != nil {
log.Fatalf("%+v", errors.Wrap(err, "opening db connection"))
}
defer db.Close()
if err := db.Open(); err != nil {
log.Fatalf("%+v", errors.Wrap(err, "checking database connection"))
}
log.Debugf("Reading migrations from %s", globalConfig.DB.MigrationsPath)
mig, err := pop.NewFileMigrator(globalConfig.DB.MigrationsPath, db)
if err != nil {
log.Fatalf("%+v", errors.Wrap(err, "creating db migrator"))
}
log.Debugf("before status")
if log.Level == logrus.DebugLevel {
err = mig.Status(os.Stdout)
if err != nil {
log.Fatalf("%+v", errors.Wrap(err, "migration status"))
}
}
// turn off schema dump
mig.SchemaPath = ""
err = mig.Up()
if err != nil {
log.Fatalf("%v", errors.Wrap(err, "running db migrations"))
} else {
log.Infof("GoTrue migrations applied successfully")
}
log.Debugf("after status")
if log.Level == logrus.DebugLevel {
err = mig.Status(os.Stdout)
if err != nil {
log.Fatalf("%+v", errors.Wrap(err, "migration status"))
}
}
}