-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
260 lines (218 loc) · 5.67 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
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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/fatih/color"
_ "github.com/go-sql-driver/mysql"
"github.com/yuta-ron/sql-comp/database"
)
const (
codeOk = 0
codeError = 1
)
var green func(a ...interface{}) string
var yellow func(a ...interface{}) string
var red func(a ...interface{}) string
func init() {
green = color.New(color.FgGreen).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
}
func main() {
os.Exit(execute())
}
func execute() int {
flag.Parse()
if len(flag.Args()) == 0 {
return run()
}
arg := flag.Args()[0]
if strings.Contains(arg, "run") {
return run()
}
if strings.Contains(arg, "doctor") {
return doctor()
}
if strings.Contains(arg, "help") {
return showHelp()
}
if strings.Contains(arg, "info") {
return info()
}
fmt.Printf("%s option is not found\n", flag.Args())
return codeError
}
func run() int {
if !environmentCheck() {
fmt.Println("環境変数が設定されていません")
return codeError
}
// DB initialization
fromDb, err := database.NewFromDB()
if err != nil {
log.Fatal(err)
}
defer fromDb.Close()
toDb, err := database.NewToDB()
if err != nil {
log.Fatal(err)
}
defer toDb.Close()
fAccr := &database.DbAccessor{Db: fromDb}
tAccr := &database.DbAccessor{Db: toDb}
fromInfo, err := makeInfo(fAccr)
if err != nil {
log.Fatal(err)
}
toInfo, err := makeInfo(tAccr)
if err != nil {
log.Fatal(err)
}
if compare(fromInfo, toInfo) {
return codeOk
}
return codeError
}
func compare(fromInfo *database.DBStruct, toInfo *database.DBStruct) bool {
result := true
fromTxt := "比較元"
toTxt := "比較先"
allTableNames := make(map[string]string)
for k, _ := range fromInfo.Tables {
allTableNames[k] = k
}
for k, _ := range toInfo.Tables {
allTableNames[k] = k
}
for _, tblName := range allTableNames {
if _, ok := fromInfo.Tables[tblName]; !ok {
log := fmt.Sprintf("テーブル %s は%sのDBに存在しません\n", tblName, fromTxt)
fmt.Println(red(log))
continue
}
if _, ok := toInfo.Tables[tblName]; !ok {
log := fmt.Sprintf("テーブル %s は%sのDBに存在しません\n", tblName, toTxt)
fmt.Println(red(log))
continue
}
allColumnNames := make(map[string]string)
for clmnName, _ := range *fromInfo.Tables[tblName].Columns {
allColumnNames[clmnName] = clmnName
}
for clmnName, _ := range *toInfo.Tables[tblName].Columns {
allColumnNames[clmnName] = clmnName
}
for clmnName, _ := range allColumnNames {
fromClmns := *fromInfo.Tables[tblName].Columns
if _, ok := fromClmns[clmnName]; !ok {
log := fmt.Sprintf("テーブル %s の %s 列は %s のテーブルに存在しません\n", tblName, clmnName, fromTxt)
fmt.Println(red(log))
result = false
continue
}
toClmns := *toInfo.Tables[tblName].Columns
if _, ok := toClmns[clmnName]; !ok {
log := fmt.Sprintf("テーブル %s の %s 列は %s のテーブルに存在しません\n", tblName, clmnName, toTxt)
fmt.Println(red(log))
result = false
continue
}
fType := fromClmns[clmnName].Type
tType := toClmns[clmnName].Type
if fromClmns[clmnName].Type != toClmns[clmnName].Type {
log := fmt.Sprintf("テーブル %s の %s 列の型が異なります [比較元: %s <=> 比較先: %s]\n", tblName, clmnName, fType, tType)
fmt.Println(red(log))
result = false
}
fNull := fromClmns[clmnName].IsNull
tNull := toClmns[clmnName].IsNull
if fromClmns[clmnName].IsNull != toClmns[clmnName].IsNull {
log := fmt.Sprintf("テーブル %s の %s 列のIsNull定義が異なります [比較元: %v <=> 比較先: %v]\n", tblName, clmnName, fNull, tNull)
fmt.Println(yellow(log))
result = false
}
}
}
if result {
fmt.Println(green("DBは同期されています"))
}
return result
}
func makeInfo(acc *database.DbAccessor) (*database.DBStruct, error) {
ds := &database.DBStruct{
Tables: make(map[string]database.Table),
}
tbls, err := acc.Tables()
if err != nil {
return nil, err
}
for _, tableName := range tbls {
columns, err := acc.Columns(tableName)
if err != nil {
return nil, err
}
ds.Tables[tableName] = database.Table{
Columns: columns,
}
}
return ds, nil
}
func info() int {
logo :=
`
____ ____ ____ __ __ ____
| _ \| __ ) / ___| \/ | _ \
| | | | _ \| | | |\/| | |_) |
| |_| | |_) | |___| | | | __/
|____/|____/ \____|_| |_|_|
`
fmt.Println(logo)
fmt.Println("Please contact @yutaro-nishi if needs more info.")
return codeOk
}
func showHelp() int {
help :=
`
doctor: 環境変数のチェックを実行します。
run: 比較を実行します
help: コマンド一覧を表示します
info: 情報を表示します
`
fmt.Println(help)
return codeOk
}
func doctor() int {
if environmentCheck() {
fmt.Println(green("環境変数は正常に設定されています"))
return codeOk
}
fmt.Println("環境変数が設定されていません")
return codeError
}
func environmentCheck() bool {
ok := true
_, exists := os.LookupEnv("COMPARE_FROM_DB_HOST")
ok = ok && exists
_, exists = os.LookupEnv("COMPARE_FROM_DB_PORT")
ok = ok && exists
_, exists = os.LookupEnv("COMPARE_FROM_DB_USER")
ok = ok && exists
_, exists = os.LookupEnv("COMPARE_FROM_DB_PASSWORD")
ok = ok && exists
_, exists = os.LookupEnv("COMPARE_FROM_DB_NAME")
ok = ok && exists
_, exists = os.LookupEnv("COMPARE_TO_DB_HOST")
ok = ok && exists
_, exists = os.LookupEnv("COMPARE_TO_DB_PORT")
ok = ok && exists
_, exists = os.LookupEnv("COMPARE_TO_DB_USER")
ok = ok && exists
_, exists = os.LookupEnv("COMPARE_TO_DB_PASSWORD")
ok = ok && exists
_, exists = os.LookupEnv("COMPARE_TO_DB_NAME")
ok = ok && exists
return ok
}