Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(advisor): mssql disallow mix ddl dml (#12882)
p0ny8105bf5a48f8
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.goindex 30c6f4d1ad..44829287cf 100644--- a/backend/plugin/advisor/code.go+++ b/backend/plugin/advisor/code.go@@ -779,4 +779,7 @@ const (MSSQLStatementWhereDisallowFunctionsAndCalculations Type = "bb.plugin.advisor.mssql.statement.disallow-functions-and-calculations"MSSQLIndexNotRedundant Type = "bb.plugin.advisor.mssql.index.not-redundant"++ // MSSQLStatementDisallowMixDDLDML is an advisor type for MSSQL disallow mix DDL and DML.+ MSSQLStatementDisallowMixDDLDML Type = "bb.plugin.advisor.mssql.statement.disallow-mix-ddl-dml")diff --git a/backend/plugin/advisor/mssql/advisor_statement_disallow_mix_ddl_dml.go b/backend/plugin/advisor/mssql/advisor_statement_disallow_mix_ddl_dml.gonew file mode 100644index 0000000000..5b681c4293--- /dev/null+++ b/backend/plugin/advisor/mssql/advisor_statement_disallow_mix_ddl_dml.go@@ -0,0 +1,108 @@+package mssql++import (+ "github.com/antlr4-go/antlr/v4"+ "github.com/pkg/errors"++ parser "github.com/bytebase/tsql-parser"++ "github.com/bytebase/bytebase/backend/plugin/advisor"+ tsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/tsql"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"+)++var (+ _ advisor.Advisor = (*StatementDisallowMixDDLDMLAdvisor)(nil)+)++func init() {+ advisor.Register(storepb.Engine_MSSQL, advisor.MSSQLStatementDisallowMixDDLDML, &StatementDisallowMixDDLDMLAdvisor{})+}++type StatementDisallowMixDDLDMLAdvisor struct {+}++func (*StatementDisallowMixDDLDMLAdvisor) Check(ctx advisor.Context, _ string) ([]*storepb.Advice, error) {+ tree, ok := ctx.AST.(antlr.Tree)+ if !ok {+ return nil, errors.Errorf("failed to convert to Tree")+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }+ title := ctx.Rule.Type++ c := &statementDisallowMixDDLDMLChecker{+ changeType: ctx.ChangeType,+ level: level,+ title: title,+ }++ antlr.ParseTreeWalkerDefault.Walk(c, tree)++ if c.hasDDL && c.hasDML {+ c.adviceList = append(c.adviceList, &storepb.Advice{+ Status: level,+ Title: title,+ Content: "Mixing DDL with DML is not allowed",+ Code: advisor.StatementDisallowMixDDLDML.Int32(),+ })+ }++ return c.adviceList, nil+}++type statementDisallowMixDDLDMLChecker struct {+ *parser.BaseTSqlParserListener++ changeType storepb.PlanCheckRunConfig_ChangeDatabaseType+ adviceList []*storepb.Advice++ level storepb.Advice_Status+ title string++ hasDML bool+ hasDDL bool+}++func (c *statementDisallowMixDDLDMLChecker) EnterSql_clauses(ctx *parser.Sql_clausesContext) {+ if !tsqlparser.IsTopLevel(ctx.GetParent()) {+ return+ }+ var isDML bool+ if ctx.Dml_clause() != nil {+ isDML = true+ c.hasDML = true+ } else {+ c.hasDDL = true+ }++ switch c.changeType {+ case storepb.PlanCheckRunConfig_DDL, storepb.PlanCheckRunConfig_SDL, storepb.PlanCheckRunConfig_DDL_GHOST:+ if isDML {+ c.adviceList = append(c.adviceList, &storepb.Advice{+ Status: c.level,+ Title: c.title,+ Content: "Alter schema can only run DDL",+ Code: advisor.StatementDisallowMixDDLDML.Int32(),+ StartPosition: &storepb.Position{+ Line: int32(ctx.GetStart().GetLine()),+ },+ })+ }+ case storepb.PlanCheckRunConfig_DML:+ if !isDML {+ c.adviceList = append(c.adviceList, &storepb.Advice{+ Status: c.level,+ Title: c.title,+ Content: "Data change can only run DML",+ Code: advisor.StatementDisallowMixDDLDML.Int32(),+ StartPosition: &storepb.Position{+ Line: int32(ctx.GetStart().GetLine()),+ },+ })+ }+ }+}diff --git a/backend/plugin/advisor/mssql/mssql_rules_test.go b/backend/plugin/advisor/mssql/mssql_rules_test.goindex 86a5a779bb..a519745d10 100644--- a/backend/plugin/advisor/mssql/mssql_rules_test.go+++ b/backend/plugin/advisor/mssql/mssql_rules_test.go@@ -30,6 +30,7 @@ func TestMSSQLRules(t *testing.T) {advisor.SchemaRuleStatementDisallowCrossDBQueries,advisor.SchemaRuleStatementWhereDisallowFunctionsAndCaculations,advisor.SchemaRuleIndexNotRedundant,+ advisor.SchemaRuleStatementDisallowMixDDLDML,}for _, rule := range mssqlRules {diff --git a/backend/plugin/advisor/mssql/test/statement_disallow_mix_ddl_dml.yaml b/backend/plugin/advisor/mssql/test/statement_disallow_mix_ddl_dml.yamlnew file mode 100644index 0000000000..0e21dd83be--- /dev/null+++ b/backend/plugin/advisor/mssql/test/statement_disallow_mix_ddl_dml.yaml@@ -0,0 +1,14 @@+- statement: DELETE FROM tech_book WHERE a > 1;+ changeType: 0+- statement: UPDATE tech_book SET id = 1;+ changeType: 0+- statement: ALTER TABLE tech_book ADD author TEXT; UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;+ changeType: 0+ want:+ - status: 2+ code: 227+ title: statement.disallow-mix-ddl-dml+ content: Mixing DDL with DML is not allowed+ detail: ""+ startposition: null+ endposition: nulldiff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex e60fa3a090..5fb65bf187 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -1272,6 +1272,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Tyreturn PostgreSQLStatementDisallowMixDDLDML, nilcase storepb.Engine_ORACLE, storepb.Engine_DM, storepb.Engine_OCEANBASE_ORACLE:return OracleStatementDisallowMixDDLDML, nil+ case storepb.Engine_MSSQL:+ return MSSQLStatementDisallowMixDDLDML, nil}case SchemaRuleStatementPriorBackupCheck:switch engine {diff --git a/backend/plugin/parser/tsql/backup.go b/backend/plugin/parser/tsql/backup.goindex 76c32a6bbc..d44117e270 100644--- a/backend/plugin/parser/tsql/backup.go+++ b/backend/plugin/parser/tsql/backup.go@@ -131,7 +131,7 @@ func (e *suffixSelectStatementExtractor) EnterUpdate_statement(ctx *parser.Updatreturn}- if isTopLevel(ctx.GetParent()) && ctx.Ddl_object() != nil {+ if IsTopLevel(ctx.GetParent()) && ctx.Ddl_object() != nil {if ctx.CURRENT() != nil {e.err = errors.New("UPDATE statement with CURSOR clause is not supported")return@@ -196,7 +196,7 @@ func (e *suffixSelectStatementExtractor) EnterDelete_statement(ctx *parser.Deletreturn}- if isTopLevel(ctx.GetParent()) {+ if IsTopLevel(ctx.GetParent()) {if ctx.CURRENT() != nil {e.err = errors.New("DELETE statement with CURSOR clause is not supported")return@@ -282,7 +282,7 @@ type dmlExtractor struct {offset int}-func isTopLevel(ctx antlr.Tree) bool {+func IsTopLevel(ctx antlr.Tree) bool {if ctx == nil {return true}@@ -290,7 +290,7 @@ func isTopLevel(ctx antlr.Tree) bool {case *parser.Dml_clauseContext,*parser.Sql_clausesContext,*parser.Batch_without_goContext:- return isTopLevel(ctx.GetParent())+ return IsTopLevel(ctx.GetParent())case *parser.Tsql_fileContext:return truedefault:@@ -305,13 +305,13 @@ func (e *dmlExtractor) ExitBatch(ctx *parser.Batch_without_goContext) {}func (e *dmlExtractor) ExitSql_clauses(ctx *parser.Sql_clausesContext) {- if isTopLevel(ctx.GetParent()) {+ if IsTopLevel(ctx.GetParent()) {e.offset++}}func (e *dmlExtractor) EnterUpdate_statement(ctx *parser.Update_statementContext) {- if isTopLevel(ctx.GetParent()) && ctx.Ddl_object() != nil {+ if IsTopLevel(ctx.GetParent()) && ctx.Ddl_object() != nil {extractor := &tableExtractor{databaseName: e.databaseName,}@@ -332,7 +332,7 @@ func (e *dmlExtractor) EnterUpdate_statement(ctx *parser.Update_statementContext}func (e *dmlExtractor) EnterDelete_statement(ctx *parser.Delete_statementContext) {- if isTopLevel(ctx.GetParent()) {+ if IsTopLevel(ctx.GetParent()) {extractor := &tableExtractor{databaseName: e.databaseName,}diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yamlindex 7f05ca3ea7..71559dcda9 100644--- a/frontend/src/types/sql-review-schema.yaml+++ b/frontend/src/types/sql-review-schema.yaml@@ -597,6 +597,9 @@- type: statement.disallow-mix-ddl-dmlcategory: STATEMENTengine: ORACLE+- type: statement.disallow-mix-ddl-dml+ category: STATEMENT+ engine: MSSQL- type: statement.prior-backup-checkcategory: STATEMENTengine: MYSQLdiff --git a/frontend/src/types/sql-review.dev.yaml b/frontend/src/types/sql-review.dev.yamlindex 9b895879f1..15eede75f4 100644--- a/frontend/src/types/sql-review.dev.yaml+++ b/frontend/src/types/sql-review.dev.yaml@@ -471,6 +471,9 @@ ruleList:- type: statement.disallow-mix-ddl-dmllevel: WARNINGengine: ORACLE+ - type: statement.disallow-mix-ddl-dml+ level: WARNING+ engine: MSSQL- type: statement.prior-backup-checklevel: ERRORengine: MYSQLdiff --git a/frontend/src/types/sql-review.prod.yaml b/frontend/src/types/sql-review.prod.yamlindex 8da2f55ff0..ed1fa2e1c3 100644--- a/frontend/src/types/sql-review.prod.yaml+++ b/frontend/src/types/sql-review.prod.yaml@@ -482,6 +482,9 @@ ruleList:- type: statement.disallow-mix-ddl-dmllevel: WARNINGengine: ORACLE+ - type: statement.disallow-mix-ddl-dml+ level: WARNING+ engine: MSSQL- type: statement.prior-backup-checklevel: ERRORengine: MYSQL