From a4822250e48de3f13053489f780b599f98b5b249 Mon Sep 17 00:00:00 2001 From: lance6716 Date: Wed, 30 Dec 2020 15:24:16 +0800 Subject: [PATCH] filter: add VIEW related type (#405) --- pkg/binlog-filter/filter.go | 5 ++++- pkg/binlog-filter/filter_test.go | 4 +++- pkg/binlog-filter/util.go | 7 ++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/binlog-filter/filter.go b/pkg/binlog-filter/filter.go index 90b80b93c..6ec81c0cf 100644 --- a/pkg/binlog-filter/filter.go +++ b/pkg/binlog-filter/filter.go @@ -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" @@ -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 diff --git a/pkg/binlog-filter/filter_test.go b/pkg/binlog-filter/filter_test.go index aca8061ca..69db26eb6 100644 --- a/pkg/binlog-filter/filter_test.go +++ b/pkg/binlog-filter/filter_test.go @@ -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}, } @@ -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 @@ -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) diff --git a/pkg/binlog-filter/util.go b/pkg/binlog-filter/util.go index 8d9bce41e..94457ace2 100644 --- a/pkg/binlog-filter/util.go +++ b/pkg/binlog-filter/util.go @@ -17,7 +17,7 @@ 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: @@ -25,6 +25,9 @@ func AstToDDLEvent(node ast.StmtNode) EventType { case *ast.CreateTableStmt: return CreateTable case *ast.DropTableStmt: + if n.IsView { + return DropView + } return DropTable case *ast.TruncateTableStmt: return TruncateTable @@ -36,6 +39,8 @@ func AstToDDLEvent(node ast.StmtNode) EventType { return DropIndex case *ast.AlterTableStmt: return AlertTable + case *ast.CreateViewStmt: + return CreateView } return NullEvent