Skip to content

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

feat(schema system): let WHERE required rule support SELECT statement (#1280)

rebeliceec771a10309d
diff --git a/plugin/advisor/mysql/advisor_stmt_where_required.go b/plugin/advisor/mysql/advisor_stmt_where_required.go
index 3fe819cc00..b41e5ccf2e 100644
--- a/plugin/advisor/mysql/advisor_stmt_where_required.go
+++ b/plugin/advisor/mysql/advisor_stmt_where_required.go
@@ -19,11 +19,11 @@ func init() {
advisor.Register(db.TiDB, advisor.MySQLWhereRequirement, &WhereRequirementAdvisor{})
}
-// WhereRequirementAdvisor is the advisor checking for the WHERE clause requirement for UPDATE/DELETE.
+// WhereRequirementAdvisor is the advisor checking for the WHERE clause requirement.
type WhereRequirementAdvisor struct {
}
-// Check checks for the WHERE clause requirement for UPDATE/DELETE.
+// Check checks for the WHERE clause requirement.
func (adv *WhereRequirementAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
root, errAdvice := parseStatement(statement, ctx.Charset, ctx.Collation)
if errAdvice != nil {
@@ -32,7 +32,7 @@ func (adv *WhereRequirementAdvisor) Check(ctx advisor.Context, statement string)
level, err := advisor.NewStatusBySchemaReviewRuleLevel(ctx.Rule.Level)
if err != nil {
- return []advisor.Advice{}, err
+ return nil, err
}
we := &whereRequirementChecker{level: level}
for _, stmtNode := range root {
@@ -69,6 +69,11 @@ func (v *whereRequirementChecker) Enter(in ast.Node) (ast.Node, bool) {
if node.Where == nil {
code = common.StatementNoWhere
}
+ // SELECT
+ case *ast.SelectStmt:
+ if node.Where == nil {
+ code = common.StatementNoWhere
+ }
}
if code != common.Ok {
diff --git a/plugin/advisor/mysql/advisor_stmt_where_required_test.go b/plugin/advisor/mysql/advisor_stmt_where_required_test.go
index ae816b2680..00996dfe04 100644
--- a/plugin/advisor/mysql/advisor_stmt_where_required_test.go
+++ b/plugin/advisor/mysql/advisor_stmt_where_required_test.go
@@ -54,6 +54,28 @@ func TestWhereRequirement(t *testing.T) {
},
},
},
+ {
+ statement: "SELECT a FROM t",
+ want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: common.StatementNoWhere,
+ Title: "Require WHERE clause",
+ Content: "\"SELECT a FROM t\" requires WHERE clause",
+ },
+ },
+ },
+ {
+ statement: "SELECT a FROM t WHERE a > 0",
+ want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: common.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
}
runSchemaReviewRuleTests(t, tests, &WhereRequirementAdvisor{}, &api.SchemaReviewRule{