Skip to content

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

feat: add tidb naming_table advisor (#8857)

zll600543637de1c50
diff --git a/backend/plugin/advisor/mysql/advisor_naming_table.go b/backend/plugin/advisor/mysql/advisor_naming_table.go
index a0d96b1a6c..7a7e09ac9f 100644
--- a/backend/plugin/advisor/mysql/advisor_naming_table.go
+++ b/backend/plugin/advisor/mysql/advisor_naming_table.go
@@ -18,7 +18,6 @@ var (
func init() {
advisor.Register(storepb.Engine_MYSQL, advisor.MySQLNamingTableConvention, &NamingTableConventionAdvisor{})
- advisor.Register(storepb.Engine_TIDB, advisor.MySQLNamingTableConvention, &NamingTableConventionAdvisor{})
advisor.Register(storepb.Engine_MARIADB, advisor.MySQLNamingTableConvention, &NamingTableConventionAdvisor{})
advisor.Register(storepb.Engine_OCEANBASE, advisor.MySQLNamingTableConvention, &NamingTableConventionAdvisor{})
}
diff --git a/backend/plugin/advisor/tidb/advisor_naming_table.go b/backend/plugin/advisor/tidb/advisor_naming_table.go
new file mode 100644
index 0000000000..477364fc97
--- /dev/null
+++ b/backend/plugin/advisor/tidb/advisor_naming_table.go
@@ -0,0 +1,120 @@
+package tidb
+
+import (
+ "fmt"
+ "regexp"
+
+ "github.com/pingcap/tidb/parser/ast"
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+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)
+ if !ok {
+ return nil, errors.Errorf("failed to convert to StmtNode")
+ }
+
+ 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)
+ }
+
+ 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 namingTableConventionChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ format *regexp.Regexp
+ maxLength int
+}
+
+// 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)
+ }
+ }
+ // RENAME TABLE
+ case *ast.RenameTableStmt:
+ for _, table2Table := range node.TableToTables {
+ tableNames = append(tableNames, table2Table.NewTable.Name.O)
+ }
+ }
+
+ 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(),
+ })
+ }
+ }
+ 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/test/naming_table.yaml b/backend/plugin/advisor/tidb/test/naming_table.yaml
new file mode 100644
index 0000000000..0a879c1070
--- /dev/null
+++ b/backend/plugin/advisor/tidb/test/naming_table.yaml
@@ -0,0 +1,70 @@
+- 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
+ details: ""
+- statement: CREATE TABLE tech_book_copy(id int, name varchar(255))
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- 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
+ details: ""
+- 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
+ details: ""
+- statement: ALTER TABLE tech_book RENAME TO tech_book_copy
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- 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
+ details: ""
+- statement: CREATE TABLE literary_book(a int);RENAME TABLE tech_book TO TechBook, literary_book 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
+ details: ""
+ - status: WARN
+ code: 301
+ title: naming.table
+ 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
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
diff --git a/backend/plugin/advisor/tidb/tidb_rules_test.go b/backend/plugin/advisor/tidb/tidb_rules_test.go
new file mode 100644
index 0000000000..131fadbbca
--- /dev/null
+++ b/backend/plugin/advisor/tidb/tidb_rules_test.go
@@ -0,0 +1,18 @@
+package tidb
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+func TestTiDBRules(t *testing.T) {
+ tidbRules := []advisor.SQLReviewRuleType{
+ advisor.SchemaRuleTableNaming,
+ }
+
+ for _, rule := range tidbRules {
+ advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_TIDB, false /* record */)
+ }
+}