From e7a85732c69d21fa81a08946807c393e4b55bf02 Mon Sep 17 00:00:00 2001 From: zhengshaofeng Date: Wed, 17 Jan 2024 12:15:00 +0800 Subject: [PATCH 1/3] add support for createIndexStmt and dropIndexStmt --- canal/sync.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/canal/sync.go b/canal/sync.go index b71be536a..b7006b17e 100644 --- a/canal/sync.go +++ b/canal/sync.go @@ -216,6 +216,18 @@ func parseStmt(stmt ast.StmtNode) (ns []*node) { table: t.Table.Name.String(), } ns = []*node{n} + case *ast.CreateIndexStmt: + n := &node{ + db: t.Table.Schema.String(), + table: t.Table.Name.String(), + } + ns = []*node{n} + case *ast.DropIndexStmt: + n := &node{ + db: t.Table.Schema.String(), + table: t.Table.Name.String(), + } + ns = []*node{n} } return ns } From 0d4e59403efe2bcada4f3fe07f80b0b48c7a0bac Mon Sep 17 00:00:00 2001 From: zhengshaofeng Date: Wed, 17 Jan 2024 17:35:26 +0800 Subject: [PATCH 2/3] add TestCreateIndexExp and TestDropIndexExp --- canal/canal_test.go | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/canal/canal_test.go b/canal/canal_test.go index 954c3648c..fe3b6e093 100644 --- a/canal/canal_test.go +++ b/canal/canal_test.go @@ -323,3 +323,93 @@ func TestWithoutSchemeExp(t *testing.T) { } } } + +func TestCreateIndexExp(t *testing.T) { + cases := []string{ + "create index test0 on test.test (id)", + "create index test0 ON test.test (id)", + "CREATE INDEX test0 on `test`.test (id)", + "CREATE INDEX test0 ON test.test (id)", + "CREATE index test0 on `test`.test (id)", + "CREATE index test0 ON test.test (id)", + "create INDEX test0 on `test`.test (id)", + "create INDEX test0 ON test.test (id)", + "CREATE INDEX `test0` ON `test`.`test` (`id`) /* generated by server */", + "CREATE /*generated by server */ INDEX `test0` ON `test`.`test` (`id`)", + "CREATE INDEX `test0` ON `test`.test (id)", + "CREATE INDEX `test0` ON test.`test` (id)", + "CREATE INDEX `test0` ON test.test (`id`)", + "CREATE INDEX test0 ON `test`.`test` (`id`)", + "CREATE INDEX test0 ON `test`.`test` (id)", + "CREATE INDEX test0 ON test.test (`id`)", + } + + baseTable := "test" + db := "test" + pr := parser.New() + for _, s := range cases { + stmts, _, err := pr.Parse(s, "", "") + require.NoError(t, err) + for _, st := range stmts { + nodes := parseStmt(st) + if len(nodes) == 0 { + t.Fatalf("TestCreateIndexExp: parse fail\n") + continue + } + for _, node := range nodes { + rdb := node.db + rtable := node.table + if (len(rdb) != 0 && rdb != db) || rtable != baseTable { + t.Fatalf("TestCreateIndexExp:case %s failed db %s,table %s\n", s, rdb, rtable) + } + } + } + } +} + +func TestDropIndexExp(t *testing.T) { + cases := []string{ + "drop index test0 on test.test", + "DROP INDEX test0 ON test.test", + "drop INDEX test0 on test.test", + "DROP index test0 ON test.test", + "drop INDEX `test0` on `test`.`test`", + "drop INDEX test0 ON `test`.`test`", + "drop INDEX test0 on `test`.test", + "drop INDEX test0 on test.`test`", + "DROP index `test0` on `test`.`test`", + "DROP index test0 ON `test`.`test`", + "DROP index test0 on `test`.test", + "DROP index test0 on test.`test`", + "DROP INDEX `test0` ON `test`.`test` /* generated by server */", + "DROP /*generated by server */ INDEX `test0` ON `test`.`test`", + "DROP INDEX `test0` ON `test`.test", + "DROP INDEX `test0` ON test.`test`", + "DROP INDEX `test0` ON test.test", + "DROP INDEX test0 ON `test`.`test`", + "DROP INDEX test0 ON `test`.`test`", + "DROP INDEX test0 ON test.test", + } + + baseTable := "test" + db := "test" + pr := parser.New() + for _, s := range cases { + stmts, _, err := pr.Parse(s, "", "") + require.NoError(t, err) + for _, st := range stmts { + nodes := parseStmt(st) + if len(nodes) == 0 { + t.Fatalf("TestDropIndexExp: parse fail\n") + continue + } + for _, node := range nodes { + rdb := node.db + rtable := node.table + if (len(rdb) != 0 && rdb != db) || rtable != baseTable { + t.Fatalf("TestDropIndexExp:case %s failed db %s,table %s\n", s, rdb, rtable) + } + } + } + } +} From 3c19a631156623cbc47a1c6371d059300f5fb837 Mon Sep 17 00:00:00 2001 From: zhengshaofeng Date: Thu, 18 Jan 2024 10:49:28 +0800 Subject: [PATCH 3/3] align test case styles --- canal/canal_test.go | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/canal/canal_test.go b/canal/canal_test.go index fe3b6e093..333729997 100644 --- a/canal/canal_test.go +++ b/canal/canal_test.go @@ -352,16 +352,12 @@ func TestCreateIndexExp(t *testing.T) { require.NoError(t, err) for _, st := range stmts { nodes := parseStmt(st) - if len(nodes) == 0 { - t.Fatalf("TestCreateIndexExp: parse fail\n") - continue - } + require.NotZero(t, nodes) for _, node := range nodes { rdb := node.db rtable := node.table - if (len(rdb) != 0 && rdb != db) || rtable != baseTable { - t.Fatalf("TestCreateIndexExp:case %s failed db %s,table %s\n", s, rdb, rtable) - } + require.Equal(t, db, rdb) + require.Equal(t, baseTable, rtable) } } } @@ -399,16 +395,12 @@ func TestDropIndexExp(t *testing.T) { require.NoError(t, err) for _, st := range stmts { nodes := parseStmt(st) - if len(nodes) == 0 { - t.Fatalf("TestDropIndexExp: parse fail\n") - continue - } + require.NotZero(t, nodes) for _, node := range nodes { rdb := node.db rtable := node.table - if (len(rdb) != 0 && rdb != db) || rtable != baseTable { - t.Fatalf("TestDropIndexExp:case %s failed db %s,table %s\n", s, rdb, rtable) - } + require.Equal(t, db, rdb) + require.Equal(t, baseTable, rtable) } } }