Skip to content

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

chore(sql-review): support comment convention rules (#3063)

ecmadao29bb68e9ff45
diff --git a/frontend/src/components/SQLReview/SQLReviewPreview.vue b/frontend/src/components/SQLReview/SQLReviewPreview.vue
index 1f3a336b2a..2b4a128362 100644
--- a/frontend/src/components/SQLReview/SQLReviewPreview.vue
+++ b/frontend/src/components/SQLReview/SQLReviewPreview.vue
@@ -80,6 +80,7 @@
v-if="
component.payload.type === 'STRING' ||
component.payload.type === 'NUMBER' ||
+ component.payload.type === 'BOOLEAN' ||
component.payload.type === 'TEMPLATE'
"
class="bg-gray-100 rounded text-sm font-semibold p-2"
diff --git a/frontend/src/components/SQLReview/components/SQLRuleConfig.vue b/frontend/src/components/SQLReview/components/SQLRuleConfig.vue
index d4ed9d10d2..6b8b642518 100644
--- a/frontend/src/components/SQLReview/components/SQLRuleConfig.vue
+++ b/frontend/src/components/SQLReview/components/SQLRuleConfig.vue
@@ -115,7 +115,7 @@
:placeholder="config.payload.default"
/>
<input
- v-if="config.payload.type == 'NUMBER'"
+ v-else-if="config.payload.type == 'NUMBER'"
v-model="state.payload[index]"
type="number"
:disabled="disabled"
@@ -125,6 +125,20 @@
]"
:placeholder="`${config.payload.default}`"
/>
+ <BBCheckbox
+ v-else-if="config.payload.type == 'BOOLEAN'"
+ :title="
+ $t(
+ `sql-review.rule.${getRuleLocalizationKey(
+ selectedRule.type
+ )}.component.${config.key}.title`
+ )
+ "
+ :value="state.payload[index]"
+ @toggle="(on: boolean) => {
+ state.payload[index] = on;
+ }"
+ />
<div
v-else-if="
config.payload.type == 'STRING_ARRAY' &&
@@ -187,7 +201,7 @@ import {
import { useSubscriptionStore } from "@/store";
import { planTypeToString } from "@/types/plan";
-type PayloadValueList = (string | number | string[])[];
+type PayloadValueList = (boolean | string | number | string[])[];
interface LocalState {
payload: PayloadValueList;
}
diff --git a/frontend/src/locales/sql-review/en-US.json b/frontend/src/locales/sql-review/en-US.json
index bfcd9389e4..0d10fb7171 100644
--- a/frontend/src/locales/sql-review/en-US.json
+++ b/frontend/src/locales/sql-review/en-US.json
@@ -45,6 +45,18 @@
"title": "Disallow partition",
"description": ""
},
+ "table-comment": {
+ "title": "Comment convention",
+ "description": "Configure whether the table requires comments and the maximum comment length.",
+ "component": {
+ "required": {
+ "title": "Require comment"
+ },
+ "maxLength": {
+ "title": "Max length"
+ }
+ }
+ },
"naming-table": {
"title": "Table naming check",
"description": "Enforce the table name format and length limit. Default snake_lower_case with 63 characters.",
@@ -185,6 +197,18 @@
"title": "Set unsigned attribute on auto-increment column",
"description": "Auto-increment column must be the UNSIGNED type."
},
+ "column-comment": {
+ "title": "Comment convention",
+ "description": "Configure whether the column requires comments and the maximum comment length.",
+ "component": {
+ "required": {
+ "title": "Require comment"
+ },
+ "maxLength": {
+ "title": "Max length"
+ }
+ }
+ },
"statement-select-no-select-all": {
"title": "Disallow \"SELECT *\"",
"description": "Disallow 'SELECT *' statement."
diff --git a/frontend/src/locales/sql-review/zh-CN.json b/frontend/src/locales/sql-review/zh-CN.json
index acb0e5e985..6f70235dc9 100644
--- a/frontend/src/locales/sql-review/zh-CN.json
+++ b/frontend/src/locales/sql-review/zh-CN.json
@@ -46,6 +46,18 @@
"title": "禁止分区",
"description": ""
},
+ "table-comment": {
+ "title": "注释检查",
+ "description": "配置表是否需要注释和最大注释长度。",
+ "component": {
+ "required": {
+ "title": "必须注释"
+ },
+ "maxLength": {
+ "title": "长度限制"
+ }
+ }
+ },
"naming-table": {
"title": "表名命名检查",
"description": "限制表名命名风格和长度,默认为小写字母_下划线,且不超过 63 个字符。",
@@ -186,6 +198,18 @@
"title": "自增列需要设置无符号",
"description": ""
},
+ "column-comment": {
+ "title": "注释检查",
+ "description": "配置字段是否需要注释和最大注释长度。",
+ "component": {
+ "required": {
+ "title": "必须注释"
+ },
+ "maxLength": {
+ "title": "长度限制"
+ }
+ }
+ },
"statement-select-no-select-all": {
"title": "禁止 \"SELECT *\"",
"description": "不允许使用 \"SELECT *\" 语句。"
diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yaml
index 076506dbae..85b0e3d2cd 100644
--- a/frontend/src/types/sql-review-schema.yaml
+++ b/frontend/src/types/sql-review-schema.yaml
@@ -38,7 +38,20 @@ ruleList:
payload:
type: STRING
default: _del$
- # TODO: - type: table.comment
+ - type: table.comment
+ category: TABLE
+ engineList:
+ - MYSQL
+ - TIDB
+ componentList:
+ - key: required
+ payload:
+ type: BOOLEAN
+ default: false
+ - key: maxLength
+ payload:
+ type: NUMBER
+ default: 64
- type: table.disallow-partition
category: TABLE
engineList:
@@ -252,7 +265,20 @@ ruleList:
- MYSQL
- TIDB
componentList: []
- # TODO: - type: column.comment
+ - type: column.comment
+ category: COLUMN
+ engineList:
+ - MYSQL
+ - TIDB
+ componentList:
+ - key: required
+ payload:
+ type: BOOLEAN
+ default: false
+ - key: maxLength
+ payload:
+ type: NUMBER
+ default: 64
- type: column.auto-increment-must-integer
category: COLUMN
engineList:
diff --git a/frontend/src/types/sqlReview.ts b/frontend/src/types/sqlReview.ts
index 7766c312c6..eba8ee50bd 100644
--- a/frontend/src/types/sqlReview.ts
+++ b/frontend/src/types/sqlReview.ts
@@ -52,6 +52,14 @@ interface StringPayload {
value?: string;
}
+// BooleanPayload is the boolean type payload configuration options and default value.
+// Used by the frontend.
+interface BooleanPayload {
+ type: "BOOLEAN";
+ default: boolean;
+ value?: boolean;
+}
+
// StringArrayPayload is the string array type payload configuration options and default value.
// Used by the frontend.
interface StringArrayPayload {
@@ -73,7 +81,12 @@ interface TemplatePayload {
// Used by the frontend.
export interface RuleConfigComponent {
key: string;
- payload: StringPayload | NumberPayload | TemplatePayload | StringArrayPayload;
+ payload:
+ | StringPayload
+ | NumberPayload
+ | TemplatePayload
+ | StringArrayPayload
+ | BooleanPayload;
}
// The identifier for rule template
@@ -83,6 +96,7 @@ export type RuleType =
| "table.no-foreign-key"
| "table.drop-naming-convention"
| "table.disallow-partition"
+ | "table.comment"
| "naming.table"
| "naming.column"
| "naming.index.uk"
@@ -91,6 +105,7 @@ export type RuleType =
| "naming.index.idx"
| "column.required"
| "column.no-null"
+ | "column.comment"
| "column.type-disallow-list"
| "column.disallow-change-type"
| "column.set-default-for-not-null"
@@ -133,12 +148,22 @@ interface StringArrayLimitPayload {
list: string[];
}
+// The comment format rule payload.
+// Used by the backend.
+interface CommentFormatPayload {
+ required: boolean;
+ maxLength: number;
+}
+
// The SchemaPolicyRule stores the rule configuration by users.
// Used by the backend
export interface SchemaPolicyRule {
type: RuleType;
level: RuleLevel;
- payload?: NamingFormatPayload | StringArrayLimitPayload;
+ payload?:
+ | NamingFormatPayload
+ | StringArrayLimitPayload
+ | CommentFormatPayload;
}
// The API for SQL review policy in backend.
@@ -292,6 +317,9 @@ export const convertPolicyRuleToRuleTemplate = (
const numberComponent = ruleTemplate.componentList.find(
(c) => c.payload.type === "NUMBER"
);
+ const booleanComponent = ruleTemplate.componentList.find(
+ (c) => c.payload.type === "BOOLEAN"
+ );
const templateComponent = ruleTemplate.componentList.find(
(c) => c.payload.type === "TEMPLATE"
);
@@ -422,6 +450,31 @@ export const convertPolicyRuleToRuleTemplate = (
],
};
}
+ case "column.comment":
+ case "table.comment":
+ if (!booleanComponent || !numberComponent) {
+ throw new Error(`Invalid rule ${ruleTemplate.type}`);
+ }
+
+ return {
+ ...res,
+ componentList: [
+ {
+ ...booleanComponent,
+ payload: {
+ ...booleanComponent.payload,
+ value: (policyRule.payload as CommentFormatPayload).required,
+ } as BooleanPayload,
+ },
+ {
+ ...numberComponent,
+ payload: {
+ ...numberComponent.payload,
+ value: (policyRule.payload as CommentFormatPayload).maxLength,
+ } as NumberPayload,
+ },
+ ],
+ };
}
throw new Error(`Invalid rule ${ruleTemplate.type}`);
@@ -449,6 +502,9 @@ export const convertRuleTemplateToPolicyRule = (
const templatePayload = rule.componentList.find(
(c) => c.payload.type === "TEMPLATE"
)?.payload as TemplatePayload | undefined;
+ const booleanPayload = rule.componentList.find(
+ (c) => c.payload.type === "BOOLEAN"
+ )?.payload as BooleanPayload | undefined;
switch (rule.type) {
case "table.drop-naming-convention":
@@ -511,6 +567,18 @@ export const convertRuleTemplateToPolicyRule = (
},
};
}
+ case "column.comment":
+ case "table.comment":
+ if (!booleanPayload || !numberPayload) {
+ throw new Error(`Invalid rule ${rule.type}`);
+ }
+ return {
+ ...base,
+ payload: {
+ required: booleanPayload.value ?? booleanPayload.default,
+ maxLength: 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 258d19d57b..8bebe52bfe 100644
--- a/plugin/advisor/config/sql-review.dev.yaml
+++ b/plugin/advisor/config/sql-review.dev.yaml
@@ -12,6 +12,11 @@ ruleList:
format: _del$
- type: table.disallow-partition
level: ERROR
+ - type: table.comment
+ level: WARNING
+ payload:
+ required: true
+ maxLength: 64
- type: statement.select.no-select-all
level: WARNING
- type: statement.where.require
@@ -90,6 +95,11 @@ ruleList:
level: ERROR
- type: column.auto-increment-must-unsigned
level: ERROR
+ - type: column.comment
+ level: WARNING
+ payload:
+ required: true
+ maxLength: 64
- type: schema.backward-compatibility
level: WARNING
- type: database.drop-empty-database
diff --git a/plugin/advisor/config/sql-review.prod.yaml b/plugin/advisor/config/sql-review.prod.yaml
index 62b2dbbab6..8d7132c0e2 100644
--- a/plugin/advisor/config/sql-review.prod.yaml
+++ b/plugin/advisor/config/sql-review.prod.yaml
@@ -12,6 +12,11 @@ ruleList:
format: _del$
- type: table.disallow-partition
level: ERROR
+ - type: table.comment
+ level: ERROR
+ payload:
+ required: true
+ maxLength: 64
- type: statement.select.no-select-all
level: ERROR
- type: statement.where.require
@@ -90,6 +95,11 @@ ruleList:
level: ERROR
- type: column.auto-increment-must-unsigned
level: ERROR
+ - type: column.comment
+ level: ERROR
+ payload:
+ required: true
+ maxLength: 64
- type: schema.backward-compatibility
level: WARNING
- type: database.drop-empty-database