Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: SQL review rules (#2271)
ecmadao3451ef1760fe
diff --git a/frontend/src/locales/sql-review/en-US.json b/frontend/src/locales/sql-review/en-US.jsonindex 9ce8c10dc1..cfcffde7fd 100644--- a/frontend/src/locales/sql-review/en-US.json+++ b/frontend/src/locales/sql-review/en-US.json@@ -41,31 +41,31 @@},"naming-table": {"title": "Table naming check",- "description": "Enforce the table name format and length limit. Default snake_lower_case with 64 characters.",+ "description": "Enforce the table name format and length limit. Default snake_lower_case with 63 characters.","component": {"format": {"title": "Table name format (regex)"},"maxLength": {- "title": "Length limit (PostgreSQL is not supported)"+ "title": "Length limit"}}},"naming-column": {"title": "Column naming check",- "description": "Enforce the column name format and length limit. Default snake_lower_case with 64 characters.",+ "description": "Enforce the column name format and length limit. Default snake_lower_case with 63 characters.","component": {"format": {"title": "Column name format (regex)"},"maxLength": {- "title": "Length limit (PostgreSQL is not supported)"+ "title": "Length limit"}}},"naming-index-uk": {"title": "Unique key naming check",- "description": "Enforce the unique key name format and length limit. Default uk_<table_name>_<column_list> with 64 characters.",+ "description": "Enforce the unique key name format and length limit. Default uk_<table_name>_<column_list> or empty within 63 characters.","component": {"format": {"title": "Unique key name format",@@ -75,13 +75,13 @@}},"maxLength": {- "title": "Length limit (PostgreSQL is not supported)"+ "title": "Length limit"}}},"naming-index-pk": {"title": "Primary key naming check",- "description": "Enforce the primary key name format and length limit. Default pk_<table_name>_<column_list>.",+ "description": "Enforce the primary key name format and length limit. Default pk_<table_name>_<column_list> or empty within 63 characters.","component": {"format": {"title": "Primary key name format",@@ -89,12 +89,15 @@"table": "The table name","column_list": "Index column names, joined by _"}+ },+ "maxLength": {+ "title": "Length limit"}}},"naming-index-fk": {"title": "Foreign key naming check",- "description": "Enforce the foreign key name format and length limit. Default fk_<referencing_table>_<referencing_column>_<referenced_table>_<referenced_column> with 64 characters.",+ "description": "Enforce the foreign key name format and length limit. Default fk_<referencing_table>_<referencing_column>_<referenced_table>_<referenced_column> or empty within 63 characters.","component": {"format": {"title": "Foreign key name format",@@ -106,13 +109,13 @@}},"maxLength": {- "title": "Length limit (PostgreSQL is not supported)"+ "title": "Length limit"}}},"naming-index-idx": {"title": "Index naming check",- "description": "Enforce the index name format and length limit. Default idx_<table_name>_<column_list> with 64 characters.",+ "description": "Enforce the index name format and length limit. Default idx_<table_name>_<column_list> or empty within 63 characters.","component": {"format": {"title": "Index name format",@@ -122,7 +125,7 @@}},"maxLength": {- "title": "Length limit (PostgreSQL is not supported)"+ "title": "Length limit"}}},diff --git a/frontend/src/locales/sql-review/zh-CN.json b/frontend/src/locales/sql-review/zh-CN.jsonindex 258ce106ee..e5e187c41c 100644--- a/frontend/src/locales/sql-review/zh-CN.json+++ b/frontend/src/locales/sql-review/zh-CN.json@@ -41,31 +41,31 @@},"naming-table": {"title": "表名命名检查",- "description": "限制表名命名风格和长度,默认为小写字母_下划线,且不超过 64 个字符。",+ "description": "限制表名命名风格和长度,默认为小写字母_下划线,且不超过 63 个字符。","component": {"format": {"title": "表命名规则(正则)"},"maxLength": {- "title": "长度限制(暂不支持 PostgreSQL)"+ "title": "长度限制"}}},"naming-column": {"title": "列名命名检查",- "description": "限制列名命名风格和长度,默认为小写字母_下划线,且不超过 64 个字符。",+ "description": "限制列名命名风格和长度,默认为小写字母_下划线,且不超过 63 个字符。","component": {"format": {"title": "列命名规则(正则)"},"maxLength": {- "title": "长度限制(暂不支持 PostgreSQL)"+ "title": "长度限制"}}},"naming-index-uk": {"title": "唯一键命名检查",- "description": "限制唯一键命名风格和长度,默认为 uk_<表名>_<主键包含的字段名组合>,且不超过 64 个字符。",+ "description": "限制唯一键命名风格和长度,默认为 uk_<表名>_<主键包含的字段名组合> 或空,且不超过 63 个字符。","component": {"format": {"title": "唯一键命名规则",@@ -75,13 +75,13 @@}},"maxLength": {- "title": "长度限制(暂不支持 PostgreSQL)"+ "title": "长度限制"}}},"naming-index-pk": {"title": "主键命名检查",- "description": "限制主键命名风格和长度,默认为 pk_<表名>_<主键包含的字段名组合>。",+ "description": "限制主键命名风格和长度,默认为 pk_<表名>_<主键包含的字段名组合> 或空,且不超过 63 个字符。","component": {"format": {"title": "主键命名规则",@@ -89,12 +89,15 @@"table": "表名","column_list": "索引包含的字段名,通过 _ 连接"}+ },+ "maxLength": {+ "title": "长度限制"}}},"naming-index-fk": {"title": "外键命名检查",- "description": "限制外键命名风格和长度,默认为 fk_<目标表名>_<目标字段名>_<被引用表名>_<被引用字段名>,且不超过 64 个字符。",+ "description": "限制外键命名风格和长度,默认为 fk_<目标表名>_<目标字段名>_<被引用表名>_<被引用字段名> 或空,且不超过 63 个字符。","component": {"format": {"title": "外键命名规则",@@ -106,13 +109,13 @@}},"maxLength": {- "title": "长度限制(暂不支持 PostgreSQL)"+ "title": "长度限制"}}},"naming-index-idx": {"title": "索引命名检查",- "description": "限制索引命名风格和长度,默认为 idx_<表名>_<索引包含的字段名组合>,且不超过 64 个字符。",+ "description": "限制索引命名风格和长度,默认为 idx_<表名>_<索引包含的字段名组合> 或空,且不超过 63 个字符。","component": {"format": {"title": "索引命名规则",@@ -122,7 +125,7 @@}},"maxLength": {- "title": "长度限制(暂不支持 PostgreSQL)"+ "title": "长度限制"}}},diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yamlindex bca2c09ff5..daa6dbebc4 100644--- a/frontend/src/types/sql-review-schema.yaml+++ b/frontend/src/types/sql-review-schema.yaml@@ -155,6 +155,10 @@ ruleList:templateList:- table- column_list+ - key: maxLength+ payload:+ type: NUMBER+ default: 63- type: column.requiredcategory: COLUMNengineList:diff --git a/plugin/advisor/config/sql-review.dev.yaml b/plugin/advisor/config/sql-review.dev.yamlindex 520a69759b..5bf5a3e8ea 100644--- a/plugin/advisor/config/sql-review.dev.yaml+++ b/plugin/advisor/config/sql-review.dev.yaml@@ -20,32 +20,32 @@ ruleList:level: WARNINGpayload:format: "^[a-z]+(_[a-z]+)*$"- maxLength: 64+ maxLength: 63- type: naming.columnlevel: WARNINGpayload:format: "^[a-z]+(_[a-z]+)*$"- maxLength: 64+ maxLength: 63- type: naming.index.uklevel: WARNINGpayload:- format: "^uk_{{table}}_{{column_list}}$"- maxLength: 64+ format: "^$|^uk_{{table}}_{{column_list}}$"+ maxLength: 63- type: naming.index.pklevel: WARNINGpayload:- format: "^pk_{{table}}_{{column_list}}$"- maxLength: 64+ format: "^$|^pk_{{table}}_{{column_list}}$"+ maxLength: 63- type: naming.index.idxlevel: WARNINGpayload:- format: "^idx_{{table}}_{{column_list}}$"- maxLength: 64+ format: "^$|^idx_{{table}}_{{column_list}}$"+ maxLength: 63- type: naming.index.fklevel: WARNINGpayload:- format: "^fk_{{referencing_table}}_{{referencing_column}}_{{referenced_table}}_{{referenced_column}}$"- maxLength: 64+ format: "^$|^fk_{{referencing_table}}_{{referencing_column}}_{{referenced_table}}_{{referenced_column}}$"+ maxLength: 63- type: column.requiredlevel: WARNINGpayload:diff --git a/plugin/advisor/config/sql-review.prod.yaml b/plugin/advisor/config/sql-review.prod.yamlindex 0e4c7dce12..3734aa47e6 100644--- a/plugin/advisor/config/sql-review.prod.yaml+++ b/plugin/advisor/config/sql-review.prod.yaml@@ -20,32 +20,32 @@ ruleList:level: WARNINGpayload:format: "^[a-z]+(_[a-z]+)*$"- maxLength: 64+ maxLength: 63- type: naming.columnlevel: WARNINGpayload:format: "^[a-z]+(_[a-z]+)*$"- maxLength: 64+ maxLength: 63- type: naming.index.uklevel: WARNINGpayload:- format: "^uk_{{table}}_{{column_list}}$"- maxLength: 64+ format: "^$|^uk_{{table}}_{{column_list}}$"+ maxLength: 63- type: naming.index.pklevel: WARNINGpayload:- format: "^pk_{{table}}_{{column_list}}$"- maxLength: 64+ format: "^$|^pk_{{table}}_{{column_list}}$"+ maxLength: 63- type: naming.index.idxlevel: WARNINGpayload:- format: "^idx_{{table}}_{{column_list}}$"- maxLength: 64+ format: "^$|^idx_{{table}}_{{column_list}}$"+ maxLength: 63- type: naming.index.fklevel: WARNINGpayload:- format: "^fk_{{referencing_table}}_{{referencing_column}}_{{referenced_table}}_{{referenced_column}}$"- maxLength: 64+ format: "^$|^fk_{{referencing_table}}_{{referencing_column}}_{{referenced_table}}_{{referenced_column}}$"+ maxLength: 63- type: column.requiredlevel: WARNINGpayload:diff --git a/plugin/advisor/config_test.go b/plugin/advisor/config_test.goindex d0a44000ee..5de0603d1e 100644--- a/plugin/advisor/config_test.go+++ b/plugin/advisor/config_test.go@@ -55,7 +55,7 @@ func TestConfigOverride(t *testing.T) {require.NoError(t, err)assert.Equal(t, "^table_[a-z]+(_[a-z]+)*$", nr.Format)- assert.Equal(t, 64, nr.MaxLength)+ assert.Equal(t, 63, nr.MaxLength)case "naming.column":assert.Equal(t, SchemaRuleLevelWarning, rule.Level)diff --git a/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go b/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.goindex e33f32b26e..2ca0fec97e 100644--- a/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go+++ b/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go@@ -31,7 +31,7 @@ func TestNamingFKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingFKConventionMismatch,Title: "naming.index.fk",- Content: "Foreign key in table `tech_book` mismatches the naming convention, expect \"^fk_tech_book_author_id_author_id$\" but found `fk_author_id`",+ Content: "Foreign key in table `tech_book` mismatches the naming convention, expect \"^$|^fk_tech_book_author_id_author_id$\" but found `fk_author_id`",},},},@@ -42,7 +42,7 @@ func TestNamingFKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingFKConventionMismatch,Title: "naming.index.fk",- Content: fmt.Sprintf("Foreign key in table `tech_book` mismatches the naming convention, expect \"^fk_tech_book_author_id_author_id$\" but found `%s`", invalidFKName),+ Content: fmt.Sprintf("Foreign key in table `tech_book` mismatches the naming convention, expect \"^$|^fk_tech_book_author_id_author_id$\" but found `%s`", invalidFKName),},{Status: advisor.Error,@@ -63,6 +63,17 @@ func TestNamingFKConvention(t *testing.T) {},},},+ {+ Statement: "CREATE TABLE book(id INT, author_id INT, FOREIGN KEY (author_id) REFERENCES author (id))",+ Want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },{Statement: "CREATE TABLE book(id INT, author_id INT, FOREIGN KEY fk_book_author_id (author_id) REFERENCES author (id))",Want: []advisor.Advice{@@ -70,14 +81,14 @@ func TestNamingFKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingFKConventionMismatch,Title: "naming.index.fk",- Content: "Foreign key in table `book` mismatches the naming convention, expect \"^fk_book_author_id_author_id$\" but found `fk_book_author_id`",+ Content: "Foreign key in table `book` mismatches the naming convention, expect \"^$|^fk_book_author_id_author_id$\" but found `fk_book_author_id`",},},},}payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^fk_{{referencing_table}}_{{referencing_column}}_{{referenced_table}}_{{referenced_column}}$",+ Format: "^$|^fk_{{referencing_table}}_{{referencing_column}}_{{referenced_table}}_{{referenced_column}}$",MaxLength: 64,})require.NoError(t, err)diff --git a/plugin/advisor/mysql/advisor_naming_index_convention_test.go b/plugin/advisor/mysql/advisor_naming_index_convention_test.goindex 60cabec18b..c699e15712 100644--- a/plugin/advisor/mysql/advisor_naming_index_convention_test.go+++ b/plugin/advisor/mysql/advisor_naming_index_convention_test.go@@ -32,7 +32,7 @@ func TestNamingIndexConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingIndexConventionMismatch,Title: "naming.index.idx",- Content: "Index in table `tech_book` mismatches the naming convention, expect \"^idx_tech_book_id_name$\" but found `tech_book_id_name`",+ Content: "Index in table `tech_book` mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found `tech_book_id_name`",},},},@@ -43,7 +43,7 @@ func TestNamingIndexConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingIndexConventionMismatch,Title: "naming.index.idx",- Content: fmt.Sprintf("Index in table `tech_book` mismatches the naming convention, expect \"^idx_tech_book_id_name$\" but found `%s`", invalidIndexName),+ Content: fmt.Sprintf("Index in table `tech_book` mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found `%s`", invalidIndexName),},{Status: advisor.Error,@@ -78,7 +78,7 @@ func TestNamingIndexConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingIndexConventionMismatch,Title: "naming.index.idx",- Content: "Index in table `tech_book` mismatches the naming convention, expect \"^idx_tech_book_id_name$\" but found `idx_tech_book`",+ Content: "Index in table `tech_book` mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found `idx_tech_book`",},},},@@ -100,7 +100,7 @@ func TestNamingIndexConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingIndexConventionMismatch,Title: "naming.index.idx",- Content: "Index in table `tech_book` mismatches the naming convention, expect \"^idx_tech_book_id_name$\" but found `tech_book_id_name`",+ Content: "Index in table `tech_book` mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found `tech_book_id_name`",},},},@@ -119,17 +119,17 @@ func TestNamingIndexConvention(t *testing.T) {Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), INDEX (name))",Want: []advisor.Advice{{- Status: advisor.Error,- Code: advisor.NamingIndexConventionMismatch,- Title: "naming.index.idx",- Content: "Index in table `tech_book` mismatches the naming convention, expect \"^idx_tech_book_name$\" but found ``",+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",},},},}payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^idx_{{table}}_{{column_list}}$",+ Format: "^$|^idx_{{table}}_{{column_list}}$",MaxLength: 64,})require.NoError(t, err)diff --git a/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go b/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.goindex 6f8608f716..32dd03f0d6 100644--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go@@ -32,7 +32,7 @@ func TestNamingUKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingUKConventionMismatch,Title: "naming.index.uk",- Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^uk_tech_book_id_name$\" but found `tech_book_id_name`",+ Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found `tech_book_id_name`",},},},@@ -43,7 +43,7 @@ func TestNamingUKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingUKConventionMismatch,Title: "naming.index.uk",- Content: fmt.Sprintf("Unique key in table `tech_book` mismatches the naming convention, expect \"^uk_tech_book_id_name$\" but found `%s`", invalidUKName),+ Content: fmt.Sprintf("Unique key in table `tech_book` mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found `%s`", invalidUKName),},{Status: advisor.Error,@@ -71,7 +71,7 @@ func TestNamingUKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingUKConventionMismatch,Title: "naming.index.uk",- Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^uk_tech_book_id_name$\" but found `tech_book_id_name`",+ Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found `tech_book_id_name`",},},},@@ -100,7 +100,7 @@ func TestNamingUKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingUKConventionMismatch,Title: "naming.index.uk",- Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^uk_tech_book_id_name$\" but found `uk_tech_book`",+ Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found `uk_tech_book`",},},},@@ -119,10 +119,10 @@ func TestNamingUKConvention(t *testing.T) {Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE KEY (name))",Want: []advisor.Advice{{- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- Title: "naming.index.uk",- Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^uk_tech_book_name$\" but found ``",+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",},},},@@ -130,28 +130,17 @@ func TestNamingUKConvention(t *testing.T) {Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE INDEX (name))",Want: []advisor.Advice{{- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- Title: "naming.index.uk",- Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^uk_tech_book_name$\" but found ``",- },- },- },- {- Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE KEY (name))",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- Title: "naming.index.uk",- Content: "Unique key in table `tech_book` mismatches the naming convention, expect \"^uk_tech_book_name$\" but found ``",+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",},},},}payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^uk_{{table}}_{{column_list}}$",+ Format: "^$|^uk_{{table}}_{{column_list}}$",MaxLength: 64,})require.NoError(t, err)diff --git a/plugin/advisor/pg/advisor_naming_column.go b/plugin/advisor/pg/advisor_naming_column.goindex 96be31115a..1c53b24504 100644--- a/plugin/advisor/pg/advisor_naming_column.go+++ b/plugin/advisor/pg/advisor_naming_column.go@@ -33,16 +33,16 @@ func (*NamingColumnConventionAdvisor) Check(ctx advisor.Context, statement strinif err != nil {return nil, err}- // Naming length limit is not enabled for PG, the PG parser will auto slice the name to make sure its length <= 63- // Reference: https://stackoverflow.com/questions/27865770/how-long-can-postgresql-table-names-be- format, _, err := advisor.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)++ format, maxLength, err := advisor.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)if err != nil {return nil, err}checker := &namingColumnConventionChecker{- level: level,- title: string(ctx.Rule.Type),- format: format,+ level: level,+ title: string(ctx.Rule.Type),+ format: format,+ maxLength: maxLength,}for _, stmt := range stmts {@@ -65,6 +65,7 @@ type namingColumnConventionChecker struct {level advisor.Statustitle stringformat *regexp.Regexp+ maxLength int}// Visit implements the ast.Visitor interface.@@ -100,6 +101,15 @@ func (checker *namingColumnConventionChecker) Visit(node ast.Node) ast.Visitor {Content: fmt.Sprintf("\"%s\".\"%s\" mismatches column naming convention, naming format should be %q", tableName, column, checker.format),})}++ if checker.maxLength > 0 && len(column) > checker.maxLength {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.NamingColumnConventionMismatch,+ Title: checker.title,+ Content: fmt.Sprintf("\"%s\".\"%s\" mismatches column naming convention, its length should be within %d characters", tableName, column, checker.maxLength),+ })+ }}return checkerdiff --git a/plugin/advisor/pg/advisor_naming_column_test.go b/plugin/advisor/pg/advisor_naming_column_test.goindex 9f6a85c59e..28794cb509 100644--- a/plugin/advisor/pg/advisor_naming_column_test.go+++ b/plugin/advisor/pg/advisor_naming_column_test.go@@ -2,6 +2,7 @@ package pgimport ("encoding/json"+ "fmt""testing""github.com/bytebase/bytebase/plugin/advisor"@@ -9,6 +10,8 @@ import ()func TestNamingColumnConvention(t *testing.T) {+ invalidColumnName := advisor.RandomString(33)+tests := []advisor.TestCase{{Statement: "CREATE TABLE book(id int, \"creatorId\" int)",@@ -21,6 +24,17 @@ func TestNamingColumnConvention(t *testing.T) {},},},+ {+ Statement: fmt.Sprintf("CREATE TABLE book(id int, %s int)", invalidColumnName),+ Want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: advisor.NamingColumnConventionMismatch,+ Title: "naming.column",+ Content: fmt.Sprintf("\"book\".\"%s\" mismatches column naming convention, its length should be within 32 characters", invalidColumnName),+ },+ },+ },{// PostgreSQL uses lowercase by default unless using double quotes.Statement: "CREATE TABLE book(id int, creator_Id int)",@@ -94,7 +108,8 @@ func TestNamingColumnConvention(t *testing.T) {},}payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^[a-z]+(_[a-z]+)*$",+ Format: "^[a-z]+(_[a-z]+)*$",+ MaxLength: 32,})require.NoError(t, err)advisor.RunSQLReviewRuleTests(t, tests, &NamingColumnConventionAdvisor{}, &advisor.SQLReviewRule{diff --git a/plugin/advisor/pg/advisor_naming_index_convention_test.go b/plugin/advisor/pg/advisor_naming_index_convention_test.goindex ab7224333d..7130102a85 100644--- a/plugin/advisor/pg/advisor_naming_index_convention_test.go+++ b/plugin/advisor/pg/advisor_naming_index_convention_test.go@@ -32,7 +32,7 @@ func TestNamingIndexConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingIndexConventionMismatch,Title: "naming.index.idx",- Content: "Index in table \"tech_book\" mismatches the naming convention, expect \"^idx_tech_book_id_name$\" but found \"tech_book_id_name\"",+ Content: "Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"tech_book_id_name\"",},},},@@ -43,7 +43,7 @@ func TestNamingIndexConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingIndexConventionMismatch,Title: "naming.index.idx",- Content: fmt.Sprintf("Index in table \"tech_book\" mismatches the naming convention, expect \"^idx_tech_book_id_name$\" but found \"%s\"", invalidIndexName),+ Content: fmt.Sprintf("Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"%s\"", invalidIndexName),},{Status: advisor.Error,@@ -78,14 +78,14 @@ func TestNamingIndexConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingIndexConventionMismatch,Title: "naming.index.idx",- Content: "Index in table \"tech_book\" mismatches the naming convention, expect \"^idx_tech_book_id_name$\" but found \"idx_tech_book\"",+ Content: "Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"idx_tech_book\"",},},},}payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^idx_{{table}}_{{column_list}}$",+ Format: "^$|^idx_{{table}}_{{column_list}}$",MaxLength: 32,})require.NoError(t, err)diff --git a/plugin/advisor/pg/advisor_naming_primary_key_convention_test.go b/plugin/advisor/pg/advisor_naming_primary_key_convention_test.goindex 2d08a71887..4c9918b496 100644--- a/plugin/advisor/pg/advisor_naming_primary_key_convention_test.go+++ b/plugin/advisor/pg/advisor_naming_primary_key_convention_test.go@@ -12,6 +12,7 @@ import (func TestNamingPKConvention(t *testing.T) {maxLength := 32+ invalidPKName := advisor.RandomString(33)tests := []advisor.TestCase{{@@ -32,7 +33,24 @@ func TestNamingPKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingPKConventionMismatch,Title: "naming.index.pk",- Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^pk_tech_book_id_name$\" but found \"tech_book_id_name\"",+ Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"tech_book_id_name\"",+ },+ },+ },+ {+ Statement: fmt.Sprintf("ALTER TABLE tech_book ADD CONSTRAINT %s PRIMARY KEY (id, name)", invalidPKName),+ Want: []advisor.Advice{+ {+ Status: advisor.Error,+ Code: advisor.NamingPKConventionMismatch,+ Title: "naming.index.pk",+ Content: fmt.Sprintf("Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"%s\"", invalidPKName),+ },+ {+ Status: advisor.Error,+ Code: advisor.NamingPKConventionMismatch,+ Title: "naming.index.pk",+ Content: fmt.Sprintf(`Primary key "%s" in table "tech_book" mismatches the naming convention, its length should be within %d characters`, invalidPKName, maxLength),},},},@@ -54,7 +72,7 @@ func TestNamingPKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingPKConventionMismatch,Title: "naming.index.pk",- Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^pk_tech_book_name$\" but found \"tech_book_name\"",+ Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_name$\" but found \"tech_book_name\"",},},},@@ -62,10 +80,10 @@ func TestNamingPKConvention(t *testing.T) {Statement: "CREATE TABLE tech_book(id INT, name VARCHAR(20), PRIMARY KEY (name))",Want: []advisor.Advice{{- Status: advisor.Error,- Code: advisor.NamingPKConventionMismatch,- Title: "naming.index.pk",- Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^pk_tech_book_name$\" but found \"\"",+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",},},},@@ -73,21 +91,10 @@ func TestNamingPKConvention(t *testing.T) {Statement: "CREATE TABLE tech_book(id INT, name VARCHAR(20) PRIMARY KEY)",Want: []advisor.Advice{{- Status: advisor.Error,- Code: advisor.NamingPKConventionMismatch,- Title: "naming.index.pk",- Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^pk_tech_book_name$\" but found \"\"",- },- },- },- {- Statement: "CREATE TABLE tech_book(id INT, name VARCHAR(20), PRIMARY KEY (name))",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingPKConventionMismatch,- Title: "naming.index.pk",- Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^pk_tech_book_name$\" but found \"\"",+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",},},},@@ -116,7 +123,7 @@ func TestNamingPKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingPKConventionMismatch,Title: "naming.index.pk",- Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^pk_tech_book_id_name$\" but found \"pk_tech_book\"",+ Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"pk_tech_book\"",},},},@@ -145,7 +152,7 @@ func TestNamingPKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingPKConventionMismatch,Title: "naming.index.pk",- Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^pk_tech_book_id_name$\" but found \"pk_tech_book\"",+ Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"pk_tech_book\"",},},},@@ -174,14 +181,14 @@ func TestNamingPKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingPKConventionMismatch,Title: "naming.index.pk",- Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^pk_tech_book_id_name$\" but found \"pk_tech_book\"",+ Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"pk_tech_book\"",},},},}payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^pk_{{table}}_{{column_list}}$",+ Format: "^$|^pk_{{table}}_{{column_list}}$",MaxLength: maxLength,})require.NoError(t, err)diff --git a/plugin/advisor/pg/advisor_naming_table.go b/plugin/advisor/pg/advisor_naming_table.goindex f726f67f6a..318df22d2c 100644--- a/plugin/advisor/pg/advisor_naming_table.go+++ b/plugin/advisor/pg/advisor_naming_table.go@@ -33,16 +33,16 @@ func (*NamingTableConventionAdvisor) Check(ctx advisor.Context, statement stringif err != nil {return nil, err}- // Naming length limit is not enabled for PG, the PG parser will auto slice the name to make sure its length <= 63- // Reference: https://stackoverflow.com/questions/27865770/how-long-can-postgresql-table-names-be- format, _, err := advisor.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)++ format, maxLength, err := advisor.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)if err != nil {return nil, err}checker := &namingTableConventionChecker{- level: level,- title: string(ctx.Rule.Type),- format: format,+ level: level,+ title: string(ctx.Rule.Type),+ format: format,+ maxLength: maxLength,}for _, stmt := range stmts {@@ -65,6 +65,7 @@ type namingTableConventionChecker struct {level advisor.Statustitle stringformat *regexp.Regexp+ maxLength int}// Visit implements the ast.Visitor interface.@@ -89,6 +90,14 @@ func (checker *namingTableConventionChecker) Visit(node ast.Node) ast.Visitor {Content: fmt.Sprintf(`"%s" mismatches table naming convention, naming format should be %q`, tableName, checker.format),})}+ if checker.maxLength > 0 && len(tableName) > checker.maxLength {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.NamingTableConventionMismatch,+ Title: checker.title,+ Content: fmt.Sprintf("\"%s\" mismatches table naming convention, its length should be within %d characters", tableName, checker.maxLength),+ })+ }}return checkerdiff --git a/plugin/advisor/pg/advisor_naming_table_test.go b/plugin/advisor/pg/advisor_naming_table_test.goindex 15dbcf289d..ccd5f20d79 100644--- a/plugin/advisor/pg/advisor_naming_table_test.go+++ b/plugin/advisor/pg/advisor_naming_table_test.go@@ -2,6 +2,7 @@ package pgimport ("encoding/json"+ "fmt""testing""github.com/bytebase/bytebase/plugin/advisor"@@ -9,6 +10,8 @@ import ()func TestPostgreSQLNamingTableConvention(t *testing.T) {+ invalidTableName := advisor.RandomString(33)+tests := []advisor.TestCase{{Statement: "CREATE TABLE \"techBook\"(id int, name varchar(255))",@@ -21,6 +24,17 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {},},},+ {+ Statement: fmt.Sprintf("CREATE TABLE \"%s\"(id int, name varchar(255))", invalidTableName),+ Want: []advisor.Advice{+ {+ Status: advisor.Error,+ Code: advisor.NamingTableConventionMismatch,+ Title: "naming.table",+ Content: fmt.Sprintf("\"%s\" mismatches table naming convention, its length should be within 32 characters", invalidTableName),+ },+ },+ },{Statement: "CREATE TABLE _techBook(id int, name varchar(255))",Want: []advisor.Advice{@@ -97,7 +111,8 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {},}payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^[a-z]+(_[a-z]+)*$",+ Format: "^[a-z]+(_[a-z]+)*$",+ MaxLength: 32,})require.NoError(t, err)advisor.RunSQLReviewRuleTests(t, tests, &NamingTableConventionAdvisor{}, &advisor.SQLReviewRule{diff --git a/plugin/advisor/pg/advisor_naming_unique_key_convention_test.go b/plugin/advisor/pg/advisor_naming_unique_key_convention_test.goindex d880ef0e59..ff505a4e17 100644--- a/plugin/advisor/pg/advisor_naming_unique_key_convention_test.go+++ b/plugin/advisor/pg/advisor_naming_unique_key_convention_test.go@@ -33,7 +33,7 @@ func TestNamingUKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingUKConventionMismatch,Title: "naming.index.uk",- Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^uk_tech_book_id_name$\" but found \"tech_book_id_name\"",+ Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"tech_book_id_name\"",},},},@@ -44,7 +44,7 @@ func TestNamingUKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingUKConventionMismatch,Title: "naming.index.uk",- Content: fmt.Sprintf("Unique key in table \"tech_book\" mismatches the naming convention, expect \"^uk_tech_book_id_name$\" but found \"%s\"", invalidUKName),+ Content: fmt.Sprintf("Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"%s\"", invalidUKName),},{Status: advisor.Error,@@ -72,7 +72,7 @@ func TestNamingUKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingUKConventionMismatch,Title: "naming.index.uk",- Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^uk_tech_book_id_name$\" but found \"tech_book_id_name\"",+ Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"tech_book_id_name\"",},},},@@ -94,7 +94,7 @@ func TestNamingUKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingUKConventionMismatch,Title: "naming.index.uk",- Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^uk_tech_book_name$\" but found \"tech_book_name\"",+ Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_name$\" but found \"tech_book_name\"",},},},@@ -102,10 +102,10 @@ func TestNamingUKConvention(t *testing.T) {Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE (name))",Want: []advisor.Advice{{- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- Title: "naming.index.uk",- Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^uk_tech_book_name$\" but found \"\"",+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",},},},@@ -113,21 +113,10 @@ func TestNamingUKConvention(t *testing.T) {Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20) UNIQUE)",Want: []advisor.Advice{{- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- Title: "naming.index.uk",- Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^uk_tech_book_name$\" but found \"\"",- },- },- },- {- Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), UNIQUE (name))",- Want: []advisor.Advice{- {- Status: advisor.Error,- Code: advisor.NamingUKConventionMismatch,- Title: "naming.index.uk",- Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^uk_tech_book_name$\" but found \"\"",+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",},},},@@ -156,7 +145,7 @@ func TestNamingUKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingUKConventionMismatch,Title: "naming.index.uk",- Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^uk_tech_book_id_name$\" but found \"uk_tech_book\"",+ Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"uk_tech_book\"",},},},@@ -185,7 +174,7 @@ func TestNamingUKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingUKConventionMismatch,Title: "naming.index.uk",- Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^uk_tech_book_id_name$\" but found \"uk_tech_book\"",+ Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"uk_tech_book\"",},},},@@ -214,14 +203,14 @@ func TestNamingUKConvention(t *testing.T) {Status: advisor.Error,Code: advisor.NamingUKConventionMismatch,Title: "naming.index.uk",- Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^uk_tech_book_id_name$\" but found \"uk_tech_book\"",+ Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"uk_tech_book\"",},},},}payload, err := json.Marshal(advisor.NamingRulePayload{- Format: "^uk_{{table}}_{{column_list}}$",+ Format: "^$|^uk_{{table}}_{{column_list}}$",MaxLength: maxLength,})require.NoError(t, err)diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.goindex 0107ed390f..9a1fc602a0 100644--- a/plugin/advisor/sql_review.go+++ b/plugin/advisor/sql_review.go@@ -86,7 +86,10 @@ const (ReferencedColumnNameTemplateToken = "{{referenced_column}}"// defaultNameLengthLimit is the default length limit for naming rules.- defaultNameLengthLimit = 64+ // PostgreSQL has it's own naming length limit, will auto slice the name to make sure its length <= 63+ // https://www.postgresql.org/docs/current/limits.html.+ // While MySQL does not enforce the limit, thus we use PostgreSQL's 63 as the default limit.+ defaultNameLengthLimit = 63)var (