/
statics.go
244 lines (225 loc) · 5.82 KB
/
statics.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
package internal
import (
"flag"
"fmt"
"html"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
)
type statics struct {
timer *myTimer
Config *Config
tables []*tableStatics
}
type tableStatics struct {
timer *myTimer
table string
alter *TableAlterData
alterRet error
schemaAfter string
}
func newStatics(cfg *Config) *statics {
return &statics{
timer: newMyTimer(),
tables: make([]*tableStatics, 0),
Config: cfg,
}
}
func (s *statics) newTableStatics(table string, sd *TableAlterData, index int) *tableStatics {
ts := &tableStatics{
timer: newMyTimer(),
table: table,
alter: sd,
}
if sd.Type == alterTypeNo {
return ts
}
if s.Config.SingleSchemaChange {
sds := sd.Split()
nts := &tableStatics{}
*nts = *ts
nts.alter = sds[index]
s.tables = append(s.tables, nts)
} else {
s.tables = append(s.tables, ts)
}
return ts
}
func (s *statics) toHTML() string {
code := "<h2>运行结果</h2>\n"
code += "<h3>Tables</h3>\n"
code += `<table class='tb_1'>
<thead>
<tr>
<th width="60px">序号</th>
<th>Table </th>
<th>同步(alter) 结果</th>
<th>耗时</th>
</tr>
</thead><tbody>
`
for idx, tb := range s.tables {
code += "<tr>"
code += "<td>" + strconv.Itoa(idx+1) + "</td>\n"
code += "<td><a href='#detail_" + tb.table + "'>" + tb.table + "</a></td>\n"
code += "<td>"
if s.Config.Sync {
if tb.alterRet == nil {
code += "<font color=green>成功</font>"
} else {
code += "<font color=red>失败:" + html.EscapeString(tb.alterRet.Error()) + "</font>"
}
} else {
code += "未同步"
}
code += "</td>\n"
code += "<td>" + tb.timer.usedSecond() + "</td>\n"
code += "</tr>\n"
}
code += "</tbody></table>\n<h3>SQLs</h3>\n<pre>"
for _, tb := range s.tables {
code += "<a name='detail_" + tb.table + "'></a>"
code += html.EscapeString(tb.alter.String()) + "\n\n"
}
code += "</pre>\n\n"
code += "<h3>详情</h3>\n"
code += `<table class='tb_1'>
<thead>
<tr>
<th width="40px">序号</th>
<th width="80px">Table</th>
<th> </th>
<th> </th>
</tr>
</thead><tbody>
`
for idx, tb := range s.tables {
code += "<tr>"
code += "<th rowspan=2>" + strconv.Itoa(idx+1) + "</th>\n"
code += "<td rowspan=2>" + tb.table + "<br/><br/>"
if s.Config.Sync {
if tb.alterRet == nil {
code += "<font color=green>成功</font>"
} else {
code += "<font color=red>失败:" + tb.alterRet.Error() + "</font>"
}
} else {
code += "未同步"
}
code += "</td>\n"
code += "<td valign=top><b>数据源 Schema:</b><br/>"
if len(tb.alter.SchemaDiff.Source.SchemaRaw) == 0 {
code += "<font color=red>在源数据源不存在,在目标数据库存在</font>"
} else {
code += htmlPre(tb.alter.SchemaDiff.Source.SchemaRaw)
}
code += "</td>\n"
code += "<td valign=top><b>目标 Schema:</b><br/>"
if len(tb.alter.SchemaDiff.Dest.SchemaRaw) == 0 {
code += "不存在"
} else {
code += htmlPre(tb.alter.SchemaDiff.Dest.SchemaRaw)
}
code += "</td>\n"
code += "</tr>\n"
code += "<tr>\n"
code += "<td valign=top><b>请在目标库执行如下 SQL:</b><br/>"
code += htmlPre(strings.Join(tb.alter.SQL, ","))
code += "</td>\n"
code += "<td valign=top>"
if s.Config.Sync {
code += "<b>执行后:</b><br/>" + htmlPre(tb.schemaAfter)
}
code += "</td>\n"
code += "</tr>\n"
}
code += "</tbody></table>\n"
return code
}
func (s *statics) alterFailedNum() int {
n := 0
for _, tb := range s.tables {
if tb.alterRet != nil {
n++
}
}
return n
}
func (s *statics) sendMailNotice(cfg *Config) {
alterTotal := len(s.tables)
if alterTotal < 1 {
writeHTMLResult("no table change")
log.Println("no table change, skip send mail")
return
}
title := "[mysql_schema_sync] " + strconv.Itoa(alterTotal) + " tables change [" + dsnSort(cfg.DestDSN) + "]"
body := `
<style>
.tb_1,.tb_1 td,.tb_1 th{border: 1px solid;border-collapse: collapse;}
.tb_1 thead{ background-color: #e0e0e0;}
</style>`
if !s.Config.Sync {
title += "[preview]"
body += "<font color=red>所有 SQL 均未执行!</font>\n"
}
hostName, _ := os.Hostname()
body += "<h2>任务信息</h2>\n<pre>"
body += " 数据源:" + dsnSort(cfg.SourceDSN) + "\n"
body += " 目标:" + dsnSort(cfg.DestDSN) + "\n"
body += " 有变化:" + strconv.Itoa(len(s.tables)) + " 张表/条语句\n"
body += "<font color=green>是否同步:" + fmt.Sprintf("%t", s.Config.Sync) + "</font>\n"
if s.Config.Sync {
fn := s.alterFailedNum()
body += "<font color=red>失败数 : " + strconv.Itoa(fn) + "</font>\n"
if fn > 0 {
title += " [failed=" + strconv.Itoa(fn) + "]"
}
}
body += "\n"
body += " 主机名: " + hostName + "\n"
body += "开始时间: " + s.timer.start.Format(timeFormatStd) + "\n"
body += "截止时间: " + s.timer.end.Format(timeFormatStd) + "\n"
body += "运行耗时: " + s.timer.usedSecond() + "\n"
body += "</pre>\n"
body += s.toHTML()
writeHTMLResult(body)
if cfg.Email != nil {
cfg.Email.SendMail(title, body)
}
if cfg.HTTPAddress != "" {
startWebServer(cfg.HTTPAddress)
}
}
func startWebServer(addr string) {
fp := filepath.Join(os.TempDir(), "mysql-schema-sync_last.html")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
bf, err := os.ReadFile(fp)
if err != nil {
http.NotFoundHandler().ServeHTTP(w, r)
return
}
_, _ = w.Write(bf)
})
log.Printf("http://%s", addr)
log.Println("Press Ctrl-C to terminate the program")
ser := &http.Server{
Addr: addr,
}
log.Println(ser.ListenAndServe())
}
func writeHTMLResult(str string) {
fp := filepath.Join(os.TempDir(), "mysql-schema-sync_last.html")
if len(htmlResultPath) > 0 {
fp = htmlResultPath
}
err := os.WriteFile(fp, []byte(str), 0666)
log.Println("html result:", fp, err)
}
func init() {
flag.StringVar(&htmlResultPath, "html", "", "html result file path")
}
var htmlResultPath string