Skip to content

Commit

Permalink
add support for createIndexStmt and dropIndexStmt (#844)
Browse files Browse the repository at this point in the history
* add support for createIndexStmt and dropIndexStmt

* add TestCreateIndexExp and TestDropIndexExp

* align test case styles
  • Loading branch information
deadspacewii authored Jan 18, 2024
1 parent e817d98 commit b390029
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
82 changes: 82 additions & 0 deletions canal/canal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,85 @@ 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)
require.NotZero(t, nodes)
for _, node := range nodes {
rdb := node.db
rtable := node.table
require.Equal(t, db, rdb)
require.Equal(t, baseTable, 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)
require.NotZero(t, nodes)
for _, node := range nodes {
rdb := node.db
rtable := node.table
require.Equal(t, db, rdb)
require.Equal(t, baseTable, rtable)
}
}
}
}
12 changes: 12 additions & 0 deletions canal/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit b390029

Please sign in to comment.