Skip to content

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

chore: support index.pk-type-limit rule (#3078)

ecmadaoe483ca10b22a
diff --git a/frontend/src/locales/sql-review/en-US.json b/frontend/src/locales/sql-review/en-US.json
index 7c07d64dac..7b823368d6 100644
--- a/frontend/src/locales/sql-review/en-US.json
+++ b/frontend/src/locales/sql-review/en-US.json
@@ -288,6 +288,10 @@
"title": "Disallow BLOB",
"description": "Disallow using BLOB type column in index."
},
+ "index-pk-type-limit": {
+ "title": "Primary key type limit",
+ "description": "Enforce the primary key type to be INT or BIGINT."
+ },
"index-key-number-limit": {
"title": "Column count limit in index",
"description": "Limit the related row count in a single index.",
diff --git a/frontend/src/locales/sql-review/zh-CN.json b/frontend/src/locales/sql-review/zh-CN.json
index 4f9bb066bf..3a83c526aa 100644
--- a/frontend/src/locales/sql-review/zh-CN.json
+++ b/frontend/src/locales/sql-review/zh-CN.json
@@ -289,6 +289,10 @@
"title": "BLOB 列不能建作为索引",
"description": ""
},
+ "index-pk-type-limit": {
+ "title": "主键类型限制",
+ "description": "限制主键类型必须是 INT 或 BIGINT。"
+ },
"index-key-number-limit": {
"title": "索引包含的字段数量限制",
"description": "",
diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yaml
index 2d1428e737..d24e8aa26f 100644
--- a/frontend/src/types/sql-review-schema.yaml
+++ b/frontend/src/types/sql-review-schema.yaml
@@ -367,7 +367,12 @@ ruleList:
payload:
type: NUMBER
default: 5
- # TODO: - type: index.pk-type
+ - type: index.pk-type-limit
+ category: INDEX
+ engineList:
+ - MYSQL
+ - TIDB
+ componentList: []
- type: index.type-no-blob
category: INDEX
engineList:
diff --git a/frontend/src/types/sqlReview.ts b/frontend/src/types/sqlReview.ts
index 68bb3c708c..99af9a88b9 100644
--- a/frontend/src/types/sqlReview.ts
+++ b/frontend/src/types/sqlReview.ts
@@ -133,7 +133,8 @@ export type RuleType =
| "index.no-duplicate-column"
| "index.type-no-blob"
| "index.key-number-limit"
- | "index.total-number-limit";
+ | "index.total-number-limit"
+ | "index.pk-type-limit";
export const availableRulesForFreePlan: RuleType[] = [
"statement.where.require",
diff --git a/plugin/advisor/config/sql-review.dev.yaml b/plugin/advisor/config/sql-review.dev.yaml
index 1203a5b11e..28bd49a876 100644
--- a/plugin/advisor/config/sql-review.dev.yaml
+++ b/plugin/advisor/config/sql-review.dev.yaml
@@ -120,6 +120,8 @@ ruleList:
level: ERROR
- type: index.type-no-blob
level: ERROR
+ - type: index.pk-type-limit
+ level: WARNING
- type: index.key-number-limit
level: WARNING
payload:
diff --git a/plugin/advisor/config/sql-review.prod.yaml b/plugin/advisor/config/sql-review.prod.yaml
index 29bdecc822..da704c1b91 100644
--- a/plugin/advisor/config/sql-review.prod.yaml
+++ b/plugin/advisor/config/sql-review.prod.yaml
@@ -120,6 +120,8 @@ ruleList:
level: ERROR
- type: index.type-no-blob
level: ERROR
+ - type: index.pk-type-limit
+ level: ERROR
- type: index.key-number-limit
level: WARNING
payload:
diff --git a/plugin/advisor/mysql/advisor_index_pk_type_test.go b/plugin/advisor/mysql/advisor_index_pk_type_test.go
index 7e8a978b6f..173edb1b55 100644
--- a/plugin/advisor/mysql/advisor_index_pk_type_test.go
+++ b/plugin/advisor/mysql/advisor_index_pk_type_test.go
@@ -17,7 +17,7 @@ func TestIndexPkType(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)",
Line: 1,
},
@@ -41,7 +41,7 @@ func TestIndexPkType(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)",
Line: 1,
},
@@ -53,14 +53,14 @@ func TestIndexPkType(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id2` is varchar(5)",
Line: 1,
},
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id3` is varchar(5)",
Line: 1,
},
@@ -97,7 +97,7 @@ func TestIndexPkType(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)",
Line: 3,
},
@@ -125,7 +125,7 @@ func TestIndexPkType(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)",
Line: 3,
},
@@ -139,14 +139,14 @@ func TestIndexPkType(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id2` is varchar(5)",
Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id3` is varchar(5)",
Line: 3,
},
@@ -187,7 +187,7 @@ func TestIndexPkType(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)",
Line: 2,
},
@@ -201,14 +201,14 @@ func TestIndexPkType(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id2` is varchar(5)",
Line: 2,
},
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id3` is varchar(5)",
Line: 2,
},
@@ -249,7 +249,7 @@ func TestIndexPkType(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id` is varchar(5)",
Line: 3,
},
@@ -277,7 +277,7 @@ func TestIndexPkType(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.IndexPKType,
- Title: "index.pk-type",
+ Title: "index.pk-type-limit",
Content: "Columns in primary key must be INT/BIGINT but `t`.`id2` is varchar(5)",
Line: 3,
},
@@ -299,7 +299,7 @@ func TestIndexPkType(t *testing.T) {
}
advisor.RunSQLReviewRuleTests(t, tests, &IndexPkTypeAdvisor{}, &advisor.SQLReviewRule{
- Type: advisor.SchemaRuleIndexPKType,
+ Type: advisor.SchemaRuleIndexPKTypeLimit,
Level: advisor.SchemaRuleLevelWarning,
Payload: "",
}, advisor.MockMySQLDatabase)
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 6719a6f0e2..3ca6d291fa 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -124,8 +124,8 @@ const (
SchemaRuleIndexNoDuplicateColumn SQLReviewRuleType = "index.no-duplicate-column"
// SchemaRuleIndexKeyNumberLimit enforce the index key number limit.
SchemaRuleIndexKeyNumberLimit SQLReviewRuleType = "index.key-number-limit"
- // SchemaRuleIndexPKType enforce the type restriction of columns in primary key.
- SchemaRuleIndexPKType SQLReviewRuleType = "index.pk-type"
+ // SchemaRuleIndexPKTypeLimit enforce the type restriction of columns in primary key.
+ SchemaRuleIndexPKTypeLimit SQLReviewRuleType = "index.pk-type-limit"
// SchemaRuleIndexTypeNoBlob enforce the type restriction of columns in index.
SchemaRuleIndexTypeNoBlob SQLReviewRuleType = "index.type-no-blob"
// SchemaRuleIndexTotalNumberLimit enforce the index total number limit.
@@ -835,7 +835,7 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLCollationAllowlist, nil
}
- case SchemaRuleIndexPKType:
+ case SchemaRuleIndexPKTypeLimit:
switch engine {
case db.MySQL, db.TiDB:
return MySQLIndexPKType, nil