/
cmd_migrate.go
321 lines (251 loc) · 7.27 KB
/
cmd_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
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
package cmd
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/dosco/graphjin/internal/cmd/internal/migrate"
"github.com/dosco/graphjin/serv"
"github.com/spf13/cobra"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
func migrateCmd() *cobra.Command {
c := &cobra.Command{
Use: "migrate",
Short: "Migrate the database",
Long: `Migrate the database to destination migration version.
Destination migration version can be one of the following value types:
Migrate to the most recent migration.
e.g. db migrate up
Rollback the most recent migration.
e.g. db migrate down
Migrate to a specific migration.
e.g. db migrate 42
Migrate forward N steps.
e.g. db migrate +3
Migrate backward N steps.
e.g. db migrate -2
Redo previous N steps (migrate backward N steps then forward N steps).
e.g. db migrate -+1
`,
Run: cmdDBMigrate,
}
c1 := &cobra.Command{
Use: "new NAME",
Short: "Generate a new migration",
Long: "Generate a new migration with the next sequence number and provided name",
Run: cmdMigrateNew,
}
c.AddCommand(c1)
c2 := &cobra.Command{
Use: "status",
Short: "Print current migration status",
Run: cmdMigrateStatus,
}
c.AddCommand(c2)
return c
}
var newMigrationText = `-- Write your migrate up statements here
---- create above / drop below ----
-- Write your migrate down statements here. If this migration is irreversible
-- Then delete the separator line above.
`
func cmdDBMigrate(cmd *cobra.Command, args []string) {
doneSomething := false
if len(args) == 0 {
cmd.Help() //nolint: errcheck
os.Exit(1)
}
dest := args[0]
setup(cpath)
initDB(true)
if conf.DB.Type == "mysql" {
log.Fatal("Migrations not support with MySQL")
}
m, err := migrate.NewMigrator(db)
if err != nil {
log.Fatalf("Error initializing migrations: %s", err)
}
m.Data = getMigrationVars(conf)
err = m.LoadMigrations(conf.RelPath(conf.MigrationsPath))
if err != nil {
log.Fatalf("Failed to load migrations: %s", err)
}
if len(m.Migrations) == 0 {
log.Fatalf("No migrations found")
}
m.OnStart = func(name, direction, sql string) {
var action string
if direction == "up" {
action = "Migrating: "
} else if direction == "down" {
action = "Rolling back: "
} else {
log.Fatalf("Migration direction %s not supported", direction)
}
log.Infof("%s %s", action, name)
if conf.Debug {
log.Infof("SQL:\n%s", sql)
}
doneSomething = true
}
m.OnFinish = func(name, direction string, durationMs int64) {
var action string
if direction == "up" {
action = "Migrated: "
} else if direction == "down" {
action = "Rolled back: "
} else {
log.Fatalf("Migration direction %s not supported", direction)
}
log.Infof("%s %s (%d ms)", action, name, durationMs)
}
m.OnError = func(name string, err error, sql string) {
sql = strings.TrimSpace(sql)
sql = "> " + strings.ReplaceAll(sql, "\n", "\n> ")
log.Infof("Error in %s\n%s\n%s", name, sql, err)
}
currentVersion, err := m.GetCurrentVersion()
if err != nil {
log.Fatalf("Failed fetching current migrations version: %s", err)
log.Fatalf("Unable to get current migration version:\n %v\n", err)
}
mustParseDestination := func(d string) int32 {
var n int64
n, err = strconv.ParseInt(d, 10, 32)
if err != nil {
log.Fatalf("Invalid migration version: %s", err)
}
return int32(n)
}
if dest == "up" {
err = m.Migrate()
} else if dest == "down" {
err = m.MigrateTo(currentVersion - 1)
} else if len(dest) >= 3 && dest[0:2] == "-+" {
err = m.MigrateTo(currentVersion - mustParseDestination(dest[2:]))
if err == nil {
err = m.MigrateTo(currentVersion)
}
} else if len(dest) >= 2 && dest[0] == '-' {
err = m.MigrateTo(currentVersion - mustParseDestination(dest[1:]))
} else if len(dest) >= 2 && dest[0] == '+' {
err = m.MigrateTo(currentVersion + mustParseDestination(dest[1:]))
} else {
cmd.Help() //nolint: errcheck
os.Exit(1)
}
if err != nil {
log.Fatalf("Error with migrations: %s", err)
// if err, ok := err.(m.MigrationPgError); ok {
// if err.Detail != "" {
// log.Fatalf("ERR %s", err.Detail)
// }
// if err.Position != 0 {
// ele, err := ExtractErrorLine(err.Sql, int(err.Position))
// if err != nil {
// log.Fatalf("ERR %s", err)
// }
// log.Fatalf("INF line %d, %s%s", ele.LineNum, ele.Text)
// }
// }
}
if !doneSomething {
log.Infof("Nothing to do")
}
}
func cmdMigrateStatus(cmd *cobra.Command, args []string) {
setup(cpath)
initDB(true)
if conf.DB.Type == "mysql" {
log.Fatal("Migrations not support with MySQL")
}
m, err := migrate.NewMigrator(db)
if err != nil {
log.Fatalf("Error initializing migrations: %s", err)
}
m.Data = getMigrationVars(conf)
err = m.LoadMigrations(conf.RelPath(conf.MigrationsPath))
if err != nil {
log.Fatalf("Failed to load migrations: %s", err)
}
if len(m.Migrations) == 0 {
log.Fatal("No migrations found")
}
mver, err := m.GetCurrentVersion()
if err != nil {
log.Fatalf("Failed to retrieve current migration version: %s", err)
}
var status string
behindCount := len(m.Migrations) - int(mver)
if behindCount == 0 {
status = "up to date"
} else {
status = "migration(s) pending"
}
log.Infof("Status: %s, version: %d of %d, host: %s, database: %s",
status, mver, len(m.Migrations), conf.DB.Host, conf.DB.DBName)
}
func cmdMigrateNew(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.Help() //nolint: errcheck
os.Exit(1)
}
setup(cpath)
initDB(false)
name := args[0]
migrationsPath := conf.RelPath(conf.MigrationsPath)
m, err := migrate.FindMigrations(migrationsPath)
if err != nil {
log.Fatalf("Error loading migrations: %s", err)
}
mname := fmt.Sprintf("%d_%s.sql", len(m), name)
// Write new migration
mpath := filepath.Join(migrationsPath, mname)
mfile, err := os.OpenFile(mpath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
if err != nil {
log.Fatalf("Error creating migration file: %s", err)
}
defer mfile.Close()
_, err = mfile.WriteString(newMigrationText)
if err != nil {
log.Fatalf("Error creating migration file: %s", err)
}
log.Infof("Migration file created: %s", mpath)
}
type ErrorLineExtract struct {
LineNum int // Line number starting with 1
ColumnNum int // Column number starting with 1
Text string // Text of the line without a new line character.
}
// ExtractErrorLine takes source and character position extracts the line
// number, column number, and the line of text.
//
// The first character is position 1.
func ExtractErrorLine(source string, position int) (ErrorLineExtract, error) {
ele := ErrorLineExtract{LineNum: 1}
if position > len(source) {
return ele, fmt.Errorf("position (%d) is greater than source length (%d)", position, len(source))
}
lines := strings.SplitAfter(source, "\n")
for _, ele.Text = range lines {
if position-len(ele.Text) < 1 {
ele.ColumnNum = position
break
}
ele.LineNum += 1
position -= len(ele.Text)
}
ele.Text = strings.TrimSuffix(ele.Text, "\n")
return ele, nil
}
func getMigrationVars(c *serv.Config) map[string]interface{} {
en := cases.Title(language.English)
return map[string]interface{}{
"AppName": en.String(c.AppName),
"AppNameSlug": strings.ToLower(strings.ReplaceAll(c.AppName, " ", "_")),
"Env": strings.ToLower(os.Getenv("GO_ENV")),
}
}