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): add the update/delete-affected-row-limit rule framework (#3087)
rebelicecaabeb067bf9
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex b827bed5a7..08695dcfcc 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -193,6 +193,9 @@ const (// MySQLMergeAlterTable is an advisor type for MySQL no redundant ALTER TABLE statements.MySQLMergeAlterTable Type = "bb.plugin.advisor.mysql.statement.merge-alter-table"+ // MySQLStatementAffectedRowLimit is an advisor type for MySQL UPDATE/DELETE affected row limit.+ MySQLStatementAffectedRowLimit Type = "bb.plugin.advisor.mysql.statement.affected-row-limit"+// PostgreSQL Advisor.// PostgreSQLSyntax is an advisor type for PostgreSQL syntax.diff --git a/plugin/advisor/mysql/advisor_statement_affected_row_limit.go b/plugin/advisor/mysql/advisor_statement_affected_row_limit.gonew file mode 100644index 0000000000..4df05921dd--- /dev/null+++ b/plugin/advisor/mysql/advisor_statement_affected_row_limit.go@@ -0,0 +1,85 @@+package mysql++// Framework code is generated by the generator.++import (+ "github.com/pingcap/tidb/parser/ast"++ "github.com/bytebase/bytebase/plugin/advisor"+ "github.com/bytebase/bytebase/plugin/advisor/db"+)++var (+ _ advisor.Advisor = (*StatementAffectedRowLimitAdvisor)(nil)+ _ ast.Visitor = (*statementAffectedRowLimitChecker)(nil)+)++func init() {+ advisor.Register(db.MySQL, advisor.MySQLStatementAffectedRowLimit, &StatementAffectedRowLimitAdvisor{})+ advisor.Register(db.TiDB, advisor.MySQLStatementAffectedRowLimit, &StatementAffectedRowLimitAdvisor{})+}++// StatementAffectedRowLimitAdvisor is the advisor checking for UPDATE/DELETE affected row limit.+type StatementAffectedRowLimitAdvisor struct {+}++// Check checks for UPDATE/DELETE affected row limit.+func (*StatementAffectedRowLimitAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {+ stmtList, errAdvice := parseStatement(statement, ctx.Charset, ctx.Collation)+ if errAdvice != nil {+ return errAdvice, nil+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }+ payload, err := advisor.UnmarshalNumberTypeRulePayload(ctx.Rule.Payload)+ if err != nil {+ return nil, err+ }+ checker := &statementAffectedRowLimitChecker{+ level: level,+ title: string(ctx.Rule.Type),+ maxRow: payload.Number,+ }++ for _, stmt := range stmtList {+ checker.text = stmt.Text()+ checker.line = stmt.OriginTextPosition()+ (stmt).Accept(checker)+ }++ 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 statementAffectedRowLimitChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+ text string+ line int+ maxRow int+}++// Enter implements the ast.Visitor interface.+func (*statementAffectedRowLimitChecker) Enter(in ast.Node) (ast.Node, bool) {+ // TODO: implement it+ // switch node := in.(type) {+ // }++ return in, false+}++// Leave implements the ast.Visitor interface.+func (*statementAffectedRowLimitChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}diff --git a/plugin/advisor/mysql/advisor_statement_affected_row_limit_test.go b/plugin/advisor/mysql/advisor_statement_affected_row_limit_test.gonew file mode 100644index 0000000000..c4fc9c4caa--- /dev/null+++ b/plugin/advisor/mysql/advisor_statement_affected_row_limit_test.go@@ -0,0 +1,37 @@+package mysql++// Framework code is generated by the generator.++import (+ "encoding/json"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/plugin/advisor"+)++func TestStatementAffectedRowLimit(t *testing.T) {+ tests := []advisor.TestCase{+ {+ Statement: ``,+ Want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ }++ payload, err := json.Marshal(advisor.NumberTypeRulePayload{+ Number: 5,+ })+ require.NoError(t, err)+ advisor.RunSQLReviewRuleTests(t, tests, &StatementAffectedRowLimitAdvisor{}, &advisor.SQLReviewRule{+ Level: advisor.SchemaRuleLevelWarning,+ Payload: string(payload),+ }, advisor.MockMySQLDatabase)+}diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.goindex ee0cd05021..3f67aa7fa6 100644--- a/plugin/advisor/sql_review.go+++ b/plugin/advisor/sql_review.go@@ -71,6 +71,8 @@ const (SchemaRuleStatementInsertMustSpecifyColumn SQLReviewRuleType = "statement.insert.must-specify-column"// SchemaRuleStatementInsertDisallowOrderByRand disallow the order by rand in the INSERT statement.SchemaRuleStatementInsertDisallowOrderByRand SQLReviewRuleType = "statement.insert.disallow-order-by-rand"+ // SchemaRuleStatementAffectedRowLimit enforce the UPDATE/DELETE affected row limit.+ SchemaRuleStatementAffectedRowLimit SQLReviewRuleType = "statement.affected-row-limit"// SchemaRuleTableRequirePK require the table to have a primary key.SchemaRuleTableRequirePK SQLReviewRuleType = "table.require-pk"@@ -235,7 +237,7 @@ func (rule *SQLReviewRule) Validate() error {return err}case SchemaRuleIndexKeyNumberLimit, SchemaRuleStatementInsertRowLimit, SchemaRuleIndexTotalNumberLimit,- SchemaRuleColumnMaximumCharacterLength, SchemaRuleColumnAutoIncrementInitialValue:+ SchemaRuleColumnMaximumCharacterLength, SchemaRuleColumnAutoIncrementInitialValue, SchemaRuleStatementAffectedRowLimit:if _, err := UnmarshalNumberTypeRulePayload(rule.Payload); err != nil {return err}@@ -884,6 +886,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errcase db.MySQL, db.TiDB:return MySQLMergeAlterTable, nil}+ case SchemaRuleStatementAffectedRowLimit:+ switch engine {+ case db.MySQL, db.TiDB:+ return MySQLStatementAffectedRowLimit, nil+ }}return Fake, errors.Errorf("unknown SQL review rule type %v for %v", ruleType, engine)}