Skip to content

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

feat(sql-review): implement MySQL insert-row-limit rule (#2829)

rebeliceaddb17b5445c
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 66581a4d79..2728dc88db 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -148,6 +148,9 @@ const (
// MySQLStatementDisallowCommit is an advisor type for MySQL to disallow commit.
MySQLStatementDisallowCommit Type = "bb.plugin.advisor.mysql.statement.disallow-commit"
+ // MySQLInsertRowLimit is an advisor type for MySQL to limit INSERT rows.
+ MySQLInsertRowLimit Type = "bb.plugin.advisor.mysql.insert.row-limit"
+
// PostgreSQL Advisor.
// PostgreSQLSyntax is an advisor type for PostgreSQL syntax.
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index 9de622c81b..c336b2bf9b 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -96,6 +96,9 @@ const (
// 1001 ~ 1099 charset error code.
DisabledCharset Code = 1001
+
+ // 1101 ~ 1199 insert error code.
+ InsertTooManyRows Code = 1101
)
// Int returns the int type of code.
diff --git a/plugin/advisor/mysql/advisor_insert_row_limit.go b/plugin/advisor/mysql/advisor_insert_row_limit.go
new file mode 100644
index 0000000000..39de0cebaf
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_insert_row_limit.go
@@ -0,0 +1,97 @@
+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 = (*InsertRowLimitAdvisor)(nil)
+ _ ast.Visitor = (*insertRowLimitChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLInsertRowLimit, &InsertRowLimitAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLInsertRowLimit, &InsertRowLimitAdvisor{})
+}
+
+// InsertRowLimitAdvisor is the advisor checking for to limit INSERT rows.
+type InsertRowLimitAdvisor struct {
+}
+
+// Check checks for to limit INSERT rows.
+func (*InsertRowLimitAdvisor) 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
+ }
+ payload, err := advisor.UnmarshalNumberLimitRulePayload(ctx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+ checker := &insertRowLimitChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ maxRow: payload.Number,
+ }
+
+ if payload.Number > 0 {
+ 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 insertRowLimitChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ line int
+ maxRow int
+}
+
+// Enter implements the ast.Visitor interface.
+func (checker *insertRowLimitChecker) Enter(in ast.Node) (ast.Node, bool) {
+ if node, ok := in.(*ast.InsertStmt); ok {
+ if len(node.Lists) > checker.maxRow {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.InsertTooManyRows,
+ Title: checker.title,
+ Content: fmt.Sprintf("The value rows in one INSERT statement should be no more than %d, but found %d", checker.maxRow, len(node.Lists)),
+ Line: checker.line,
+ })
+ }
+ }
+
+ return in, false
+}
+
+// Leave implements the ast.Visitor interface.
+func (*insertRowLimitChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
diff --git a/plugin/advisor/mysql/advisor_insert_row_limit_test.go b/plugin/advisor/mysql/advisor_insert_row_limit_test.go
new file mode 100644
index 0000000000..2a1ff4ddad
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_insert_row_limit_test.go
@@ -0,0 +1,49 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestInsertRowLimit(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 values(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.InsertTooManyRows,
+ Title: "insert.row-limit",
+ Content: "The value rows in one INSERT statement should be no more than 5, but found 6",
+ Line: 1,
+ },
+ },
+ },
+ }
+ payload, err := json.Marshal(advisor.NumberLimitRulePayload{
+ Number: 5,
+ })
+ require.NoError(t, err)
+ advisor.RunSQLReviewRuleTests(t, tests, &InsertRowLimitAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleInsertRowLimit,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: string(payload),
+ }, advisor.MockMySQLDatabase)
+}
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 84e052356b..1b55afd041 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -108,6 +108,9 @@ const (
// SchemaRuleCharsetAllowlist enforce the charset allowlist.
SchemaRuleCharsetAllowlist SQLReviewRuleType = "charset.allowlist"
+ // SchemaRuleInsertRowLimit enforce the insert row limit.
+ SchemaRuleInsertRowLimit SQLReviewRuleType = "insert.row-limit"
+
// TableNameTemplateToken is the token for table name.
TableNameTemplateToken = "{{table}}"
// ColumnListTemplateToken is the token for column name list.
@@ -200,7 +203,7 @@ func (rule *SQLReviewRule) Validate() error {
if _, err := UnmarshalCommentConventionRulePayload(rule.Payload); err != nil {
return err
}
- case SchemaRuleIndexKeyNumberLimit:
+ case SchemaRuleIndexKeyNumberLimit, SchemaRuleInsertRowLimit:
if _, err := UnmarshalNumberLimitRulePayload(rule.Payload); err != nil {
return err
}
@@ -764,6 +767,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLIndexTypeNoBlob, nil
}
+ case SchemaRuleInsertRowLimit:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLInsertRowLimit, nil
+ }
}
return Fake, errors.Errorf("unknown SQL review rule type %v for %v", ruleType, engine)
}