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: migrate statement_disallow_commit (#9005)
zll600765fd4b3ded5
diff --git a/backend/plugin/advisor/mysqlwip/advisor_stmt_disallow_commit.go b/backend/plugin/advisor/mysqlwip/advisor_stmt_disallow_commit.goindex 2dc57d3fbc..bdae9c244e 100644--- a/backend/plugin/advisor/mysqlwip/advisor_stmt_disallow_commit.go+++ b/backend/plugin/advisor/mysqlwip/advisor_stmt_disallow_commit.go@@ -5,26 +5,33 @@ package mysqlwipimport ("fmt"- "github.com/pingcap/tidb/parser/ast"+ "github.com/antlr4-go/antlr/v4"+ mysql "github.com/bytebase/mysql-parser""github.com/pkg/errors""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 = (*StatementDisallowCommitAdvisor)(nil)- _ ast.Visitor = (*statementDisallowCommitChecker)(nil))+func init() {+ // only for mysqlwip test.+ advisor.Register(storepb.Engine_ENGINE_UNSPECIFIED, advisor.MySQLStatementDisallowCommit, &StatementDisallowCommitAdvisor{})+}+// StatementDisallowCommitAdvisor is the advisor checking for index type no blob.type StatementDisallowCommitAdvisor struct {}// Check checks for index type no blob.func (*StatementDisallowCommitAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {- stmtList, ok := ctx.AST.([]ast.StmtNode)+ stmtList, ok := ctx.AST.([]*mysqlparser.ParseResult)if !ok {- return nil, errors.Errorf("failed to convert to StmtNode")+ return nil, errors.Errorf("failed to convert to mysql parser result")}level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)@@ -37,9 +44,8 @@ func (*StatementDisallowCommitAdvisor) Check(ctx advisor.Context, _ string) ([]a}for _, stmt := range stmtList {- checker.text = stmt.Text()- checker.line = stmt.OriginTextPosition()- (stmt).Accept(checker)+ checker.baseLine = stmt.BaseLine+ antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)}if len(checker.adviceList) == 0 {@@ -54,29 +60,30 @@ func (*StatementDisallowCommitAdvisor) Check(ctx advisor.Context, _ string) ([]a}type statementDisallowCommitChecker struct {+ *mysql.BaseMySQLParserListener++ baseLine intadviceList []advisor.Advicelevel advisor.Statustitle stringtext string- line int}-// Enter implements the ast.Visitor interface.-func (c *statementDisallowCommitChecker) Enter(in ast.Node) (ast.Node, bool) {- if _, ok := in.(*ast.CommitStmt); ok {- c.adviceList = append(c.adviceList, advisor.Advice{- Status: c.level,- Code: advisor.StatementDisallowCommit,- Title: c.title,- Content: fmt.Sprintf("Commit is not allowed, related statement: \"%s\"", c.text),- Line: c.line,- })- }-- return in, false+func (checker *statementDisallowCommitChecker) EnterQuery(ctx *mysql.QueryContext) {+ checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)}-// Leave implements the ast.Visitor interface.-func (*statementDisallowCommitChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true+// EnterTransactionStatement is called when production transactionStatement is entered.+func (checker *statementDisallowCommitChecker) EnterTransactionStatement(ctx *mysql.TransactionStatementContext) {+ if ctx.COMMIT_SYMBOL() == nil {+ return+ }++ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.StatementDisallowCommit,+ Title: checker.title,+ Content: fmt.Sprintf("Commit is not allowed, related statement: \"%s\"", checker.text),+ Line: checker.baseLine + ctx.GetStart().GetLine(),+ })}diff --git a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.goindex d1675885df..2debd0c4f5 100644--- a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go+++ b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go@@ -31,6 +31,8 @@ func TestMySQLWIPRules(t *testing.T) {advisor.SchemaRuleStatementRequireWhere,// advisor.SchemaRuleStatementNoLeadingWildcardLike disallow leading '%' in LIKE, e.g. LIKE foo = '%x' is not allowed.advisor.SchemaRuleStatementNoLeadingWildcardLike,+ // advisor.SchemaRuleStatementDisallowCommit disallow using commit in the issue.+ advisor.SchemaRuleStatementDisallowCommit,// advisor.SchemaRuleStatementInsertRowLimit enforce the insert row limit.advisor.SchemaRuleStatementInsertRowLimit,diff --git a/backend/plugin/advisor/mysqlwip/test/statement_disallow_commit.yaml b/backend/plugin/advisor/mysqlwip/test/statement_disallow_commit.yamlindex 632bb3b3ee..f5682ce4ca 100644--- a/backend/plugin/advisor/mysqlwip/test/statement_disallow_commit.yaml+++ b/backend/plugin/advisor/mysqlwip/test/statement_disallow_commit.yaml@@ -7,10 +7,8 @@code: 206title: statement.disallow-commitcontent: |-- Commit is not allowed, related statement: "- COMMIT- ;"- line: 4+ Commit is not allowed, related statement: "COMMIT;"+ line: 3details: ""- statement: |-START TRANSACTION;@@ -25,16 +23,13 @@code: 206title: statement.disallow-commitcontent: |-- Commit is not allowed, related statement: "- COMMIT;"+ Commit is not allowed, related statement: "COMMIT;"line: 3details: ""- status: WARNcode: 206title: statement.disallow-commitcontent: |-- Commit is not allowed, related statement: "- COMMIT- ;"- line: 8+ Commit is not allowed, related statement: "COMMIT;"+ line: 7details: ""diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex 8e46af66e4..9485e72be2 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -1377,6 +1377,9 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty}case SchemaRuleStatementDisallowCommit:switch engine {+ // only for mysqlwip test.+ case storepb.Engine_ENGINE_UNSPECIFIED:+ return MySQLStatementDisallowCommit, nilcase storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:return MySQLStatementDisallowCommit, nilcase storepb.Engine_POSTGRES: