forked from hidu/mysql-schema-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statics.go
227 lines (208 loc) · 5.17 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
package internal
import (
"fmt"
"html"
"log"
"os"
"time"
)
type Statics struct {
timer *myTimer
tables []*tableStatics
Config *Config
failedNum int
result *string
diff bool
}
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,
failedNum: -1,
diff: true,
}
}
func (s *Statics) newTableStatics(table string, sd *TableAlterData) *tableStatics {
ts := &tableStatics{
timer: NewMyTimer(),
table: table,
alter: sd,
}
if sd.Type != AlterTypeNo {
s.tables = append(s.tables, ts)
}
return ts
}
func (s *Statics) String() string {
if s.result != nil {
return *s.result
}
result := s.toHTML()
s.result = &result
return result
}
func (s *Statics) ChangeTableNum() int {
return len(s.tables)
}
func (s *Statics) ChangeTables() []string {
tables := make([]string, len(s.tables))
for k, t := range s.tables {
tables[k] = t.table
}
return tables
}
func (s *Statics) FailedNum() int {
return s.alterFailedNum()
}
func (s *Statics) StartTime() time.Time {
return s.timer.start
}
func (s *Statics) Diff(on bool) *Statics {
s.diff = on
return s
}
func (s *Statics) EndTime() time.Time {
return s.timer.end
}
func (s *Statics) Elapsed() time.Duration {
return s.timer.end.Sub(s.timer.start)
}
func (s *Statics) toHTML() string {
cfg := s.Config
hostName, _ := os.Hostname()
code := "<h2>Info</h2>\n<pre>"
code += " from : " + dsnSort(cfg.SourceDSN) + "\n"
code += " to : " + dsnSort(cfg.DestDSN) + "\n"
code += " alter : " + fmt.Sprintf("%d", len(s.tables)) + " tables\n"
code += "<font color=green> sync : " + fmt.Sprintf("%t", s.Config.Sync) + "</font>\n"
if s.Config.Sync {
fn := s.alterFailedNum()
code += "<font color=red>failed : " + fmt.Sprintf("%d", fn) + "</font>\n"
}
code += "\n"
code += " host : " + hostName + "\n"
code += " start : " + s.timer.start.Format(timeFormatStd) + "\n"
code += " end : " + s.timer.end.Format(timeFormatStd) + "\n"
code += " used : " + s.timer.UsedSecond() + "\n"
code += "</pre>\n"
code += "<h2>Result</h2>\n"
code += "<h3>Tables</h3>\n"
code += `<table class='table table-bordered tb_1'>
<thead>
<tr>
<th width="60px">no</th>
<th>table</th>
<th>alter result</th>
<th>used</th>
</tr>
</thead><tbody>
`
for idx, tb := range s.tables {
code += "<tr>"
code += "<td>" + fmt.Sprintf("%d", 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>success</font>"
} else {
code += "<font color=red>failed," + html.EscapeString(tb.alterRet.Error()) + "</font>"
}
} else {
code += "not sync"
}
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"
if !s.diff {
return code
}
code += "<h3>Detail</h3>\n"
code += `<table class='table table-bordered tb_1'>
<thead>
<tr>
<th width="40px">no</th>
<th width="80px">table</th>
<th>source</th>
<th>destination</th>
</tr>
</thead><tbody>
`
for idx, tb := range s.tables {
code += "<tr>"
code += "<th rowspan=2>" + fmt.Sprintf("%d", idx+1) + "</th>\n"
code += "<td rowspan=2>" + tb.table + "<br/><br/>"
if s.Config.Sync {
if tb.alterRet == nil {
code += "<font color=green>success</font>"
} else {
code += "<font color=red>failed," + tb.alterRet.Error() + "</font>"
}
} else {
code += "(no sync)"
}
code += "</td>\n"
code += "<td valign=top><b>source schema:</b><br/>" + htmlPre(tb.alter.SchemaDiff.Source.SchemaRaw) + "</td>\n"
code += "<td valign=top><b>dest schema:</b><br/>" + htmlPre(tb.alter.SchemaDiff.Dest.SchemaRaw) + "</td>\n"
code += "</tr>\n"
code += "<tr>\n"
code += "<td valign=top><b>alter:</b><br/>" + htmlPre(tb.alter.SQL) + "</td>\n"
code += "<td valign=top><b>alter after:</b><br/>" + htmlPre(tb.schemaAfter) + "</td>\n"
code += "</tr>\n"
}
code += "</tbody></table>\n"
return code
}
func (s *Statics) alterFailedNum() int {
if s.failedNum > -1 {
return s.failedNum
}
n := 0
for _, tb := range s.tables {
if tb.alterRet != nil {
n++
}
}
s.failedNum = n
return n
}
func (s *Statics) sendMailNotice() {
cfg := s.Config
if cfg.Email == nil {
log.Println("mail conf is not set,skip send mail")
return
}
alterTotal := len(s.tables)
if alterTotal < 1 {
log.Println("no table change,skip send mail")
return
}
title := "[mysql_schema_sync] " + fmt.Sprintf("%d", alterTotal) + " tables change [" + dsnSort(cfg.DestDSN) + "]"
var body string
if !s.Config.Sync {
title += "[preview]"
body += "<font color=red>this is preview,all sql never execute!</font>\n"
} else {
fn := s.alterFailedNum()
if fn > 0 {
title += " [failed=" + fmt.Sprintf("%d", fn) + "]"
}
}
body += s.toHTML()
cfg.Email.SendMail(title, body)
}