Skip to content

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

chore: migrate naming_table rule (#8897)

zll600cbd43d032adf
diff --git a/backend/plugin/advisor/mysqlwip/advisor_naming_table.go b/backend/plugin/advisor/mysqlwip/advisor_naming_table.go
index 619352f000..8e7f96996c 100644
--- a/backend/plugin/advisor/mysqlwip/advisor_naming_table.go
+++ b/backend/plugin/advisor/mysqlwip/advisor_naming_table.go
@@ -4,51 +4,63 @@ import (
"fmt"
"regexp"
- "github.com/pingcap/tidb/parser/ast"
+ "github.com/antlr4-go/antlr/v4"
+ mysql "github.com/bytebase/mysql-parser"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/plugin/advisor"
- storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
)
var (
_ advisor.Advisor = (*NamingTableConventionAdvisor)(nil)
- _ ast.Visitor = (*namingTableConventionChecker)(nil)
)
-func init() {
- advisor.Register(storepb.Engine_TIDB, advisor.MySQLNamingTableConvention, &NamingTableConventionAdvisor{})
-}
-
// NamingTableConventionAdvisor is the advisor checking for table naming convention.
type NamingTableConventionAdvisor struct {
}
// Check checks for table naming convention.
func (*NamingTableConventionAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {
- root, ok := ctx.AST.([]ast.StmtNode)
+ list, ok := ctx.AST.([]*mysqlparser.ParseResult)
if !ok {
- return nil, errors.Errorf("failed to convert to StmtNode")
+ return nil, errors.Errorf("failed to convert to Tree")
}
level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
if err != nil {
return nil, err
}
+
format, maxLength, err := advisor.UnmarshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)
if err != nil {
return nil, err
}
+
checker := &namingTableConventionChecker{
level: level,
title: string(ctx.Rule.Type),
format: format,
maxLength: maxLength,
}
- for _, stmtNode := range root {
- (stmtNode).Accept(checker)
+
+ for _, stmt := range list {
+ antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)
}
+ return checker.generateAdvice()
+}
+
+type namingTableConventionChecker struct {
+ *mysql.BaseMySQLParserListener
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ format *regexp.Regexp
+ maxLength int
+}
+
+func (checker *namingTableConventionChecker) generateAdvice() ([]advisor.Advice, error) {
if len(checker.adviceList) == 0 {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: advisor.Success,
@@ -60,61 +72,67 @@ func (*NamingTableConventionAdvisor) Check(ctx advisor.Context, _ string) ([]adv
return checker.adviceList, nil
}
-type namingTableConventionChecker struct {
- adviceList []advisor.Advice
- level advisor.Status
- title string
- format *regexp.Regexp
- maxLength int
+// EnterCreateTable is called when production createTable is entered.
+func (checker *namingTableConventionChecker) EnterCreateTable(ctx *mysql.CreateTableContext) {
+ if ctx.TableName() == nil {
+ return
+ }
+
+ _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
+ checker.handleTableName(tableName, ctx.GetStart().GetLine())
}
-// Enter implements the ast.Visitor interface.
-func (v *namingTableConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
- var tableNames []string
- switch node := in.(type) {
- // CREATE TABLE
- case *ast.CreateTableStmt:
- // Original string
- tableNames = append(tableNames, node.Table.Name.O)
- // ALTER TABLE
- case *ast.AlterTableStmt:
- for _, spec := range node.Specs {
- // RENAME TABLE
- if spec.Tp == ast.AlterTableRenameTable {
- tableNames = append(tableNames, spec.NewTable.Name.O)
- }
+// EnterAlterTable is called when production alterTable is entered.
+func (checker *namingTableConventionChecker) EnterAlterTable(ctx *mysql.AlterTableContext) {
+ if ctx.AlterTableActions() == nil {
+ return
+ }
+ if ctx.AlterTableActions().AlterCommandList() == nil {
+ return
+ }
+ if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+ return
+ }
+ for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
+ if item.RENAME_SYMBOL() == nil {
+ continue
}
- // RENAME TABLE
- case *ast.RenameTableStmt:
- for _, table2Table := range node.TableToTables {
- tableNames = append(tableNames, table2Table.NewTable.Name.O)
+ if item.TableName() == nil {
+ continue
}
+ _, tableName := mysqlparser.NormalizeMySQLTableName(item.TableName())
+ checker.handleTableName(tableName, ctx.GetStart().GetLine())
}
+}
- for _, tableName := range tableNames {
- if !v.format.MatchString(tableName) {
- v.adviceList = append(v.adviceList, advisor.Advice{
- Status: v.level,
- Code: advisor.NamingTableConventionMismatch,
- Title: v.title,
- Content: fmt.Sprintf("`%s` mismatches table naming convention, naming format should be %q", tableName, v.format),
- Line: in.OriginTextPosition(),
- })
- }
- 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 be within %d characters", tableName, v.maxLength),
- Line: in.OriginTextPosition(),
- })
+// EnterRenameTableStatement is called when production renameTableStatement is entered.
+func (checker *namingTableConventionChecker) EnterRenameTableStatement(ctx *mysql.RenameTableStatementContext) {
+ for _, pair := range ctx.AllRenamePair() {
+ if pair.TableName() == nil {
+ continue
}
+ _, tableName := mysqlparser.NormalizeMySQLTableName(pair.TableName())
+ checker.handleTableName(tableName, ctx.GetStart().GetLine())
}
- return in, false
}
-// Leave implements the ast.Visitor interface.
-func (*namingTableConventionChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
+func (checker *namingTableConventionChecker) handleTableName(tableName string, lineNumber int) {
+ if !checker.format.MatchString(tableName) {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.NamingTableConventionMismatch,
+ Title: checker.title,
+ Content: fmt.Sprintf("`%s` mismatches table naming convention, naming format should be %q", tableName, checker.format),
+ Line: lineNumber,
+ })
+ }
+ 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),
+ Line: lineNumber,
+ })
+ }
}
diff --git a/backend/plugin/advisor/mysqlwip/test/naming_table.yaml b/backend/plugin/advisor/mysqlwip/test/naming_table.yaml
index c6990b158a..2336e69666 100644
--- a/backend/plugin/advisor/mysqlwip/test/naming_table.yaml
+++ b/backend/plugin/advisor/mysqlwip/test/naming_table.yaml
@@ -1,12 +1,12 @@
-- statement: CREATE TABLE techBook(id int, name varchar(255))
+- statement: CREATE TABLE techBook(id int, name varchar(255));
want:
- status: WARN
code: 301
title: naming.table
content: '`techBook` mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'
- line: 2
+ line: 1
details: ""
-- statement: CREATE TABLE tech_book_copy(id int, name varchar(255))
+- statement: CREATE TABLE tech_book_copy(id int, name varchar(255));
want:
- status: SUCCESS
code: 0
@@ -14,23 +14,23 @@
content: ""
line: 0
details: ""
-- statement: CREATE TABLE cjnubexocfhqoogdmihudyahmmghviqkzvpixnwvxtxumvuannpwdcbtsgwrvzpde(id int, name varchar(255))
+- statement: CREATE TABLE cjnubexocfhqoogdmihudyahmmghviqkzvpixnwvxtxumvuannpwdcbtsgwrvzpde(id int, name varchar(255));
want:
- status: WARN
code: 301
title: naming.table
- content: '`cjnubexocfhqoogdmihudyahmmghviqkzvpixnwvxtxumvuannpwdcbtsgwrvzpde` mismatches table naming convention, its length should be within 64 characters'
- line: 2
+ content: "`cjnubexocfhqoogdmihudyahmmghviqkzvpixnwvxtxumvuannpwdcbtsgwrvzpde` mismatches table naming convention, its length should be within 64 characters"
+ line: 1
details: ""
-- statement: ALTER TABLE tech_book RENAME TO TechBook
+- statement: ALTER TABLE tech_book RENAME TO TechBook;
want:
- status: WARN
code: 301
title: naming.table
content: '`TechBook` mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'
- line: 2
+ line: 1
details: ""
-- statement: ALTER TABLE tech_book RENAME TO tech_book_copy
+- statement: ALTER TABLE tech_book RENAME TO tech_book_copy;
want:
- status: SUCCESS
code: 0
@@ -38,15 +38,17 @@
content: ""
line: 0
details: ""
-- statement: RENAME TABLE tech_book TO tech_book_copy, tech_book_copy TO LiteraryBook
+- statement: RENAME TABLE tech_book TO tech_book_copy, tech_book_copy TO LiteraryBook;
want:
- status: WARN
code: 301
title: naming.table
content: '`LiteraryBook` mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'
- line: 2
+ line: 1
details: ""
-- statement: CREATE TABLE literary_book(a int);RENAME TABLE tech_book TO TechBook, literary_book TO LiteraryBook
+- statement: |
+ CREATE TABLE literary_book(a int);
+ RENAME TABLE tech_book TO TechBook, literary_book TO LiteraryBook;
want:
- status: WARN
code: 301
@@ -60,7 +62,9 @@
content: '`TechBook` mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'
line: 2
details: ""
-- statement: CREATE TABLE literary_book(a int);RENAME TABLE tech_book TO tech_book_copy, literary_book TO literary_book_copy
+- statement: |
+ CREATE TABLE literary_book(a int);
+ RENAME TABLE tech_book TO tech_book_copy, literary_book TO literary_book_copy;
want:
- status: SUCCESS
code: 0