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 number type payload (#3070)

ecmadao19cf580a4bf0
diff --git a/frontend/src/locales/sql-review/en-US.json b/frontend/src/locales/sql-review/en-US.json
index 0d10fb7171..7c07d64dac 100644
--- a/frontend/src/locales/sql-review/en-US.json
+++ b/frontend/src/locales/sql-review/en-US.json
@@ -151,7 +151,7 @@
"title": "Required columns",
"description": "Enforce the required columns in each table.",
"component": {
- "columnList": {
+ "list": {
"title": "Required column names"
}
}
@@ -160,7 +160,7 @@
"title": "Type disallow list",
"description": "Set the disallow list for column type.",
"component": {
- "disallowList": {
+ "list": {
"title": "Disallow list"
}
}
@@ -209,6 +209,24 @@
}
}
},
+ "column-maximum-character-length": {
+ "title": "Maximum CHAR length",
+ "description": "Force to change CHAR to VARCHAR if maximum length is reached.",
+ "component": {
+ "required": {
+ "number": "Maximum length"
+ }
+ }
+ },
+ "column-auto-increment-initial-value": {
+ "title": "Auto-increment initial value",
+ "description": "Set initial value for auto-increment column.",
+ "component": {
+ "required": {
+ "number": "Initial value"
+ }
+ }
+ },
"statement-select-no-select-all": {
"title": "Disallow \"SELECT *\"",
"description": "Disallow 'SELECT *' statement."
@@ -245,6 +263,15 @@
"title": "Disallow \"ORDER BY rand()\" statement",
"description": ""
},
+ "statement-insert-row-limit": {
+ "title": "Insert row limit",
+ "description": "",
+ "component": {
+ "number": {
+ "title": "Maximum insert amount"
+ }
+ }
+ },
"schema-backward-compatibility": {
"title": "Backward compatibility",
"description": "MySQL and TiDB support checking whether the schema change is backward compatible."
@@ -261,11 +288,29 @@
"title": "Disallow BLOB",
"description": "Disallow using BLOB type column in index."
},
+ "index-key-number-limit": {
+ "title": "Column count limit in index",
+ "description": "Limit the related row count in a single index.",
+ "component": {
+ "number": {
+ "title": "Maximum column count"
+ }
+ }
+ },
+ "index-total-number-limit": {
+ "title": "Index count limit",
+ "description": "",
+ "component": {
+ "number": {
+ "title": "Maximum index count"
+ }
+ }
+ },
"system-charset-allowlist": {
"title": "Charset allow list",
"description": "",
"component": {
- "allowList": {
+ "list": {
"title": "Allow list"
}
}
@@ -274,7 +319,7 @@
"title": "Collation allow list",
"description": "",
"component": {
- "allowList": {
+ "list": {
"title": "Allow list"
}
}
diff --git a/frontend/src/locales/sql-review/zh-CN.json b/frontend/src/locales/sql-review/zh-CN.json
index 6f70235dc9..4f9bb066bf 100644
--- a/frontend/src/locales/sql-review/zh-CN.json
+++ b/frontend/src/locales/sql-review/zh-CN.json
@@ -152,7 +152,7 @@
"title": "必须包含的字段",
"description": "强制要求表中必须包含某些字段。",
"component": {
- "columnList": {
+ "list": {
"title": "必须包含的字段名"
}
}
@@ -161,7 +161,7 @@
"title": "类型约束",
"description": "为字段设置类型黑名单。",
"component": {
- "disallowList": {
+ "list": {
"title": "禁止的类型"
}
}
@@ -210,6 +210,24 @@
}
}
},
+ "column-maximum-character-length": {
+ "title": "CHAR 长度限制",
+ "description": "CHAR 长度大于指定值的时候需要改为 VARCHAR 类型。",
+ "component": {
+ "required": {
+ "number": "最大长度"
+ }
+ }
+ },
+ "column-auto-increment-initial-value": {
+ "title": "自增初始值",
+ "description": "配置自增字段的初始值。",
+ "component": {
+ "required": {
+ "number": "初始值"
+ }
+ }
+ },
"statement-select-no-select-all": {
"title": "禁止 \"SELECT *\"",
"description": "不允许使用 \"SELECT *\" 语句。"
@@ -246,6 +264,15 @@
"title": "不能有 \"ORDER BY rand()\" 语句",
"description": ""
},
+ "statement-insert-row-limit": {
+ "title": "插入行数限制",
+ "description": "",
+ "component": {
+ "number": {
+ "title": "最大插入行数"
+ }
+ }
+ },
"schema-backward-compatibility": {
"title": "向后兼容",
"description": "MySQL 和 TiDB 支持检测 schema 变更是否向后兼容。"
@@ -262,11 +289,29 @@
"title": "BLOB 列不能建作为索引",
"description": ""
},
+ "index-key-number-limit": {
+ "title": "索引包含的字段数量限制",
+ "description": "",
+ "component": {
+ "number": {
+ "title": "字段数量上限"
+ }
+ }
+ },
+ "index-total-number-limit": {
+ "title": "索引数量限制",
+ "description": "",
+ "component": {
+ "number": {
+ "title": "索引数量上限"
+ }
+ }
+ },
"system-charset-allowlist": {
"title": "字符集限制",
"description": "",
"component": {
- "allowList": {
+ "list": {
"title": "允许的字符集"
}
}
@@ -275,7 +320,7 @@
"title": "排序规则限制",
"description": "",
"component": {
- "allowList": {
+ "list": {
"title": "允许的规则限制"
}
}
diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yaml
index 85b0e3d2cd..2d1428e737 100644
--- a/frontend/src/types/sql-review-schema.yaml
+++ b/frontend/src/types/sql-review-schema.yaml
@@ -103,7 +103,17 @@ ruleList:
- MYSQL
- TIDB
componentList: []
- # TODO: - type: statement.insert.row-limit
+ - type: statement.insert.row-limit
+ category: STATEMENT
+ engineList:
+ - MYSQL
+ - TIDB
+ - POSTGRES
+ componentList:
+ - key: number
+ payload:
+ type: NUMBER
+ default: 5
- type: statement.insert.must-specify-column
category: STATEMENT
engineList:
@@ -225,7 +235,7 @@ ruleList:
- TIDB
- POSTGRES
componentList:
- - key: columnList
+ - key: list
payload:
type: STRING_ARRAY
default:
@@ -291,7 +301,7 @@ ruleList:
- MYSQL
- TIDB
componentList:
- - key: disallowList
+ - key: list
payload:
type: STRING_ARRAY
default: []
@@ -301,8 +311,26 @@ ruleList:
- MYSQL
- TIDB
componentList: []
- # TODO: - type: column.maximum-character-length
- # TODO: - type: column.auto-increment-initial-value
+ - type: column.maximum-character-length
+ category: COLUMN
+ engineList:
+ - MYSQL
+ - TIDB
+ componentList:
+ - key: number
+ payload:
+ type: NUMBER
+ default: 5
+ - type: column.auto-increment-initial-value
+ category: COLUMN
+ engineList:
+ - MYSQL
+ - TIDB
+ componentList:
+ - key: number
+ payload:
+ type: NUMBER
+ default: 1
- type: column.auto-increment-must-unsigned
category: COLUMN
engineList:
@@ -328,7 +356,17 @@ ruleList:
- MYSQL
- TIDB
componentList: []
- # TODO: - type: index.key-number-limit
+ - type: index.key-number-limit
+ category: INDEX
+ engineList:
+ - MYSQL
+ - TIDB
+ - POSTGRES
+ componentList:
+ - key: number
+ payload:
+ type: NUMBER
+ default: 5
# TODO: - type: index.pk-type
- type: index.type-no-blob
category: INDEX
@@ -336,7 +374,16 @@ ruleList:
- MYSQL
- TIDB
componentList: []
- # TODO: - type: index.total-number-limit
+ - type: index.total-number-limit
+ category: INDEX
+ engineList:
+ - MYSQL
+ - TIDB
+ componentList:
+ - key: number
+ payload:
+ type: NUMBER
+ default: 5
- type: system.charset.allowlist
category: SYSTEM
engineList:
@@ -344,7 +391,7 @@ ruleList:
- TIDB
- POSTGRES
componentList:
- - key: allowList
+ - key: list
payload:
type: STRING_ARRAY
default: []
@@ -354,7 +401,7 @@ ruleList:
- MYSQL
- TIDB
componentList:
- - key: allowList
+ - key: list
payload:
type: STRING_ARRAY
default: []
diff --git a/frontend/src/types/sqlReview.ts b/frontend/src/types/sqlReview.ts
index eba8ee50bd..68bb3c708c 100644
--- a/frontend/src/types/sqlReview.ts
+++ b/frontend/src/types/sqlReview.ts
@@ -114,6 +114,8 @@ export type RuleType =
| "column.auto-increment-must-integer"
| "column.disallow-set-charset"
| "column.auto-increment-must-unsigned"
+ | "column.maximum-character-length"
+ | "column.auto-increment-initial-value"
| "statement.select.no-select-all"
| "statement.where.require"
| "statement.where.no-leading-wildcard-like"
@@ -123,12 +125,15 @@ export type RuleType =
| "statement.merge-alter-table"
| "statement.insert.must-specify-column"
| "statement.insert.disallow-order-by-rand"
+ | "statement.insert.row-limit"
| "schema.backward-compatibility"
| "database.drop-empty-database"
| "system.charset.allowlist"
| "system.collation.allowlist"
| "index.no-duplicate-column"
- | "index.type-no-blob";
+ | "index.type-no-blob"
+ | "index.key-number-limit"
+ | "index.total-number-limit";
export const availableRulesForFreePlan: RuleType[] = [
"statement.where.require",
@@ -155,6 +160,12 @@ interface CommentFormatPayload {
maxLength: number;
}
+// The number limit rule payload.
+// Used by the backend.
+interface NumberLimitPayload {
+ number: number;
+}
+
// The SchemaPolicyRule stores the rule configuration by users.
// Used by the backend
export interface SchemaPolicyRule {
@@ -163,7 +174,8 @@ export interface SchemaPolicyRule {
payload?:
| NamingFormatPayload
| StringArrayLimitPayload
- | CommentFormatPayload;
+ | CommentFormatPayload
+ | NumberLimitPayload;
}
// The API for SQL review policy in backend.
@@ -475,6 +487,27 @@ export const convertPolicyRuleToRuleTemplate = (
},
],
};
+ case "statement.insert.row-limit":
+ case "column.maximum-character-length":
+ case "column.auto-increment-initial-value":
+ case "index.key-number-limit":
+ case "index.total-number-limit":
+ if (!numberComponent) {
+ throw new Error(`Invalid rule ${ruleTemplate.type}`);
+ }
+
+ return {
+ ...res,
+ componentList: [
+ {
+ ...numberComponent,
+ payload: {
+ ...numberComponent.payload,
+ value: (policyRule.payload as NumberLimitPayload).number,
+ } as NumberPayload,
+ },
+ ],
+ };
}
throw new Error(`Invalid rule ${ruleTemplate.type}`);
@@ -579,6 +612,20 @@ export const convertRuleTemplateToPolicyRule = (
maxLength: numberPayload.value ?? numberPayload.default,
},
};
+ case "statement.insert.row-limit":
+ case "column.maximum-character-length":
+ case "column.auto-increment-initial-value":
+ case "index.key-number-limit":
+ case "index.total-number-limit":
+ if (!numberPayload) {
+ throw new Error(`Invalid rule ${rule.type}`);
+ }
+ return {
+ ...base,
+ payload: {
+ number: numberPayload.value ?? numberPayload.default,
+ },
+ };
}
throw new Error(`Invalid rule ${rule.type}`);
diff --git a/plugin/advisor/config/sql-review.dev.yaml b/plugin/advisor/config/sql-review.dev.yaml
index 8bebe52bfe..1203a5b11e 100644
--- a/plugin/advisor/config/sql-review.dev.yaml
+++ b/plugin/advisor/config/sql-review.dev.yaml
@@ -35,6 +35,10 @@ ruleList:
level: WARNING
- type: statement.insert.disallow-order-by-rand
level: WARNING
+ - type: statement.insert.row-limit
+ level: WARNING
+ payload:
+ number: 5
- type: naming.table
level: WARNING
payload:
@@ -68,7 +72,7 @@ ruleList:
- type: column.required
level: WARNING
payload:
- columnList:
+ list:
- id
- created_ts
- updated_ts
@@ -77,7 +81,7 @@ ruleList:
- type: column.type-disallow-list
level: WARNING
payload:
- disallowList:
+ list:
- JSON
- type: column.no-null
level: WARNING
@@ -100,6 +104,14 @@ ruleList:
payload:
required: true
maxLength: 64
+ - type: column.maximum-character-length
+ level: WARNING
+ payload:
+ number: 5
+ - type: column.auto-increment-initial-value
+ level: WARNING
+ payload:
+ number: 1
- type: schema.backward-compatibility
level: WARNING
- type: database.drop-empty-database
@@ -108,13 +120,21 @@ ruleList:
level: ERROR
- type: index.type-no-blob
level: ERROR
+ - type: index.key-number-limit
+ level: WARNING
+ payload:
+ number: 5
+ - type: index.total-number-limit
+ level: WARNING
+ payload:
+ number: 5
- type: system.charset.allowlist
level: WARNING
payload:
- allowList:
+ list:
- utf8mb4
- type: system.collation.allowlist
level: WARNING
payload:
- allowList:
+ list:
- utf8mb4_0900_ai_ci
diff --git a/plugin/advisor/config/sql-review.prod.yaml b/plugin/advisor/config/sql-review.prod.yaml
index 8d7132c0e2..29bdecc822 100644
--- a/plugin/advisor/config/sql-review.prod.yaml
+++ b/plugin/advisor/config/sql-review.prod.yaml
@@ -35,6 +35,10 @@ ruleList:
level: ERROR
- type: statement.insert.disallow-order-by-rand
level: ERROR
+ - type: statement.insert.row-limit
+ level: WARNING
+ payload:
+ number: 5
- type: naming.table
level: WARNING
payload:
@@ -68,7 +72,7 @@ ruleList:
- type: column.required
level: WARNING
payload:
- columnList:
+ list:
- id
- created_ts
- updated_ts
@@ -77,7 +81,7 @@ ruleList:
- type: column.type-disallow-list
level: ERROR
payload:
- disallowList:
+ list:
- JSON
- type: column.no-null
level: WARNING
@@ -100,6 +104,14 @@ ruleList:
payload:
required: true
maxLength: 64
+ - type: column.maximum-character-length
+ level: WARNING
+ payload:
+ number: 5
+ - type: column.auto-increment-initial-value
+ level: WARNING
+ payload:
+ number: 1
- type: schema.backward-compatibility
level: WARNING
- type: database.drop-empty-database
@@ -108,13 +120,21 @@ ruleList:
level: ERROR
- type: index.type-no-blob
level: ERROR
+ - type: index.key-number-limit
+ level: WARNING
+ payload:
+ number: 5
+ - type: index.total-number-limit
+ level: WARNING
+ payload:
+ number: 5
- type: system.charset.allowlist
level: ERROR
payload:
- allowList:
+ list:
- utf8mb4
- type: system.collation.allowlist
level: ERROR
payload:
- allowList:
+ list:
- utf8mb4_0900_ai_ci
diff --git a/plugin/advisor/config_test.go b/plugin/advisor/config_test.go
index 5de0603d1e..7dd86484ce 100644
--- a/plugin/advisor/config_test.go
+++ b/plugin/advisor/config_test.go
@@ -27,7 +27,7 @@ ruleList:
- type: column.required
level: ERROR
payload:
- columnList:
+ list:
- name
`
@@ -68,12 +68,12 @@ func TestConfigOverride(t *testing.T) {
case "column.required":
assert.Equal(t, SchemaRuleLevelError, rule.Level)
- var payload RequiredColumnRulePayload
+ var payload StringArrayTypeRulePayload
err := json.Unmarshal([]byte(rule.Payload), &payload)
require.NoError(t, err)
- assert.Equal(t, 1, len(payload.ColumnList))
- assert.Equal(t, "name", payload.ColumnList[0])
+ assert.Equal(t, 1, len(payload.List))
+ assert.Equal(t, "name", payload.List[0])
}
}
}