Skip to content

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

chore(schema system backend): support length limit for naming rules in the advisor (#1847)

ecmadao68b8a03020ca
diff --git a/plugin/advisor/mysql/advisor_naming_column.go b/plugin/advisor/mysql/advisor_naming_column.go
index ccb27d3d10..893f833cc9 100644
--- a/plugin/advisor/mysql/advisor_naming_column.go
+++ b/plugin/advisor/mysql/advisor_naming_column.go
@@ -32,15 +32,16 @@ func (adv *NamingColumnConventionAdvisor) Check(ctx advisor.Context, statement s
if err != nil {
return nil, err
}
- 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,
- tables: make(tableState),
+ level: level,
+ title: string(ctx.Rule.Type),
+ format: format,
+ maxLength: maxLength,
+ tables: make(tableState),
}
for _, stmtNode := range root {
@@ -64,6 +65,7 @@ type namingColumnConventionChecker struct {
level advisor.Status
title string
format *regexp.Regexp
+ maxLength int
tables tableState
}
@@ -107,6 +109,14 @@ func (v *namingColumnConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, naming format should be %q", tableName, column, v.format),
})
}
+ if v.maxLength > 0 && len(column) > v.maxLength {
+ v.adviceList = append(v.adviceList, advisor.Advice{
+ Status: v.level,
+ Code: advisor.NamingColumnConventionMismatch,
+ Title: v.title,
+ Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, its length should within %d characters", tableName, column, v.maxLength),
+ })
+ }
}
return in, false
diff --git a/plugin/advisor/mysql/advisor_naming_column_test.go b/plugin/advisor/mysql/advisor_naming_column_test.go
index 20578757c8..b0fca12962 100644
--- a/plugin/advisor/mysql/advisor_naming_column_test.go
+++ b/plugin/advisor/mysql/advisor_naming_column_test.go
@@ -2,6 +2,7 @@ package mysql
import (
"encoding/json"
+ "fmt"
"testing"
"github.com/bytebase/bytebase/plugin/advisor"
@@ -9,6 +10,8 @@ import (
)
func TestNamingColumnConvention(t *testing.T) {
+ invalidColumnName := advisor.RandomString(65)
+
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 within 64 characters", invalidColumnName),
+ },
+ },
+ },
{
Statement: "CREATE TABLE book(id int, creator_id int)",
Want: []advisor.Advice{
@@ -150,7 +164,8 @@ func TestNamingColumnConvention(t *testing.T) {
}
payload, err := json.Marshal(advisor.NamingRulePayload{
- Format: "^[a-z]+(_[a-z]+)*$",
+ Format: "^[a-z]+(_[a-z]+)*$",
+ MaxLength: 64,
})
require.NoError(t, err)
advisor.RunSchemaReviewRuleTests(t, tests, &NamingColumnConventionAdvisor{}, &advisor.SchemaReviewRule{
diff --git a/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go b/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
index 658bd63608..1a8ef707b2 100644
--- a/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_foreign_key_convention.go
@@ -33,7 +33,7 @@ func (check *NamingFKConventionAdvisor) Check(ctx advisor.Context, statement str
return nil, err
}
- format, templateList, err := advisor.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)
+ format, templateList, maxLength, err := advisor.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)
if err != nil {
return nil, err
}
@@ -41,6 +41,7 @@ func (check *NamingFKConventionAdvisor) Check(ctx advisor.Context, statement str
level: level,
title: string(ctx.Rule.Type),
format: format,
+ maxLength: maxLength,
templateList: templateList,
}
for _, stmtNode := range root {
@@ -64,6 +65,7 @@ type namingFKConventionChecker struct {
level advisor.Status
title string
format string
+ maxLength int
templateList []string
}
@@ -90,6 +92,14 @@ func (checker *namingFKConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
Content: fmt.Sprintf("Foreign key in table `%s` mismatches the naming convention, expect %q but found `%s`", indexData.tableName, regex, indexData.indexName),
})
}
+ if checker.maxLength > 0 && len(indexData.indexName) > checker.maxLength {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.NamingFKConventionMismatch,
+ Title: checker.title,
+ Content: fmt.Sprintf("Foreign key `%s` in table `%s` mismatches the naming convention, its length should within %d characters", indexData.indexName, indexData.tableName, checker.maxLength),
+ })
+ }
}
return in, false
diff --git a/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go b/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go
index 0011181acb..7bb124d0fa 100644
--- a/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_foreign_key_convention_test.go
@@ -2,6 +2,7 @@ package mysql
import (
"encoding/json"
+ "fmt"
"testing"
"github.com/bytebase/bytebase/plugin/advisor"
@@ -9,6 +10,8 @@ import (
)
func TestNamingFKConvention(t *testing.T) {
+ invalidFKName := advisor.RandomString(65)
+
tests := []advisor.TestCase{
{
Statement: "ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id)",
@@ -32,6 +35,23 @@ func TestNamingFKConvention(t *testing.T) {
},
},
},
+ {
+ Statement: fmt.Sprintf("ALTER TABLE tech_book ADD CONSTRAINT %s FOREIGN KEY (author_id) REFERENCES author (id)", invalidFKName),
+ Want: []advisor.Advice{
+ {
+ 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),
+ },
+ {
+ Status: advisor.Error,
+ Code: advisor.NamingFKConventionMismatch,
+ Title: "naming.index.fk",
+ Content: fmt.Sprintf("Foreign key `%s` in table `tech_book` mismatches the naming convention, its length should within 64 characters", invalidFKName),
+ },
+ },
+ },
{
Statement: "CREATE TABLE book(id INT, author_id INT, FOREIGN KEY fk_book_author_id_author_id (author_id) REFERENCES author (id))",
Want: []advisor.Advice{
@@ -57,7 +77,8 @@ func TestNamingFKConvention(t *testing.T) {
}
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)
advisor.RunSchemaReviewRuleTests(t, tests, &NamingFKConventionAdvisor{}, &advisor.SchemaReviewRule{
diff --git a/plugin/advisor/mysql/advisor_naming_index_convention.go b/plugin/advisor/mysql/advisor_naming_index_convention.go
index f5da2ae1fb..60ad170704 100644
--- a/plugin/advisor/mysql/advisor_naming_index_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_index_convention.go
@@ -37,7 +37,7 @@ func (check *NamingIndexConventionAdvisor) Check(ctx advisor.Context, statement
return nil, err
}
- format, templateList, err := advisor.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)
+ format, templateList, maxLength, err := advisor.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)
if err != nil {
return nil, err
}
@@ -45,6 +45,7 @@ func (check *NamingIndexConventionAdvisor) Check(ctx advisor.Context, statement
level: level,
title: string(ctx.Rule.Type),
format: format,
+ maxLength: maxLength,
templateList: templateList,
catalog: ctx.Catalog,
}
@@ -69,6 +70,7 @@ type namingIndexConventionChecker struct {
level advisor.Status
title string
format string
+ maxLength int
templateList []string
catalog catalog.Catalog
}
@@ -96,6 +98,14 @@ func (checker *namingIndexConventionChecker) Enter(in ast.Node) (ast.Node, bool)
Content: fmt.Sprintf("Index in table `%s` mismatches the naming convention, expect %q but found `%s`", indexData.tableName, regex, indexData.indexName),
})
}
+ if checker.maxLength > 0 && len(indexData.indexName) > checker.maxLength {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.NamingIndexConventionMismatch,
+ Title: checker.title,
+ Content: fmt.Sprintf("Index `%s` in table `%s` mismatches the naming convention, its length should within %d characters", indexData.indexName, indexData.tableName, checker.maxLength),
+ })
+ }
}
return in, false
diff --git a/plugin/advisor/mysql/advisor_naming_index_convention_test.go b/plugin/advisor/mysql/advisor_naming_index_convention_test.go
index b3f0abca71..488a3f15ac 100644
--- a/plugin/advisor/mysql/advisor_naming_index_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_index_convention_test.go
@@ -11,6 +11,8 @@ import (
)
func TestNamingIndexConvention(t *testing.T) {
+ invalidIndexName := advisor.RandomString(65)
+
tests := []advisor.TestCase{
{
Statement: "CREATE INDEX idx_tech_book_id_name ON tech_book(id, name)",
@@ -34,6 +36,23 @@ func TestNamingIndexConvention(t *testing.T) {
},
},
},
+ {
+ Statement: fmt.Sprintf("CREATE INDEX %s ON tech_book(id, name)", invalidIndexName),
+ Want: []advisor.Advice{
+ {
+ 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),
+ },
+ {
+ Status: advisor.Error,
+ Code: advisor.NamingIndexConventionMismatch,
+ Title: "naming.index.idx",
+ Content: fmt.Sprintf("Index `%s` in table `tech_book` mismatches the naming convention, its length should within 64 characters", invalidIndexName),
+ },
+ },
+ },
{
Statement: fmt.Sprintf(
"ALTER TABLE tech_book RENAME INDEX %s TO idx_tech_book_%s",
@@ -110,7 +129,8 @@ func TestNamingIndexConvention(t *testing.T) {
}
payload, err := json.Marshal(advisor.NamingRulePayload{
- Format: "^idx_{{table}}_{{column_list}}$",
+ Format: "^idx_{{table}}_{{column_list}}$",
+ MaxLength: 64,
})
require.NoError(t, err)
advisor.RunSchemaReviewRuleTests(t, tests, &NamingIndexConventionAdvisor{}, &advisor.SchemaReviewRule{
diff --git a/plugin/advisor/mysql/advisor_naming_table.go b/plugin/advisor/mysql/advisor_naming_table.go
index b1e513957c..f64e79701c 100644
--- a/plugin/advisor/mysql/advisor_naming_table.go
+++ b/plugin/advisor/mysql/advisor_naming_table.go
@@ -32,14 +32,15 @@ func (adv *NamingTableConventionAdvisor) Check(ctx advisor.Context, statement st
if err != nil {
return nil, err
}
- 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 _, stmtNode := range root {
(stmtNode).Accept(checker)
@@ -61,6 +62,7 @@ type namingTableConventionChecker struct {
level advisor.Status
title string
format *regexp.Regexp
+ maxLength int
}
// Enter implements the ast.Visitor interface
@@ -95,6 +97,14 @@ func (v *namingTableConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
Content: fmt.Sprintf("`%s` mismatches table naming convention, naming format should be %q", tableName, v.format),
})
}
+ if v.maxLength > 0 && len(tableName) > v.maxLength {
+ v.adviceList = append(v.adviceList, advisor.Advice{
+ Status: v.level,
+ Code: advisor.NamingTableConventionMismatch,
+ Title: v.title,
+ Content: fmt.Sprintf("`%s` mismatches table naming convention, its length should within %d characters", tableName, v.maxLength),
+ })
+ }
}
return in, false
}
diff --git a/plugin/advisor/mysql/advisor_naming_table_test.go b/plugin/advisor/mysql/advisor_naming_table_test.go
index 0ade956178..d74541d247 100644
--- a/plugin/advisor/mysql/advisor_naming_table_test.go
+++ b/plugin/advisor/mysql/advisor_naming_table_test.go
@@ -2,6 +2,7 @@ package mysql
import (
"encoding/json"
+ "fmt"
"testing"
"github.com/bytebase/bytebase/plugin/advisor"
@@ -9,6 +10,8 @@ import (
)
func TestMySQLNamingTableConvention(t *testing.T) {
+ invalidTableName := advisor.RandomString(65)
+
tests := []advisor.TestCase{
{
Statement: "CREATE TABLE techBook(id int, name varchar(255))",
@@ -32,6 +35,17 @@ func TestMySQLNamingTableConvention(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 within 64 characters", invalidTableName),
+ },
+ },
+ },
{
Statement: "ALTER TABLE techBook RENAME TO TechBook",
Want: []advisor.Advice{
@@ -95,7 +109,8 @@ func TestMySQLNamingTableConvention(t *testing.T) {
},
}
payload, err := json.Marshal(advisor.NamingRulePayload{
- Format: "^[a-z]+(_[a-z]+)*$",
+ Format: "^[a-z]+(_[a-z]+)*$",
+ MaxLength: 64,
})
require.NoError(t, err)
advisor.RunSchemaReviewRuleTests(t, tests, &NamingTableConventionAdvisor{}, &advisor.SchemaReviewRule{
diff --git a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go b/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
index 524921bf94..6fd28a773f 100644
--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention.go
@@ -36,7 +36,7 @@ func (check *NamingUKConventionAdvisor) Check(ctx advisor.Context, statement str
return nil, err
}
- format, templateList, err := advisor.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)
+ format, templateList, maxLength, err := advisor.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)
if err != nil {
return nil, err
}
@@ -44,6 +44,7 @@ func (check *NamingUKConventionAdvisor) Check(ctx advisor.Context, statement str
level: level,
title: string(ctx.Rule.Type),
format: format,
+ maxLength: maxLength,
templateList: templateList,
catalog: ctx.Catalog,
}
@@ -67,6 +68,7 @@ type namingUKConventionChecker struct {
level advisor.Status
title string
format string
+ maxLength int
templateList []string
catalog catalog.Catalog
}
@@ -94,6 +96,14 @@ func (checker *namingUKConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
Content: fmt.Sprintf("Unique key in table `%s` mismatches the naming convention, expect %q but found `%s`", indexData.tableName, regex, indexData.indexName),
})
}
+ if checker.maxLength > 0 && len(indexData.indexName) > checker.maxLength {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.NamingUKConventionMismatch,
+ Title: checker.title,
+ Content: fmt.Sprintf("Unique key `%s` in table `%s` mismatches the naming convention, its length should within %d characters", indexData.indexName, indexData.tableName, checker.maxLength),
+ })
+ }
}
return in, false
diff --git a/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go b/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
index 76bd1b1665..21322aee09 100644
--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention_test.go
@@ -11,6 +11,8 @@ import (
)
func TestNamingUKConvention(t *testing.T) {
+ invalidUKName := advisor.RandomString(65)
+
tests := []advisor.TestCase{
{
Statement: "CREATE UNIQUE INDEX uk_tech_book_id_name ON tech_book(id, name)",
@@ -34,6 +36,23 @@ func TestNamingUKConvention(t *testing.T) {
},
},
},
+ {
+ Statement: fmt.Sprintf("CREATE UNIQUE INDEX %s ON tech_book(id, name)", invalidUKName),
+ Want: []advisor.Advice{
+ {
+ 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),
+ },
+ {
+ Status: advisor.Error,
+ Code: advisor.NamingUKConventionMismatch,
+ Title: "naming.index.uk",
+ Content: fmt.Sprintf("Unique key `%s` in table `tech_book` mismatches the naming convention, its length should within 64 characters", invalidUKName),
+ },
+ },
+ },
{
Statement: "ALTER TABLE tech_book ADD UNIQUE uk_tech_book_id_name (id, name)",
Want: []advisor.Advice{
@@ -132,7 +151,8 @@ func TestNamingUKConvention(t *testing.T) {
}
payload, err := json.Marshal(advisor.NamingRulePayload{
- Format: "^uk_{{table}}_{{column_list}}$",
+ Format: "^uk_{{table}}_{{column_list}}$",
+ MaxLength: 64,
})
require.NoError(t, err)
advisor.RunSchemaReviewRuleTests(t, tests, &NamingUKConventionAdvisor{}, &advisor.SchemaReviewRule{
diff --git a/plugin/advisor/pg/advisor_naming_column.go b/plugin/advisor/pg/advisor_naming_column.go
index 93ccfab50a..b898a8dc0e 100644
--- a/plugin/advisor/pg/advisor_naming_column.go
+++ b/plugin/advisor/pg/advisor_naming_column.go
@@ -31,7 +31,9 @@ func (adv *NamingColumnConventionAdvisor) Check(ctx advisor.Context, statement s
if err != nil {
return nil, err
}
- format, err := advisor.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)
+ // 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)
if err != nil {
return nil, err
}
diff --git a/plugin/advisor/pg/advisor_naming_table.go b/plugin/advisor/pg/advisor_naming_table.go
index 51fc9019d8..30c8a9c0fc 100644
--- a/plugin/advisor/pg/advisor_naming_table.go
+++ b/plugin/advisor/pg/advisor_naming_table.go
@@ -31,7 +31,9 @@ func (adv *NamingTableConventionAdvisor) Check(ctx advisor.Context, statement st
if err != nil {
return nil, err
}
- format, err := advisor.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)
+ // 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)
if err != nil {
return nil, err
}
diff --git a/plugin/advisor/schema_review.go b/plugin/advisor/schema_review.go
index 571df41460..b83302e0f6 100644
--- a/plugin/advisor/schema_review.go
+++ b/plugin/advisor/schema_review.go
@@ -74,6 +74,9 @@ const (
ReferencedTableNameTemplateToken = "{{referenced_table}}"
// ReferencedColumnNameTemplateToken is the token for referenced column name
ReferencedColumnNameTemplateToken = "{{referenced_column}}"
+
+ // defaultNameLengthLimit is the default length limit for naming rules.
+ defaultNameLengthLimit = 64
)
var (
@@ -129,11 +132,11 @@ func (rule *SchemaReviewRule) Validate() error {
// TODO(rebelice): add other schema review rule validation.
switch rule.Type {
case SchemaRuleTableNaming, SchemaRuleColumnNaming:
- if _, err := UnamrshalNamingRulePayloadAsRegexp(rule.Payload); err != nil {
+ if _, _, err := UnamrshalNamingRulePayloadAsRegexp(rule.Payload); err != nil {
return err
}
case SchemaRuleFKNaming, SchemaRuleIDXNaming, SchemaRuleUKNaming:
- if _, _, err := UnmarshalNamingRulePayloadAsTemplate(rule.Type, rule.Payload); err != nil {
+ if _, _, _, err := UnmarshalNamingRulePayloadAsTemplate(rule.Type, rule.Payload); err != nil {
return err
}
case SchemaRuleRequiredColumn:
@@ -156,25 +159,33 @@ type RequiredColumnRulePayload struct {
}
// UnamrshalNamingRulePayloadAsRegexp will unmarshal payload to NamingRulePayload and compile it as regular expression.
-func UnamrshalNamingRulePayloadAsRegexp(payload string) (*regexp.Regexp, error) {
+func UnamrshalNamingRulePayloadAsRegexp(payload string) (*regexp.Regexp, int, error) {
var nr NamingRulePayload
if err := json.Unmarshal([]byte(payload), &nr); err != nil {
- return nil, fmt.Errorf("failed to unmarshal naming rule payload %q: %q", payload, err)
+ return nil, 0, fmt.Errorf("failed to unmarshal naming rule payload %q: %q", payload, err)
}
+
format, err := regexp.Compile(nr.Format)
if err != nil {
- return nil, fmt.Errorf("failed to compile regular expression: %v, err: %v", nr.Format, err)
+ return nil, 0, fmt.Errorf("failed to compile regular expression: %v, err: %v", nr.Format, err)
+ }
+
+ // We need to be compatible with existed naming rules in the database. 0 means using the default length limit.
+ maxLength := nr.MaxLength
+ if maxLength == 0 {
+ maxLength = defaultNameLengthLimit
}
- return format, nil
+
+ return format, maxLength, nil
}
// UnmarshalNamingRulePayloadAsTemplate will unmarshal payload to NamingRulePayload and extract all the template keys.
// For example, "hard_code_{{table}}_{{column}}_end" will return
// "hard_code_{{table}}_{{column}}_end", ["{{table}}", "{{column}}"]
-func UnmarshalNamingRulePayloadAsTemplate(ruleType SchemaReviewRuleType, payload string) (string, []string, error) {
+func UnmarshalNamingRulePayloadAsTemplate(ruleType SchemaReviewRuleType, payload string) (string, []string, int, error) {
var nr NamingRulePayload
if err := json.Unmarshal([]byte(payload), &nr); err != nil {
- return "", nil, fmt.Errorf("failed to unmarshal naming rule payload %q: %q", payload, err)
+ return "", nil, 0, fmt.Errorf("failed to unmarshal naming rule payload %q: %q", payload, err)
}
template := nr.Format
@@ -182,11 +193,17 @@ func UnmarshalNamingRulePayloadAsTemplate(ruleType SchemaReviewRuleType, payload
for _, key := range keys {
if _, ok := TemplateNamingTokens[ruleType][key]; !ok {
- return "", nil, fmt.Errorf("invalid template %s for rule %s", key, ruleType)
+ return "", nil, 0, fmt.Errorf("invalid template %s for rule %s", key, ruleType)
}
}
- return template, keys, nil
+ // We need to be compatible with existed naming rules in the database. 0 means using the default length limit.
+ maxLength := nr.MaxLength
+ if maxLength == 0 {
+ maxLength = defaultNameLengthLimit
+ }
+
+ return template, keys, maxLength, nil
}
// parseTemplateTokens parses the template and returns template tokens and their delimiters.
diff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.go
index b8d866decb..dc40bf6552 100644
--- a/plugin/advisor/utils_for_tests.go
+++ b/plugin/advisor/utils_for_tests.go
@@ -3,7 +3,9 @@ package advisor
import (
"context"
"fmt"
+ "math/rand"
"testing"
+ "time"
"github.com/bytebase/bytebase/plugin/advisor/catalog"
"github.com/stretchr/testify/assert"
@@ -77,3 +79,15 @@ func RunSchemaReviewRuleTests(
assert.Equal(t, tc.Want, adviceList, tc.Statement)
}
}
+
+// RandomString returns random string with specific length.
+func RandomString(length int) string {
+ var letters = []rune("abcdefghijklmnopqrstuvwxyz")
+ rand.Seed(time.Now().UnixNano())
+
+ b := make([]rune, length)
+ for i := range b {
+ b[i] = letters[rand.Intn(len(letters))]
+ }
+ return string(b)
+}