Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor(advisor/tidb): migrate naming-table/column/drop rules to omni AST (#20195)
Vincent Huang8e6f4d290870
diff --git a/backend/plugin/advisor/tidb/advisor_naming_column.go b/backend/plugin/advisor/tidb/advisor_naming_column.goindex 24b8b05e94..fcd0d672e9 100644--- a/backend/plugin/advisor/tidb/advisor_naming_column.go+++ b/backend/plugin/advisor/tidb/advisor_naming_column.go@@ -5,7 +5,7 @@ import ("fmt""regexp"- "github.com/pingcap/tidb/pkg/parser/ast"+ "github.com/bytebase/omni/tidb/ast""github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"@@ -16,7 +16,6 @@ import (var (_ advisor.Advisor = (*NamingColumnConventionAdvisor)(nil)- _ ast.Visitor = (*namingColumnConventionChecker)(nil))func init() {@@ -29,8 +28,7 @@ type NamingColumnConventionAdvisor struct {// Check checks for column naming convention.func (*NamingColumnConventionAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {- root, err := getTiDBNodes(checkCtx)-+ stmts, err := getTiDBOmniNodes(checkCtx)if err != nil {return nil, err}@@ -60,8 +58,8 @@ func (*NamingColumnConventionAdvisor) Check(_ context.Context, checkCtx advisor.maxLength: maxLength,}- for _, stmtNode := range root {- (stmtNode).Accept(checker)+ for _, ostmt := range stmts {+ checker.checkStmt(ostmt)}return checker.adviceList, nil@@ -75,81 +73,102 @@ type namingColumnConventionChecker struct {maxLength int}-// Enter implements the ast.Visitor interface.-func (v *namingColumnConventionChecker) Enter(in ast.Node) (ast.Node, bool) {- type columnData struct {- name string- line int- }- var columnList []columnData+type columnDataNamingColumn struct {+ name string+ line int+}++func (c *namingColumnConventionChecker) checkStmt(ostmt OmniStmt) {+ var columnList []columnDataNamingColumnvar tableName string- switch node := in.(type) {- // CREATE TABLE+ switch n := ostmt.Node.(type) {case *ast.CreateTableStmt:- tableName = node.Table.Name.O- for _, column := range node.Cols {- columnList = append(columnList, columnData{- name: column.Name.Name.O,- line: column.OriginTextPosition(),+ if n.Table == nil {+ return+ }+ tableName = n.Table.Name+ for _, col := range n.Columns {+ if col == nil {+ continue+ }+ columnList = append(columnList, columnDataNamingColumn{+ name: col.Name,+ line: ostmt.AbsoluteLine(col.Loc.Start),})}- // ALTER TABLEcase *ast.AlterTableStmt:- tableName = node.Table.Name.O- for _, spec := range node.Specs {- switch spec.Tp {- // RENAME COLUMN- case ast.AlterTableRenameColumn:- columnList = append(columnList, columnData{- name: spec.NewColumnName.Name.O,- line: in.OriginTextPosition(),- })- // ADD COLUMNS- case ast.AlterTableAddColumns:- for _, column := range spec.NewColumns {- columnList = append(columnList, columnData{- name: column.Name.Name.O,- line: in.OriginTextPosition(),+ if n.Table == nil {+ return+ }+ tableName = n.Table.Name+ // Use the ALTER statement's line for all column references — pingcap parity.+ stmtLine := ostmt.AbsoluteLine(n.Loc.Start)+ for _, cmd := range n.Commands {+ if cmd == nil {+ continue+ }+ switch cmd.Type {+ case ast.ATRenameColumn:+ if cmd.NewName != "" {+ columnList = append(columnList, columnDataNamingColumn{+ name: cmd.NewName,+ line: stmtLine,+ })+ }+ case ast.ATAddColumn:+ // omni populates either Columns (multi-form) or Column (single).+ // Mutually exclusive in practice but not enforced by the type.+ if len(cmd.Columns) > 0 {+ for _, col := range cmd.Columns {+ if col != nil {+ columnList = append(columnList, columnDataNamingColumn{+ name: col.Name,+ line: stmtLine,+ })+ }+ }+ } else if cmd.Column != nil {+ columnList = append(columnList, columnDataNamingColumn{+ name: cmd.Column.Name,+ line: stmtLine,+ })+ }+ case ast.ATChangeColumn:+ // Only the new column name matters here.+ if cmd.Column != nil {+ columnList = append(columnList, columnDataNamingColumn{+ name: cmd.Column.Name,+ line: stmtLine,})}- // CHANGE COLUMN- case ast.AlterTableChangeColumn:- columnList = append(columnList, columnData{- name: spec.NewColumns[0].Name.Name.O,- line: in.OriginTextPosition(),- })default:- // Skip other alter table specification types+ // MODIFY COLUMN (ATModifyColumn) and other forms intentionally+ // not handled — pre-existing inheritance from the pingcap-AST+ // version of this advisor.}}default:+ return}for _, column := range columnList {- if !v.format.MatchString(column.name) {- v.adviceList = append(v.adviceList, &storepb.Advice{- Status: v.level,+ if !c.format.MatchString(column.name) {+ c.adviceList = append(c.adviceList, &storepb.Advice{+ Status: c.level,Code: code.NamingColumnConventionMismatch.Int32(),- Title: v.title,- Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, naming format should be %q", tableName, column.name, v.format),+ Title: c.title,+ Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, naming format should be %q", tableName, column.name, c.format),StartPosition: common.ConvertANTLRLineToPosition(column.line),})}- if v.maxLength > 0 && len(column.name) > v.maxLength {- v.adviceList = append(v.adviceList, &storepb.Advice{- Status: v.level,+ if c.maxLength > 0 && len(column.name) > c.maxLength {+ c.adviceList = append(c.adviceList, &storepb.Advice{+ Status: c.level,Code: code.NamingColumnConventionMismatch.Int32(),- Title: v.title,- Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, its length should be within %d characters", tableName, column.name, v.maxLength),+ Title: c.title,+ Content: fmt.Sprintf("`%s`.`%s` mismatches column naming convention, its length should be within %d characters", tableName, column.name, c.maxLength),StartPosition: common.ConvertANTLRLineToPosition(column.line),})}}-- return in, false-}--// Leave implements the ast.Visitor interface.-func (*namingColumnConventionChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true}diff --git a/backend/plugin/advisor/tidb/advisor_naming_table.go b/backend/plugin/advisor/tidb/advisor_naming_table.goindex 844232320f..2a5840bc71 100644--- a/backend/plugin/advisor/tidb/advisor_naming_table.go+++ b/backend/plugin/advisor/tidb/advisor_naming_table.go@@ -5,7 +5,7 @@ import ("fmt""regexp"- "github.com/pingcap/tidb/pkg/parser/ast"+ "github.com/bytebase/omni/tidb/ast""github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"@@ -16,7 +16,6 @@ import (var (_ advisor.Advisor = (*NamingTableConventionAdvisor)(nil)- _ ast.Visitor = (*namingTableConventionChecker)(nil))func init() {@@ -29,8 +28,7 @@ type NamingTableConventionAdvisor struct {// Check checks for table naming convention.func (*NamingTableConventionAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {- root, err := getTiDBNodes(checkCtx)-+ stmts, err := getTiDBOmniNodes(checkCtx)if err != nil {return nil, err}@@ -59,8 +57,9 @@ func (*NamingTableConventionAdvisor) Check(_ context.Context, checkCtx advisor.Cformat: format,maxLength: maxLength,}- for _, stmtNode := range root {- (stmtNode).Accept(checker)++ for _, ostmt := range stmts {+ checker.checkStmt(ostmt)}return checker.adviceList, nil@@ -74,54 +73,56 @@ type namingTableConventionChecker struct {maxLength int}-// Enter implements the ast.Visitor interface.-func (v *namingTableConventionChecker) Enter(in ast.Node) (ast.Node, bool) {+func (c *namingTableConventionChecker) checkStmt(ostmt OmniStmt) {var tableNames []string- switch node := in.(type) {- // CREATE TABLE+ var line int+ switch n := ostmt.Node.(type) {case *ast.CreateTableStmt:- // Original string- tableNames = append(tableNames, node.Table.Name.O)- // ALTER TABLE+ if n.Table == nil {+ return+ }+ tableNames = append(tableNames, n.Table.Name)+ line = ostmt.AbsoluteLine(n.Loc.Start)case *ast.AlterTableStmt:- for _, spec := range node.Specs {- // RENAME TABLE- if spec.Tp == ast.AlterTableRenameTable {- tableNames = append(tableNames, spec.NewTable.Name.O)+ for _, cmd := range n.Commands {+ if cmd == nil {+ continue+ }+ if cmd.Type == ast.ATRenameTable && cmd.NewName != "" {+ tableNames = append(tableNames, cmd.NewName)}}- // RENAME TABLE+ line = ostmt.AbsoluteLine(n.Loc.Start)case *ast.RenameTableStmt:- for _, table2Table := range node.TableToTables {- tableNames = append(tableNames, table2Table.NewTable.Name.O)+ for _, pair := range n.Pairs {+ if pair == nil || pair.New == nil {+ continue+ }+ tableNames = append(tableNames, pair.New.Name)}+ line = ostmt.AbsoluteLine(n.Loc.Start)default:+ return}for _, tableName := range tableNames {- if !v.format.MatchString(tableName) {- v.adviceList = append(v.adviceList, &storepb.Advice{- Status: v.level,+ if !c.format.MatchString(tableName) {+ c.adviceList = append(c.adviceList, &storepb.Advice{+ Status: c.level,Code: code.NamingTableConventionMismatch.Int32(),- Title: v.title,- Content: fmt.Sprintf("`%s` mismatches table naming convention, naming format should be %q", tableName, v.format),- StartPosition: common.ConvertANTLRLineToPosition(in.OriginTextPosition()),+ Title: c.title,+ Content: fmt.Sprintf("`%s` mismatches table naming convention, naming format should be %q", tableName, c.format),+ StartPosition: common.ConvertANTLRLineToPosition(line),})}- if v.maxLength > 0 && len(tableName) > v.maxLength {- v.adviceList = append(v.adviceList, &storepb.Advice{- Status: v.level,+ if c.maxLength > 0 && len(tableName) > c.maxLength {+ c.adviceList = append(c.adviceList, &storepb.Advice{+ Status: c.level,Code: code.NamingTableConventionMismatch.Int32(),- Title: v.title,- Content: fmt.Sprintf("`%s` mismatches table naming convention, its length should be within %d characters", tableName, v.maxLength),- StartPosition: common.ConvertANTLRLineToPosition(in.OriginTextPosition()),+ Title: c.title,+ Content: fmt.Sprintf("`%s` mismatches table naming convention, its length should be within %d characters", tableName, c.maxLength),+ StartPosition: common.ConvertANTLRLineToPosition(line),})}}- return in, false-}--// Leave implements the ast.Visitor interface.-func (*namingTableConventionChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true}diff --git a/backend/plugin/advisor/tidb/advisor_table_drop_naming_convention.go b/backend/plugin/advisor/tidb/advisor_table_drop_naming_convention.goindex a7ad31571f..ad604fb4ab 100644--- a/backend/plugin/advisor/tidb/advisor_table_drop_naming_convention.go+++ b/backend/plugin/advisor/tidb/advisor_table_drop_naming_convention.go@@ -5,19 +5,17 @@ import ("fmt""regexp"- "github.com/bytebase/bytebase/backend/plugin/advisor/code"-- "github.com/pingcap/tidb/pkg/parser/ast"+ "github.com/bytebase/omni/tidb/ast""github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"+ "github.com/bytebase/bytebase/backend/plugin/advisor/code")var (_ advisor.Advisor = (*TableDropNamingConventionAdvisor)(nil)- _ ast.Visitor = (*namingDropTableConventionChecker)(nil))func init() {@@ -30,8 +28,7 @@ type TableDropNamingConventionAdvisor struct {// Check checks for drop table naming convention.func (*TableDropNamingConventionAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {- root, err := getTiDBNodes(checkCtx)-+ stmts, err := getTiDBOmniNodes(checkCtx)if err != nil {return nil, err}@@ -55,8 +52,9 @@ func (*TableDropNamingConventionAdvisor) Check(_ context.Context, checkCtx advistitle: checkCtx.Rule.Type.String(),format: format,}- for _, stmtNode := range root {- (stmtNode).Accept(checker)++ for _, ostmt := range stmts {+ checker.checkStmt(ostmt)}return checker.adviceList, nil@@ -69,26 +67,24 @@ type namingDropTableConventionChecker struct {format *regexp.Regexp}-// Enter implements the ast.Visitor interface.-func (v *namingDropTableConventionChecker) Enter(in ast.Node) (ast.Node, bool) {- if node, ok := in.(*ast.DropTableStmt); ok {- for _, table := range node.Tables {- if !v.format.MatchString(table.Name.O) {- v.adviceList = append(v.adviceList, &storepb.Advice{- Status: v.level,- Code: code.TableDropNamingConventionMismatch.Int32(),- Title: v.title,- Content: fmt.Sprintf("`%s` mismatches drop table naming convention, naming format should be %q", table.Name.O, v.format),- StartPosition: common.ConvertANTLRLineToPosition(node.OriginTextPosition()),- })- }+func (c *namingDropTableConventionChecker) checkStmt(ostmt OmniStmt) {+ node, ok := ostmt.Node.(*ast.DropTableStmt)+ if !ok {+ return+ }+ line := ostmt.AbsoluteLine(node.Loc.Start)+ for _, table := range node.Tables {+ if table == nil {+ continue+ }+ if !c.format.MatchString(table.Name) {+ c.adviceList = append(c.adviceList, &storepb.Advice{+ Status: c.level,+ Code: code.TableDropNamingConventionMismatch.Int32(),+ Title: c.title,+ Content: fmt.Sprintf("`%s` mismatches drop table naming convention, naming format should be %q", table.Name, c.format),+ StartPosition: common.ConvertANTLRLineToPosition(line),+ })}}-- return in, false-}--// Leave implements the ast.Visitor interface.-func (*namingDropTableConventionChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true}