Skip to content

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

feat(advisor): implement PG drop-table-naming-convention (#4198)

rebelice675f06885d36
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index f61b1ca0db..8ec316dae7 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -286,6 +286,9 @@ const (
// PostgreSQLStatementDMLDryRun is an advisor type for PostgreSQL DML dry run.
PostgreSQLStatementDMLDryRun Type = "bb.plugin.advisor.postgresql.statement.dml-dry-run"
+
+ // PostgreSQLTableDropNamingConvention is an advisor type for PostgreSQL table drop with naming convention.
+ PostgreSQLTableDropNamingConvention Type = "bb.plugin.advisor.postgresql.table.drop-naming-convention"
)
// Advice is the result of an advisor.
diff --git a/plugin/advisor/pg/advisor_table_drop_naming_convention.go b/plugin/advisor/pg/advisor_table_drop_naming_convention.go
new file mode 100644
index 0000000000..29ae683d86
--- /dev/null
+++ b/plugin/advisor/pg/advisor_table_drop_naming_convention.go
@@ -0,0 +1,88 @@
+package pg
+
+// Framework code is generated by the generator.
+
+import (
+ "fmt"
+ "regexp"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/db"
+ "github.com/bytebase/bytebase/plugin/parser/ast"
+)
+
+var (
+ _ advisor.Advisor = (*TableDropNamingConventionAdvisor)(nil)
+ _ ast.Visitor = (*tableDropNamingConventionChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.Postgres, advisor.PostgreSQLTableDropNamingConvention, &TableDropNamingConventionAdvisor{})
+}
+
+// TableDropNamingConventionAdvisor is the advisor checking for table drop with naming convention.
+type TableDropNamingConventionAdvisor struct {
+}
+
+// Check checks for table drop with naming convention.
+func (*TableDropNamingConventionAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ stmtList, errAdvice := parseStatement(statement)
+ if errAdvice != nil {
+ return errAdvice, nil
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ format, _, err := advisor.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+ checker := &tableDropNamingConventionChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ format: format,
+ }
+
+ for _, stmt := range stmtList {
+ ast.Walk(checker, stmt)
+ }
+
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return checker.adviceList, nil
+}
+
+type tableDropNamingConventionChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ format *regexp.Regexp
+}
+
+// Visit implements ast.Visitor interface.
+func (checker *tableDropNamingConventionChecker) Visit(in ast.Node) ast.Visitor {
+ if node, ok := in.(*ast.DropTableStmt); ok {
+ for _, table := range node.TableList {
+ if !checker.format.MatchString(table.Name) {
+ 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", table.Name, checker.format),
+ Line: node.LastLine(),
+ })
+ }
+ }
+ }
+
+ return checker
+}
diff --git a/plugin/advisor/pg/pg_rules_test.go b/plugin/advisor/pg/pg_rules_test.go
index 6fa536bb29..5deda25a48 100644
--- a/plugin/advisor/pg/pg_rules_test.go
+++ b/plugin/advisor/pg/pg_rules_test.go
@@ -37,6 +37,7 @@ func TestPostgreSQLRules(t *testing.T) {
advisor.SchemaRuleStatementDMLDryRun,
advisor.SchemaRuleStatementInsertMustSpecifyColumn,
advisor.SchemaRuleStatementInsertDisallowOrderByRand,
+ advisor.SchemaRuleTableDropNamingConvention,
}
for _, rule := range pgRules {
diff --git a/plugin/advisor/pg/test/table_drop_naming_convention.yaml b/plugin/advisor/pg/test/table_drop_naming_convention.yaml
new file mode 100644
index 0000000000..7143a405ab
--- /dev/null
+++ b/plugin/advisor/pg/test/table_drop_naming_convention.yaml
@@ -0,0 +1,21 @@
+- statement: DROP TABLE IF EXISTS foo_delete
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+- statement: DROP TABLE IF EXISTS foo
+ want:
+ - status: WARN
+ code: 603
+ title: table.drop-naming-convention
+ content: '`foo` mismatches drop table naming convention, naming format should be "_delete$"'
+ line: 1
+- statement: 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: 1
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 58a76b3531..67a8ebf4c4 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -799,6 +799,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
switch engine {
case db.MySQL, db.TiDB:
return MySQLTableDropNamingConvention, nil
+ case db.Postgres:
+ return PostgreSQLTableDropNamingConvention, nil
}
case SchemaRuleTableCommentConvention:
switch engine {