Skip to content

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

feat: disallow ddl/dml in dml/ddl (#11159)

Danny Xu7ee321c95cb6
diff --git a/backend/plugin/advisor/mysql/advisor_statement_disallow_mix_ddl_dml.go b/backend/plugin/advisor/mysql/advisor_statement_disallow_mix_ddl_dml.go
index eb3605b102..d3ed70a0d6 100644
--- a/backend/plugin/advisor/mysql/advisor_statement_disallow_mix_ddl_dml.go
+++ b/backend/plugin/advisor/mysql/advisor_statement_disallow_mix_ddl_dml.go
@@ -35,7 +35,6 @@ func (*StatementDisallowmixDDLDMLAdvisor) Check(ctx advisor.Context, _ string) (
title := string(ctx.Rule.Type)
var adviceList []advisor.Advice
-
var hasDDL, hasDML bool
for _, stmt := range stmtList {
checker := &mysqlparser.StatementTypeChecker{}
diff --git a/backend/plugin/advisor/pg/advisor_statement_disallow_mix_ddl_dml.go b/backend/plugin/advisor/pg/advisor_statement_disallow_mix_ddl_dml.go
index c659bd6515..b4acd3dfc6 100644
--- a/backend/plugin/advisor/pg/advisor_statement_disallow_mix_ddl_dml.go
+++ b/backend/plugin/advisor/pg/advisor_statement_disallow_mix_ddl_dml.go
@@ -3,6 +3,8 @@ package pg
// Framework code is generated by the generator.
import (
+ "fmt"
+
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/plugin/advisor"
@@ -35,12 +37,42 @@ func (*StatementDisallowMixDdlDmlAdvisor) Check(ctx advisor.Context, _ string) (
}
title := string(ctx.Rule.Type)
+ var adviceList []advisor.Advice
var hasDDL, hasDML bool
for _, stmt := range stmtList {
+ var isDDL, isDML bool
if _, ok := stmt.(ast.DDLNode); ok {
- hasDDL = true
+ isDDL = true
}
if _, ok := stmt.(ast.DMLNode); ok {
+ isDML = true
+ }
+ switch ctx.ChangeType {
+ case storepb.PlanCheckRunConfig_DDL, storepb.PlanCheckRunConfig_SDL, storepb.PlanCheckRunConfig_DDL_GHOST:
+ if isDML {
+ adviceList = append(adviceList, advisor.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Alter schema can only run DDL, \"%s\" is not DDL", stmt.Text()),
+ Code: advisor.StatementDisallowMixDDLDML,
+ Line: stmt.LastLine(),
+ })
+ }
+ case storepb.PlanCheckRunConfig_DML:
+ if isDDL {
+ adviceList = append(adviceList, advisor.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Data change can only run DML, \"%s\" is not DML", stmt.Text()),
+ Code: advisor.StatementDisallowMixDDLDML,
+ Line: stmt.LastLine(),
+ })
+ }
+ }
+ if isDDL {
+ hasDDL = true
+ }
+ if isDML {
hasDML = true
}
if hasDDL && hasDML {
@@ -49,22 +81,22 @@ func (*StatementDisallowMixDdlDmlAdvisor) Check(ctx advisor.Context, _ string) (
}
if hasDDL && hasDML {
- return []advisor.Advice{
- {
- Status: level,
- Title: title,
- Content: "Mixing DDL with DML is not allowed",
- Code: advisor.StatementDisallowMixDDLDML,
- },
- }, nil
+ adviceList = append(adviceList, advisor.Advice{
+ Status: level,
+ Title: title,
+ Content: "Mixing DDL with DML is not allowed",
+ Code: advisor.StatementDisallowMixDDLDML,
+ })
}
- return []advisor.Advice{
- {
+ if len(adviceList) == 0 {
+ adviceList = append(adviceList, advisor.Advice{
Status: advisor.Success,
Code: advisor.Ok,
Title: "OK",
Content: "",
- },
- }, nil
+ })
+ }
+
+ return adviceList, nil
}
diff --git a/backend/plugin/advisor/tidb/advisor_statement_disallow_mix_ddl_dml.go b/backend/plugin/advisor/tidb/advisor_statement_disallow_mix_ddl_dml.go
index 2dda02afc0..b43982cd1f 100644
--- a/backend/plugin/advisor/tidb/advisor_statement_disallow_mix_ddl_dml.go
+++ b/backend/plugin/advisor/tidb/advisor_statement_disallow_mix_ddl_dml.go
@@ -1,6 +1,8 @@
package tidb
import (
+ "fmt"
+
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pkg/errors"
@@ -33,12 +35,43 @@ func (*StatementDisallowMixDDLDMLAdvisor) Check(ctx advisor.Context, _ string) (
}
title := string(ctx.Rule.Type)
+ var adviceList []advisor.Advice
var hasDDL, hasDML bool
for _, stmtNode := range root {
+ var isDDL, isDML bool
if _, ok := stmtNode.(ast.DDLNode); ok {
- hasDDL = true
+ isDDL = true
}
if _, ok := stmtNode.(ast.DMLNode); ok {
+ isDML = true
+ }
+
+ switch ctx.ChangeType {
+ case storepb.PlanCheckRunConfig_DDL, storepb.PlanCheckRunConfig_SDL, storepb.PlanCheckRunConfig_DDL_GHOST:
+ if isDML {
+ adviceList = append(adviceList, advisor.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Alter schema can only run DDL, \"%s\" is not DDL", stmtNode.Text()),
+ Code: advisor.StatementDisallowMixDDLDML,
+ Line: stmtNode.OriginTextPosition(),
+ })
+ }
+ case storepb.PlanCheckRunConfig_DML:
+ if isDDL {
+ adviceList = append(adviceList, advisor.Advice{
+ Status: level,
+ Title: title,
+ Content: fmt.Sprintf("Data change can only run DML, \"%s\" is not DML", stmtNode.Text()),
+ Code: advisor.StatementDisallowMixDDLDML,
+ Line: stmtNode.OriginTextPosition(),
+ })
+ }
+ }
+ if isDDL {
+ hasDDL = true
+ }
+ if isDML {
hasDML = true
}
if hasDDL && hasDML {
@@ -47,22 +80,22 @@ func (*StatementDisallowMixDDLDMLAdvisor) Check(ctx advisor.Context, _ string) (
}
if hasDDL && hasDML {
- return []advisor.Advice{
- {
- Status: level,
- Title: title,
- Content: "Mixing DDL with DML is not allowed",
- Code: advisor.StatementDisallowMixDDLDML,
- },
- }, nil
+ adviceList = append(adviceList, advisor.Advice{
+ Status: level,
+ Title: title,
+ Content: "Mixing DDL with DML is not allowed",
+ Code: advisor.StatementDisallowMixDDLDML,
+ })
}
- return []advisor.Advice{
- {
+ if len(adviceList) == 0 {
+ adviceList = append(adviceList, advisor.Advice{
Status: advisor.Success,
Code: advisor.Ok,
Title: "OK",
Content: "",
- },
- }, nil
+ })
+ }
+
+ return adviceList, nil
}