-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (137 loc) · 3.6 KB
/
main.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
package main
import (
"database/sql"
"errors"
"fmt"
"os"
"strconv"
"strings"
)
func echoVersion() {
fmt.Println(` ____ __ _ __ `)
fmt.Println(` / __ ) / / __ __ ___ ____ _____ (_) ____ / /_`)
fmt.Println(` / __ | / / / / / / / _ \ / __ \ / ___/ / / / __ \ / __/`)
fmt.Println(` / /_/ / / / / /_/ / / __/ / /_/ / / / / / / / / // /_ `)
fmt.Println(`/_____/ /_/ \__,_/ \___/ / .___/ /_/ /_/ /_/ /_/ \__/ `)
fmt.Println(` /_/ `)
fmt.Println()
fmt.Println("version: 1.0.0-beta")
fmt.Println()
}
func echoHelp() {
fmt.Println(`Commands:`)
fmt.Println(` init Init a Blueprint repo in current work directory`)
fmt.Println(` run Exec migrations`)
fmt.Println(` create, update Create a pair(include rollback) migration sql files`)
fmt.Println(` dump Dump schema from database`)
fmt.Println(` rollback Rollback`)
fmt.Println(` --step specify how many step(s) for rollback`)
fmt.Println(` --batch specify how many batch(es) for rollback`)
fmt.Println(` Only one of --step or --batch can be specified at a time,`)
fmt.Println(` default is --batch 1`)
fmt.Println(` help Display this infomation`)
}
var dbs []*sql.DB // 数据库连接
func bootstrap(workDir string) {
err := loadJsonConfig(workDir)
if err != nil {
fmt.Println("Parse config failed:", err.Error())
os.Exit(1)
}
for _, dbCnf := range config.Databases {
db, err := connectToDb(dbCnf.Host, dbCnf.Port, dbCnf.User, dbCnf.Pass, dbCnf.Name)
if err != nil {
fmt.Printf("connect to db[%s] error: %s\n", dbCnf.Host, err)
os.Exit(1)
}
dbs = append(dbs, db)
}
}
func cleanup() {
for _, db := range dbs {
db.Close()
}
}
func main() {
echoVersion()
cwd, err := os.Getwd()
if err != nil {
fmt.Println("Get cwd failed:", err.Error())
return
}
args := os.Args
if len(args) == 1 {
bootstrap(cwd)
defer cleanup()
err = runMigration(cwd, dbs)
} else {
action := strings.ToLower(args[1])
params := args[2:]
switch action {
case "init":
err = initBlueprint(cwd)
case "run":
bootstrap(cwd)
defer cleanup()
err = runMigration(cwd, dbs)
case "create",
"update":
err = createMigration(cwd, action, params)
case "dump":
bootstrap(cwd)
defer cleanup()
forceDump := false
for _, param := range params {
if param == "--force" {
forceDump = true
}
}
err = dumpSchemas(dbs[0], cwd, forceDump)
case "rollback":
bootstrap(cwd)
defer cleanup()
step := 0
batch := 0
for idx := 0; idx < len(params); idx++ {
param := params[idx]
if param == "--step" || param == "--batch" {
if idx+1 >= len(params) {
err = errors.New("invalid param: " + param)
break
}
value := 0
value, err = strconv.Atoi(params[idx+1])
if err != nil {
break
}
if value < 1 {
err = errors.New("invalid param value: " + param + " = " + params[idx+1])
break
}
switch param {
case "--step":
step = value
case "--batch":
batch = value
}
idx++
}
}
if step != 0 && batch != 0 {
err = errors.New("only one of --step or --batch can be specified at a time")
}
if err == nil {
err = rollbackMigration(cwd, dbs, step, batch)
}
case "help":
fallthrough
default:
echoHelp()
err = nil
}
}
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
}