Skip to content

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

feat: add a MySQL UPDATE/DELTE statement WHERE clause requirement advisor (#1099)

rebelicee69afd7ad53a
diff --git a/api/task_check_run.go b/api/task_check_run.go
index a581240f82..1d9dbd0884 100644
--- a/api/task_check_run.go
+++ b/api/task_check_run.go
@@ -73,6 +73,8 @@ const (
TaskCheckDatabaseStatementSyntax TaskCheckType = "bb.task-check.database.statement.syntax"
// TaskCheckDatabaseStatementCompatibility is the task check type for statement compatibility.
TaskCheckDatabaseStatementCompatibility TaskCheckType = "bb.task-check.database.statement.compatibility"
+ // TaskCheckDatabaseStatementRequireWhere is the task check type for the WHRER clause requirement for UPDATE/DELETE.
+ TaskCheckDatabaseStatementRequireWhere TaskCheckType = "bb.task-check.database.statement.where.require"
// TaskCheckDatabaseConnect is the task check type for database connection.
TaskCheckDatabaseConnect TaskCheckType = "bb.task-check.database.connect"
// TaskCheckInstanceMigrationSchema is the task check type for migrating schemas.
diff --git a/common/error.go b/common/error.go
index e870c79bd4..d3de8b2ad2 100644
--- a/common/error.go
+++ b/common/error.go
@@ -47,6 +47,8 @@ const (
CompatibilityAddCheck Code = 10009
CompatibilityAlterCheck Code = 10010
CompatibilityAlterColumn Code = 10011
+
+ StatementNoWhere Code = 10101
)
// Error represents an application-specific error. Application errors can be
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 053136c518..6a3ae87ba6 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -45,6 +45,8 @@ const (
MySQLSyntax Type = "bb.plugin.advisor.mysql.syntax"
// MySQLMigrationCompatibility is an advisor type for MySQL migration compatibility.
MySQLMigrationCompatibility Type = "bb.plugin.advisor.mysql.migration-compatibility"
+ // MySQLWhereRequirement is an advisor type for MySQL WHERE clause requirement.
+ MySQLWhereRequirement Type = "bb.plugin.advisor.mysql.where.require"
)
// Advice is the result of an advisor.
diff --git a/plugin/advisor/mysql/mysql_migration_compatibility_advisor_test.go b/plugin/advisor/mysql/mysql_migration_compatibility_advisor_test.go
index 47bec4fff8..6a46a5b498 100644
--- a/plugin/advisor/mysql/mysql_migration_compatibility_advisor_test.go
+++ b/plugin/advisor/mysql/mysql_migration_compatibility_advisor_test.go
@@ -17,8 +17,7 @@ type test struct {
want []advisor.Advice
}
-func runTests(t *testing.T, tests []test) {
- adv := CompatibilityAdvisor{}
+func runTests(t *testing.T, tests []test, adv advisor.Advisor) {
logger, _ := zap.NewDevelopmentConfig().Build()
ctx := advisor.Context{
Logger: logger,
@@ -108,7 +107,7 @@ func TestBasic(t *testing.T) {
},
}
- runTests(t, tests)
+ runTests(t, tests, &CompatibilityAdvisor{})
}
func TestAlterTable(t *testing.T) {
@@ -267,7 +266,7 @@ func TestAlterTable(t *testing.T) {
},
}
- runTests(t, tests)
+ runTests(t, tests, &CompatibilityAdvisor{})
}
func TestAlterTableChangeColumnType(t *testing.T) {
@@ -329,5 +328,5 @@ func TestAlterTableChangeColumnType(t *testing.T) {
},
}
- runTests(t, tests)
+ runTests(t, tests, &CompatibilityAdvisor{})
}
diff --git a/plugin/advisor/mysql/mysql_where_requirement_advisor.go b/plugin/advisor/mysql/mysql_where_requirement_advisor.go
new file mode 100644
index 0000000000..033db178ee
--- /dev/null
+++ b/plugin/advisor/mysql/mysql_where_requirement_advisor.go
@@ -0,0 +1,89 @@
+package mysql
+
+import (
+ "fmt"
+
+ "github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/db"
+
+ "github.com/pingcap/tidb/parser/ast"
+)
+
+var (
+ _ advisor.Advisor = (*WhereRequirementAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLWhereRequirement, &WhereRequirementAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLWhereRequirement, &WhereRequirementAdvisor{})
+}
+
+// WhereRequirementAdvisor is the advisor checking for the WHERE clause requirement for UPDATE/DELETE.
+type WhereRequirementAdvisor struct {
+}
+
+// Check checks for the WHERE clause requirement for UPDATE/DELETE.
+func (adv *WhereRequirementAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ p := newParser()
+
+ root, _, err := p.Parse(statement, ctx.Charset, ctx.Collation)
+ if err != nil {
+ return []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Title: "Syntax error",
+ Content: err.Error(),
+ },
+ }, nil
+ }
+
+ we := &whereRequirementChecker{}
+ for _, stmtNode := range root {
+ (stmtNode).Accept(we)
+ }
+
+ if len(we.advisorList) == 0 {
+ we.advisorList = append(we.advisorList, advisor.Advice{
+ Status: advisor.Success,
+ Code: common.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return we.advisorList, nil
+}
+
+type whereRequirementChecker struct {
+ advisorList []advisor.Advice
+}
+
+func (v *whereRequirementChecker) Enter(in ast.Node) (ast.Node, bool) {
+ code := common.Ok
+ switch node := in.(type) {
+ // DELETE
+ case *ast.DeleteStmt:
+ if node.Where == nil {
+ code = common.StatementNoWhere
+ }
+ // UPDATE
+ case *ast.UpdateStmt:
+ if node.Where == nil {
+ code = common.StatementNoWhere
+ }
+ }
+
+ if code != common.Ok {
+ v.advisorList = append(v.advisorList, advisor.Advice{
+ Status: advisor.Warn,
+ Code: code,
+ Title: "Require WHERE clause",
+ Content: fmt.Sprintf("%q requires WHERE clause", in.Text()),
+ })
+ }
+ return in, false
+}
+
+func (v *whereRequirementChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
diff --git a/plugin/advisor/mysql/mysql_where_requirement_advisor_test.go b/plugin/advisor/mysql/mysql_where_requirement_advisor_test.go
new file mode 100644
index 0000000000..df67949716
--- /dev/null
+++ b/plugin/advisor/mysql/mysql_where_requirement_advisor_test.go
@@ -0,0 +1,59 @@
+package mysql
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestWhereRequirement(t *testing.T) {
+ tests := []test{
+ {
+ statement: "DELETE FROM t1",
+ want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: common.StatementNoWhere,
+ Title: "Require WHERE clause",
+ Content: "\"DELETE FROM t1\" requires WHERE clause",
+ },
+ },
+ },
+ {
+ statement: "UPDATE t1 SET a = 1",
+ want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: common.StatementNoWhere,
+ Title: "Require WHERE clause",
+ Content: "\"UPDATE t1 SET a = 1\" requires WHERE clause",
+ },
+ },
+ },
+ {
+ statement: "DELETE FROM t1 WHERE a > 0",
+ want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: common.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ statement: "UPDATE t1 SET a = 1 WHERE a > 10",
+ want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: common.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ }
+
+ runTests(t, tests, &WhereRequirementAdvisor{})
+}
diff --git a/server/task_check_executor_statement_advisor.go b/server/task_check_executor_statement_advisor.go
index 73e96d84b7..f79983c641 100644
--- a/server/task_check_executor_statement_advisor.go
+++ b/server/task_check_executor_statement_advisor.go
@@ -41,6 +41,8 @@ func (exec *TaskCheckStatementAdvisorExecutor) Run(ctx context.Context, server *
return []api.TaskCheckResult{}, common.Errorf(common.NotAuthorized, fmt.Errorf(api.FeatureBackwardCompatibility.AccessErrorMessage()))
}
advisorType = advisor.MySQLMigrationCompatibility
+ case api.TaskCheckDatabaseStatementRequireWhere:
+ advisorType = advisor.MySQLWhereRequirement
}
adviceList, err := advisor.Check(