Skip to content

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

feat(advisor): limit value list len in PostgreSQL INSERT statement (#2929)

zeroslopeaee90e7b3e7e
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index e601a72497..c4a368f399 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -212,6 +212,9 @@ const (
// PostgreSQLTableNoFK is an advisor type for PostgreSQL table disallow foreign key.
PostgreSQLTableNoFK Type = "bb.plugin.advisor.postgresql.table.no-foreign-key"
+
+ // PostgreSQLInsertRowLimit is an advisor type for PostgreSQL to limit INSERT rows.
+ PostgreSQLInsertRowLimit Type = "bb.plugin.advisor.postgresql.insert.row-limit"
)
// Advice is the result of an advisor.
diff --git a/plugin/advisor/pg/advisor_insert_row_limit.go b/plugin/advisor/pg/advisor_insert_row_limit.go
new file mode 100644
index 0000000000..37eff4aa6b
--- /dev/null
+++ b/plugin/advisor/pg/advisor_insert_row_limit.go
@@ -0,0 +1,96 @@
+package pg
+
+import (
+ "fmt"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/db"
+ "github.com/bytebase/bytebase/plugin/parser/ast"
+)
+
+var (
+ _ advisor.Advisor = (*InsertRowLimitAdvisor)(nil)
+ _ ast.Visitor = (*insertRowLimitChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.Postgres, advisor.PostgreSQLInsertRowLimit, &InsertRowLimitAdvisor{})
+}
+
+// InsertRowLimitAdvisor is the advisor checking for to limit INSERT rows.
+type InsertRowLimitAdvisor struct {
+}
+
+// Check checks for the WHERE clause requirement.
+func (*InsertRowLimitAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ stmts, errAdvice := parseStatement(statement)
+ 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 stmts {
+ checker.text = stmt.Text()
+ checker.line = stmt.LastLine()
+ ast.Walk(checker, stmt)
+ }
+ }
+
+ 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
+}
+
+// Visit implements the ast.Visitor interface.
+func (checker *insertRowLimitChecker) Visit(node ast.Node) ast.Visitor {
+ code := advisor.Ok
+ rows := 0
+
+ n, ok := node.(*ast.InsertStmt)
+ if ok {
+ if len(n.ValueList) > checker.maxRow {
+ code = advisor.InsertTooManyRows
+ rows = len(n.ValueList)
+ }
+ }
+
+ if code != advisor.Ok {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: code,
+ Title: checker.title,
+ Content: fmt.Sprintf("The value rows in \"%s\" should be no more than %d, but found %d", checker.text, checker.maxRow, rows),
+ Line: checker.line,
+ })
+ }
+ return checker
+}
diff --git a/plugin/advisor/pg/advisor_insert_row_limit_test.go b/plugin/advisor/pg/advisor_insert_row_limit_test.go
new file mode 100644
index 0000000000..6e7f89c084
--- /dev/null
+++ b/plugin/advisor/pg/advisor_insert_row_limit_test.go
@@ -0,0 +1,49 @@
+package pg
+
+// 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 \"INSERT INTO tech_book values(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f')\" 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.MockPostgreSQLDatabase)
+}