-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmigrate.go
230 lines (190 loc) · 5.66 KB
/
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
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
package cmd
import (
"errors"
"fmt"
"os/exec"
"path/filepath"
"strings"
"github.com/kat-co/vala"
"github.com/spf13/cobra"
"github.com/volatiletech/abcweb/v5/config"
)
// migrateCmd represents the "migrate" command
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Run migration tasks (up, down, redo, status, version)",
Long: `Run migration tasks on the migrations in your migrations directory.
Migrations can be generated by using the "abcweb gen migration" command.
`,
Example: "abcweb migrate up\nabcweb migrate down",
}
var errNoMigrations = errors.New("No migrations to run")
var upCmd = &cobra.Command{
Use: "up",
Short: "Migrate the database to the most recent version",
RunE: func(cmd *cobra.Command, args []string) error {
err := migrateExec(cmd, args, "up")
if err != nil && err != errNoMigrations {
return err
}
return nil
},
}
var upOneCmd = &cobra.Command{
Use: "upone",
Short: "Migrate the database by one version",
RunE: func(cmd *cobra.Command, args []string) error {
err := migrateExec(cmd, args, "upone")
if err != nil && err != errNoMigrations {
return err
}
return nil
},
}
var downCmd = &cobra.Command{
Use: "down",
Short: "Roll back the version by one",
RunE: func(cmd *cobra.Command, args []string) error {
err := migrateExec(cmd, args, "down")
if err != nil && err != errNoMigrations {
return err
}
return nil
},
}
var downAllCmd = &cobra.Command{
Use: "downall",
Short: "Roll back all migrations",
RunE: func(cmd *cobra.Command, args []string) error {
err := migrateExec(cmd, args, "downall")
if err != nil && err != errNoMigrations {
return err
}
return nil
},
}
var redoCmd = &cobra.Command{
Use: "redo",
Short: "Down then up the latest migration",
RunE: func(cmd *cobra.Command, args []string) error {
err := migrateExec(cmd, args, "redo")
if err != nil && err != errNoMigrations {
return err
}
return nil
},
}
var redoAllCmd = &cobra.Command{
Use: "redoall",
Short: "Down then up all migrations",
RunE: func(cmd *cobra.Command, args []string) error {
err := migrateExec(cmd, args, "redoall")
if err != nil && err != errNoMigrations {
return err
}
return nil
},
}
var statusCmd = &cobra.Command{
Use: "status",
Short: "Dump the migration status for the current database",
RunE: func(cmd *cobra.Command, args []string) error {
return migrateExec(cmd, args, "status")
},
}
var dbVersionCmd = &cobra.Command{
Use: "version",
Short: "Print the current version of the database",
RunE: func(cmd *cobra.Command, args []string) error {
return migrateExec(cmd, args, "version")
},
}
func init() {
// migrate flags
migrateCmd.PersistentFlags().StringP("env", "e", "dev", "The config.toml file environment to load")
RootCmd.AddCommand(migrateCmd)
migrateCmd.AddCommand(upCmd)
migrateCmd.AddCommand(upOneCmd)
migrateCmd.AddCommand(downCmd)
migrateCmd.AddCommand(downAllCmd)
migrateCmd.AddCommand(redoCmd)
migrateCmd.AddCommand(redoAllCmd)
migrateCmd.AddCommand(statusCmd)
migrateCmd.AddCommand(dbVersionCmd)
migrateCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
// Usually the RootCmd persistent pre-run does this init for us,
// but since we have to override the persistent pre-run here
// to provide configuration to all children commands, we have to
// do the init ourselves.
var err error
cnf, err = config.Initialize(cmd.Flags().Lookup("env"))
if err != nil {
return err
}
cnf.ModeViper.BindPFlags(migrateCmd.PersistentFlags())
cnf.ModeViper.BindPFlags(cmd.Flags())
return nil
}
}
func migrateExec(cmd *cobra.Command, args []string, subCmd string) error {
checkDep("mig")
migrateCmdConfig.DBName = cnf.ModeViper.GetString("dbname")
migrateCmdConfig.User = cnf.ModeViper.GetString("user")
migrateCmdConfig.Pass = cnf.ModeViper.GetString("pass")
migrateCmdConfig.Host = cnf.ModeViper.GetString("host")
migrateCmdConfig.Port = cnf.ModeViper.GetInt("port")
migrateCmdConfig.SSLMode = cnf.ModeViper.GetString("sslmode")
var connStr string
if migrateCmdConfig.SSLMode == "" {
migrateCmdConfig.SSLMode = "require"
cnf.ModeViper.Set("sslmode", migrateCmdConfig.SSLMode)
}
if migrateCmdConfig.Port == 0 {
migrateCmdConfig.Port = 5432
cnf.ModeViper.Set("port", migrateCmdConfig.Port)
}
connStr = postgresConnStr(migrateCmdConfig)
err := vala.BeginValidation().Validate(
vala.StringNotEmpty(migrateCmdConfig.User, "user"),
vala.StringNotEmpty(migrateCmdConfig.Host, "host"),
vala.Not(vala.Equals(migrateCmdConfig.Port, 0, "port")),
vala.StringNotEmpty(migrateCmdConfig.DBName, "dbname"),
vala.StringNotEmpty(migrateCmdConfig.SSLMode, "sslmode"),
).Check()
if err != nil {
return err
}
excArgs := []string{
subCmd,
"postgres",
connStr,
}
exc := exec.Command("mig", excArgs...)
exc.Dir = filepath.Join(cnf.AppPath, migrationsDirectory)
out, err := exc.CombinedOutput()
fmt.Print(string(out))
if strings.HasPrefix(string(out), "No migrations to run") {
return errNoMigrations
}
return err
}
// postgressConnStr returns a postgres connection string compatible with the
// Go pq driver package, in the format:
// user=bob password=secret host=1.2.3.4 port=5432 dbname=mydb sslmode=verify-full
func postgresConnStr(cfg migrateConfig) string {
connStrs := []string{
fmt.Sprintf("user=%s", cfg.User),
}
if len(cfg.Pass) > 0 {
connStrs = append(connStrs, fmt.Sprintf("password=%s", cfg.Pass))
}
connStrs = append(connStrs, []string{
fmt.Sprintf("host=%s", cfg.Host),
fmt.Sprintf("port=%d", cfg.Port),
fmt.Sprintf("dbname=%s", cfg.DBName),
}...)
if len(cfg.SSLMode) > 0 {
connStrs = append(connStrs, fmt.Sprintf("sslmode=%s", cfg.SSLMode))
}
return strings.Join(connStrs, " ")
}