Skip to content

Commit de6c3a8

Browse files
lintanghuisiddontang
authored andcommitted
bug fix. fix empty schema (#390)
1 parent 35983ce commit de6c3a8

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

canal/canal_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/pingcap/parser"
1212
"github.com/siddontang/go-log/log"
1313
"github.com/siddontang/go-mysql/mysql"
14+
"github.com/siddontang/go-mysql/replication"
1415
)
1516

1617
var testHost = flag.String("host", "127.0.0.1", "MySQL host")
@@ -62,7 +63,7 @@ func (s *canalTestSuite) SetUpSuite(c *C) {
6263
s.execute(c, "INSERT INTO test.canal_test (content, name) VALUES (?, ?), (?, ?), (?, ?)", "1", "a", `\0\ndsfasdf`, "b", "", "c")
6364

6465
s.execute(c, "SET GLOBAL binlog_format = 'ROW'")
65-
66+
6667
s.c.SetEventHandler(&testEventHandler{c: c})
6768
go func() {
6869
set, _ := mysql.ParseGTIDSet("mysql", "")
@@ -275,3 +276,42 @@ func TestDropTableExp(t *testing.T) {
275276
}
276277
}
277278
}
279+
func TestWithoutSchemeExp(t *testing.T) {
280+
281+
cases := []replication.QueryEvent{
282+
replication.QueryEvent{
283+
Schema: []byte("test"),
284+
Query: []byte("drop table test0"),
285+
},
286+
replication.QueryEvent{
287+
Schema: []byte("test"),
288+
Query: []byte("rename table `test0` to `testtmp`"),
289+
},
290+
replication.QueryEvent{
291+
Schema: []byte("test"),
292+
Query: []byte("ALTER TABLE `test0` ADD `field2` DATE NULL AFTER `field1`;"),
293+
},
294+
replication.QueryEvent{
295+
Schema: []byte("test"),
296+
Query: []byte("CREATE TABLE IF NOT EXISTS test0 (`id` int(10)) ENGINE=InnoDB"),
297+
},
298+
}
299+
table := "test0"
300+
db := "test"
301+
pr := parser.New()
302+
for _, s := range cases {
303+
stmts, _, err := pr.Parse(string(s.Query), "", "")
304+
if err != nil {
305+
t.Fatalf("TestCreateTableExp:case %s failed\n", s.Query)
306+
}
307+
for _, st := range stmts {
308+
nodes := parseStmt(st)
309+
if len(nodes) == 0 {
310+
continue
311+
}
312+
if nodes[0].db != "" || nodes[0].table != table || string(s.Schema) != db {
313+
t.Fatalf("TestCreateTableExp:case %s failed\n", s.Query)
314+
}
315+
}
316+
}
317+
}

canal/sync.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ func (c *Canal) runSyncBinlog() error {
124124
for _, stmt := range stmts {
125125
nodes := parseStmt(stmt)
126126
for _, node := range nodes {
127+
if node.db == "" {
128+
node.db = string(e.Schema)
129+
}
127130
if err = c.updateTable(node.db, node.table); err != nil {
128131
return errors.Trace(err)
129132
}

0 commit comments

Comments
 (0)