Skip to content

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

chore(sql-review): support SQL review rules with string array payload (part 3) (#3061)

ecmadao5437da5e95b4
diff --git a/frontend/src/locales/sql-review/en-US.json b/frontend/src/locales/sql-review/en-US.json
index 8241b8ac1a..bfcd9389e4 100644
--- a/frontend/src/locales/sql-review/en-US.json
+++ b/frontend/src/locales/sql-review/en-US.json
@@ -12,7 +12,8 @@
"column": "Column",
"schema": "Schema",
"database": "Database",
- "index": "Index"
+ "index": "Index",
+ "system": "System"
},
"template": {
"bb-sql-review-prod": "Template for Prod Environment",
@@ -143,6 +144,15 @@
}
}
},
+ "column-type-disallow-list": {
+ "title": "Type disallow list",
+ "description": "Set the disallow list for column type.",
+ "component": {
+ "disallowList": {
+ "title": "Disallow list"
+ }
+ }
+ },
"column-no-null": {
"title": "Disallow NULL",
"description": "Columns cannot have NULL value."
@@ -226,6 +236,24 @@
"index-type-no-blob": {
"title": "Disallow BLOB",
"description": "Disallow using BLOB type column in index."
+ },
+ "system-charset-allowlist": {
+ "title": "Charset allow list",
+ "description": "",
+ "component": {
+ "allowList": {
+ "title": "Allow list"
+ }
+ }
+ },
+ "system-collation-allowlist": {
+ "title": "Collation allow list",
+ "description": "",
+ "component": {
+ "allowList": {
+ "title": "Allow list"
+ }
+ }
}
},
"level": {
diff --git a/frontend/src/locales/sql-review/zh-CN.json b/frontend/src/locales/sql-review/zh-CN.json
index 92083e2506..acb0e5e985 100644
--- a/frontend/src/locales/sql-review/zh-CN.json
+++ b/frontend/src/locales/sql-review/zh-CN.json
@@ -13,7 +13,8 @@
"schema": "Schema",
"database": "数据库",
"index": "索引",
- "insert": "插入"
+ "insert": "插入",
+ "system": "系统"
},
"template": {
"bb-sql-review-prod": "针对生产环境的审核策略模板",
@@ -144,6 +145,15 @@
}
}
},
+ "column-type-disallow-list": {
+ "title": "类型约束",
+ "description": "为字段设置类型黑名单。",
+ "component": {
+ "disallowList": {
+ "title": "禁止的类型"
+ }
+ }
+ },
"column-no-null": {
"title": "禁止字段为 NULL",
"description": "表中的字段不允许存在 NULL 值。"
@@ -227,6 +237,24 @@
"index-type-no-blob": {
"title": "BLOB 列不能建作为索引",
"description": ""
+ },
+ "system-charset-allowlist": {
+ "title": "字符集限制",
+ "description": "",
+ "component": {
+ "allowList": {
+ "title": "允许的字符集"
+ }
+ }
+ },
+ "system-collation-allowlist": {
+ "title": "排序规则限制",
+ "description": "",
+ "component": {
+ "allowList": {
+ "title": "允许的规则限制"
+ }
+ }
}
},
"level": {
diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yaml
index 052a643226..076506dbae 100644
--- a/frontend/src/types/sql-review-schema.yaml
+++ b/frontend/src/types/sql-review-schema.yaml
@@ -6,6 +6,7 @@ categoryList:
- TABLE
- SCHEMA
- COLUMN
+ - SYSTEM
- DATABASE
ruleList:
- type: engine.mysql.use-innodb
@@ -91,13 +92,13 @@ ruleList:
componentList: []
# TODO: - type: statement.insert.row-limit
- type: statement.insert.must-specify-column
- category: INSERT
+ category: STATEMENT
engineList:
- MYSQL
- TIDB
componentList: []
- type: statement.insert.disallow-order-by-rand
- category: INSERT
+ category: STATEMENT
engineList:
- MYSQL
- TIDB
@@ -258,7 +259,16 @@ ruleList:
- MYSQL
- TIDB
componentList: []
- # TODO: - type: column.type-restriction
+ - type: column.type-disallow-list
+ category: COLUMN
+ engineList:
+ - MYSQL
+ - TIDB
+ componentList:
+ - key: disallowList
+ payload:
+ type: STRING_ARRAY
+ default: []
- type: column.disallow-set-charset
category: COLUMN
engineList:
@@ -301,5 +311,24 @@ ruleList:
- TIDB
componentList: []
# TODO: - type: index.total-number-limit
- # TODO: - type: charset.allowlist
- # TODO: - type: collation.allowlist
+ - type: system.charset.allowlist
+ category: SYSTEM
+ engineList:
+ - MYSQL
+ - TIDB
+ - POSTGRES
+ componentList:
+ - key: allowList
+ payload:
+ type: STRING_ARRAY
+ default: []
+ - type: system.collation.allowlist
+ category: SYSTEM
+ engineList:
+ - MYSQL
+ - TIDB
+ componentList:
+ - key: allowList
+ payload:
+ type: STRING_ARRAY
+ default: []
diff --git a/frontend/src/types/sqlReview.ts b/frontend/src/types/sqlReview.ts
index 951d713003..7766c312c6 100644
--- a/frontend/src/types/sqlReview.ts
+++ b/frontend/src/types/sqlReview.ts
@@ -20,7 +20,8 @@ export type CategoryType =
| "COLUMN"
| "SCHEMA"
| "DATABASE"
- | "INDEX";
+ | "INDEX"
+ | "SYSTEM";
// The rule level
export enum RuleLevel {
@@ -81,6 +82,7 @@ export type RuleType =
| "table.require-pk"
| "table.no-foreign-key"
| "table.drop-naming-convention"
+ | "table.disallow-partition"
| "naming.table"
| "naming.column"
| "naming.index.uk"
@@ -89,12 +91,29 @@ export type RuleType =
| "naming.index.idx"
| "column.required"
| "column.no-null"
+ | "column.type-disallow-list"
+ | "column.disallow-change-type"
+ | "column.set-default-for-not-null"
+ | "column.disallow-change"
+ | "column.disallow-changing-order"
+ | "column.auto-increment-must-integer"
+ | "column.disallow-set-charset"
+ | "column.auto-increment-must-unsigned"
| "statement.select.no-select-all"
| "statement.where.require"
| "statement.where.no-leading-wildcard-like"
| "statement.disallow-commit"
+ | "statement.disallow-limit"
+ | "statement.disallow-order-by"
+ | "statement.merge-alter-table"
+ | "statement.insert.must-specify-column"
+ | "statement.insert.disallow-order-by-rand"
| "schema.backward-compatibility"
- | "database.drop-empty-database";
+ | "database.drop-empty-database"
+ | "system.charset.allowlist"
+ | "system.collation.allowlist"
+ | "index.no-duplicate-column"
+ | "index.type-no-blob";
export const availableRulesForFreePlan: RuleType[] = [
"statement.where.require",
@@ -385,6 +404,24 @@ export const convertPolicyRuleToRuleTemplate = (
],
};
}
+ case "column.type-disallow-list":
+ case "system.charset.allowlist":
+ case "system.collation.allowlist": {
+ const stringArrayComponent = ruleTemplate.componentList[0];
+ const stringArrayPayload = {
+ ...stringArrayComponent.payload,
+ value: (policyRule.payload as StringArrayLimitPayload).list,
+ } as StringArrayPayload;
+ return {
+ ...res,
+ componentList: [
+ {
+ ...stringArrayComponent,
+ payload: stringArrayPayload,
+ },
+ ],
+ };
+ }
}
throw new Error(`Invalid rule ${ruleTemplate.type}`);
@@ -461,7 +498,10 @@ export const convertRuleTemplateToPolicyRule = (
maxLength: numberPayload.value ?? numberPayload.default,
},
};
- case "column.required": {
+ case "column.required":
+ case "column.type-disallow-list":
+ case "system.charset.allowlist":
+ case "system.collation.allowlist": {
const stringArrayPayload = rule.componentList[0]
.payload as StringArrayPayload;
return {
diff --git a/plugin/advisor/config/sql-review.dev.yaml b/plugin/advisor/config/sql-review.dev.yaml
index 8a146dd891..258d19d57b 100644
--- a/plugin/advisor/config/sql-review.dev.yaml
+++ b/plugin/advisor/config/sql-review.dev.yaml
@@ -69,6 +69,11 @@ ruleList:
- updated_ts
- creator_id
- updater_id
+ - type: column.type-disallow-list
+ level: WARNING
+ payload:
+ disallowList:
+ - JSON
- type: column.no-null
level: WARNING
- type: column.disallow-change-type
@@ -93,3 +98,13 @@ ruleList:
level: ERROR
- type: index.type-no-blob
level: ERROR
+ - type: system.charset.allowlist
+ level: WARNING
+ payload:
+ allowList:
+ - utf8mb4
+ - type: system.collation.allowlist
+ level: WARNING
+ payload:
+ allowList:
+ - utf8mb4_0900_ai_ci
diff --git a/plugin/advisor/config/sql-review.prod.yaml b/plugin/advisor/config/sql-review.prod.yaml
index b9278edcb3..62b2dbbab6 100644
--- a/plugin/advisor/config/sql-review.prod.yaml
+++ b/plugin/advisor/config/sql-review.prod.yaml
@@ -69,6 +69,11 @@ ruleList:
- updated_ts
- creator_id
- updater_id
+ - type: column.type-disallow-list
+ level: ERROR
+ payload:
+ disallowList:
+ - JSON
- type: column.no-null
level: WARNING
- type: column.disallow-change-type
@@ -93,3 +98,13 @@ ruleList:
level: ERROR
- type: index.type-no-blob
level: ERROR
+ - type: system.charset.allowlist
+ level: ERROR
+ payload:
+ allowList:
+ - utf8mb4
+ - type: system.collation.allowlist
+ level: ERROR
+ payload:
+ allowList:
+ - utf8mb4_0900_ai_ci
diff --git a/plugin/advisor/mysql/advisor_charset_allowlist_test.go b/plugin/advisor/mysql/advisor_charset_allowlist_test.go
index 119bee1d0f..7cbc3655f3 100644
--- a/plugin/advisor/mysql/advisor_charset_allowlist_test.go
+++ b/plugin/advisor/mysql/advisor_charset_allowlist_test.go
@@ -41,7 +41,7 @@ func TestCharsetAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
- Title: "charset.allowlist",
+ Title: "system.charset.allowlist",
Content: "\"CREATE TABLE t(a int) CHARSET ascii\" used disabled charset 'ascii'",
Line: 1,
},
@@ -55,7 +55,7 @@ func TestCharsetAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
- Title: "charset.allowlist",
+ Title: "system.charset.allowlist",
Content: "\"ALTER TABLE t CHARSET ascii\" used disabled charset 'ascii'",
Line: 3,
},
@@ -67,7 +67,7 @@ func TestCharsetAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
- Title: "charset.allowlist",
+ Title: "system.charset.allowlist",
Content: "\"ALTER DATABASE test CHARSET ascii\" used disabled charset 'ascii'",
Line: 1,
},
@@ -79,7 +79,7 @@ func TestCharsetAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
- Title: "charset.allowlist",
+ Title: "system.charset.allowlist",
Content: "\"CREATE TABLE t(a varchar(255) CHARSET ascii)\" used disabled charset 'ascii'",
Line: 1,
},
@@ -93,7 +93,7 @@ func TestCharsetAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
- Title: "charset.allowlist",
+ Title: "system.charset.allowlist",
Content: "\"ALTER TABLE t ADD COLUMN a varchar(255) CHARSET ascii\" used disabled charset 'ascii'",
Line: 3,
},
@@ -107,7 +107,7 @@ func TestCharsetAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
- Title: "charset.allowlist",
+ Title: "system.charset.allowlist",
Content: "\"ALTER TABLE t MODIFY COLUMN a varchar(255) CHARSET ascii\" used disabled charset 'ascii'",
Line: 3,
},
@@ -121,7 +121,7 @@ func TestCharsetAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
- Title: "charset.allowlist",
+ Title: "system.charset.allowlist",
Content: "\"ALTER TABLE t CHANGE COLUMN a a varchar(255) CHARSET ascii\" used disabled charset 'ascii'",
Line: 3,
},
diff --git a/plugin/advisor/mysql/advisor_collation_allowlist_test.go b/plugin/advisor/mysql/advisor_collation_allowlist_test.go
index 71a953be77..60408ae7c3 100644
--- a/plugin/advisor/mysql/advisor_collation_allowlist_test.go
+++ b/plugin/advisor/mysql/advisor_collation_allowlist_test.go
@@ -41,7 +41,7 @@ func TestCollationAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCollation,
- Title: "collation.allowlist",
+ Title: "system.collation.allowlist",
Content: "\"CREATE TABLE t(a int) COLLATE latin1_bin\" used disabled collation 'latin1_bin'",
Line: 1,
},
@@ -55,7 +55,7 @@ func TestCollationAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCollation,
- Title: "collation.allowlist",
+ Title: "system.collation.allowlist",
Content: "\"ALTER TABLE t COLLATE latin1_bin\" used disabled collation 'latin1_bin'",
Line: 3,
},
@@ -67,7 +67,7 @@ func TestCollationAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCollation,
- Title: "collation.allowlist",
+ Title: "system.collation.allowlist",
Content: "\"ALTER DATABASE test COLLATE latin1_bin\" used disabled collation 'latin1_bin'",
Line: 1,
},
@@ -79,7 +79,7 @@ func TestCollationAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCollation,
- Title: "collation.allowlist",
+ Title: "system.collation.allowlist",
Content: "\"CREATE TABLE t(a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin)\" used disabled collation 'latin1_bin'",
Line: 1,
},
@@ -93,7 +93,7 @@ func TestCollationAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCollation,
- Title: "collation.allowlist",
+ Title: "system.collation.allowlist",
Content: "\"ALTER TABLE t ADD COLUMN a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin\" used disabled collation 'latin1_bin'",
Line: 3,
},
@@ -107,7 +107,7 @@ func TestCollationAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCollation,
- Title: "collation.allowlist",
+ Title: "system.collation.allowlist",
Content: "\"ALTER TABLE t MODIFY COLUMN a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin\" used disabled collation 'latin1_bin'",
Line: 3,
},
@@ -121,7 +121,7 @@ func TestCollationAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCollation,
- Title: "collation.allowlist",
+ Title: "system.collation.allowlist",
Content: "\"ALTER TABLE t CHANGE COLUMN a a varchar(255) CHARACTER SET latin1 COLLATE latin1_bin\" used disabled collation 'latin1_bin'",
Line: 3,
},
diff --git a/plugin/advisor/mysql/advisor_column_type_restriction_test.go b/plugin/advisor/mysql/advisor_column_type_restriction_test.go
index a20874a268..1bcc4f54bb 100644
--- a/plugin/advisor/mysql/advisor_column_type_restriction_test.go
+++ b/plugin/advisor/mysql/advisor_column_type_restriction_test.go
@@ -30,21 +30,21 @@ func TestColumnTypeRestriction(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
- Title: "column.type-restriction",
+ Title: "column.type-disallow-list",
Content: "Disallow column type JSON but column `t`.`a` is",
Line: 1,
},
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
- Title: "column.type-restriction",
+ Title: "column.type-disallow-list",
Content: "Disallow column type BLOB but column `t`.`b` is",
Line: 1,
},
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
- Title: "column.type-restriction",
+ Title: "column.type-disallow-list",
Content: "Disallow column type TEXT but column `t`.`c` is",
Line: 1,
},
@@ -58,21 +58,21 @@ func TestColumnTypeRestriction(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
- Title: "column.type-restriction",
+ Title: "column.type-disallow-list",
Content: "Disallow column type JSON but column `t`.`a` is",
Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
- Title: "column.type-restriction",
+ Title: "column.type-disallow-list",
Content: "Disallow column type BLOB but column `t`.`b` is",
Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
- Title: "column.type-restriction",
+ Title: "column.type-disallow-list",
Content: "Disallow column type TEXT but column `t`.`c` is",
Line: 3,
},
@@ -86,14 +86,14 @@ func TestColumnTypeRestriction(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
- Title: "column.type-restriction",
+ Title: "column.type-disallow-list",
Content: "Disallow column type JSON but column `t`.`a` is",
Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.DisabledColumnType,
- Title: "column.type-restriction",
+ Title: "column.type-disallow-list",
Content: "Disallow column type BLOB but column `t`.`b` is",
Line: 3,
},
@@ -106,7 +106,7 @@ func TestColumnTypeRestriction(t *testing.T) {
})
require.NoError(t, err)
advisor.RunSQLReviewRuleTests(t, tests, &ColumnTypeRestrictionAdvisor{}, &advisor.SQLReviewRule{
- Type: advisor.SchemaRuleColumnTypeRestriction,
+ Type: advisor.SchemaRuleColumnTypeDisallowList,
Level: advisor.SchemaRuleLevelWarning,
Payload: string(payload),
}, advisor.MockMySQLDatabase)
diff --git a/plugin/advisor/pg/advisor_encoding_allowlist_test.go b/plugin/advisor/pg/advisor_encoding_allowlist_test.go
index 6b98074666..c8eab498a7 100644
--- a/plugin/advisor/pg/advisor_encoding_allowlist_test.go
+++ b/plugin/advisor/pg/advisor_encoding_allowlist_test.go
@@ -39,7 +39,7 @@ func TestCharsetAllowlist(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.DisabledCharset,
- Title: "charset.allowlist",
+ Title: "system.charset.allowlist",
Content: "\"\" used disabled encoding 'latin1'",
},
},
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 914e8f7d13..d7ec917ab0 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -101,8 +101,8 @@ const (
SchemaRuleColumnCommentConvention SQLReviewRuleType = "column.comment"
// SchemaRuleColumnAutoIncrementMustInteger require the auto-increment column to be integer.
SchemaRuleColumnAutoIncrementMustInteger SQLReviewRuleType = "column.auto-increment-must-integer"
- // SchemaRuleColumnTypeRestriction enforce the column type restriction.
- SchemaRuleColumnTypeRestriction SQLReviewRuleType = "column.type-restriction"
+ // SchemaRuleColumnTypeDisallowList enforce the column type disallow list.
+ SchemaRuleColumnTypeDisallowList SQLReviewRuleType = "column.type-disallow-list"
// SchemaRuleColumnDisallowSetCharset disallow set column charset.
SchemaRuleColumnDisallowSetCharset SQLReviewRuleType = "column.disallow-set-charset"
// SchemaRuleColumnMaximumCharacterLength enforce the maximum character length.
@@ -130,10 +130,10 @@ const (
SchemaRuleIndexTotalNumberLimit SQLReviewRuleType = "index.total-number-limit"
// SchemaRuleCharsetAllowlist enforce the charset allowlist.
- SchemaRuleCharsetAllowlist SQLReviewRuleType = "charset.allowlist"
+ SchemaRuleCharsetAllowlist SQLReviewRuleType = "system.charset.allowlist"
// SchemaRuleCollationAllowlist enforce the collation allowlist.
- SchemaRuleCollationAllowlist SQLReviewRuleType = "collation.allowlist"
+ SchemaRuleCollationAllowlist SQLReviewRuleType = "system.collation.allowlist"
// SchemaRuleCommentLength limit comment length.
SchemaRuleCommentLength SQLReviewRuleType = "comment.length"
@@ -234,7 +234,7 @@ func (rule *SQLReviewRule) Validate() error {
if _, err := UnmarshalNumberTypeRulePayload(rule.Payload); err != nil {
return err
}
- case SchemaRuleColumnTypeRestriction, SchemaRuleCharsetAllowlist, SchemaRuleCollationAllowlist:
+ case SchemaRuleColumnTypeDisallowList, SchemaRuleCharsetAllowlist, SchemaRuleCollationAllowlist:
if _, err := UnmarshalStringArrayTypeRulePayload(rule.Payload); err != nil {
return err
}
@@ -727,7 +727,7 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLAutoIncrementColumnMustInteger, nil
}
- case SchemaRuleColumnTypeRestriction:
+ case SchemaRuleColumnTypeDisallowList:
switch engine {
case db.MySQL, db.TiDB:
return MySQLColumnTypeRestriction, nil