Skip to content

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

chore(sql-review): add the dml-dry-run rule framework (#3088)

rebelice3e863b6259b1
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 08695dcfcc..0157896120 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -196,6 +196,9 @@ const (
// MySQLStatementAffectedRowLimit is an advisor type for MySQL UPDATE/DELETE affected row limit.
MySQLStatementAffectedRowLimit Type = "bb.plugin.advisor.mysql.statement.affected-row-limit"
+ // MySQLStatementDMLDryRun is an advisor type for MySQL DML dry run.
+ MySQLStatementDMLDryRun Type = "bb.plugin.advisor.mysql.statement.dml-dry-run"
+
// PostgreSQL Advisor.
// PostgreSQLSyntax is an advisor type for PostgreSQL syntax.
diff --git a/plugin/advisor/mysql/advisor_statement_dml_dry_run.go b/plugin/advisor/mysql/advisor_statement_dml_dry_run.go
new file mode 100644
index 0000000000..6dc24832da
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_statement_dml_dry_run.go
@@ -0,0 +1,79 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "github.com/pingcap/tidb/parser/ast"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/db"
+)
+
+var (
+ _ advisor.Advisor = (*StatementDmlDryRunAdvisor)(nil)
+ _ ast.Visitor = (*statementDmlDryRunChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLStatementDMLDryRun, &StatementDmlDryRunAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLStatementDMLDryRun, &StatementDmlDryRunAdvisor{})
+}
+
+// StatementDmlDryRunAdvisor is the advisor checking for DML dry run.
+type StatementDmlDryRunAdvisor struct {
+}
+
+// Check checks for DML dry run.
+func (*StatementDmlDryRunAdvisor) 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 := &statementDmlDryRunChecker{
+ 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 statementDmlDryRunChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ line int
+}
+
+// Enter implements the ast.Visitor interface.
+func (*statementDmlDryRunChecker) Enter(in ast.Node) (ast.Node, bool) {
+ // TODO: implement it
+ // switch node := in.(type) {
+ // }
+
+ return in, false
+}
+
+// Leave implements the ast.Visitor interface.
+func (*statementDmlDryRunChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
diff --git a/plugin/advisor/mysql/advisor_statement_dml_dry_run_test.go b/plugin/advisor/mysql/advisor_statement_dml_dry_run_test.go
new file mode 100644
index 0000000000..8eefe76c06
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_statement_dml_dry_run_test.go
@@ -0,0 +1,30 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestStatementDmlDryRun(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: ``,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ }
+
+ advisor.RunSQLReviewRuleTests(t, tests, &StatementDmlDryRunAdvisor{}, &advisor.SQLReviewRule{
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: "",
+ }, advisor.MockMySQLDatabase)
+}
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 3f67aa7fa6..93fa10f251 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -73,6 +73,8 @@ const (
SchemaRuleStatementInsertDisallowOrderByRand SQLReviewRuleType = "statement.insert.disallow-order-by-rand"
// SchemaRuleStatementAffectedRowLimit enforce the UPDATE/DELETE affected row limit.
SchemaRuleStatementAffectedRowLimit SQLReviewRuleType = "statement.affected-row-limit"
+ // SchemaRuleStatementDMLDryRun dry run the dml.
+ SchemaRuleStatementDMLDryRun SQLReviewRuleType = "statement.dml-dry-run"
// SchemaRuleTableRequirePK require the table to have a primary key.
SchemaRuleTableRequirePK SQLReviewRuleType = "table.require-pk"
@@ -891,6 +893,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLStatementAffectedRowLimit, nil
}
+ case SchemaRuleStatementDMLDryRun:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLStatementDMLDryRun, nil
+ }
}
return Fake, errors.Errorf("unknown SQL review rule type %v for %v", ruleType, engine)
}