Skip to content

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

chore(schema system frontend): support length limit for naming rules in the frontend (#1837)

ecmadaof41fcc27b606
diff --git a/frontend/src/components/DatabaseSchemaReview/SchemaReviewConfig.vue b/frontend/src/components/DatabaseSchemaReview/SchemaReviewConfig.vue
index 903c8a108e..ee6fab1060 100644
--- a/frontend/src/components/DatabaseSchemaReview/SchemaReviewConfig.vue
+++ b/frontend/src/components/DatabaseSchemaReview/SchemaReviewConfig.vue
@@ -102,7 +102,10 @@ const onRuleActivate = (type: RuleType) => {
}
};
-const onPayloadChange = (rule: RuleTemplate, data: (string | string[])[]) => {
+const onPayloadChange = (
+ rule: RuleTemplate,
+ data: (string | number | string[])[]
+) => {
if (!rule.componentList) {
return;
}
@@ -121,6 +124,15 @@ const onPayloadChange = (rule: RuleTemplate, data: (string | string[])[]) => {
},
});
break;
+ case "NUMBER":
+ list.push({
+ ...component,
+ payload: {
+ ...component.payload,
+ value: data[index] as number,
+ },
+ });
+ break;
default:
list.push({
...component,
diff --git a/frontend/src/components/DatabaseSchemaReview/SchemaReviewPreview.vue b/frontend/src/components/DatabaseSchemaReview/SchemaReviewPreview.vue
index 43ade5102a..cb3bab1bc5 100644
--- a/frontend/src/components/DatabaseSchemaReview/SchemaReviewPreview.vue
+++ b/frontend/src/components/DatabaseSchemaReview/SchemaReviewPreview.vue
@@ -50,7 +50,13 @@
<p class="py-2 text-gray-400">
{{ getRuleLocalization(rule.type).description }}
</p>
- <ul role="list" class="space-y-4 list-disc list-inside">
+ <ul
+ role="list"
+ :class="[
+ 'space-y-2 list-disc list-inside',
+ rule.componentList.length > 0 ? 'mt-3' : '',
+ ]"
+ >
<li
v-for="(component, i) in rule.componentList"
:key="i"
@@ -66,6 +72,7 @@
<span
v-if="
component.payload.type === 'STRING' ||
+ component.payload.type === 'NUMBER' ||
component.payload.type === 'TEMPLATE'
"
class="bg-gray-100 rounded text-sm font-semibold p-2"
diff --git a/frontend/src/components/DatabaseSchemaReview/components/SchemaRuleConfig.vue b/frontend/src/components/DatabaseSchemaReview/components/SchemaRuleConfig.vue
index 7302099178..8a199ef3f7 100644
--- a/frontend/src/components/DatabaseSchemaReview/components/SchemaRuleConfig.vue
+++ b/frontend/src/components/DatabaseSchemaReview/components/SchemaRuleConfig.vue
@@ -54,7 +54,7 @@
<div
v-for="(config, index) in selectedRule.componentList"
:key="index"
- class="mb-1"
+ class="mb-4"
>
<p class="mb-3">
{{
@@ -72,6 +72,13 @@
class="shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full border-gray-300 rounded-md"
:placeholder="config.payload.default"
/>
+ <input
+ v-if="config.payload.type == 'NUMBER'"
+ v-model="state.payload[index]"
+ type="number"
+ class="shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full border-gray-300 rounded-md"
+ :placeholder="`${config.payload.default}`"
+ />
<div
v-else-if="
config.payload.type == 'STRING_ARRAY' &&
@@ -82,7 +89,7 @@
<BBBadge
v-for="(val, i) in state.payload[index]"
:key="`${index}-${i}`"
- :text="val"
+ :text="`${val}`"
@remove="() => removeFromList(index, val)"
/>
</div>
@@ -125,7 +132,7 @@ import {
getRuleLocalizationKey,
} from "@/types/schemaSystem";
-type PayloadValueList = (string | string[])[];
+type PayloadValueList = (string | number | string[])[];
interface LocalState {
payload: PayloadValueList;
}
@@ -169,7 +176,7 @@ const removeFromList = (i: number, val: any) => {
const values = state.payload[i] as string[];
const index = values.indexOf(val);
- pullAt(state.payload[i], index);
+ pullAt(state.payload[i] as string[], index);
};
const pushToList = (i: number, e: any) => {
@@ -187,3 +194,17 @@ const getStringPayload = (i: number): string => {
return state.payload[i] as string;
};
</script>
+
+<style scoped>
+/* Removed the ticker in the number field */
+input::-webkit-outer-spin-button,
+input::-webkit-inner-spin-button {
+ -webkit-appearance: none;
+ margin: 0;
+}
+
+/* Firefox */
+input[type="number"] {
+ -moz-appearance: textfield;
+}
+</style>
diff --git a/frontend/src/locales/sql-review/en-US.json b/frontend/src/locales/sql-review/en-US.json
index e0f194c5c4..124c6901e5 100644
--- a/frontend/src/locales/sql-review/en-US.json
+++ b/frontend/src/locales/sql-review/en-US.json
@@ -26,25 +26,31 @@
},
"naming-table": {
"title": "Table naming check",
- "description": "Enforce the table name format. Default snake_lower_case.",
+ "description": "Enforce the table name format and length limit. Default snake_lower_case with 64 characters.",
"component": {
"name_format": {
"title": "Table name format (regex)"
+ },
+ "length_limit": {
+ "title": "Length limit"
}
}
},
"naming-column": {
"title": "Column naming check",
- "description": "Enforce the column name format. Default snake_lower_case.",
+ "description": "Enforce the column name format and length limit. Default snake_lower_case with 64 characters.",
"component": {
"name_format": {
"title": "Column name format (regex)"
+ },
+ "length_limit": {
+ "title": "Length limit"
}
}
},
"naming-index-uk": {
"title": "Unique key naming check",
- "description": "Enforce the unique key name format. Default uk_<table_name>_<column_list>.",
+ "description": "Enforce the unique key name format and length limit. Default uk_<table_name>_<column_list> with 64 characters.",
"component": {
"name_format": {
"title": "Unique key name format",
@@ -52,12 +58,15 @@
"table": "The table name",
"column_list": "Index column names, joined by _"
}
+ },
+ "length_limit": {
+ "title": "Length limit"
}
}
},
"naming-index-fk": {
"title": "Foreign key naming check",
- "description": "Enforce the foreign key name format. Default fk_<referencing_table>_<referencing_column>_<referenced_table>_<referenced_column>.",
+ "description": "Enforce the foreign key name format and length limit. Default fk_<referencing_table>_<referencing_column>_<referenced_table>_<referenced_column> with 64 characters.",
"component": {
"name_format": {
"title": "Foreign key name format",
@@ -67,12 +76,15 @@
"referenced_table": "The referenced table name",
"referenced_column": "The referenced column name"
}
+ },
+ "length_limit": {
+ "title": "Length limit"
}
}
},
"naming-index-idx": {
"title": "Index naming check",
- "description": "Enforce the index name format. Default idx_<table_name>_<column_list>.",
+ "description": "Enforce the index name format and length limit. Default idx_<table_name>_<column_list> with 64 characters.",
"component": {
"name_format": {
"title": "Index name format",
@@ -80,6 +92,9 @@
"table": "The table name",
"column_list": "Index column names, joined by _"
}
+ },
+ "length_limit": {
+ "title": "Length limit"
}
}
},
diff --git a/frontend/src/locales/sql-review/zh-CN.json b/frontend/src/locales/sql-review/zh-CN.json
index 45922f7916..7af482b932 100644
--- a/frontend/src/locales/sql-review/zh-CN.json
+++ b/frontend/src/locales/sql-review/zh-CN.json
@@ -26,25 +26,31 @@
},
"naming-table": {
"title": "表名命名检查",
- "description": "限制表名命名风格,默认为小写字母_下划线。",
+ "description": "限制表名命名风格和长度,默认为小写字母_下划线,且不超过 64 个字符。",
"component": {
"name_format": {
"title": "表命名规则(正则)"
+ },
+ "length_limit": {
+ "title": "长度限制"
}
}
},
"naming-column": {
"title": "列名命名检查",
- "description": "限制列名命名风格,默认为小写字母_下划线。",
+ "description": "限制列名命名风格和长度,默认为小写字母_下划线,且不超过 64 个字符。",
"component": {
"name_format": {
"title": "列命名规则(正则)"
+ },
+ "length_limit": {
+ "title": "长度限制"
}
}
},
"naming-index-uk": {
"title": "唯一键命名检查",
- "description": "限制唯一键命名风格,默认为 uk_<表名>_<主键包含的字段名组合>。",
+ "description": "限制唯一键命名风格和长度,默认为 uk_<表名>_<主键包含的字段名组合>,且不超过 64 个字符。",
"component": {
"name_format": {
"title": "唯一键命名规则",
@@ -52,12 +58,15 @@
"table": "表名",
"column_list": "索引包含的字段名,通过 _ 连接"
}
+ },
+ "length_limit": {
+ "title": "长度限制"
}
}
},
"naming-index-fk": {
"title": "外键命名检查",
- "description": "限制外键命名风格,默认为 fk_<目标表名>_<目标字段名>_<被引用表名>_<被引用字段名>。",
+ "description": "限制外键命名风格和长度,默认为 fk_<目标表名>_<目标字段名>_<被引用表名>_<被引用字段名>,且不超过 64 个字符。",
"component": {
"name_format": {
"title": "外键命名规则",
@@ -67,12 +76,15 @@
"referenced_table": "被引用的表名",
"referenced_column": "被引用的字段名"
}
+ },
+ "length_limit": {
+ "title": "长度限制"
}
}
},
"naming-index-idx": {
"title": "索引命名检查",
- "description": "限制索引命名风格,默认为 idx_<表名>_<索引包含的字段名组合>。",
+ "description": "限制索引命名风格和长度,默认为 idx_<表名>_<索引包含的字段名组合>,且不超过 64 个字符。",
"component": {
"name_format": {
"title": "索引命名规则",
@@ -80,6 +92,9 @@
"table": "表名",
"column_list": "索引包含的字段名,通过 _ 连接"
}
+ },
+ "length_limit": {
+ "title": "长度限制"
}
}
},
diff --git a/frontend/src/types/schemaSystem.ts b/frontend/src/types/schemaSystem.ts
index 56e8bdf821..de69fb71ec 100644
--- a/frontend/src/types/schemaSystem.ts
+++ b/frontend/src/types/schemaSystem.ts
@@ -30,6 +30,14 @@ export const LEVEL_LIST = [
RuleLevel.DISABLED,
];
+// NumberPayload is the number type payload configuration options and default value.
+// Used by the frontend.
+interface NumberPayload {
+ type: "NUMBER";
+ default: number;
+ value?: number;
+}
+
// StringPayload is the string type payload configuration options and default value.
// Used by the frontend.
interface StringPayload {
@@ -59,7 +67,7 @@ interface TemplatePayload {
// Used by the frontend.
export interface RuleConfigComponent {
key: string;
- payload: StringPayload | TemplatePayload | StringArrayPayload;
+ payload: StringPayload | NumberPayload | TemplatePayload | StringArrayPayload;
}
// The identifier for rule template
@@ -82,6 +90,7 @@ export type RuleType =
// Used by the backend.
interface NamingFormatPayload {
format: string;
+ maxLength: number;
}
// The naming format rule payload.
@@ -196,11 +205,24 @@ export const convertPolicyRuleToRuleTemplate = (
switch (ruleTemplate.type) {
case "naming.column":
case "naming.table":
- const stringComponent = ruleTemplate.componentList[0];
+ const stringComponent = ruleTemplate.componentList.find(
+ (c) => c.payload.type === "STRING"
+ );
+ const numberComponent = ruleTemplate.componentList.find(
+ (c) => c.payload.type === "NUMBER"
+ );
+ if (!stringComponent || !numberComponent) {
+ throw new Error(`Invalid rule ${ruleTemplate.type}`);
+ }
+
const namingRulepayload = {
...stringComponent.payload,
value: (policyRule.payload as NamingFormatPayload).format,
} as StringPayload;
+ const numberPayload = {
+ ...numberComponent.payload,
+ value: (policyRule.payload as NamingFormatPayload).maxLength,
+ } as NumberPayload;
return {
...res,
componentList: [
@@ -208,16 +230,33 @@ export const convertPolicyRuleToRuleTemplate = (
...stringComponent,
payload: namingRulepayload,
},
+ {
+ ...numberComponent,
+ payload: numberPayload,
+ },
],
};
case "naming.index.idx":
case "naming.index.uk":
case "naming.index.fk":
- const templateComponent = ruleTemplate.componentList[0];
+ const templateComponent = ruleTemplate.componentList.find(
+ (c) => c.payload.type === "TEMPLATE"
+ );
+ const lengthLimitComponent = ruleTemplate.componentList.find(
+ (c) => c.payload.type === "NUMBER"
+ );
+ if (!templateComponent || !lengthLimitComponent) {
+ throw new Error(`Invalid rule ${ruleTemplate.type}`);
+ }
+
const indexRulePayload = {
...templateComponent.payload,
value: (policyRule.payload as NamingFormatPayload).format,
} as TemplatePayload;
+ const lengthLimitPayload = {
+ ...lengthLimitComponent.payload,
+ value: (policyRule.payload as NamingFormatPayload).maxLength,
+ } as NumberPayload;
return {
...res,
componentList: [
@@ -225,6 +264,10 @@ export const convertPolicyRuleToRuleTemplate = (
...templateComponent,
payload: indexRulePayload,
},
+ {
+ ...lengthLimitComponent,
+ payload: lengthLimitPayload,
+ },
],
};
case "column.required":
@@ -263,21 +306,41 @@ export const convertRuleTemplateToPolicyRule = (
switch (rule.type) {
case "naming.column":
case "naming.table":
- const stringPayload = rule.componentList[0].payload as StringPayload;
+ const stringPayload = rule.componentList.find(
+ (c) => c.payload.type === "STRING"
+ )?.payload as StringPayload | undefined;
+ const numberPayload = rule.componentList.find(
+ (c) => c.payload.type === "NUMBER"
+ )?.payload as NumberPayload | undefined;
+ if (!stringPayload || !numberPayload) {
+ throw new Error(`Invalid rule ${rule.type}`);
+ }
+
return {
...base,
payload: {
format: stringPayload.value ?? stringPayload.default,
+ maxLength: numberPayload.value ?? numberPayload.default,
},
};
case "naming.index.idx":
case "naming.index.uk":
case "naming.index.fk":
- const templatePayload = rule.componentList[0].payload as TemplatePayload;
+ const templatePayload = rule.componentList.find(
+ (c) => c.payload.type === "TEMPLATE"
+ )?.payload as TemplatePayload | undefined;
+ const lengthPayload = rule.componentList.find(
+ (c) => c.payload.type === "NUMBER"
+ )?.payload as NumberPayload | undefined;
+
+ if (!templatePayload || !lengthPayload) {
+ throw new Error(`Invalid rule ${rule.type}`);
+ }
return {
...base,
payload: {
format: templatePayload.value ?? templatePayload.default,
+ maxLength: lengthPayload.value ?? lengthPayload.default,
},
};
case "column.required":
diff --git a/frontend/src/types/sqlReviewConfig.yaml b/frontend/src/types/sqlReviewConfig.yaml
index fb8fc15b59..0f3777adb8 100644
--- a/frontend/src/types/sqlReviewConfig.yaml
+++ b/frontend/src/types/sqlReviewConfig.yaml
@@ -42,6 +42,10 @@ templateList:
payload:
type: STRING
default: "^[a-z]+(_[a-z]+)*$"
+ - key: length_limit
+ payload:
+ type: NUMBER
+ default: 64
level: WARNING
- type: naming.column
category: NAMING
@@ -51,6 +55,10 @@ templateList:
payload:
type: STRING
default: "^[a-z]+(_[a-z]+)*$"
+ - key: length_limit
+ payload:
+ type: NUMBER
+ default: 64
level: WARNING
- type: naming.index.uk
category: NAMING
@@ -63,6 +71,10 @@ templateList:
templateList:
- table
- column_list
+ - key: length_limit
+ payload:
+ type: NUMBER
+ default: 64
level: WARNING
- type: naming.index.idx
category: NAMING
@@ -75,6 +87,10 @@ templateList:
templateList:
- table
- column_list
+ - key: length_limit
+ payload:
+ type: NUMBER
+ default: 64
level: WARNING
- type: naming.index.fk
category: NAMING
@@ -89,6 +105,10 @@ templateList:
- referencing_column
- referenced_table
- referenced_column
+ - key: length_limit
+ payload:
+ type: NUMBER
+ default: 64
level: WARNING
- type: column.required
category: COLUMN
@@ -134,6 +154,10 @@ templateList:
payload:
type: STRING
default: "^[a-z]+(_[a-z]+)*$"
+ - key: length_limit
+ payload:
+ type: NUMBER
+ default: 64
level: WARNING
- type: naming.column
category: NAMING
@@ -143,6 +167,10 @@ templateList:
payload:
type: STRING
default: "^[a-z]+(_[a-z]+)*$"
+ - key: length_limit
+ payload:
+ type: NUMBER
+ default: 64
level: WARNING
- type: naming.index.uk
category: NAMING
@@ -155,6 +183,10 @@ templateList:
templateList:
- table
- column_list
+ - key: length_limit
+ payload:
+ type: NUMBER
+ default: 64
level: WARNING
- type: naming.index.idx
category: NAMING
@@ -167,6 +199,10 @@ templateList:
templateList:
- table
- column_list
+ - key: length_limit
+ payload:
+ type: NUMBER
+ default: 64
level: WARNING
- type: naming.index.fk
category: NAMING
@@ -181,6 +217,10 @@ templateList:
- referencing_column
- referenced_table
- referenced_column
+ - key: length_limit
+ payload:
+ type: NUMBER
+ default: 64
level: WARNING
- type: column.required
category: COLUMN
diff --git a/plugin/advisor/schema_review.go b/plugin/advisor/schema_review.go
index 27123d62c9..571df41460 100644
--- a/plugin/advisor/schema_review.go
+++ b/plugin/advisor/schema_review.go
@@ -146,7 +146,8 @@ func (rule *SchemaReviewRule) Validate() error {
// NamingRulePayload is the payload for naming rule.
type NamingRulePayload struct {
- Format string `json:"format"`
+ MaxLength int `json:"maxLength"`
+ Format string `json:"format"`
}
// RequiredColumnRulePayload is the payload for required column rule.