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 disallow ORDER BY clause in INSERT/UPDATE (#2835)
rebelice397e72d89972
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex 82a3fd2a94..e9588324c6 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -157,6 +157,9 @@ const (// MySQLInsertUpdateNoLimit is an advisor type for MySQL no LIMIT clause in INSERT/UPDATE statement.MySQLInsertUpdateNoLimit Type = "bb.plugin.advisor.mysql.insert-update.no-limit"+ // MySQLInsertUpdateNoOrderBy is an advisor type for MySQL no ORDER BY clause in INSERT/UPDATE statement.+ MySQLInsertUpdateNoOrderBy Type = "bb.plugin.advisor.mysql.insert-update.no-order-by"+// PostgreSQL Advisor.// PostgreSQLSyntax is an advisor type for PostgreSQL syntax.diff --git a/plugin/advisor/code.go b/plugin/advisor/code.goindex 35825c56d3..e4cdfa8d1a 100644--- a/plugin/advisor/code.go+++ b/plugin/advisor/code.go@@ -102,6 +102,8 @@ const (InsertTooManyRows Code = 1101UpdateUseLimit Code = 1102InsertUseLimit Code = 1103+ UpdateUseOrderBy Code = 1104+ InsertUseOrderBy Code = 1105)// Int returns the int type of code.diff --git a/plugin/advisor/mysql/advisor_insert_update_no_order_by.go b/plugin/advisor/mysql/advisor_insert_update_no_order_by.gonew file mode 100644index 0000000000..640ed3cd8c--- /dev/null+++ b/plugin/advisor/mysql/advisor_insert_update_no_order_by.go@@ -0,0 +1,110 @@+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 = (*InsertUpdateNoOrderByAdvisor)(nil)+ _ ast.Visitor = (*insertUpdateNoOrderByChecker)(nil)+)++func init() {+ advisor.Register(db.MySQL, advisor.MySQLInsertUpdateNoOrderBy, &InsertUpdateNoOrderByAdvisor{})+ advisor.Register(db.TiDB, advisor.MySQLInsertUpdateNoOrderBy, &InsertUpdateNoOrderByAdvisor{})+}++// InsertUpdateNoOrderByAdvisor is the advisor checking for no ORDER BY clause in INSERT/UPDATE statement.+type InsertUpdateNoOrderByAdvisor struct {+}++// Check checks for no ORDER BY clause in INSERT/UPDATE statement.+func (*InsertUpdateNoOrderByAdvisor) 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 := &insertUpdateNoOrderByChecker{+ 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 insertUpdateNoOrderByChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+ text string+ line int+}++// Enter implements the ast.Visitor interface.+func (checker *insertUpdateNoOrderByChecker) Enter(in ast.Node) (ast.Node, bool) {+ code := advisor.Ok+ switch node := in.(type) {+ case *ast.UpdateStmt:+ if node.Order != nil {+ code = advisor.UpdateUseOrderBy+ }+ case *ast.InsertStmt:+ if useOrderBy(node) {+ code = advisor.InsertUseOrderBy+ }+ }++ if code != advisor.Ok {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: code,+ Title: checker.title,+ Content: fmt.Sprintf("ORDER BY clause is forbidden in INSERT and UPDATE statement, but \"%s\" uses", checker.text),+ Line: checker.line,+ })+ }+ return in, false+}++// Leave implements the ast.Visitor interface.+func (*insertUpdateNoOrderByChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}++func useOrderBy(node *ast.InsertStmt) bool {+ if node.Select != nil {+ switch stmt := node.Select.(type) {+ case *ast.SelectStmt:+ return stmt.OrderBy != nil+ case *ast.SetOprStmt:+ return stmt.OrderBy != nil+ }+ }+ return false+}diff --git a/plugin/advisor/mysql/advisor_insert_update_no_order_by_test.go b/plugin/advisor/mysql/advisor_insert_update_no_order_by_test.gonew file mode 100644index 0000000000..5baa3d6055--- /dev/null+++ b/plugin/advisor/mysql/advisor_insert_update_no_order_by_test.go@@ -0,0 +1,67 @@+package mysql++// Framework code is generated by the generator.++import (+ "testing"++ "github.com/bytebase/bytebase/plugin/advisor"+)++func TestInsertUpdateNoOrderBy(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 id`,+ Want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: advisor.InsertUseOrderBy,+ Title: "insert-update.no-order-by",+ Content: "ORDER BY clause is forbidden in INSERT and UPDATE statement, but \"INSERT INTO tech_book SELECT * FROM tech_book ORDER BY id\" uses",+ Line: 1,+ },+ },+ },+ {+ Statement: `INSERT INTO tech_book SELECT * FROM tech_book UNION SELECT * FROM tech_book ORDER BY id`,+ Want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: advisor.InsertUseOrderBy,+ Title: "insert-update.no-order-by",+ Content: "ORDER BY clause is forbidden in INSERT and UPDATE statement, but \"INSERT INTO tech_book SELECT * FROM tech_book UNION SELECT * FROM tech_book ORDER BY id\" uses",+ Line: 1,+ },+ },+ },+ {+ Statement: `UPDATE tech_book SET name = 'my name' ORDER BY id`,+ Want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: advisor.UpdateUseOrderBy,+ Title: "insert-update.no-order-by",+ Content: "ORDER BY clause is forbidden in INSERT and UPDATE statement, but \"UPDATE tech_book SET name = 'my name' ORDER BY id\" uses",+ Line: 1,+ },+ },+ },+ }++ advisor.RunSQLReviewRuleTests(t, tests, &InsertUpdateNoOrderByAdvisor{}, &advisor.SQLReviewRule{+ Type: advisor.SchemaRuleInsertUpdateNoOrderBy,+ Level: advisor.SchemaRuleLevelWarning,+ Payload: "",+ }, advisor.MockMySQLDatabase)+}diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.goindex 45d8ef4d02..283a984b4b 100644--- a/plugin/advisor/sql_review.go+++ b/plugin/advisor/sql_review.go@@ -114,6 +114,8 @@ const (SchemaRuleInsertRowLimit SQLReviewRuleType = "insert.row-limit"// SchemaRuleInsertUpdateNoLimit disallow the LIMIT clause in INSERT and UPDATE statement.SchemaRuleInsertUpdateNoLimit SQLReviewRuleType = "insert-update.no-limit"+ // SchemaRuleInsertUpdateNoOrderBy disallow the ORDER BY clause in INSERT and UPDATE statement.+ SchemaRuleInsertUpdateNoOrderBy SQLReviewRuleType = "insert-update.no-order-by"// TableNameTemplateToken is the token for table name.TableNameTemplateToken = "{{table}}"@@ -786,6 +788,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errcase db.MySQL, db.TiDB:return MySQLInsertUpdateNoLimit, nil}+ case SchemaRuleInsertUpdateNoOrderBy:+ switch engine {+ case db.MySQL, db.TiDB:+ return MySQLInsertUpdateNoOrderBy, nil+ }}return Fake, errors.Errorf("unknown SQL review rule type %v for %v", ruleType, engine)}