-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.go
536 lines (494 loc) · 14 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package migration
import (
"bytes"
"context"
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
"github.com/sandwich-go/boost/xos"
"github.com/sandwich-go/boost/xpanic"
"github.com/sandwich-go/boost/xproc"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
type Migration interface {
// Generate
// Generate migration file and initializes migration support for the application.
Generate(opts ...GenerateConfOption) (err error)
// Migrate
// Create database if not exists.
// Generate script revision and diff from remote database for update SQL DDL.
Migrate(submitComment string) (revision Revision, err error)
// MigrateOnly
// "flask db migrate" only
MigrateOnly(submitComment string) (err error)
// ShowLocalRevision
// Show the revision denoted by the given symbol.
ShowLocalRevision(version string) (revision Revision, err error)
// ShowDatabaseRevision
// Shows the current revision of the database.
ShowDatabaseRevision() (revision Revision, err error)
// ShowDDL
// Use The --sql option present in several commands performs an ‘offline’ mode migration.
// Instead of executing the database commands the SQL statements that need to be executed are printed to the console.
// params:
// ddlFileName - The name of the generated ddl file
// latest - Write only the latest version of the update to the ddl file
ShowDDL(ddlFileName string, latest bool) (ddl string, err error)
// Upgrade
// Upgrades the database.
Upgrade() (err error)
// Downgrade
// Downgrades the database.
Downgrade() (err error)
// History
// Shows the list of migrations.
History() (revisions []Revision, err error)
// Command
// Exec command.
Command(env string, name string, arg ...string) (output []byte, err error)
}
type migrate struct {
logger *Logger
conf ConfInterface
}
func New(logger *log.Logger, opts ...ConfOption) Migration {
return &migrate{logger: NewLogger(logger), conf: NewConf(opts...)}
}
func (g *migrate) flaskEnv() string {
return fmt.Sprintf("FLASK_APP=%s", g.conf.GetFileName())
}
func (g *migrate) migrationBuildDir() (migrationBuildDir string) {
return g.conf.GetScriptRoot()
}
func (g *migrate) Generate(opts ...GenerateConfOption) error {
g.logger.Info("generate migration python script file...")
conf := NewGenerateConf(opts...)
args := []string{
"migration",
"--dir", g.migrationBuildDir(),
"--file_name", g.conf.GetFileName(),
"--db_host", conf.GetMysqlHost(),
"--db_port", strconv.Itoa(conf.GetMysqlPort()),
"--db_user", conf.GetMysqlUser(),
"--db_pass", conf.GetMysqlPassword(),
"--db_name", conf.GetMysqlDbName(),
"--config", conf.GetProtokitGoSettingPath(),
"--log_level=4",
}
var err error
xpanic.Try(func() {
_, err = xproc.Run(conf.GetProtokitPath(), xproc.WithArgs(args...))
}).Catch(func(err xpanic.E) {
err = fmt.Errorf("panic as error:%v", err)
})
g.logger.InfoWithFlag(err, "generate migration python script file", ", args:", args)
return err
}
func (g *migrate) Command(env string, name string, arg ...string) (output []byte, err error) {
var stderr bytes.Buffer
var stdout bytes.Buffer
xpanic.Try(func() {
cmd := exec.Command(name, arg...)
if env != "" {
cmd.Env = append(cmd.Env, env)
}
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
output = stdout.Bytes()
}).Catch(func(err xpanic.E) {
err = fmt.Errorf("panic as error:%v", err)
})
if stderr.String() != "" {
if err != nil {
err = fmt.Errorf("error: %v, stderr:%s", err, stderr.String())
}
}
return
}
func (g *migrate) prepare() (deferFunc func(), err error) {
g.logger.Info("prepare...")
var (
output []byte
dir string
)
defer func() {
g.logger.InfoWithFlag(err, "prepare", ", dir:", dir, ", file:", g.conf.GetFileName(), ", output:\n", string(output))
}()
dir = g.migrationBuildDir()
deferFunc, err = Chdir(dir)
if err != nil {
return
}
output, err = g.Command(g.flaskEnv(), "flask", "db", "init")
if err != nil {
if strings.Contains(err.Error(), migrationsAlreadyExists) {
g.logger.WarnWithFlag(migrationsAlreadyExists)
err = nil
} else if strings.Contains(err.Error(), migrationsAlreadyDone) {
g.logger.WarnWithFlag(migrationsAlreadyDone)
err = nil
}
}
return
}
func (g *migrate) fetchDsnFromFile() (dsn string, err error) {
g.logger.Info("fetch DSN from migration python script...")
var migrationBuildDir string
defer func() {
g.logger.InfoWithFlag(err, "fetch DSN from migration python script", ", migrationBuildDir:", migrationBuildDir, ", file:", g.conf.GetFileName(), ", dsn:", dsn)
}()
migrationBuildDir = g.migrationBuildDir()
file := filepath.Join(migrationBuildDir, g.conf.GetFileName())
// 需要解析下migration.py中的`SQLALCHEMY_DATABASE_URI`
if !xos.ExistsFile(file) {
err = fmt.Errorf("not found '%s' migration python script", file)
return
}
var content []byte
content, err = xos.FileGetContents(file)
if err != nil {
return
}
reg := regexp.MustCompile(`app.config\['SQLALCHEMY_DATABASE_URI'] = '\s*(.*)\s*'`)
all := reg.FindAllStringSubmatch(string(content), -1)
if len(all) < 1 || len(all[0]) < 2 {
err = fmt.Errorf("invalid migration file, not found 'SQLALCHEMY_DATABASE_URI' in '%s'", file)
return
}
// 需要查看dsn,是否有这样的库
dsn = strings.TrimPrefix(all[0][1], mysqlDSNPrefix)
dsns := strings.Split(dsn, ":")
for i, j := range dsns {
if strings.Contains(j, "@") {
dsns[i] = strings.Replace(j, "@", "@tcp(", 1)
ss := strings.Split(dsns[i+1], "/")
ss[0] += ")"
dsns[i+1] = strings.Join(ss, "/")
dsn = strings.Join(dsns, ":")
break
}
}
return
}
func (g *migrate) createDatabaseIfNotExists() (err error) {
g.logger.Info("create database if not exists...")
var dsn, dbName string
defer func() {
g.logger.InfoWithFlag(err, "create database if not exists", ", dbName:", dbName)
}()
dsn, err = g.fetchDsnFromFile()
if err != nil {
return
}
var config *mysql.Config
if config, err = mysql.ParseDSN(dsn); err != nil {
return
}
dbName = config.DBName
config.DBName = ""
var mdb *sql.DB
if mdb, err = sql.Open("mysql", config.FormatDSN()); err != nil {
return err
}
_, err = mdb.ExecContext(context.Background(), fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", dbName))
return
}
func (g *migrate) generateRevisionScript(_ string) (err error) {
g.logger.Info("execute flask db migrate...")
var output []byte
defer func() {
g.logger.InfoWithFlag(err, "execute flask db migrate", ", output:\n", string(output))
}()
// 检查是否有migrations/versions目录,versions目录为空的时候,git不会上传空目录
// 需要手动创建一次 以免migrate报错
// 检查目录是否存在
dirPath := "./migrations/versions"
_, err = os.Stat(dirPath)
if os.IsNotExist(err) {
err = os.Mkdir(dirPath, 0755)
if err != nil {
return
}
}
message := fmt.Sprintf(`--message=%s`, fmt.Sprintf("%s_%d", g.conf.GetCommitID(), time.Now().Unix())) // 用时"间戳+CommitID"作为本次migrate的提交内容(因为无法支持中文,且提交内容对用户无用)
revisionId := fmt.Sprintf(`--rev-id=%s`, g.conf.GetCommitID()) // 用CommitID作为本次migrate的版本号
output, err = g.Command(g.flaskEnv(), "flask", "db", "migrate", message, revisionId)
if err != nil {
if strings.Contains(err.Error(), dbNotUpToDate) {
g.logger.WarnWithFlag(dbNotUpToDate)
err = nil
} else if strings.Contains(err.Error(), SchemaNoChanges) {
g.logger.WarnWithFlag(SchemaNoChanges)
err = nil
}
}
return
}
func (g *migrate) Migrate(submitComment string) (revision Revision, err error) {
var deferFunc func()
deferFunc, err = g.prepare()
defer deferFunc()
if err != nil {
return
}
// 创建远程版本库
err = g.createDatabaseIfNotExists()
if err != nil {
return
}
err = g.generateRevisionScript(submitComment)
if err != nil {
return
}
return g.ShowLocalRevision("")
}
func (g *migrate) MigrateOnly(submitComment string) (err error) {
var deferFunc func()
deferFunc, err = g.prepare()
defer deferFunc()
if err != nil {
return
}
return g.generateRevisionScript(submitComment)
}
type Revision struct {
Rev string
Parent string
Path string
Message string
RevisionId string
Revises string
CreateDate string
Content string
}
func parseRevisions(s string) ([]Revision, error) {
var err error
var valid bool
var out []Revision
var index = -1
ss := strings.Split(s, "\n")
for _, str := range ss {
str = strings.TrimSpace(str)
if len(str) == 0 {
continue
}
strs := strings.SplitN(str, ":", 2)
if strs[0] == "Rev" {
valid = true
index++
out = append(out, Revision{Content: s})
}
if !valid {
continue
}
switch strs[0] {
case "Rev":
out[index].Rev = strings.TrimSpace(strs[1])
case "Parent":
out[index].Parent = strings.TrimSpace(strs[1])
case "Path":
out[index].Path = strings.TrimSpace(strs[1])
case "Revision ID":
out[index].RevisionId = strings.TrimSpace(strs[1])
case "Revises":
out[index].Revises = strings.TrimSpace(strs[1])
case "Create Date":
source := strings.TrimSpace(strs[1])
var target time.Time
target, err = time.Parse("2006-01-02 15:04:05.999999", source)
out[index].CreateDate = target.Format("2006-01-02 15:04:05")
default:
out[index].Message = strings.TrimSpace(strs[0])
}
}
return out, err
}
func (g *migrate) ShowLocalRevision(version string) (revision Revision, err error) {
g.logger.Info("show local revision...")
var output []byte
defer func() {
g.logger.InfoWithFlag(err, "show local revision", ", revision:", revision, ", output:\n", string(output))
}()
var deferFunc func()
deferFunc, err = g.prepare()
defer deferFunc()
if err != nil {
return
}
if len(version) > 0 {
output, err = g.Command(g.flaskEnv(), "flask", "db", "show", version)
} else {
output, err = g.Command(g.flaskEnv(), "flask", "db", "show")
}
if err != nil {
return
}
var revisions []Revision
if revisions, err = parseRevisions(string(output)); err != nil {
return
}
if len(revisions) == 0 {
return
}
return revisions[0], nil
}
func (g *migrate) ShowDatabaseRevision() (revision Revision, err error) {
g.logger.Info("show remote revision...")
var output []byte
defer func() {
g.logger.InfoWithFlag(err, "show remote revision", ", revision:", revision, ", output:\n", string(output))
}()
var deferFunc func()
deferFunc, err = g.prepare()
defer deferFunc()
if err != nil {
return
}
output, err = g.Command(g.flaskEnv(), "flask", "db", "current", "--verbose")
if err != nil {
return
}
var revisions []Revision
if revisions, err = parseRevisions(string(output)); err != nil {
return
}
if len(revisions) == 0 {
return
}
return revisions[0], nil
}
func (g *migrate) ShowDDL(ddlFileName string, latest bool) (ddl string, err error) {
g.logger.Info("show ddl...")
var output []byte
defer func() {
g.logger.InfoWithFlag(err, "show ddl", ", output:\n", string(output))
}()
var deferFunc func()
deferFunc, err = g.prepare()
defer deferFunc()
if err != nil {
return
}
output, err = g.Command(g.flaskEnv(), "flask", "db", "upgrade", "--sql")
if err != nil {
return
}
var migrationBuildDir = g.migrationBuildDir()
if len(ddlFileName) > 0 {
filePath := filepath.Join(migrationBuildDir, ddlFileName)
if latest {
output, err = g.generateUpdateDDLFile(output)
if err != nil {
return
}
// 自动构建 本地迁移库是一个 需要一直往里面写入migrate版本号
//output, err = g.deleteAlembicVersionUpdateAndInsertContent(output)
//if err != nil {
// return
//}
}
err = xos.FilePutContents(filePath, output)
if err != nil {
return
}
}
ddl = string(output)
return
}
func (g *migrate) generateUpdateDDLFile(content []byte) (updateContent []byte, err error) {
var deferFunc func()
deferFunc, err = g.prepare()
defer deferFunc()
if err != nil {
return
}
// 获取远程数据库的版本号
var dbRevision, scriptRevision Revision
if dbRevision, err = g.ShowDatabaseRevision(); err != nil {
return
}
// 获取当前脚本的版本号
if scriptRevision, err = g.ShowLocalRevision(""); err != nil {
return
}
if scriptRevision.RevisionId == dbRevision.RevisionId {
// 远程数据库最新,无需生成
return
}
contents := strings.SplitN(string(content), scriptRevision.RevisionId, 2)
if len(contents) > 1 {
updateContent = []byte(contents[1])
}
return
}
func (g *migrate) deleteAlembicVersionUpdateAndInsertContent(content []byte) (ddlContent []byte, err error) {
c := strings.TrimSpace(string(content))
cs := strings.Split(c, ";")
var contentAfterReplaced string
for i := len(cs) - 1; i >= 0; i-- {
checkContent := cs[i]
if strings.Contains(checkContent, updateAlembicVersionPrefix) {
contentAfterReplaced = strings.Replace(c, checkContent, "", -1)
}
if strings.Contains(checkContent, insertAlembicVersionPrefix) {
contentAfterReplaced = strings.Replace(c, checkContent, "", -1)
}
}
return []byte(contentAfterReplaced), nil
}
func (g *migrate) Upgrade() (err error) {
g.logger.Info("upgrade...")
var output []byte
defer func() {
g.logger.InfoWithFlag(err, "upgrade", ", output:\n", string(output))
}()
var deferFunc func()
deferFunc, err = g.prepare()
defer deferFunc()
if err != nil {
return
}
output, err = g.Command(g.flaskEnv(), "flask", "db", "upgrade")
return
}
func (g *migrate) Downgrade() (err error) {
g.logger.Info("downgrade...")
var output []byte
defer func() {
g.logger.InfoWithFlag(err, "downgrade", ", output:\n", string(output))
}()
var deferFunc func()
deferFunc, err = g.prepare()
defer deferFunc()
if err != nil {
return
}
output, err = g.Command(g.flaskEnv(), "flask", "db", "downgrade")
return
}
func (g *migrate) History() (revisions []Revision, err error) {
g.logger.Info("history...")
var output []byte
defer func() {
g.logger.InfoWithFlag(err, "history", ", output:\n", string(output))
}()
var deferFunc func()
deferFunc, err = g.prepare()
defer deferFunc()
if err != nil {
return
}
output, err = g.Command(g.flaskEnv(), "flask", "db", "history", "--verbose")
if err != nil {
return
}
return parseRevisions(string(output))
}