Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(sql-review): implement MySQL insert-disallow-order-by-rand rule (#3000)
rebelice3cc61c885ac0
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex 0faf6ec9b5..72c86b7d6f 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -169,6 +169,9 @@ const (// MySQLInsertMustSpecifyColumn is an advisor type for MySQL to enforce column specified.MySQLInsertMustSpecifyColumn Type = "bb.plugin.advisor.mysql.insert.must-specify-column"+ // MySQLInsertDisallowOrderByRand is an advisor type for MySQL to disallow order by rand in INSERT statements.+ MySQLInsertDisallowOrderByRand Type = "bb.plugin.advisor.mysql.insert.disallow-order-by-rand"+// MySQLDisallowOrderBy is an advisor type for MySQL no ORDER BY clause in DELETE/UPDATE statement.MySQLDisallowOrderBy Type = "bb.plugin.advisor.mysql.statement.disallow-order-by"diff --git a/plugin/advisor/code.go b/plugin/advisor/code.goindex 92b86f2e37..53849942f1 100644--- a/plugin/advisor/code.go+++ b/plugin/advisor/code.go@@ -106,6 +106,7 @@ const (DeleteUseOrderBy Code = 1105DeleteUseLimit Code = 1106InsertNotSpecifyColumn Code = 1107+ InsertUseOrderByRand Code = 1108// 1201 ~ 1299 collation error code.DisabledCollation Code = 1201diff --git a/plugin/advisor/mysql/advisor_insert_disallow_order_by_rand.go b/plugin/advisor/mysql/advisor_insert_disallow_order_by_rand.gonew file mode 100644index 0000000000..8379a60e47--- /dev/null+++ b/plugin/advisor/mysql/advisor_insert_disallow_order_by_rand.go@@ -0,0 +1,105 @@+package mysql++// Framework code is generated by the generator.++import (+ "fmt"++ "github.com/pingcap/tidb/parser/ast"++ "github.com/bytebase/bytebase/plugin/advisor"+ "github.com/bytebase/bytebase/plugin/advisor/db"+)++var (+ _ advisor.Advisor = (*InsertDisallowOrderByRandAdvisor)(nil)+ _ ast.Visitor = (*insertDisallowOrderByRandChecker)(nil)+)++func init() {+ advisor.Register(db.MySQL, advisor.MySQLInsertDisallowOrderByRand, &InsertDisallowOrderByRandAdvisor{})+ advisor.Register(db.TiDB, advisor.MySQLInsertDisallowOrderByRand, &InsertDisallowOrderByRandAdvisor{})+}++// InsertDisallowOrderByRandAdvisor is the advisor checking for to disallow order by rand in INSERT statements.+type InsertDisallowOrderByRandAdvisor struct {+}++// Check checks for to disallow order by rand in INSERT statements.+func (*InsertDisallowOrderByRandAdvisor) 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+ }+ checker := &insertDisallowOrderByRandChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }++ 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 insertDisallowOrderByRandChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+ text string+ line int+}++// Enter implements the ast.Visitor interface.+func (checker *insertDisallowOrderByRandChecker) Enter(in ast.Node) (ast.Node, bool) {+ code := advisor.Ok+ if insert, ok := in.(*ast.InsertStmt); ok {+ if insert.Select != nil {+ if selectNode, ok := insert.Select.(*ast.SelectStmt); ok {+ if selectNode.OrderBy != nil {+ for _, item := range selectNode.OrderBy.Items {+ if f, ok := item.Expr.(*ast.FuncCallExpr); ok {+ if f.FnName.L == ast.Rand {+ code = advisor.InsertUseOrderByRand+ break+ }+ }+ }+ }+ }+ }+ }++ if code != advisor.Ok {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: code,+ Title: checker.title,+ Content: fmt.Sprintf("\"%s\" uses ORDER BY RAND in the INSERT statement", checker.text),+ Line: checker.line,+ })+ }++ return in, false+}++// Leave implements the ast.Visitor interface.+func (*insertDisallowOrderByRandChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}diff --git a/plugin/advisor/mysql/advisor_insert_disallow_order_by_rand_test.go b/plugin/advisor/mysql/advisor_insert_disallow_order_by_rand_test.gonew file mode 100644index 0000000000..b767c50a3f--- /dev/null+++ b/plugin/advisor/mysql/advisor_insert_disallow_order_by_rand_test.go@@ -0,0 +1,43 @@+package mysql++// Framework code is generated by the generator.++import (+ "testing"++ "github.com/bytebase/bytebase/plugin/advisor"+)++func TestInsertDisallowOrderByRand(t *testing.T) {+ tests := []advisor.TestCase{+ {+ Statement: `INSERT INTO tech_book VALUES(1, "a")`,+ Want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ Statement: `INSERT INTO tech_book SELECT * FROM tech_book ORDER BY rand()`,+ Want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: advisor.InsertUseOrderByRand,+ Title: "insert.disallow-order-by-rand",+ Content: "\"INSERT INTO tech_book SELECT * FROM tech_book ORDER BY rand()\" uses ORDER BY RAND in the INSERT statement",+ Line: 1,+ },+ },+ },+ }++ advisor.RunSQLReviewRuleTests(t, tests, &InsertDisallowOrderByRandAdvisor{}, &advisor.SQLReviewRule{+ Type: advisor.SchemaRuleInsertDisallowOrderByRand,+ Level: advisor.SchemaRuleLevelWarning,+ Payload: "",+ }, advisor.MockMySQLDatabase)+}diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.goindex a6624edc8d..f1c1c1f5ae 100644--- a/plugin/advisor/sql_review.go+++ b/plugin/advisor/sql_review.go@@ -125,6 +125,8 @@ const (SchemaRuleInsertRowLimit SQLReviewRuleType = "insert.row-limit"// SchemaRuleInsertMustSpecifyColumn enforce the insert column specified.SchemaRuleInsertMustSpecifyColumn SQLReviewRuleType = "insert.must-specify-column"+ // SchemaRuleInsertDisallowOrderByRand disallow the order by rand in the INSERT statement.+ SchemaRuleInsertDisallowOrderByRand SQLReviewRuleType = "insert.disallow-order-by-rand"// TableNameTemplateToken is the token for table name.TableNameTemplateToken = "{{table}}"@@ -812,6 +814,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errcase db.MySQL, db.TiDB:return MySQLInsertMustSpecifyColumn, nil}+ case SchemaRuleInsertDisallowOrderByRand:+ switch engine {+ case db.MySQL, db.TiDB:+ return MySQLInsertDisallowOrderByRand, nil+ }case SchemaRuleStatementDisallowLimit:switch engine {case db.MySQL, db.TiDB: