Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor(sql-review): refactor MySQL stmt-disallow-order-by rule (#2939)

rebeliceae28fcebbf9b
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index c4a368f399..f6e29a18e0 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -158,13 +158,13 @@ const (
MySQLStatementDisallowCommit Type = "bb.plugin.advisor.mysql.statement.disallow-commit"
// MySQLDisallowLimit is an advisor type for MySQL no LIMIT clause in INSERT/UPDATE/DELETE statement.
- MySQLDisallowLimit Type = "bb.plugin.advisor.mysql.disallow.disallow-limit"
+ MySQLDisallowLimit Type = "bb.plugin.advisor.mysql.statement.disallow-limit"
// MySQLInsertRowLimit is an advisor type for MySQL to limit INSERT rows.
MySQLInsertRowLimit Type = "bb.plugin.advisor.mysql.insert.row-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"
+ // 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"
// PostgreSQL Advisor.
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index 5941bee75c..c122abc469 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -103,7 +103,7 @@ const (
UpdateUseLimit Code = 1102
InsertUseLimit Code = 1103
UpdateUseOrderBy Code = 1104
- InsertUseOrderBy Code = 1105
+ DeleteUseOrderBy Code = 1105
DeleteUseLimit Code = 1106
)
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.go
deleted file mode 100644
index 5baa3d6055..0000000000
--- a/plugin/advisor/mysql/advisor_insert_update_no_order_by_test.go
+++ /dev/null
@@ -1,67 +0,0 @@
-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/mysql/advisor_insert_update_no_order_by.go b/plugin/advisor/mysql/advisor_stmt_disallow_order_by.go
similarity index 53%
rename from plugin/advisor/mysql/advisor_insert_update_no_order_by.go
rename to plugin/advisor/mysql/advisor_stmt_disallow_order_by.go
index 640ed3cd8c..35aa19fd39 100644
--- a/plugin/advisor/mysql/advisor_insert_update_no_order_by.go
+++ b/plugin/advisor/mysql/advisor_stmt_disallow_order_by.go
@@ -12,21 +12,21 @@ import (
)
var (
- _ advisor.Advisor = (*InsertUpdateNoOrderByAdvisor)(nil)
- _ ast.Visitor = (*insertUpdateNoOrderByChecker)(nil)
+ _ advisor.Advisor = (*DisallowOrderByAdvisor)(nil)
+ _ ast.Visitor = (*disallowOrderByChecker)(nil)
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLInsertUpdateNoOrderBy, &InsertUpdateNoOrderByAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLInsertUpdateNoOrderBy, &InsertUpdateNoOrderByAdvisor{})
+ advisor.Register(db.MySQL, advisor.MySQLDisallowOrderBy, &DisallowOrderByAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLDisallowOrderBy, &DisallowOrderByAdvisor{})
}
-// InsertUpdateNoOrderByAdvisor is the advisor checking for no ORDER BY clause in INSERT/UPDATE statement.
-type InsertUpdateNoOrderByAdvisor struct {
+// DisallowOrderByAdvisor is the advisor checking for no ORDER BY clause in DELETE/UPDATE statements.
+type DisallowOrderByAdvisor struct {
}
-// Check checks for no ORDER BY clause in INSERT/UPDATE statement.
-func (*InsertUpdateNoOrderByAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+// Check checks for no ORDER BY clause in DELETE/UPDATE statements.
+func (*DisallowOrderByAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
stmtList, errAdvice := parseStatement(statement, ctx.Charset, ctx.Collation)
if errAdvice != nil {
return errAdvice, nil
@@ -36,7 +36,7 @@ func (*InsertUpdateNoOrderByAdvisor) Check(ctx advisor.Context, statement string
if err != nil {
return nil, err
}
- checker := &insertUpdateNoOrderByChecker{
+ checker := &disallowOrderByChecker{
level: level,
title: string(ctx.Rule.Type),
}
@@ -58,7 +58,7 @@ func (*InsertUpdateNoOrderByAdvisor) Check(ctx advisor.Context, statement string
return checker.adviceList, nil
}
-type insertUpdateNoOrderByChecker struct {
+type disallowOrderByChecker struct {
adviceList []advisor.Advice
level advisor.Status
title string
@@ -67,16 +67,16 @@ type insertUpdateNoOrderByChecker struct {
}
// Enter implements the ast.Visitor interface.
-func (checker *insertUpdateNoOrderByChecker) Enter(in ast.Node) (ast.Node, bool) {
+func (checker *disallowOrderByChecker) 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
+ case *ast.DeleteStmt:
+ if node.Order != nil {
+ code = advisor.DeleteUseOrderBy
}
}
@@ -85,7 +85,7 @@ func (checker *insertUpdateNoOrderByChecker) Enter(in ast.Node) (ast.Node, bool)
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),
+ Content: fmt.Sprintf("ORDER BY clause is forbidden in DELETE and UPDATE statements, but \"%s\" uses", checker.text),
Line: checker.line,
})
}
@@ -93,18 +93,6 @@ func (checker *insertUpdateNoOrderByChecker) Enter(in ast.Node) (ast.Node, bool)
}
// Leave implements the ast.Visitor interface.
-func (*insertUpdateNoOrderByChecker) Leave(in ast.Node) (ast.Node, bool) {
+func (*disallowOrderByChecker) 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_stmt_disallow_order_by_test.go b/plugin/advisor/mysql/advisor_stmt_disallow_order_by_test.go
new file mode 100644
index 0000000000..528dcc821f
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_stmt_disallow_order_by_test.go
@@ -0,0 +1,55 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestDisallowOrderBy(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: `UPDATE tech_book SET name = 'my name' ORDER BY id`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.UpdateUseOrderBy,
+ Title: "statement.disallow-order-by",
+ Content: "ORDER BY clause is forbidden in DELETE and UPDATE statements, but \"UPDATE tech_book SET name = 'my name' ORDER BY id\" uses",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `DELETE FROM tech_book ORDER BY id`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.DeleteUseOrderBy,
+ Title: "statement.disallow-order-by",
+ Content: "ORDER BY clause is forbidden in DELETE and UPDATE statements, but \"DELETE FROM tech_book ORDER BY id\" uses",
+ Line: 1,
+ },
+ },
+ },
+ }
+
+ advisor.RunSQLReviewRuleTests(t, tests, &DisallowOrderByAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleStatementDisallowOrderBy,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: "",
+ }, advisor.MockMySQLDatabase)
+}
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 9803e0101e..65cbe13bee 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -57,8 +57,10 @@ const (
SchemaRuleStatementNoCreateTableAs SQLReviewRuleType = "statement.create-table.no-create-table-as"
// SchemaRuleStatementDisallowCommit disallow using commit in the issue.
SchemaRuleStatementDisallowCommit SQLReviewRuleType = "statement.disallow-commit"
- // SchemaRuleStatementDisallowLimit disallow the LIMIT clause in INSERT and UPDATE statement.
+ // SchemaRuleStatementDisallowLimit disallow the LIMIT clause in INSERT, DELETE and UPDATE statements.
SchemaRuleStatementDisallowLimit SQLReviewRuleType = "statement.disallow-limit"
+ // SchemaRuleStatementDisallowOrderBy disallow the ORDER BY clause in DELETE and UPDATE statements.
+ SchemaRuleStatementDisallowOrderBy SQLReviewRuleType = "statement.disallow-order-by"
// SchemaRuleTableRequirePK require the table to have a primary key.
SchemaRuleTableRequirePK SQLReviewRuleType = "table.require-pk"
@@ -118,8 +120,6 @@ const (
// SchemaRuleInsertRowLimit enforce the insert row limit.
SchemaRuleInsertRowLimit SQLReviewRuleType = "insert.row-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}}"
@@ -802,10 +802,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLDisallowLimit, nil
}
- case SchemaRuleInsertUpdateNoOrderBy:
+ case SchemaRuleStatementDisallowOrderBy:
switch engine {
case db.MySQL, db.TiDB:
- return MySQLInsertUpdateNoOrderBy, nil
+ return MySQLDisallowOrderBy, nil
}
}
return Fake, errors.Errorf("unknown SQL review rule type %v for %v", ruleType, engine)