Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: remove the pk naming convention (#1294)
ecmadao01dd0c9d80d6
diff --git a/api/schema_system.go b/api/schema_system.goindex fdfd3bf690..4fcc839745 100644--- a/api/schema_system.go+++ b/api/schema_system.go@@ -27,8 +27,6 @@ const (SchemaRuleTableNaming SchemaReviewRuleType = "naming.table"// SchemaRuleColumnNaming enforce the column name formatSchemaRuleColumnNaming SchemaReviewRuleType = "naming.column"- // SchemaRulePKNaming enforce the primary key name formaty.- SchemaRulePKNaming SchemaReviewRuleType = "naming.index.pk"// SchemaRuleUKNaming enforce the unique key name format.SchemaRuleUKNaming SchemaReviewRuleType = "naming.index.uk"// SchemaRuleFKNaming enforce the foreign key name format.@@ -71,10 +69,6 @@ const (var (// TemplateNamingTokens is the mapping for rule type to template tokenTemplateNamingTokens = map[SchemaReviewRuleType]map[string]bool{- SchemaRulePKNaming: {- TableNameTemplateToken: true,- ColumnListTemplateToken: true,- },SchemaRuleIDXNaming: {TableNameTemplateToken: true,ColumnListTemplateToken: true,@@ -128,7 +122,7 @@ func (rule *SchemaReviewRule) Validate() error {if _, err := UnamrshalNamingRulePayloadAsRegexp(rule.Payload); err != nil {return err}- case SchemaRulePKNaming, SchemaRuleFKNaming, SchemaRuleIDXNaming, SchemaRuleUKNaming:+ case SchemaRuleFKNaming, SchemaRuleIDXNaming, SchemaRuleUKNaming:if _, _, err := UnmarshalNamingRulePayloadAsTemplate(rule.Type, rule.Payload); err != nil {return err}diff --git a/common/error.go b/common/error.goindex 63062d2216..8014668303 100644--- a/common/error.go+++ b/common/error.go@@ -59,12 +59,10 @@ const (NamingColumnConventionMismatch Code = 10202// 10203 index naming advisor error codeNamingIndexConventionMismatch Code = 10203- // 10203 unique key naming advisor error code+ // 10204 unique key naming advisor error codeNamingUKConventionMismatch Code = 10204- // 10203 primary key naming advisor error code- NamingPKConventionMismatch Code = 10205- // 10203 foreign key naming advisor error code- NamingFKConventionMismatch Code = 10206+ // 10205 foreign key naming advisor error code+ NamingFKConventionMismatch Code = 10205// 10301 column rule advisor error codeNoRequiredColumn Code = 10301diff --git a/frontend/src/components/DatabaseSchemaReview/SchemaReviewCreation.vue b/frontend/src/components/DatabaseSchemaReview/SchemaReviewCreation.vueindex b30c0b6228..adf3ca70cb 100644--- a/frontend/src/components/DatabaseSchemaReview/SchemaReviewCreation.vue+++ b/frontend/src/components/DatabaseSchemaReview/SchemaReviewCreation.vue@@ -156,7 +156,6 @@ const TEMPLATE_LIST: SchemaReviewPolicyTemplate[] = [["naming.table","naming.column",- "naming.index.pk","naming.index.uk","naming.index.fk","naming.index.idx",@@ -191,7 +190,6 @@ const TEMPLATE_LIST: SchemaReviewPolicyTemplate[] = [["naming.table","naming.column",- "naming.index.pk","naming.index.uk","naming.index.fk","naming.index.idx",diff --git a/frontend/src/types/schemaSystem.ts b/frontend/src/types/schemaSystem.tsindex 4afcaac613..76a5aa7a2f 100644--- a/frontend/src/types/schemaSystem.ts+++ b/frontend/src/types/schemaSystem.ts@@ -68,7 +68,6 @@ export type RuleType =| "table.require-pk"| "naming.table"| "naming.column"- | "naming.index.pk"| "naming.index.uk"| "naming.index.fk"| "naming.index.idx"@@ -162,31 +161,6 @@ export const RULE_TEMPLATE_PAYLOAD_MAP: Map<RuleType, RuleConfigComponent[]> =},],],- [- "naming.index.pk",- [- {- title: "pk-name-format",- description: "",- payload: {- type: "TEMPLATE",- default: "^pk_{{table}}_{{column_list}}$",- templateList: [- {- id: "table",- description:- "schema-review-policy.payload-config.template.table-name",- },- {- id: "column_list",- description:- "schema-review-policy.payload-config.template.column-list",- },- ],- },- },- ],- ],["naming.index.uk",[@@ -324,13 +298,6 @@ export const ruleTemplateList: RuleTemplate[] = [componentList: RULE_TEMPLATE_PAYLOAD_MAP.get("naming.column") ?? [],level: RuleLevel.ERROR,},- {- type: "naming.index.pk",- category: "NAMING",- engine: "COMMON",- componentList: RULE_TEMPLATE_PAYLOAD_MAP.get("naming.index.pk") ?? [],- level: RuleLevel.ERROR,- },{type: "naming.index.uk",category: "NAMING",@@ -469,7 +436,6 @@ export const convertPolicyRuleToRuleTemplate = (],};case "naming.index.idx":- case "naming.index.pk":case "naming.index.uk":case "naming.index.fk":const templateComponent = ruleTemplate.componentList[0];@@ -530,7 +496,6 @@ export const convertRuleTemplateToPolicyRule = (},};case "naming.index.idx":- case "naming.index.pk":case "naming.index.uk":case "naming.index.fk":const templatePayload = rule.componentList[0].payload as TemplatePayload;diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex 0c71ce75bf..12d9dcfb0d 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -82,9 +82,6 @@ const (// MySQLNamingUKConvention is an advisor type for MySQL unique key naming convention.MySQLNamingUKConvention Type = "bb.plugin.advisor.mysql.naming.uk"- // MySQLNamingPKConvention is an advisor type for MySQL primary key naming convention.- MySQLNamingPKConvention Type = "bb.plugin.advisor.mysql.naming.pk"-// MySQLNamingFKConvention is an advisor type for MySQL foreign key naming convention.MySQLNamingFKConvention Type = "bb.plugin.advisor.mysql.naming.fk"diff --git a/plugin/advisor/mysql/advisor_naming_primary_key_convention.go b/plugin/advisor/mysql/advisor_naming_primary_key_convention.godeleted file mode 100644index 43840d17d6..0000000000--- a/plugin/advisor/mysql/advisor_naming_primary_key_convention.go+++ /dev/null@@ -1,148 +0,0 @@-package mysql--import (- "fmt"- "strings"-- "github.com/bytebase/bytebase/api"- "github.com/bytebase/bytebase/common"- "github.com/bytebase/bytebase/plugin/advisor"- "github.com/bytebase/bytebase/plugin/db"- "github.com/pingcap/tidb/parser/ast"-)--var (- _ advisor.Advisor = (*NamingPKConventionAdvisor)(nil)-)--func init() {- advisor.Register(db.MySQL, advisor.MySQLNamingPKConvention, &NamingPKConventionAdvisor{})- advisor.Register(db.TiDB, advisor.MySQLNamingPKConvention, &NamingPKConventionAdvisor{})-}--// NamingPKConventionAdvisor is the advisor checking for primary key naming convention.-type NamingPKConventionAdvisor struct {-}--// Check checks for primary key naming convention.-func (check *NamingPKConventionAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {- root, errAdvice := parseStatement(statement, ctx.Charset, ctx.Collation)- if errAdvice != nil {- return errAdvice, nil- }-- level, err := advisor.NewStatusBySchemaReviewRuleLevel(ctx.Rule.Level)- if err != nil {- return nil, err- }-- format, templateList, err := api.UnmarshalNamingRulePayloadAsTemplate(ctx.Rule.Type, ctx.Rule.Payload)- if err != nil {- return nil, err- }- checker := &namingPKConventionChecker{- level: level,- format: format,- templateList: templateList,- }- for _, stmtNode := range root {- (stmtNode).Accept(checker)- }-- if len(checker.adviceList) == 0 {- checker.adviceList = append(checker.adviceList, advisor.Advice{- Status: advisor.Success,- Code: common.Ok,- Title: "OK",- Content: "",- })- }- return checker.adviceList, nil-}--type namingPKConventionChecker struct {- adviceList []advisor.Advice- level advisor.Status- format string- templateList []string-}--// Enter implements the ast.Visitor interface-func (checker *namingPKConventionChecker) Enter(in ast.Node) (ast.Node, bool) {- indexDataList := checker.getMetaDataList(in)-- for _, indexData := range indexDataList {- regex, err := getTemplateRegexp(checker.format, checker.templateList, indexData.metaData)- if err != nil {- checker.adviceList = append(checker.adviceList, advisor.Advice{- Status: checker.level,- Code: common.Internal,- Title: "Internal error for primary key naming convention rule",- Content: fmt.Sprintf("%q meet internal error %q", in.Text(), err.Error()),- })- continue- }- if !regex.MatchString(indexData.indexName) {- checker.adviceList = append(checker.adviceList, advisor.Advice{- Status: checker.level,- Code: common.NamingPKConventionMismatch,- Title: "Mismatch primary key naming convention",- Content: fmt.Sprintf("Primary key in table `%s` mismatches the naming convention, expect %q but found `%s`", indexData.tableName, regex, indexData.indexName),- })- }- }-- return in, false-}--// Leave implements the ast.Visitor interface-func (checker *namingPKConventionChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true-}--// getMetaDataList returns the list of primary key with meta data.-func (checker *namingPKConventionChecker) getMetaDataList(in ast.Node) []*indexMetaData {- var res []*indexMetaData-- switch node := in.(type) {- case *ast.CreateTableStmt:- for _, constraint := range node.Constraints {- if constraint.Tp == ast.ConstraintPrimaryKey {- var columnList []string- for _, key := range constraint.Keys {- columnList = append(columnList, key.Column.Name.String())- }- metaData := map[string]string{- api.ColumnListTemplateToken: strings.Join(columnList, "_"),- api.TableNameTemplateToken: node.Table.Name.String(),- }- res = append(res, &indexMetaData{- indexName: constraint.Name,- tableName: node.Table.Name.String(),- metaData: metaData,- })- }- }- case *ast.AlterTableStmt:- for _, spec := range node.Specs {- if spec.Tp == ast.AlterTableAddConstraint && spec.Constraint.Tp == ast.ConstraintPrimaryKey {- var columnList []string- for _, key := range spec.Constraint.Keys {- columnList = append(columnList, key.Column.Name.String())- }-- metaData := map[string]string{- api.ColumnListTemplateToken: strings.Join(columnList, "_"),- api.TableNameTemplateToken: node.Table.Name.String(),- }- res = append(res, &indexMetaData{- indexName: spec.Constraint.Name,- tableName: node.Table.Name.String(),- metaData: metaData,- })- }- }- }-- return res-}diff --git a/plugin/advisor/mysql/advisor_naming_primary_key_convention_test.go b/plugin/advisor/mysql/advisor_naming_primary_key_convention_test.godeleted file mode 100644index b5ca0f6fdf..0000000000--- a/plugin/advisor/mysql/advisor_naming_primary_key_convention_test.go+++ /dev/null@@ -1,70 +0,0 @@-package mysql--import (- "encoding/json"- "testing"-- "github.com/bytebase/bytebase/api"- "github.com/bytebase/bytebase/common"- "github.com/bytebase/bytebase/plugin/advisor"- "github.com/stretchr/testify/require"-)--func TestNamingPKConvention(t *testing.T) {- tests := []test{- {- statement: "ALTER TABLE tech_book ADD PRIMARY KEY pk_tech_book_id (id)",- want: []advisor.Advice{- {- Status: advisor.Success,- Code: common.Ok,- Title: "OK",- Content: "",- },- },- },- {- statement: "ALTER TABLE tech_book ADD PRIMARY KEY tech_book_id (id)",- want: []advisor.Advice{- {- Status: advisor.Error,- Code: common.NamingPKConventionMismatch,- Title: "Mismatch primary key naming convention",- Content: "Primary key in table `tech_book` mismatches the naming convention, expect \"^pk_tech_book_id$\" but found `tech_book_id`",- },- },- },- {- statement: "CREATE TABLE tech_book(id INT, name VARCHAR(20), PRIMARY KEY pk_tech_book_name (name))",- want: []advisor.Advice{- {- Status: advisor.Success,- Code: common.Ok,- Title: "OK",- Content: "",- },- },- },- {- statement: "CREATE TABLE tech_book(id INT, name VARCHAR(20), PRIMARY KEY (name))",- want: []advisor.Advice{- {- Status: advisor.Error,- Code: common.NamingPKConventionMismatch,- Title: "Mismatch primary key naming convention",- Content: "Primary key in table `tech_book` mismatches the naming convention, expect \"^pk_tech_book_name$\" but found ``",- },- },- },- }-- payload, err := json.Marshal(api.NamingRulePayload{- Format: "^pk_{{table}}_{{column_list}}$",- })- require.NoError(t, err)- runSchemaReviewRuleTests(t, tests, &NamingPKConventionAdvisor{}, &api.SchemaReviewRule{- Type: api.SchemaRulePKNaming,- Level: api.SchemaRuleLevelError,- Payload: string(payload),- }, &MockCatalogService{})-}diff --git a/server/task_check_executor_statement_advisor_composite.go b/server/task_check_executor_statement_advisor_composite.goindex 2515305334..ef1f20c1c9 100644--- a/server/task_check_executor_statement_advisor_composite.go+++ b/server/task_check_executor_statement_advisor_composite.go@@ -159,11 +159,6 @@ func getAdvisorTypeByRule(ruleType api.SchemaReviewRuleType, engine db.Type) (adcase db.MySQL, db.TiDB:return advisor.MySQLNamingUKConvention, nil}- case api.SchemaRulePKNaming:- switch engine {- case db.MySQL, db.TiDB:- return advisor.MySQLNamingPKConvention, nil- }case api.SchemaRuleFKNaming:switch engine {case db.MySQL, db.TiDB: