Skip to content

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

chore: migrate drop_empty_db rule (#8925)

zll600889c51f06f10
diff --git a/backend/plugin/advisor/mysqlwip/advisor_database_drop_empty_db.go b/backend/plugin/advisor/mysqlwip/advisor_database_drop_empty_db.go
index b897757ba2..d6e043a20b 100644
--- a/backend/plugin/advisor/mysqlwip/advisor_database_drop_empty_db.go
+++ b/backend/plugin/advisor/mysqlwip/advisor_database_drop_empty_db.go
@@ -3,27 +3,33 @@ package mysqlwip
import (
"fmt"
- "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"
"github.com/bytebase/bytebase/backend/plugin/advisor/catalog"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
var (
_ advisor.Advisor = (*DatabaseAllowDropIfEmptyAdvisor)(nil)
- _ ast.Visitor = (*allowDropEmptyDBChecker)(nil)
)
+func init() {
+ advisor.Register(storepb.Engine_ENGINE_UNSPECIFIED, advisor.MySQLDatabaseAllowDropIfEmpty, &DatabaseAllowDropIfEmptyAdvisor{})
+}
+
// DatabaseAllowDropIfEmptyAdvisor is the advisor checking the MySQLDatabaseAllowDropIfEmpty rule.
type DatabaseAllowDropIfEmptyAdvisor struct {
}
// Check checks for drop table naming convention.
func (*DatabaseAllowDropIfEmptyAdvisor) 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 ParseResult")
}
level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
@@ -36,8 +42,10 @@ func (*DatabaseAllowDropIfEmptyAdvisor) Check(ctx advisor.Context, _ string) ([]
title: string(ctx.Rule.Type),
catalog: ctx.Catalog,
}
- 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 {
@@ -52,37 +60,37 @@ func (*DatabaseAllowDropIfEmptyAdvisor) Check(ctx advisor.Context, _ string) ([]
}
type allowDropEmptyDBChecker struct {
+ *mysql.BaseMySQLParserListener
+
+ baseLine int
adviceList []advisor.Advice
level advisor.Status
title string
catalog *catalog.Finder
}
-// Enter implements the ast.Visitor interface.
-func (v *allowDropEmptyDBChecker) Enter(in ast.Node) (ast.Node, bool) {
- if node, ok := in.(*ast.DropDatabaseStmt); ok {
- if v.catalog.Origin.DatabaseName() != node.Name.O {
- v.adviceList = append(v.adviceList, advisor.Advice{
- Status: v.level,
- Code: advisor.NotCurrentDatabase,
- Title: v.title,
- Content: fmt.Sprintf("Database `%s` that is trying to be deleted is not the current database `%s`", node.Name, v.catalog.Origin.DatabaseName()),
- Line: node.OriginTextPosition(),
- })
- } else if !v.catalog.Origin.HasNoTable() {
- v.adviceList = append(v.adviceList, advisor.Advice{
- Status: v.level,
- Code: advisor.DatabaseNotEmpty,
- Title: v.title,
- Content: fmt.Sprintf("Database `%s` is not allowed to drop if not empty", node.Name),
- Line: node.OriginTextPosition(),
- })
- }
+// EnterDropDatabase is called when production dropDatabase is entered.
+func (checker *allowDropEmptyDBChecker) EnterDropDatabase(ctx *mysql.DropDatabaseContext) {
+ if ctx.SchemaRef() == nil {
+ return
}
- return in, false
-}
-// Leave implements the ast.Visitor interface.
-func (*allowDropEmptyDBChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
+ dbName := mysqlparser.NormalizeMySQLSchemaRef(ctx.SchemaRef())
+ if checker.catalog.Origin.DatabaseName() != dbName {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.NotCurrentDatabase,
+ Title: checker.title,
+ Content: fmt.Sprintf("Database `%s` that is trying to be deleted is not the current database `%s`", dbName, checker.catalog.Origin.DatabaseName()),
+ Line: checker.baseLine + ctx.GetStart().GetLine(),
+ })
+ } else if !checker.catalog.Origin.HasNoTable() {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.DatabaseNotEmpty,
+ Title: checker.title,
+ Content: fmt.Sprintf("Database `%s` is not allowed to drop if not empty", dbName),
+ 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 5ca8b15376..07ec164ee8 100644
--- a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
+++ b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
@@ -15,6 +15,9 @@ func TestMySQLWIPRules(t *testing.T) {
advisor.SchemaRuleTableNaming,
// advisor.SchemaRuleColumnNaming enforce the column name format.
advisor.SchemaRuleColumnNaming,
+
+ // advisor.SchemaRuleDropEmptyDatabase enforce the MySQL support check if the database is empty before users drop it.
+ advisor.SchemaRuleDropEmptyDatabase,
}
for _, rule := range mysqlwipRules {
diff --git a/backend/plugin/advisor/mysqlwip/test/database_drop_empty_database.yaml b/backend/plugin/advisor/mysqlwip/test/database_drop_empty_database.yaml
index c5b2d15f6a..bc9f08e43c 100644
--- a/backend/plugin/advisor/mysqlwip/test/database_drop_empty_database.yaml
+++ b/backend/plugin/advisor/mysqlwip/test/database_drop_empty_database.yaml
@@ -1,8 +1,16 @@
-- statement: DROP DATABASE IF EXISTS test
+- statement: DROP DATABASE IF EXISTS test;
want:
- status: WARN
code: 701
title: database.drop-empty-database
content: Database `test` is not allowed to drop if not empty
- line: 2
+ line: 1
+ details: ""
+- statement: DROP DATABASE IF EXISTS bbdev;
+ want:
+ - status: WARN
+ code: 702
+ title: database.drop-empty-database
+ content: Database `bbdev` that is trying to be deleted is not the current database `test`
+ line: 1
details: ""
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 70563cd6dc..9a56ac1597 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1325,6 +1325,9 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
}
case SchemaRuleDropEmptyDatabase:
switch engine {
+ // only for mysqlwip test.
+ case storepb.Engine_ENGINE_UNSPECIFIED:
+ return MySQLDatabaseAllowDropIfEmpty, nil
case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:
return MySQLDatabaseAllowDropIfEmpty, nil
}
diff --git a/backend/plugin/parser/mysql/normalize.go b/backend/plugin/parser/mysql/normalize.go
index ba2d06c270..8fb05577dd 100644
--- a/backend/plugin/parser/mysql/normalize.go
+++ b/backend/plugin/parser/mysql/normalize.go
@@ -147,3 +147,11 @@ func NormalizeMySQLProcedureName(ctx parser.IProcedureNameContext) (string, stri
}
return "", ""
}
+
+// NormalizeMySQLSchemaRef noamalizes the given schemaRef.
+func NormalizeMySQLSchemaRef(ctx parser.ISchemaRefContext) string {
+ if ctx.Identifier() != nil {
+ return NormalizeMySQLIdentifier(ctx.Identifier())
+ }
+ return ""
+}