-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathdump_test.go
304 lines (245 loc) · 8.09 KB
/
dump_test.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
package dump
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
. "github.com/pingcap/check"
"github.com/siddontang/go-mysql/client"
"github.com/siddontang/go-mysql/mysql"
)
// use docker mysql for test
var host = flag.String("host", "127.0.0.1", "MySQL host")
var port = flag.Int("port", 3306, "MySQL host")
var execution = flag.String("exec", "mysqldump", "mysqldump execution path")
func Test(t *testing.T) {
TestingT(t)
}
type schemaTestSuite struct {
conn *client.Conn
d *Dumper
}
var _ = Suite(&schemaTestSuite{})
func (s *schemaTestSuite) SetUpSuite(c *C) {
var err error
s.conn, err = client.Connect(fmt.Sprintf("%s:%d", *host, *port), "root", "", "")
c.Assert(err, IsNil)
s.d, err = NewDumper(*execution, fmt.Sprintf("%s:%d", *host, *port), "root", "")
c.Assert(err, IsNil)
c.Assert(s.d, NotNil)
s.d.SetCharset("utf8")
s.d.SetErrOut(os.Stderr)
_, err = s.conn.Execute("CREATE DATABASE IF NOT EXISTS test1")
c.Assert(err, IsNil)
_, err = s.conn.Execute("CREATE DATABASE IF NOT EXISTS test2")
c.Assert(err, IsNil)
str := `CREATE TABLE IF NOT EXISTS test%d.t%d (
id int AUTO_INCREMENT,
name varchar(256),
PRIMARY KEY(id)
) ENGINE=INNODB`
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 1))
c.Assert(err, IsNil)
_, err = s.conn.Execute(fmt.Sprintf(str, 2, 1))
c.Assert(err, IsNil)
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 2))
c.Assert(err, IsNil)
_, err = s.conn.Execute(fmt.Sprintf(str, 2, 2))
c.Assert(err, IsNil)
str = `INSERT INTO test%d.t%d (name) VALUES ("a"), ("b"), ("\\"), ("''")`
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 1))
c.Assert(err, IsNil)
_, err = s.conn.Execute(fmt.Sprintf(str, 2, 1))
c.Assert(err, IsNil)
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 2))
c.Assert(err, IsNil)
_, err = s.conn.Execute(fmt.Sprintf(str, 2, 2))
c.Assert(err, IsNil)
}
func (s *schemaTestSuite) TearDownSuite(c *C) {
if s.conn != nil {
_, err := s.conn.Execute("DROP DATABASE IF EXISTS test1")
c.Assert(err, IsNil)
_, err = s.conn.Execute("DROP DATABASE IF EXISTS test2")
c.Assert(err, IsNil)
s.conn.Close()
}
}
func (s *schemaTestSuite) TestDump(c *C) {
// Using mysql 5.7 can't work, error:
// mysqldump: Error 1412: Table definition has changed,
// please retry transaction when dumping table `test_replication` at row: 0
// err := s.d.Dump(ioutil.Discard)
// c.Assert(err, IsNil)
s.d.AddDatabases("test1", "test2")
s.d.AddIgnoreTables("test1", "t2")
err := s.d.Dump(ioutil.Discard)
c.Assert(err, IsNil)
s.d.AddTables("test1", "t1")
err = s.d.Dump(ioutil.Discard)
c.Assert(err, IsNil)
}
type testParseHandler struct {
gset mysql.GTIDSet
}
func (h *testParseHandler) BinLog(name string, pos uint64) error {
return nil
}
func (h *testParseHandler) GtidSet(gtidsets string) (err error) {
if h.gset != nil {
err = h.gset.Update(gtidsets)
} else {
h.gset, err = mysql.ParseGTIDSet("mysql", gtidsets)
}
return err
}
func (h *testParseHandler) Data(schema string, table string, values []string) error {
return nil
}
type GtidParseTest struct {
gset mysql.GTIDSet
}
func (h *GtidParseTest) UpdateGtidSet(gtidStr string) (err error) {
if h.gset != nil {
err = h.gset.Update(gtidStr)
} else {
h.gset, err = mysql.ParseGTIDSet("mysql", gtidStr)
}
return err
}
func (s *parserTestSuite) TestParseGtidExp(c *C) {
// binlogExp := regexp.MustCompile("^CHANGE MASTER TO MASTER_LOG_FILE='(.+)', MASTER_LOG_POS=(\\d+);")
// gtidExp := regexp.MustCompile("(\\w{8}(-\\w{4}){3}-\\w{12}:\\d+-\\d+)")
tbls := []struct {
input string
expected string
}{
{`SET @@GLOBAL.GTID_PURGED='071a84e8-b253-11e8-8472-005056a27e86:1-76,
2337be48-0456-11e9-bd1c-00505690543b:1-7,
41d816cd-0455-11e9-be42-005056901a22:1-2,
5f1eea9e-b1e5-11e8-bc77-005056a221ed:1-144609156,
75848cdb-8131-11e7-b6fc-1c1b0de85e7b:1-151378598,
780ad602-0456-11e9-8bcd-005056901a22:1-516653148,
92809ddd-1e3c-11e9-9d04-00505690f6ab:1-11858565,
c59598c7-0467-11e9-bbbe-005056901a22:1-226464969,
cbd7809d-0433-11e9-b1cf-00505690543b:1-18233950,
cca778e9-8cdf-11e8-94d0-005056a247b1:1-303899574,
cf80679b-7695-11e8-8873-1c1b0d9a4ab9:1-12836047,
d0951f24-1e21-11e9-bb2e-00505690b730:1-4758092,
e7574090-b123-11e8-8bb4-005056a29643:1-12'
`, "071a84e8-b253-11e8-8472-005056a27e86:1-76,2337be48-0456-11e9-bd1c-00505690543b:1-7,41d816cd-0455-11e9-be42-005056901a22:1-2,5f1eea9e-b1e5-11e8-bc77-005056a221ed:1-144609156,75848cdb-8131-11e7-b6fc-1c1b0de85e7b:1-151378598,780ad602-0456-11e9-8bcd-005056901a22:1-516653148,92809ddd-1e3c-11e9-9d04-00505690f6ab:1-11858565,c59598c7-0467-11e9-bbbe-005056901a22:1-226464969,cbd7809d-0433-11e9-b1cf-00505690543b:1-18233950,cca778e9-8cdf-11e8-94d0-005056a247b1:1-303899574,cf80679b-7695-11e8-8873-1c1b0d9a4ab9:1-12836047,d0951f24-1e21-11e9-bb2e-00505690b730:1-4758092,e7574090-b123-11e8-8bb4-005056a29643:1-12"},
{`SET @@GLOBAL.GTID_PURGED='071a84e8-b253-11e8-8472-005056a27e86:1-76,
2337be48-0456-11e9-bd1c-00505690543b:1-7';
`, "071a84e8-b253-11e8-8472-005056a27e86:1-76,2337be48-0456-11e9-bd1c-00505690543b:1-7"},
{`SET @@GLOBAL.GTID_PURGED='c0977f88-3104-11e9-81e1-00505690245b:1-274559';
`, "c0977f88-3104-11e9-81e1-00505690245b:1-274559"},
{`CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.008995', MASTER_LOG_POS=102052485;`, ""},
{
`SET @@GLOBAL.GTID_PURGED='e50bd2d3-6ad7-11e9-890c-42010af0017c:1-5291126581:5291126583-5323107666';
`,
"e50bd2d3-6ad7-11e9-890c-42010af0017c:1-5291126581:5291126583-5323107666",
},
}
for _, tt := range tbls {
reader := strings.NewReader(tt.input)
var handler = new(testParseHandler)
err := Parse(reader, handler, true)
c.Assert(err, IsNil)
if tt.expected == "" {
if handler.gset != nil {
c.Assert(handler.gset, IsNil)
} else {
continue
}
}
expectedGtidset, err := mysql.ParseGTIDSet("mysql", tt.expected)
c.Assert(err, IsNil)
c.Assert(expectedGtidset.Equal(handler.gset), IsTrue)
}
}
func (s *parserTestSuite) TestParseFindTable(c *C) {
tbl := []struct {
sql string
table string
}{
{"INSERT INTO `note` VALUES ('title', 'here is sql: INSERT INTO `table` VALUES (\\'some value\\')');", "note"},
{"INSERT INTO `note` VALUES ('1', '2', '3');", "note"},
{"INSERT INTO `a.b` VALUES ('1');", "a.b"},
}
for _, t := range tbl {
res := valuesExp.FindAllStringSubmatch(t.sql, -1)[0][1]
c.Assert(res, Equals, t.table)
}
}
type parserTestSuite struct {
}
var _ = Suite(&parserTestSuite{})
func (s *parserTestSuite) TestUnescape(c *C) {
tbl := []struct {
escaped string
expected string
}{
{`\\n`, `\n`},
{`\\t`, `\t`},
{`\\"`, `\"`},
{`\\'`, `\'`},
{`\\0`, `\0`},
{`\\b`, `\b`},
{`\\Z`, `\Z`},
{`\\r`, `\r`},
{`abc`, `abc`},
{`abc\`, `abc`},
{`ab\c`, `abc`},
{`\abc`, `abc`},
}
for _, t := range tbl {
unesacped := unescapeString(t.escaped)
c.Assert(unesacped, Equals, t.expected)
}
}
func (s *schemaTestSuite) TestParse(c *C) {
var buf bytes.Buffer
s.d.Reset()
s.d.AddDatabases("test1", "test2")
err := s.d.Dump(&buf)
c.Assert(err, IsNil)
err = Parse(&buf, new(testParseHandler), true)
c.Assert(err, IsNil)
}
func (s *parserTestSuite) TestParseValue(c *C) {
str := `'abc\\',''`
values, err := parseValues(str)
c.Assert(err, IsNil)
c.Assert(values, DeepEquals, []string{`'abc\'`, `''`})
str = `123,'\Z#÷QÎx£. Æ‘ÇoPâÅ_\r—\\','','qn'`
values, err = parseValues(str)
c.Assert(err, IsNil)
c.Assert(values, HasLen, 4)
str = `123,'\Z#÷QÎx£. Æ‘ÇoPâÅ_\r—\\','','qn\'`
_, err = parseValues(str)
c.Assert(err, NotNil)
}
func (s *parserTestSuite) TestParseLine(c *C) {
lines := []struct {
line string
expected string
}{
{line: "INSERT INTO `test` VALUES (1, 'first', 'hello mysql; 2', 'e1', 'a,b');",
expected: "1, 'first', 'hello mysql; 2', 'e1', 'a,b'"},
{line: "INSERT INTO `test` VALUES (0x22270073646661736661736466, 'first', 'hello mysql; 2', 'e1', 'a,b');",
expected: "0x22270073646661736661736466, 'first', 'hello mysql; 2', 'e1', 'a,b'"},
}
f := func(c rune) bool {
return c == '\r' || c == '\n'
}
for _, t := range lines {
l := strings.TrimRightFunc(t.line, f)
m := valuesExp.FindAllStringSubmatch(l, -1)
c.Assert(m, HasLen, 1)
c.Assert(m[0][1], Matches, "test")
c.Assert(m[0][2], Matches, t.expected)
}
}