Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore(sql-review): support disallow create trigger rule for mysql (#10633)
boojack650cc37327bd
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex 183e4270ac..b1a726e1c2 100644--- a/backend/plugin/advisor/advisor.go+++ b/backend/plugin/advisor/advisor.go@@ -164,6 +164,9 @@ const (// MySQLTableDisallowPartition is an advisor type for MySQL disallow table partition.MySQLTableDisallowPartition Type = "bb.plugin.advisor.mysql.table.disallow-partition"+ // MySQLTableDisallowTrigger is an advisor type for MySQL disallow table trigger.+ MySQLTableDisallowTrigger Type = "bb.plugin.advisor.mysql.table.disallow-trigger"+// MySQLDatabaseAllowDropIfEmpty is an advisor type for MySQL only allow drop empty database.MySQLDatabaseAllowDropIfEmpty Type = "bb.plugin.advisor.mysql.database.drop-empty-database"diff --git a/backend/plugin/advisor/catalog/walk_through_for_mysql.go b/backend/plugin/advisor/catalog/walk_through_for_mysql.goindex 72c99ed4d4..5ceebb4fac 100644--- a/backend/plugin/advisor/catalog/walk_through_for_mysql.go+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql.go@@ -542,6 +542,23 @@ func (l *mysqlListener) EnterRenameTableStatement(ctx *mysql.RenameTableStatemen}}+func (l *mysqlListener) EnterCreateTrigger(ctx *mysql.CreateTriggerContext) {+ if ctx.TriggerName() == nil {+ return+ }++ // Check if related table exists.+ if ctx.TableRef() == nil {+ return+ }+ databaseName, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())+ _, err := l.databaseState.mysqlFindTableState(databaseName, tableName, true /* createIncompleteTable */)+ if err != nil {+ l.err = err+ return+ }+}+func (*mysqlListener) EnterCreateProcedure(ctx *mysql.CreateProcedureContext) {if ctx.ProcedureName() == nil {returndiff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.goindex c0b103fdf0..9b7aee508f 100644--- a/backend/plugin/advisor/code.go+++ b/backend/plugin/advisor/code.go@@ -97,6 +97,7 @@ const (TableExists Code = 607CreateTablePartition Code = 608TableIsReferencedByView Code = 609+ CreateTableTrigger Code = 610// 701 ~ 799 database advisor error code.DatabaseNotEmpty Code = 701diff --git a/backend/plugin/advisor/mysql/advisor_table_disallow_trigger.go b/backend/plugin/advisor/mysql/advisor_table_disallow_trigger.gonew file mode 100644index 0000000000..146b3b20fb--- /dev/null+++ b/backend/plugin/advisor/mysql/advisor_table_disallow_trigger.go@@ -0,0 +1,90 @@+package mysql++import (+ "fmt"++ "github.com/antlr4-go/antlr/v4"+ "github.com/pkg/errors"++ mysql "github.com/bytebase/mysql-parser"++ "github.com/bytebase/bytebase/backend/plugin/advisor"+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"+)++var (+ _ advisor.Advisor = (*TableDisallowTriggerAdvisor)(nil)+)++func init() {+ advisor.Register(storepb.Engine_MYSQL, advisor.MySQLTableDisallowTrigger, &TableDisallowTriggerAdvisor{})+}++// TableDisallowTriggerAdvisor is the advisor checking for disallow table trigger.+type TableDisallowTriggerAdvisor struct {+}++// Check checks for disallow table trigger.+func (*TableDisallowTriggerAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {+ stmtList, ok := ctx.AST.([]*mysqlparser.ParseResult)+ if !ok {+ return nil, errors.Errorf("failed to convert to mysql parser result")+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }+ checker := &tableDisallowTriggerChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }++ for _, stmt := range stmtList {+ checker.baseLine = stmt.BaseLine+ antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)+ }++ if len(checker.adviceList) == 0 {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ })+ }+ return checker.adviceList, nil+}++type tableDisallowTriggerChecker struct {+ *mysql.BaseMySQLParserListener++ baseLine int+ adviceList []advisor.Advice+ level advisor.Status+ title string+ text string+}++func (checker *tableDisallowTriggerChecker) EnterQuery(ctx *mysql.QueryContext) {+ checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)+}++// EnterCreateTrigger is to check the create trigger statement.+func (checker *tableDisallowTriggerChecker) EnterCreateTrigger(ctx *mysql.CreateTriggerContext) {+ code := advisor.Ok+ if ctx.TriggerName() != nil {+ code = advisor.CreateTableTrigger+ }++ if code != advisor.Ok {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: code,+ Title: checker.title,+ Content: fmt.Sprintf("Trigger is forbidden, but \"%s\" creates", checker.text),+ Line: checker.baseLine + ctx.GetStart().GetLine(),+ })+ }+}diff --git a/backend/plugin/advisor/mysql/mysql_rules_test.go b/backend/plugin/advisor/mysql/mysql_rules_test.goindex 8db2785838..6ea15adff1 100644--- a/backend/plugin/advisor/mysql/mysql_rules_test.go+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go@@ -60,6 +60,8 @@ func TestMySQLRules(t *testing.T) {advisor.SchemaRuleTableCommentConvention,// advisor.SchemaRuleTableDisallowPartition disallow the table partition.advisor.SchemaRuleTableDisallowPartition,+ // advisor.SchemaRuleTableDisallowTrigger disallow the table trigger.+ advisor.SchemaRuleTableDisallowTrigger,// advisor.SchemaRuleRequiredColumn enforce the required columns in each table.advisor.SchemaRuleRequiredColumn,diff --git a/backend/plugin/advisor/mysql/test/table_disallow_trigger.yaml b/backend/plugin/advisor/mysql/test/table_disallow_trigger.yamlnew file mode 100644index 0000000000..d956a97f49--- /dev/null+++ b/backend/plugin/advisor/mysql/test/table_disallow_trigger.yaml@@ -0,0 +1,21 @@+- statement: CREATE TABLE t(a int);+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""+- statement: |-+ CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));+ CREATE TRIGGER ins_sum BEFORE INSERT ON account+ FOR EACH ROW SET @sum = @sum + NEW.amount;+ want:+ - status: WARN+ code: 610+ title: table.disallow-trigger+ content: |-+ Trigger is forbidden, but "CREATE TRIGGER ins_sum BEFORE INSERT ON account+ FOR EACH ROW SET @sum = @sum + NEW.amount;" creates+ line: 2+ details: ""diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex d26d71a096..70fc1c61f6 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -104,6 +104,8 @@ const (SchemaRuleTableCommentConvention SQLReviewRuleType = "table.comment"// SchemaRuleTableDisallowPartition disallow the table partition.SchemaRuleTableDisallowPartition SQLReviewRuleType = "table.disallow-partition"+ // SchemaRuleTableDisallowTrigger disallow the table trigger.+ SchemaRuleTableDisallowTrigger SQLReviewRuleType = "table.disallow-trigger"// SchemaRuleRequiredColumn enforce the required columns in each table.SchemaRuleRequiredColumn SQLReviewRuleType = "column.required"@@ -1286,6 +1288,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Tycase storepb.Engine_POSTGRES:return PostgreSQLTableDisallowPartition, nil}+ case SchemaRuleTableDisallowTrigger:+ switch engine {+ case storepb.Engine_MYSQL, storepb.Engine_MARIADB:+ return MySQLTableDisallowTrigger, nil+ }case SchemaRuleMySQLEngine:switch engine {case storepb.Engine_MYSQL, storepb.Engine_MARIADB:diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.goindex 6d00321f0d..8a53fd3846 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -329,6 +329,7 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType, dbType storepb.EngSchemaRuleTableRequirePK,SchemaRuleTableNoFK,SchemaRuleTableDisallowPartition,+ SchemaRuleTableDisallowTrigger,SchemaRuleColumnNotNull,SchemaRuleColumnDisallowChangeType,SchemaRuleColumnSetDefaultForNotNull,