Skip to content

Commit

Permalink
filter: add VIEW related type (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
lance6716 authored Dec 30, 2020
1 parent 2707c97 commit a482225
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
5 changes: 4 additions & 1 deletion pkg/binlog-filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const (
RenameTable EventType = "rename table"
CreateIndex EventType = "create index"
DropIndex EventType = "drop index"
CreateView EventType = "create view"
DropView EventType = "drop view"
AlertTable EventType = "alter table"
// if need, add more AlertTableOption = "alert table option"

Expand All @@ -72,7 +74,8 @@ func ClassifyEvent(event EventType) (EventType, error) {
switch event {
case InsertEvent, UpdateEvent, DeleteEvent:
return dml, nil
case CreateDatabase, DropDatabase, CreateTable, DropTable, TruncateTable, RenameTable, CreateIndex, DropIndex, AlertTable:
case CreateDatabase, DropDatabase, CreateTable, DropTable, TruncateTable, RenameTable,
CreateIndex, DropIndex, CreateView, DropView, AlertTable:
return ddl, nil
case NullEvent:
return NullEvent, nil
Expand Down
4 changes: 3 additions & 1 deletion pkg/binlog-filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type testFilterSuite struct{}

func (t *testFilterSuite) TestFilter(c *C) {
rules := []*BinlogEventRule{
{"Test_1_*", "abc*", []EventType{DeleteEvent, InsertEvent, CreateIndex, DropIndex}, []string{"^DROP\\s+PROCEDURE", "^CREATE\\s+PROCEDURE"}, nil, Ignore},
{"Test_1_*", "abc*", []EventType{DeleteEvent, InsertEvent, CreateIndex, DropIndex, DropView}, []string{"^DROP\\s+PROCEDURE", "^CREATE\\s+PROCEDURE"}, nil, Ignore},
{"xxx_*", "abc_*", []EventType{AllDML, NoneDDL}, nil, nil, Ignore},
{"yyy_*", "abc_*", []EventType{EventType("ALL DML")}, nil, nil, Do},
}
Expand All @@ -54,6 +54,7 @@ func (t *testFilterSuite) TestFilter(c *C) {
{"xxx_1", "abc_1", CreateIndex, "", Do},
{"yyy_1", "abc_1", InsertEvent, "", Do},
{"yyy_1", "abc_1", CreateIndex, "", Ignore},
{"test_1_a", "abc1", DropView, "", Ignore},
}

// initial binlog event filter
Expand Down Expand Up @@ -87,6 +88,7 @@ func (t *testFilterSuite) TestFilter(c *C) {
cases[10].action = Ignore // match none event and create index
cases[11].action = Ignore // no match
cases[12].action = Do // match all ddl
cases[13].action = Do // match all ddl
for _, cs := range cases {
action, err := filter.Filter(cs.schema, cs.table, cs.event, cs.sql)
c.Assert(err, IsNil)
Expand Down
7 changes: 6 additions & 1 deletion pkg/binlog-filter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ import "github.com/pingcap/parser/ast"

// AstToDDLEvent returns filter.DDLEvent
func AstToDDLEvent(node ast.StmtNode) EventType {
switch node.(type) {
switch n := node.(type) {
case *ast.CreateDatabaseStmt:
return CreateDatabase
case *ast.DropDatabaseStmt:
return DropDatabase
case *ast.CreateTableStmt:
return CreateTable
case *ast.DropTableStmt:
if n.IsView {
return DropView
}
return DropTable
case *ast.TruncateTableStmt:
return TruncateTable
Expand All @@ -36,6 +39,8 @@ func AstToDDLEvent(node ast.StmtNode) EventType {
return DropIndex
case *ast.AlterTableStmt:
return AlertTable
case *ast.CreateViewStmt:
return CreateView
}

return NullEvent
Expand Down

0 comments on commit a482225

Please sign in to comment.