Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: implement Oracle table naming convention (#5958)
rebelice25f9e6c3c950
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex e7e700be74..b242c6131c 100644--- a/backend/plugin/advisor/advisor.go+++ b/backend/plugin/advisor/advisor.go@@ -340,6 +340,9 @@ const (// OracleTableNoFK is an advisor type for Oracle table disallow foreign key.OracleTableNoFK Type = "bb.plugin.advisor.oracle.table.no-foreign-key"++ // OracleNamingTableConvention is an advisor type for Oracle table naming convention.+ OracleNamingTableConvention Type = "bb.plugin.advisor.oracle.naming.table")// Advice is the result of an advisor.diff --git a/backend/plugin/advisor/oracle/advisor_naming_table.go b/backend/plugin/advisor/oracle/advisor_naming_table.gonew file mode 100644index 0000000000..bf769a40a4--- /dev/null+++ b/backend/plugin/advisor/oracle/advisor_naming_table.go@@ -0,0 +1,131 @@+// Package oracle is the advisor for oracle database.+package oracle++import (+ "fmt"+ "regexp"++ "github.com/antlr/antlr4/runtime/Go/antlr/v4"+ parser "github.com/bytebase/plsql-parser"++ "github.com/bytebase/bytebase/backend/plugin/advisor"+ "github.com/bytebase/bytebase/backend/plugin/advisor/db"+)++var (+ _ advisor.Advisor = (*NamingTableAdvisor)(nil)+)++func init() {+ advisor.Register(db.Oracle, advisor.OracleNamingTableConvention, &NamingTableAdvisor{})+}++// NamingTableAdvisor is the advisor checking for table naming convention.+type NamingTableAdvisor struct {+}++// Check checks for table naming convention.+func (*NamingTableAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {+ tree, errAdvice := parseStatement(statement)+ if errAdvice != nil {+ return errAdvice, nil+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }+ format, maxLength, err := advisor.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)+ if err != nil {+ return nil, err+ }++ listener := &namingTableListener{+ level: level,+ title: string(ctx.Rule.Type),+ currentSchema: ctx.CurrentSchema,+ format: format,+ maxLength: maxLength,+ }++ antlr.ParseTreeWalkerDefault.Walk(listener, tree)++ return listener.generateAdvice()+}++// namingTableListener is the listener for table naming convention.+type namingTableListener struct {+ *parser.BasePlSqlParserListener++ level advisor.Status+ title string+ currentSchema string+ format *regexp.Regexp+ maxLength int++ adviceList []advisor.Advice+}++func (l *namingTableListener) generateAdvice() ([]advisor.Advice, error) {+ if len(l.adviceList) == 0 {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ })+ }+ return l.adviceList, nil+}++// EnterCreate_table is called when production create_table is entered.+func (l *namingTableListener) EnterCreate_table(ctx *parser.Create_tableContext) {+ tableName := normalizeIdentifier(ctx.Table_name(), l.currentSchema)+ if !l.format.MatchString(tableName) {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.NamingTableConventionMismatch,+ Title: l.title,+ Content: fmt.Sprintf(`"%s" mismatches table naming convention, naming format should be %q`, tableName, l.format),+ Line: ctx.GetStart().GetLine(),+ })+ }+ if l.maxLength > 0 && len(tableName) > l.maxLength {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.NamingTableConventionMismatch,+ Title: l.title,+ Content: fmt.Sprintf("\"%s\" mismatches table naming convention, its length should be within %d characters", tableName, l.maxLength),+ Line: ctx.GetStart().GetLine(),+ })+ }+}++// EnterAlter_table_properties is called when production alter_table_properties is entered.+func (l *namingTableListener) EnterAlter_table_properties(ctx *parser.Alter_table_propertiesContext) {+ if ctx.Tableview_name() == nil {+ return+ }+ tableName := lastIdentifier(normalizeIdentifier(ctx.Tableview_name(), l.currentSchema))+ if tableName == "" {+ return+ }+ if !l.format.MatchString(tableName) {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.NamingTableConventionMismatch,+ Title: l.title,+ Content: fmt.Sprintf(`"%s" mismatches table naming convention, naming format should be %q`, tableName, l.format),+ Line: ctx.GetStart().GetLine(),+ })+ }+ if l.maxLength > 0 && len(tableName) > l.maxLength {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.NamingTableConventionMismatch,+ Title: l.title,+ Content: fmt.Sprintf("\"%s\" mismatches table naming convention, its length should be within %d characters", tableName, l.maxLength),+ Line: ctx.GetStart().GetLine(),+ })+ }+}diff --git a/backend/plugin/advisor/oracle/oracle_rules_test.go b/backend/plugin/advisor/oracle/oracle_rules_test.goindex e853ee6092..7ec4e44af7 100644--- a/backend/plugin/advisor/oracle/oracle_rules_test.go+++ b/backend/plugin/advisor/oracle/oracle_rules_test.go@@ -12,6 +12,7 @@ func TestOracleRules(t *testing.T) {oracleRules := []advisor.SQLReviewRuleType{advisor.SchemaRuleTableRequirePK,advisor.SchemaRuleTableNoFK,+ advisor.SchemaRuleTableNaming,}for _, rule := range oracleRules {diff --git a/backend/plugin/advisor/oracle/parser.go b/backend/plugin/advisor/oracle/parser.goindex 3179ad691d..d7b6a80dd6 100644--- a/backend/plugin/advisor/oracle/parser.go+++ b/backend/plugin/advisor/oracle/parser.go@@ -99,3 +99,8 @@ func normalizeTableName(name string) string {}return strings.Join(result, ".")}++func lastIdentifier(name string) string {+ list := strings.Split(name, ".")+ return list[len(list)-1]+}diff --git a/backend/plugin/advisor/oracle/test/naming_table.yaml b/backend/plugin/advisor/oracle/test/naming_table.yamlnew file mode 100644index 0000000000..16880dba19--- /dev/null+++ b/backend/plugin/advisor/oracle/test/naming_table.yaml@@ -0,0 +1,74 @@+- 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: 1+ details: ""+- statement: CREATE TABLE "rlcmidzlevbivwvcntihenpoibtsdfasdfasdfiutqeb"(id int, name varchar(255))+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""+- statement: CREATE TABLE tech_Book(id int, name varchar(255))+ want:+ - status: WARN+ code: 301+ title: naming.table+ content: '"TECH_BOOK" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'+ line: 1+ details: ""+- 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: 1+ details: ""+- statement: CREATE TABLE book(id int, name varchar(255))+ want:+ - status: WARN+ code: 301+ title: naming.table+ content: '"BOOK" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'+ line: 1+ 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: 1+ details: ""+- statement: |-+ create table techBook(id int);+ ALTER TABLE techBook RENAME TO tech_book_tmp;+ want:+ - status: WARN+ code: 301+ title: naming.table+ content: '"TECHBOOK" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'+ line: 1+ details: ""+ - status: WARN+ code: 301+ title: naming.table+ content: '"TECH_BOOK_TMP" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'+ line: 2+ details: ""+- statement: |-+ CREATE TABLE tech_Book(id int, name varchar(255));+ ALTER TABLE tech_book RENAME TO "tech_book";+ want:+ - status: WARN+ code: 301+ title: naming.table+ content: '"TECH_BOOK" mismatches table naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'+ line: 1+ details: ""diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex 34121d9694..2c9986a45b 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -796,6 +796,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errreturn MySQLNamingTableConvention, nilcase db.Postgres:return PostgreSQLNamingTableConvention, nil+ case db.Oracle:+ return OracleNamingTableConvention, nil}case SchemaRuleIDXNaming:switch engine {