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 clause in INSERT/UPDATE statements (#2834)

rebelice712e49c62be7
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index c840149b86..82a3fd2a94 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -154,6 +154,9 @@ const (
// 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"
+
// PostgreSQL Advisor.
// PostgreSQLSyntax is an advisor type for PostgreSQL syntax.
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index a9c8259db7..35825c56d3 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -98,8 +98,10 @@ const (
// 1001 ~ 1099 charset error code.
DisabledCharset Code = 1001
- // 1101 ~ 1199 insert error code.
+ // 1101 ~ 1199 insert/update error code.
InsertTooManyRows Code = 1101
+ UpdateUseLimit Code = 1102
+ InsertUseLimit Code = 1103
)
// Int returns the int type of code.
diff --git a/plugin/advisor/mysql/advisor_insert_update_no_limit.go b/plugin/advisor/mysql/advisor_insert_update_no_limit.go
new file mode 100644
index 0000000000..313791f018
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_insert_update_no_limit.go
@@ -0,0 +1,111 @@
+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 = (*InsertUpdateNoLimitAdvisor)(nil)
+ _ ast.Visitor = (*insertUpdateNoLimitChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLInsertUpdateNoLimit, &InsertUpdateNoLimitAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLInsertUpdateNoLimit, &InsertUpdateNoLimitAdvisor{})
+}
+
+// InsertUpdateNoLimitAdvisor is the advisor checking for no LIMIT clause in INSERT/UPDATE statement.
+type InsertUpdateNoLimitAdvisor struct {
+}
+
+// Check checks for no LIMIT clause in INSERT/UPDATE statement.
+func (*InsertUpdateNoLimitAdvisor) 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 := &insertUpdateNoLimitChecker{
+ 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 insertUpdateNoLimitChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ line int
+}
+
+// Enter implements the ast.Visitor interface.
+func (checker *insertUpdateNoLimitChecker) 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.InsertStmt:
+ if useLimit(node) {
+ code = advisor.InsertUseLimit
+ }
+ }
+
+ if code != advisor.Ok {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ 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),
+ Line: checker.line,
+ })
+ }
+
+ return in, false
+}
+
+// Leave implements the ast.Visitor interface.
+func (*insertUpdateNoLimitChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
+
+func useLimit(node *ast.InsertStmt) bool {
+ if node.Select != nil {
+ switch stmt := node.Select.(type) {
+ case *ast.SelectStmt:
+ return stmt.Limit != nil
+ case *ast.SetOprStmt:
+ return stmt.Limit != nil
+ }
+ }
+ return false
+}
diff --git a/plugin/advisor/mysql/advisor_insert_update_no_limit_test.go b/plugin/advisor/mysql/advisor_insert_update_no_limit_test.go
new file mode 100644
index 0000000000..82959b6ab5
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_insert_update_no_limit_test.go
@@ -0,0 +1,67 @@
+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/sql_review.go b/plugin/advisor/sql_review.go
index 67380d8c4a..45d8ef4d02 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -112,6 +112,8 @@ 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"
// TableNameTemplateToken is the token for table name.
TableNameTemplateToken = "{{table}}"
@@ -779,6 +781,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLInsertRowLimit, nil
}
+ case SchemaRuleInsertUpdateNoLimit:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLInsertUpdateNoLimit, nil
+ }
}
return Fake, errors.Errorf("unknown SQL review rule type %v for %v", ruleType, engine)
}