Skip to content

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

feat(advisor): add PostgreSQL rules in frontend (#4498)

rebelice476fe83a9858
diff --git a/backend/plugin/advisor/config/sql-review.dev.yaml b/backend/plugin/advisor/config/sql-review.dev.yaml
index 40f724b1f1..955b358d3c 100644
--- a/backend/plugin/advisor/config/sql-review.dev.yaml
+++ b/backend/plugin/advisor/config/sql-review.dev.yaml
@@ -45,6 +45,12 @@ ruleList:
number: 1000
- type: statement.dml-dry-run
level: WARNING
+ - type: statement.disallow-add-column-with-default
+ level: WARNING
+ - type: statement.add-check-not-valid
+ level: WARNING
+ - type: statement.disallow-add-not-null
+ level: WARNING
- type: naming.table
level: WARNING
payload:
@@ -147,6 +153,14 @@ ruleList:
level: WARNING
payload:
number: 5
+ - type: index.primary-key-type-allowlist
+ level: ERROR
+ payload:
+ list:
+ - SERIAL
+ - BIGSERIAL
+ - type: index.create-concurrently
+ level: WARNING
- type: system.charset.allowlist
level: WARNING
payload:
@@ -157,3 +171,7 @@ ruleList:
payload:
list:
- utf8mb4_0900_ai_ci
+ - type: system.comment.length
+ level: WARNING
+ payload:
+ number: 64
diff --git a/backend/plugin/advisor/config/sql-review.prod.yaml b/backend/plugin/advisor/config/sql-review.prod.yaml
index d105869a3a..6be8432bec 100644
--- a/backend/plugin/advisor/config/sql-review.prod.yaml
+++ b/backend/plugin/advisor/config/sql-review.prod.yaml
@@ -45,6 +45,12 @@ ruleList:
number: 1000
- type: statement.dml-dry-run
level: WARNING
+ - type: statement.disallow-add-column-with-default
+ level: WARNING
+ - type: statement.add-check-not-valid
+ level: WARNING
+ - type: statement.disallow-add-not-null
+ level: WARNING
- type: naming.table
level: WARNING
payload:
@@ -147,6 +153,14 @@ ruleList:
level: WARNING
payload:
number: 5
+ - type: index.primary-key-type-allowlist
+ level: ERROR
+ payload:
+ list:
+ - SERIAL
+ - BIGSERIAL
+ - type: index.create-concurrently
+ level: WARNING
- type: system.charset.allowlist
level: ERROR
payload:
@@ -157,3 +171,7 @@ ruleList:
payload:
list:
- utf8mb4_0900_ai_ci
+ - type: system.comment.length
+ level: WARNING
+ payload:
+ number: 64
diff --git a/backend/plugin/advisor/pg/test/comment_length.yaml b/backend/plugin/advisor/pg/test/system_comment_length.yaml
similarity index 91%
rename from backend/plugin/advisor/pg/test/comment_length.yaml
rename to backend/plugin/advisor/pg/test/system_comment_length.yaml
index c03ea16727..1ae2bc6fb0 100644
--- a/backend/plugin/advisor/pg/test/comment_length.yaml
+++ b/backend/plugin/advisor/pg/test/system_comment_length.yaml
@@ -9,7 +9,7 @@
want:
- status: WARN
code: 1301
- title: comment.length
+ title: system.comment.length
content: The length of comment should be within 20 characters
line: 1
- statement: comment on table public is 'is a table name'
@@ -23,6 +23,6 @@
want:
- status: WARN
code: 1301
- title: comment.length
+ title: system.comment.length
content: The length of comment should be within 20 characters
line: 1
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 7c597f69b0..34d2831a41 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -152,7 +152,7 @@ const (
SchemaRuleCollationAllowlist SQLReviewRuleType = "system.collation.allowlist"
// SchemaRuleCommentLength limit comment length.
- SchemaRuleCommentLength SQLReviewRuleType = "comment.length"
+ SchemaRuleCommentLength SQLReviewRuleType = "system.comment.length"
// TableNameTemplateToken is the token for table name.
TableNameTemplateToken = "{{table}}"
diff --git a/frontend/src/locales/sql-review/en-US.json b/frontend/src/locales/sql-review/en-US.json
index af342640b6..8175e9893d 100644
--- a/frontend/src/locales/sql-review/en-US.json
+++ b/frontend/src/locales/sql-review/en-US.json
@@ -307,6 +307,18 @@
"title": "Dry run DML statements",
"description": "Dry run DML statements by EXPLAIN."
},
+ "statement-disallow-add-column-with-default": {
+ "title": "Disallow add column with default",
+ "description": "The PostgreSQL will lock the table and rewrite the whole table when you adding column with default value. You can separate the adding column, setting default value and backfilling all existing rows."
+ },
+ "statement-add-check-not-valid": {
+ "title": "Add CHECK constraints with NOT VALID option",
+ "description": "Adding CHECK constraints without NOT VALID can cause downtime because it blocks reads and writes. You can manually verify all rows and validate the constraint after creating."
+ },
+ "statement-disallow-add-not-null": {
+ "title": "Disallow add NOT NULL constraints to an existing column",
+ "description": "It can cause downtime because it blocks reads and writes. You can add CHECK constraints with NOT VALID option to avoid this."
+ },
"schema-backward-compatibility": {
"title": "Backward compatibility",
"description": "MySQL and TiDB support checking whether the schema change is backward compatible."
@@ -345,6 +357,19 @@
}
}
},
+ "index-primary-key-type-allowlist": {
+ "title": "Primary key type allow list",
+ "description": "",
+ "component": {
+ "list": {
+ "title": "Allow list"
+ }
+ }
+ },
+ "index-create-concurrently": {
+ "title": "Create indexes concurrently",
+ "description": "Creating indexes blocks writes (but not reads) on the table until it's done. Use CONCURRENTLY when creates indexes can allow writes to continue."
+ },
"system-charset-allowlist": {
"title": "Charset allow list",
"description": "",
@@ -362,6 +387,15 @@
"title": "Allow list"
}
}
+ },
+ "system-comment-length": {
+ "title": "Comment length limit",
+ "description": "",
+ "component": {
+ "number": {
+ "title": "Maximum comment length"
+ }
+ }
}
},
"level": {
diff --git a/frontend/src/locales/sql-review/zh-CN.json b/frontend/src/locales/sql-review/zh-CN.json
index 5d6f61dd57..5c654427f8 100644
--- a/frontend/src/locales/sql-review/zh-CN.json
+++ b/frontend/src/locales/sql-review/zh-CN.json
@@ -308,6 +308,18 @@
"title": "试运行 DML 语句",
"description": "使用 EXPLAIN 语句试运行 DML。"
},
+ "statement.disallow-add-column-with-default": {
+ "title": "增加列时不能同时设置默认值",
+ "description": "增加列的同时设置默认值会导致 PostgreSQL 锁表并且重写所有行的数据。可以分别进行添加列、设置默认值和回填已有行操作避免可能的停机。"
+ },
+ "statement.add-check-not-valid": {
+ "title": "添加 CHECK 约束时使用 NOT VALID 参数",
+ "description": "添加 CHECK 约束时会阻塞读写操作,可能导致停机。可以使用 NOT VALID 参数在添加 CHECK 约束时跳过检查,并在之后分别进行手动检查已有数据和使约束生效来避免可能的停机。"
+ },
+ "statement.disallow-add-not-null": {
+ "title": "避免 NOT NULL 到已有列",
+ "description": "添加 NOT NULL 到已有列会阻塞读写操作,可能导致停机。可以使用 CHECK (column IS NOT NULL) NOT VALID 绕过,避免可能的停机。"
+ },
"schema-backward-compatibility": {
"title": "向后兼容",
"description": "MySQL 和 TiDB 支持检测 schema 变更是否向后兼容。"
@@ -346,6 +358,19 @@
}
}
},
+ "index.primary-key-type-allowlist": {
+ "title": "主键列的类型限制",
+ "description": "",
+ "component": {
+ "list": {
+ "title": "允许的主键列的类型"
+ }
+ }
+ },
+ "index.create-concurrently": {
+ "title": "创建索引时使用 CONCURRENTLY 参数",
+ "description": "创建索引会导致无法写表。可以使用 CONCURRENTLY 参数避免阻塞写表操作。"
+ },
"system-charset-allowlist": {
"title": "字符集限制",
"description": "",
@@ -363,6 +388,15 @@
"title": "允许的规则限制"
}
}
+ },
+ "system.comment.length": {
+ "title": "注释长度限制",
+ "description": "",
+ "component": {
+ "number": {
+ "title": "注释长度上限"
+ }
+ }
}
},
"level": {
diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yaml
index a35fac1f1d..d8cbbd0b9f 100644
--- a/frontend/src/types/sql-review-schema.yaml
+++ b/frontend/src/types/sql-review-schema.yaml
@@ -33,6 +33,7 @@ ruleList:
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList:
- key: format
payload:
@@ -57,6 +58,7 @@ ruleList:
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList: []
- type: statement.select.no-select-all
category: STATEMENT
@@ -84,6 +86,7 @@ ruleList:
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList: []
- type: statement.disallow-limit
category: STATEMENT
@@ -102,6 +105,7 @@ ruleList:
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList: []
- type: statement.insert.row-limit
category: STATEMENT
@@ -118,17 +122,20 @@ ruleList:
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList: []
- type: statement.insert.disallow-order-by-rand
category: STATEMENT
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList: []
- type: statement.affected-row-limit
category: STATEMENT
engineList:
- MYSQL
+ - POSTGRES
componentList:
- key: number
payload:
@@ -138,6 +145,22 @@ ruleList:
category: STATEMENT
engineList:
- MYSQL
+ - POSTGRES
+ componentList: []
+ - type: statement.disallow-add-column-with-default
+ category: STATEMENT
+ engineList:
+ - POSTGRES
+ componentList: []
+ - type: statement.add-check-not-valid
+ category: STATEMENT
+ engineList:
+ - POSTGRES
+ componentList: []
+ - type: statement.disallow-add-not-null
+ category: STATEMENT
+ engineList:
+ - POSTGRES
componentList: []
- type: naming.table
category: NAMING
@@ -283,6 +306,7 @@ ruleList:
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList: []
- type: column.set-default-for-not-null
category: COLUMN
@@ -327,6 +351,7 @@ ruleList:
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList:
- key: list
payload:
@@ -343,6 +368,7 @@ ruleList:
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList:
- key: number
payload:
@@ -375,6 +401,7 @@ ruleList:
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList: []
- type: schema.backward-compatibility
category: SCHEMA
@@ -394,12 +421,14 @@ ruleList:
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList: []
- type: index.key-number-limit
category: INDEX
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList:
- key: number
payload:
@@ -422,11 +451,27 @@ ruleList:
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList:
- key: number
payload:
type: NUMBER
default: 5
+ - type: index.primary-key-type-allowlist
+ category: INDEX
+ engineList:
+ - MYSQL
+ - TIDB
+ componentList:
+ - key: list
+ payload:
+ type: STRING_ARRAY
+ default: []
+ - type: index.create-concurrently
+ category: INDEX
+ engineList:
+ - POSTGRES
+ componentList: []
- type: system.charset.allowlist
category: SYSTEM
engineList:
@@ -443,8 +488,18 @@ ruleList:
engineList:
- MYSQL
- TIDB
+ - POSTGRES
componentList:
- key: list
payload:
type: STRING_ARRAY
default: []
+ - type: system.comment.length
+ category: SYSTEM
+ engineList:
+ - POSTGRES
+ componentList:
+ - key: number
+ payload:
+ type: NUMBER
+ default: 64
diff --git a/frontend/src/types/sqlReview.ts b/frontend/src/types/sqlReview.ts
index e47a7bdff9..cdcb639150 100644
--- a/frontend/src/types/sqlReview.ts
+++ b/frontend/src/types/sqlReview.ts
@@ -130,14 +130,20 @@ export type RuleType =
| "statement.insert.row-limit"
| "statement.affected-row-limit"
| "statement.dml-dry-run"
+ | "statement.disallow-add-column-with-default"
+ | "statement.add-check-not-valid"
+ | "statement.disallow-add-not-null"
| "schema.backward-compatibility"
| "database.drop-empty-database"
| "system.charset.allowlist"
| "system.collation.allowlist"
+ | "system.comment.length"
| "index.no-duplicate-column"
| "index.type-no-blob"
| "index.key-number-limit"
| "index.total-number-limit"
+ | "index.primary-key-type-allowlist"
+ | "index.create-concurrently"
| "index.pk-type-limit";
// The naming format rule payload.
@@ -442,6 +448,7 @@ export const convertPolicyRuleToRuleTemplate = (
};
}
case "column.type-disallow-list":
+ case "index.primary-key-type-allowlist":
case "system.charset.allowlist":
case "system.collation.allowlist": {
const stringArrayComponent = ruleTemplate.componentList[0];
@@ -490,6 +497,7 @@ export const convertPolicyRuleToRuleTemplate = (
case "column.auto-increment-initial-value":
case "index.key-number-limit":
case "index.total-number-limit":
+ case "system.comment.length":
if (!numberComponent) {
throw new Error(`Invalid rule ${ruleTemplate.type}`);
}
@@ -588,6 +596,7 @@ export const convertRuleTemplateToPolicyRule = (
};
case "column.required":
case "column.type-disallow-list":
+ case "index.primary-key-type-allowlist":
case "system.charset.allowlist":
case "system.collation.allowlist": {
const stringArrayPayload = rule.componentList[0]
@@ -617,6 +626,7 @@ export const convertRuleTemplateToPolicyRule = (
case "column.auto-increment-initial-value":
case "index.key-number-limit":
case "index.total-number-limit":
+ case "system.comment.length":
if (!numberPayload) {
throw new Error(`Invalid rule ${rule.type}`);
}