Skip to content

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

feat(sql-review): implement MySQL no LIMIT in the DELETE statement (#2854)

rebelice401acb656df0
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 718828f0c1..9a6b8d2a50 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -154,12 +154,12 @@ const (
// MySQLStatementDisallowCommit is an advisor type for MySQL to disallow commit.
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"
+
// MySQLInsertRowLimit is an advisor type for MySQL to limit INSERT rows.
MySQLInsertRowLimit Type = "bb.plugin.advisor.mysql.insert.row-limit"
- // 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"
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index 1e7baa9113..b7e19f899b 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -103,6 +103,7 @@ const (
InsertUseLimit Code = 1103
UpdateUseOrderBy Code = 1104
InsertUseOrderBy Code = 1105
+ DeleteUseLimit Code = 1106
)
// Int returns the int type of code.
diff --git a/plugin/advisor/mysql/advisor_insert_update_no_limit_test.go b/plugin/advisor/mysql/advisor_insert_update_no_limit_test.go
deleted file mode 100644
index 82959b6ab5..0000000000
--- a/plugin/advisor/mysql/advisor_insert_update_no_limit_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 TestInsertUpdateNoLimit(t *testing.T) {
- tests := []advisor.TestCase{
- {
- Statement: ``,
- Want: []advisor.Advice{
- {
- Status: advisor.Success,
- Code: advisor.Ok,
- Title: "OK",
- Content: "",
- },
- },
- },
- {
- Statement: `INSERT INTO tech_book SELECT * FROM tech_book LIMIT 1`,
- Want: []advisor.Advice{
- {
- Status: advisor.Warn,
- Code: advisor.InsertUseLimit,
- Title: "insert-update.no-limit",
- Content: "LIMIT clause is forbidden in INSERT and UPDATE statement, but \"INSERT INTO tech_book SELECT * FROM tech_book LIMIT 1\" uses",
- Line: 1,
- },
- },
- },
- {
- Statement: `INSERT INTO tech_book SELECT * FROM tech_book UNION SELECT * FROM tech_book LIMIT 1`,
- Want: []advisor.Advice{
- {
- Status: advisor.Warn,
- Code: advisor.InsertUseLimit,
- Title: "insert-update.no-limit",
- Content: "LIMIT clause is forbidden in INSERT and UPDATE statement, but \"INSERT INTO tech_book SELECT * FROM tech_book UNION SELECT * FROM tech_book LIMIT 1\" uses",
- Line: 1,
- },
- },
- },
- {
- Statement: `UPDATE tech_book SET name = 'my name' LIMIT 10`,
- Want: []advisor.Advice{
- {
- Status: advisor.Warn,
- Code: advisor.UpdateUseLimit,
- Title: "insert-update.no-limit",
- Content: "LIMIT clause is forbidden in INSERT and UPDATE statement, but \"UPDATE tech_book SET name = 'my name' LIMIT 10\" uses",
- Line: 1,
- },
- },
- },
- }
-
- advisor.RunSQLReviewRuleTests(t, tests, &InsertUpdateNoLimitAdvisor{}, &advisor.SQLReviewRule{
- Type: advisor.SchemaRuleInsertUpdateNoLimit,
- Level: advisor.SchemaRuleLevelWarning,
- Payload: "",
- }, advisor.MockMySQLDatabase)
-}
diff --git a/plugin/advisor/mysql/advisor_insert_update_no_limit.go b/plugin/advisor/mysql/advisor_stmt_disallow_limit.go
similarity index 67%
rename from plugin/advisor/mysql/advisor_insert_update_no_limit.go
rename to plugin/advisor/mysql/advisor_stmt_disallow_limit.go
index 313791f018..f043f01cb3 100644
--- a/plugin/advisor/mysql/advisor_insert_update_no_limit.go
+++ b/plugin/advisor/mysql/advisor_stmt_disallow_limit.go
@@ -12,21 +12,21 @@ import (
)
var (
- _ advisor.Advisor = (*InsertUpdateNoLimitAdvisor)(nil)
- _ ast.Visitor = (*insertUpdateNoLimitChecker)(nil)
+ _ advisor.Advisor = (*DisallowLimitAdvisor)(nil)
+ _ ast.Visitor = (*disallowLimitChecker)(nil)
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLInsertUpdateNoLimit, &InsertUpdateNoLimitAdvisor{})
- advisor.Register(db.TiDB, advisor.MySQLInsertUpdateNoLimit, &InsertUpdateNoLimitAdvisor{})
+ advisor.Register(db.MySQL, advisor.MySQLDisallowLimit, &DisallowLimitAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLDisallowLimit, &DisallowLimitAdvisor{})
}
-// InsertUpdateNoLimitAdvisor is the advisor checking for no LIMIT clause in INSERT/UPDATE statement.
-type InsertUpdateNoLimitAdvisor struct {
+// DisallowLimitAdvisor is the advisor checking for no LIMIT clause in INSERT/UPDATE statement.
+type DisallowLimitAdvisor struct {
}
// Check checks for no LIMIT clause in INSERT/UPDATE statement.
-func (*InsertUpdateNoLimitAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+func (*DisallowLimitAdvisor) 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 (*InsertUpdateNoLimitAdvisor) Check(ctx advisor.Context, statement string)
if err != nil {
return nil, err
}
- checker := &insertUpdateNoLimitChecker{
+ checker := &disallowLimitChecker{
level: level,
title: string(ctx.Rule.Type),
}
@@ -58,7 +58,7 @@ func (*InsertUpdateNoLimitAdvisor) Check(ctx advisor.Context, statement string)
return checker.adviceList, nil
}
-type insertUpdateNoLimitChecker struct {
+type disallowLimitChecker struct {
adviceList []advisor.Advice
level advisor.Status
title string
@@ -67,13 +67,17 @@ type insertUpdateNoLimitChecker struct {
}
// Enter implements the ast.Visitor interface.
-func (checker *insertUpdateNoLimitChecker) Enter(in ast.Node) (ast.Node, bool) {
+func (checker *disallowLimitChecker) Enter(in ast.Node) (ast.Node, bool) {
code := advisor.Ok
switch node := in.(type) {
case *ast.UpdateStmt:
if node.Limit != nil {
code = advisor.UpdateUseLimit
}
+ case *ast.DeleteStmt:
+ if node.Limit != nil {
+ code = advisor.DeleteUseLimit
+ }
case *ast.InsertStmt:
if useLimit(node) {
code = advisor.InsertUseLimit
@@ -85,7 +89,7 @@ func (checker *insertUpdateNoLimitChecker) Enter(in ast.Node) (ast.Node, bool) {
Status: checker.level,
Code: code,
Title: checker.title,
- Content: fmt.Sprintf("LIMIT clause is forbidden in INSERT and UPDATE statement, but \"%s\" uses", checker.text),
+ Content: fmt.Sprintf("LIMIT clause is forbidden in INSERT, UPDATE and DELETE statement, but \"%s\" uses", checker.text),
Line: checker.line,
})
}
@@ -94,7 +98,7 @@ func (checker *insertUpdateNoLimitChecker) Enter(in ast.Node) (ast.Node, bool) {
}
// Leave implements the ast.Visitor interface.
-func (*insertUpdateNoLimitChecker) Leave(in ast.Node) (ast.Node, bool) {
+func (*disallowLimitChecker) Leave(in ast.Node) (ast.Node, bool) {
return in, true
}
diff --git a/plugin/advisor/mysql/advisor_stmt_disallow_limit_test.go b/plugin/advisor/mysql/advisor_stmt_disallow_limit_test.go
new file mode 100644
index 0000000000..956b6d06f9
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_stmt_disallow_limit_test.go
@@ -0,0 +1,79 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestDisallowLimit(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: ``,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: `INSERT INTO tech_book SELECT * FROM tech_book LIMIT 1`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.InsertUseLimit,
+ Title: "statement.disallow-limit",
+ Content: "LIMIT clause is forbidden in INSERT, UPDATE and DELETE statement, but \"INSERT INTO tech_book SELECT * FROM tech_book LIMIT 1\" uses",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `INSERT INTO tech_book SELECT * FROM tech_book UNION SELECT * FROM tech_book LIMIT 1`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.InsertUseLimit,
+ Title: "statement.disallow-limit",
+ Content: "LIMIT clause is forbidden in INSERT, UPDATE and DELETE statement, but \"INSERT INTO tech_book SELECT * FROM tech_book UNION SELECT * FROM tech_book LIMIT 1\" uses",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `UPDATE tech_book SET name = 'my name' LIMIT 10`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.UpdateUseLimit,
+ Title: "statement.disallow-limit",
+ Content: "LIMIT clause is forbidden in INSERT, UPDATE and DELETE statement, but \"UPDATE tech_book SET name = 'my name' LIMIT 10\" uses",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `DELETE FROM tech_book LIMIT 10`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.DeleteUseLimit,
+ Title: "statement.disallow-limit",
+ Content: "LIMIT clause is forbidden in INSERT, UPDATE and DELETE statement, but \"DELETE FROM tech_book LIMIT 10\" uses",
+ Line: 1,
+ },
+ },
+ },
+ }
+
+ advisor.RunSQLReviewRuleTests(t, tests, &DisallowLimitAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleStatementDisallowLimit,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: "",
+ }, advisor.MockMySQLDatabase)
+}
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 153b1a57ae..aabe451075 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -57,6 +57,8 @@ 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 SQLReviewRuleType = "statement.disallow-limit"
// SchemaRuleTableRequirePK require the table to have a primary key.
SchemaRuleTableRequirePK SQLReviewRuleType = "table.require-pk"
@@ -114,8 +116,6 @@ const (
// SchemaRuleInsertRowLimit enforce the insert row limit.
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"
@@ -790,10 +790,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLInsertRowLimit, nil
}
- case SchemaRuleInsertUpdateNoLimit:
+ case SchemaRuleStatementDisallowLimit:
switch engine {
case db.MySQL, db.TiDB:
- return MySQLInsertUpdateNoLimit, nil
+ return MySQLDisallowLimit, nil
}
case SchemaRuleInsertUpdateNoOrderBy:
switch engine {