Skip to content

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

feat(test): disallow mix in ddl/dml (#14120)

p0nyd402838ecb40
diff --git a/backend/plugin/advisor/mssql/mssql_rules_test.go b/backend/plugin/advisor/mssql/mssql_rules_test.go
index 7c44a51883..a335f77a3c 100644
--- a/backend/plugin/advisor/mssql/mssql_rules_test.go
+++ b/backend/plugin/advisor/mssql/mssql_rules_test.go
@@ -32,6 +32,8 @@ func TestMSSQLRules(t *testing.T) {
advisor.SchemaRuleStatementWhereDisallowFunctionsAndCaculations,
advisor.SchemaRuleIndexNotRedundant,
advisor.SchemaRuleStatementDisallowMixDDLDML,
+ advisor.SchemaRuleStatementDisallowMixInDDL,
+ advisor.SchemaRuleStatementDisallowMixInDML,
}
for _, rule := range mssqlRules {
diff --git a/backend/plugin/advisor/mssql/test/statement_disallow_mix_in_ddl.yaml b/backend/plugin/advisor/mssql/test/statement_disallow_mix_in_ddl.yaml
new file mode 100644
index 0000000000..971e866a37
--- /dev/null
+++ b/backend/plugin/advisor/mssql/test/statement_disallow_mix_in_ddl.yaml
@@ -0,0 +1,45 @@
+- statement: DELETE FROM tech_book WHERE a > 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: UPDATE tech_book SET id = 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: ALTER TABLE tech_book ADD author TEXT; UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/mssql/test/statement_disallow_mix_in_dml.yaml b/backend/plugin/advisor/mssql/test/statement_disallow_mix_in_dml.yaml
new file mode 100644
index 0000000000..71eb18b3cf
--- /dev/null
+++ b/backend/plugin/advisor/mssql/test/statement_disallow_mix_in_dml.yaml
@@ -0,0 +1,16 @@
+- statement: DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+- statement: UPDATE tech_book SET id = 1;
+ changeType: 2
+- statement: ALTER TABLE tech_book ADD author TEXT; UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-dml
+ content: Data change can only run DML
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/mysql/mysql_rules_test.go b/backend/plugin/advisor/mysql/mysql_rules_test.go
index 4ace55f508..f9469c2d9e 100644
--- a/backend/plugin/advisor/mysql/mysql_rules_test.go
+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go
@@ -165,6 +165,8 @@ func TestMySQLRules(t *testing.T) {
advisor.SchemaRuleFunctionDisallowCreate,
// advisor.SchemaRuleFunctionDisallowList enforce the function disallow list.
advisor.SchemaRuleFunctionDisallowList,
+ advisor.SchemaRuleStatementDisallowMixInDDL,
+ advisor.SchemaRuleStatementDisallowMixInDML,
}
for _, rule := range mysqlRules {
diff --git a/backend/plugin/advisor/mysql/test/statement_disallow_mix_in_ddl.yaml b/backend/plugin/advisor/mysql/test/statement_disallow_mix_in_ddl.yaml
new file mode 100644
index 0000000000..b3e5f2d80f
--- /dev/null
+++ b/backend/plugin/advisor/mysql/test/statement_disallow_mix_in_ddl.yaml
@@ -0,0 +1,45 @@
+- statement: DELETE FROM tech_book WHERE a > 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL, "DELETE FROM tech_book WHERE a > 1;" is not DDL
+ detail: ""
+ startposition:
+ line: 0
+ column: 0
+ endposition: null
+- statement: UPDATE tech_book SET id = 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL, "UPDATE tech_book SET id = 1;" is not DDL
+ detail: ""
+ startposition:
+ line: 0
+ column: 0
+ endposition: null
+- statement: ALTER TABLE tech_book ADD COLUMN author TEXT; UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL, "DELETE FROM tech_book WHERE a > 1;" is not DDL
+ detail: ""
+ startposition:
+ line: 0
+ column: 0
+ endposition: null
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL, "UPDATE tech_book SET id = 1;" is not DDL
+ detail: ""
+ startposition:
+ line: 0
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/mysql/test/statement_disallow_mix_in_dml.yaml b/backend/plugin/advisor/mysql/test/statement_disallow_mix_in_dml.yaml
new file mode 100644
index 0000000000..b32729628f
--- /dev/null
+++ b/backend/plugin/advisor/mysql/test/statement_disallow_mix_in_dml.yaml
@@ -0,0 +1,16 @@
+- statement: DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+- statement: UPDATE tech_book SET id = 1;
+ changeType: 2
+- statement: ALTER TABLE tech_book ADD COLUMN author TEXT; UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-dml
+ content: Data change can only run DML, "ALTER TABLE tech_book ADD COLUMN author TEXT;" is not DML
+ detail: ""
+ startposition:
+ line: 0
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/oracle/oracle_rules_test.go b/backend/plugin/advisor/oracle/oracle_rules_test.go
index 1de65d9ac9..20ee42323c 100644
--- a/backend/plugin/advisor/oracle/oracle_rules_test.go
+++ b/backend/plugin/advisor/oracle/oracle_rules_test.go
@@ -30,6 +30,8 @@ func TestOracleRules(t *testing.T) {
advisor.SchemaRuleIdentifierNoKeyword,
advisor.SchemaRuleIdentifierCase,
advisor.SchemaRuleStatementDisallowMixDDLDML,
+ advisor.SchemaRuleStatementDisallowMixInDDL,
+ advisor.SchemaRuleStatementDisallowMixInDML,
}
for _, rule := range oracleRules {
diff --git a/backend/plugin/advisor/oracle/test/statement_disallow_mix_in_ddl.yaml b/backend/plugin/advisor/oracle/test/statement_disallow_mix_in_ddl.yaml
new file mode 100644
index 0000000000..d669b3532f
--- /dev/null
+++ b/backend/plugin/advisor/oracle/test/statement_disallow_mix_in_ddl.yaml
@@ -0,0 +1,45 @@
+- statement: DELETE FROM tech_book WHERE a > 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: UPDATE tech_book SET id = 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: ALTER TABLE tech_book ADD author VARCHAR2(250); UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/oracle/test/statement_disallow_mix_in_dml.yaml b/backend/plugin/advisor/oracle/test/statement_disallow_mix_in_dml.yaml
new file mode 100644
index 0000000000..65953a0022
--- /dev/null
+++ b/backend/plugin/advisor/oracle/test/statement_disallow_mix_in_dml.yaml
@@ -0,0 +1,16 @@
+- statement: DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+- statement: UPDATE tech_book SET id = 1;
+ changeType: 2
+- statement: ALTER TABLE tech_book ADD author VARCHAR2(250); UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-dml
+ content: Data change can only run DML
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/pg/pg_rules_test.go b/backend/plugin/advisor/pg/pg_rules_test.go
index 4cd9bfcac2..5ac34ddb2b 100644
--- a/backend/plugin/advisor/pg/pg_rules_test.go
+++ b/backend/plugin/advisor/pg/pg_rules_test.go
@@ -60,6 +60,8 @@ func TestPostgreSQLRules(t *testing.T) {
advisor.SchemaRuleStatementMaximumLimitValue,
advisor.SchemaRuleTableCommentConvention,
advisor.SchemaRuleColumnCommentConvention,
+ advisor.SchemaRuleStatementDisallowMixInDDL,
+ advisor.SchemaRuleStatementDisallowMixInDML,
}
for _, rule := range pgRules {
diff --git a/backend/plugin/advisor/pg/test/builtin_prior_backup_check.yaml b/backend/plugin/advisor/pg/test/builtin_prior_backup_check.yaml
index 863d108425..229929fa2a 100644
--- a/backend/plugin/advisor/pg/test/builtin_prior_backup_check.yaml
+++ b/backend/plugin/advisor/pg/test/builtin_prior_backup_check.yaml
@@ -7,7 +7,7 @@
UPDATE abc SET id = 4 WHERE id = 4 and name = 'test';
changeType: 2
want:
- - status: 3
+ - status: 2
code: 2001
title: builtin.prior-backup-check
content: Prior backup is feasible only with up to 5 statements that are either UPDATE or DELETE, or if all UPDATEs target the same table with a PRIMARY or UNIQUE KEY in the WHERE clause
@@ -35,7 +35,7 @@
UPDATE tech_book SET id = 7 WHERE id = 7;
changeType: 2
want:
- - status: 3
+ - status: 2
code: 2001
title: builtin.prior-backup-check
content: Prior backup is feasible only with up to 5 statements that are either UPDATE or DELETE, or if all UPDATEs target the same table with a PRIMARY or UNIQUE KEY in the WHERE clause
@@ -51,7 +51,7 @@
- statement: CREATE TABLE t_test_backup(a int);DELETE FROM tech_book WHERE a > 1;
changeType: 2
want:
- - status: 3
+ - status: 2
code: 2001
title: builtin.prior-backup-check
content: Data change can only run DML, "CREATE TABLE t_test_backup(a int);" is not DML
@@ -63,7 +63,7 @@
- statement: SET ROLE role1;DELETE FROM tech_book WHERE a > 1;
changeType: 2
want:
- - status: 3
+ - status: 2
code: 2001
title: builtin.prior-backup-check
content: Backup cannot deal with SET ROLE statement
diff --git a/backend/plugin/advisor/pg/test/statement_disallow_mix_in_ddl.yaml b/backend/plugin/advisor/pg/test/statement_disallow_mix_in_ddl.yaml
new file mode 100644
index 0000000000..c3fd0f30ad
--- /dev/null
+++ b/backend/plugin/advisor/pg/test/statement_disallow_mix_in_ddl.yaml
@@ -0,0 +1,45 @@
+- statement: DELETE FROM tech_book WHERE a > 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL, "DELETE FROM tech_book WHERE a > 1;" is not DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: UPDATE tech_book SET id = 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL, "UPDATE tech_book SET id = 1;" is not DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: ALTER TABLE tech_book ADD COLUMN author TEXT; UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL, "DELETE FROM tech_book WHERE a > 1;" is not DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL, "UPDATE tech_book SET id = 1;" is not DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/pg/test/statement_disallow_mix_in_dml.yaml b/backend/plugin/advisor/pg/test/statement_disallow_mix_in_dml.yaml
new file mode 100644
index 0000000000..51ccbeb2e8
--- /dev/null
+++ b/backend/plugin/advisor/pg/test/statement_disallow_mix_in_dml.yaml
@@ -0,0 +1,16 @@
+- statement: DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+- statement: UPDATE tech_book SET id = 1;
+ changeType: 2
+- statement: ALTER TABLE tech_book ADD COLUMN author TEXT; UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-dml
+ content: Data change can only run DML, "ALTER TABLE tech_book ADD COLUMN author TEXT;" is not DML
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 88114f0d14..433cc29578 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -500,6 +500,9 @@ type SQLReviewCheckContext struct {
// Snowflake specific fields
CurrentDatabase string
+
+ // Used for test only.
+ NoAppendBuiltin bool
}
// SQLReviewCheck checks the statements with sql review rules.
@@ -513,8 +516,10 @@ func SQLReviewCheck(
builtinOnly := len(ruleList) == 0
- // Append builtin rules to the rule list.
- ruleList = append(ruleList, GetBuiltinRules(checkContext.DbType)...)
+ if !checkContext.NoAppendBuiltin {
+ // Append builtin rules to the rule list.
+ ruleList = append(ruleList, GetBuiltinRules(checkContext.DbType)...)
+ }
if asts == nil || len(ruleList) == 0 {
return parseResult, nil
diff --git a/backend/plugin/advisor/tidb/test/statement_disallow_mix_in_ddl.yaml b/backend/plugin/advisor/tidb/test/statement_disallow_mix_in_ddl.yaml
new file mode 100644
index 0000000000..c3fd0f30ad
--- /dev/null
+++ b/backend/plugin/advisor/tidb/test/statement_disallow_mix_in_ddl.yaml
@@ -0,0 +1,45 @@
+- statement: DELETE FROM tech_book WHERE a > 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL, "DELETE FROM tech_book WHERE a > 1;" is not DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: UPDATE tech_book SET id = 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL, "UPDATE tech_book SET id = 1;" is not DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: ALTER TABLE tech_book ADD COLUMN author TEXT; UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;
+ changeType: 1
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL, "DELETE FROM tech_book WHERE a > 1;" is not DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-ddl
+ content: Alter schema can only run DDL, "UPDATE tech_book SET id = 1;" is not DDL
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/tidb/test/statement_disallow_mix_in_dml.yaml b/backend/plugin/advisor/tidb/test/statement_disallow_mix_in_dml.yaml
new file mode 100644
index 0000000000..51ccbeb2e8
--- /dev/null
+++ b/backend/plugin/advisor/tidb/test/statement_disallow_mix_in_dml.yaml
@@ -0,0 +1,16 @@
+- statement: DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+- statement: UPDATE tech_book SET id = 1;
+ changeType: 2
+- statement: ALTER TABLE tech_book ADD COLUMN author TEXT; UPDATE tech_book SET id = 1;DELETE FROM tech_book WHERE a > 1;
+ changeType: 2
+ want:
+ - status: 2
+ code: 227
+ title: statement.disallow-mix-in-dml
+ content: Data change can only run DML, "ALTER TABLE tech_book ADD COLUMN author TEXT;" is not DML
+ detail: ""
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/tidb/tidb_rules_test.go b/backend/plugin/advisor/tidb/tidb_rules_test.go
index ef3215e333..4a234c1846 100644
--- a/backend/plugin/advisor/tidb/tidb_rules_test.go
+++ b/backend/plugin/advisor/tidb/tidb_rules_test.go
@@ -113,6 +113,9 @@ func TestTiDBRules(t *testing.T) {
// advisor.SchemaRuleCollationAllowlist enforce the collation allowlist.
advisor.SchemaRuleCollationAllowlist,
+
+ advisor.SchemaRuleStatementDisallowMixInDDL,
+ advisor.SchemaRuleStatementDisallowMixInDML,
}
for _, rule := range tidbRules {
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index 58250dbf94..097bdf3c56 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -222,10 +222,6 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.E
},
}
- if isBuiltinRule(rule) {
- ruleList = []*storepb.SQLReviewRule{}
- }
-
ctx := SQLReviewCheckContext{
Charset: "",
Collation: "",
@@ -239,6 +235,7 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.E
PreUpdateBackupDetail: &storepb.PreUpdateBackupDetail{
Database: "instances/instanceName/databases/bbdataarchive",
},
+ NoAppendBuiltin: true,
}
adviceList, err := SQLReviewCheck(sm, tc.Statement, ruleList, ctx)
@@ -271,15 +268,6 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.E
}
}
-func isBuiltinRule(rule SQLReviewRuleType) bool {
- switch rule {
- case BuiltinRulePriorBackupCheck:
- return true
- default:
- return false
- }
-}
-
// MockDriver is the driver for test only.
type MockDriver struct {
}
@@ -402,6 +390,8 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType, dbType storepb.Eng
SchemaRuleStatementCheckSetRoleVariable,
SchemaRuleStatementWhereDisallowFunctionsAndCaculations,
SchemaRuleStatementDisallowMixDDLDML,
+ SchemaRuleStatementDisallowMixInDDL,
+ SchemaRuleStatementDisallowMixInDML,
SchemaRuleStatementPriorBackupCheck,
SchemaRuleStatementJoinStrictColumnAttrs,
SchemaRuleStatementMaxExecutionTime,