Skip to content

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

chore: migrate drop_naming_convention (#9060)

zll6005122736ed0ef
diff --git a/backend/plugin/advisor/mysqlwip/advisor_table_drop_naming_convention.go b/backend/plugin/advisor/mysqlwip/advisor_table_drop_naming_convention.go
index 7800daada7..4a6bebbb17 100644
--- a/backend/plugin/advisor/mysqlwip/advisor_table_drop_naming_convention.go
+++ b/backend/plugin/advisor/mysqlwip/advisor_table_drop_naming_convention.go
@@ -4,28 +4,34 @@ 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"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
var (
_ advisor.Advisor = (*TableDropNamingConventionAdvisor)(nil)
- _ ast.Visitor = (*namingDropTableConventionChecker)(nil)
)
+func init() {
+ // only for mysqlwip test.
+ advisor.Register(storepb.Engine_ENGINE_UNSPECIFIED, advisor.MySQLTableDropNamingConvention, &TableDropNamingConventionAdvisor{})
+}
+
// TableDropNamingConventionAdvisor is the advisor checking the MySQLTableDropNamingConvention rule.
type TableDropNamingConventionAdvisor struct {
}
// Check checks for drop table naming convention.
func (*TableDropNamingConventionAdvisor) 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 mysql parser result")
}
-
level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
if err != nil {
return nil, err
@@ -40,8 +46,10 @@ func (*TableDropNamingConventionAdvisor) Check(ctx advisor.Context, _ string) ([
title: string(ctx.Rule.Type),
format: format,
}
- for _, stmtNode := range root {
- (stmtNode).Accept(checker)
+
+ for _, stmt := range list {
+ checker.baseLine = stmt.BaseLine
+ antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)
}
if len(checker.adviceList) == 0 {
@@ -56,32 +64,31 @@ func (*TableDropNamingConventionAdvisor) Check(ctx advisor.Context, _ string) ([
}
type namingDropTableConventionChecker struct {
+ *mysql.BaseMySQLParserListener
+
+ baseLine int
adviceList []advisor.Advice
level advisor.Status
title string
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, advisor.Advice{
- Status: v.level,
- Code: advisor.TableDropNamingConventionMismatch,
- Title: v.title,
- Content: fmt.Sprintf("`%s` mismatches drop table naming convention, naming format should be %q", table.Name.O, v.format),
- Line: node.OriginTextPosition(),
- })
- }
- }
+// EnterDropTable is called when production dropTable is entered.
+func (checker *namingDropTableConventionChecker) EnterDropTable(ctx *mysql.DropTableContext) {
+ if ctx.TableRefList() == nil {
+ return
}
- return in, false
-}
-
-// Leave implements the ast.Visitor interface.
-func (*namingDropTableConventionChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
+ for _, tableRef := range ctx.TableRefList().AllTableRef() {
+ _, tableName := mysqlparser.NormalizeMySQLTableRef(tableRef)
+ if !checker.format.MatchString(tableName) {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.TableDropNamingConventionMismatch,
+ Title: checker.title,
+ Content: fmt.Sprintf("`%s` mismatches drop table naming convention, naming format should be %q", tableName, checker.format),
+ Line: checker.baseLine + ctx.GetStart().GetLine(),
+ })
+ }
+ }
}
diff --git a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
index adf17b3704..cfbdae18d3 100644
--- a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
+++ b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
@@ -50,6 +50,8 @@ func TestMySQLWIPRules(t *testing.T) {
advisor.SchemaRuleTableRequirePK,
// advisor.SchemaRuleTableNoFK require the table disallow the foreign key.
advisor.SchemaRuleTableNoFK,
+ // advisor.SchemaRuleTableDropNamingConvention require only the table following the naming convention can be deleted.
+ advisor.SchemaRuleTableDropNamingConvention,
// advisor.SchemaRuleDropEmptyDatabase enforce the MySQL support check if the database is empty before users drop it.
advisor.SchemaRuleDropEmptyDatabase,
diff --git a/backend/plugin/advisor/mysqlwip/test/table_drop_naming_convention.yaml b/backend/plugin/advisor/mysqlwip/test/table_drop_naming_convention.yaml
index 0394c75b06..10117b5f89 100644
--- a/backend/plugin/advisor/mysqlwip/test/table_drop_naming_convention.yaml
+++ b/backend/plugin/advisor/mysqlwip/test/table_drop_naming_convention.yaml
@@ -1,4 +1,7 @@
-- statement: DROP TABLE IF EXISTS foo_delete
+- statement: |-
+ CREATE TABLE foo(a INT);
+ RENAME TABLE foo to foo_delete;
+ DROP TABLE IF EXISTS foo_delete;
want:
- status: SUCCESS
code: 0
@@ -6,7 +9,9 @@
content: ""
line: 0
details: ""
-- statement: DROP TABLE IF EXISTS foo
+- statement: |-
+ CREATE TABLE foo(a INT);
+ DROP TABLE IF EXISTS foo;
want:
- status: WARN
code: 603
@@ -14,11 +19,15 @@
content: '`foo` mismatches drop table naming convention, naming format should be "_delete$"'
line: 2
details: ""
-- statement: DROP TABLE IF EXISTS foo_delete, bar
+- statement: |-
+ CREATE TABLE foo(a INT);
+ RENAME TABLE foo to foo_delete;
+ CREATE TABLE bar(a INT);
+ DROP TABLE IF EXISTS foo_delete, bar;
want:
- status: WARN
code: 603
title: table.drop-naming-convention
content: '`bar` mismatches drop table naming convention, naming format should be "_delete$"'
- line: 2
+ line: 4
details: ""
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 239f593677..344bac67fe 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1321,6 +1321,9 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
}
case SchemaRuleTableDropNamingConvention:
switch engine {
+ // only for mysqlwip test.
+ case storepb.Engine_ENGINE_UNSPECIFIED:
+ return MySQLTableDropNamingConvention, nil
case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:
return MySQLTableDropNamingConvention, nil
case storepb.Engine_POSTGRES: