Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for createIndexStmt and dropIndexStmt #844

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions canal/canal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
lance6716 marked this conversation as resolved.
Show resolved Hide resolved
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here, you can use require.Equal(t, db, rdb) and require.Equal(t, baseTable, rtable)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have apply require package to test

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)
}
}
}
}
}
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
Loading