Skip to content

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

feat(schema system): support PostgreSQL naming column convention (#1605)

rebeliceac7fca69edbc
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 027671c20f..6c1cd10793 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -108,6 +108,9 @@ const (
// PostgreSQLNamingTableConvention is an advisor type for PostgreSQL table naming convention.
PostgreSQLNamingTableConvention Type = "bb.plugin.advisor.postgresql.naming.table"
+
+ // PostgreSQLNamingColumnConvention is an advisor type for PostgreSQL column naming convention.
+ PostgreSQLNamingColumnConvention Type = "bb.plugin.advisor.postgresql.naming.column"
)
// Advice is the result of an advisor.
diff --git a/plugin/advisor/pg/advisor_naming_column.go b/plugin/advisor/pg/advisor_naming_column.go
new file mode 100644
index 0000000000..7d6f43e458
--- /dev/null
+++ b/plugin/advisor/pg/advisor_naming_column.go
@@ -0,0 +1,116 @@
+package pg
+
+import (
+ "fmt"
+ "regexp"
+
+ "github.com/auxten/postgresql-parser/pkg/sql/parser"
+ "github.com/auxten/postgresql-parser/pkg/sql/sem/tree"
+ "github.com/auxten/postgresql-parser/pkg/walk"
+ "github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/db"
+)
+
+var (
+ _ advisor.Advisor = (*NamingColumnConventionAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(db.Postgres, advisor.PostgreSQLNamingColumnConvention, &NamingColumnConventionAdvisor{})
+}
+
+// NamingColumnConventionAdvisor is the advisor checking for column convention.
+type NamingColumnConventionAdvisor struct {
+}
+
+// Check checks for column naming convention.
+func (adv *NamingColumnConventionAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ stmts, errAdvice := parseStatement(statement)
+ if errAdvice != nil {
+ return errAdvice, nil
+ }
+
+ level, err := advisor.NewStatusBySchemaReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+ format, err := advisor.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+ checker := &namingColumnConventionChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ format: format,
+ }
+
+ walker := &walk.AstWalker{
+ Fn: checker.check,
+ }
+
+ for _, stmt := range stmts {
+ if _, err := walker.Walk(parser.Statements{stmt}, nil); err != nil {
+ return nil, err
+ }
+ }
+
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: common.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return checker.adviceList, nil
+}
+
+type namingColumnConventionChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ format *regexp.Regexp
+}
+
+func (checker *namingColumnConventionChecker) check(ctx interface{}, node interface{}) (stop bool) {
+ var columnList []string
+ var tableName string
+
+ switch n := node.(type) {
+ // CREATE TABLE
+ case *tree.CreateTable:
+ tableName = n.Table.Table()
+ for _, def := range n.Defs {
+ if column, ok := def.(*tree.ColumnTableDef); ok {
+ columnList = append(columnList, string(column.Name))
+ }
+ }
+ // ALTER TABLE
+ case *tree.AlterTable:
+ tableName = string(n.Table.ToTableName().TableName)
+ for _, cmd := range n.Cmds {
+ switch c := cmd.(type) {
+ // ADD COLUMN
+ case *tree.AlterTableAddColumn:
+ columnList = append(columnList, string(c.ColumnDef.Name))
+ // RENAME COLUMN
+ case *tree.AlterTableRenameColumn:
+ columnList = append(columnList, string(c.NewName))
+ }
+ }
+ }
+
+ for _, column := range columnList {
+ if !checker.format.MatchString(column) {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: common.NamingColumnConventionMismatch,
+ Title: checker.title,
+ Content: fmt.Sprintf("\"%s\".\"%s\" mismatches column naming convention, naming format should be %q", tableName, column, checker.format),
+ })
+ }
+ }
+
+ return false
+}
diff --git a/plugin/advisor/pg/advisor_naming_column_test.go b/plugin/advisor/pg/advisor_naming_column_test.go
new file mode 100644
index 0000000000..d0c38432ca
--- /dev/null
+++ b/plugin/advisor/pg/advisor_naming_column_test.go
@@ -0,0 +1,106 @@
+package pg
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/stretchr/testify/require"
+)
+
+func TestNamingColumnConvention(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: "CREATE TABLE book(id int, \"creatorId\" int)",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: common.NamingColumnConventionMismatch,
+ Title: "naming.column",
+ Content: "\"book\".\"creatorId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ },
+ },
+ },
+ {
+ // PostgreSQL uses lowercase by default unless using double quotes.
+ Statement: "CREATE TABLE book(id int, creator_Id int)",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: common.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: "CREATE TABLE book(id int, creator_id int)",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: common.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: `CREATE TABLE book(id int, creator_id int);
+ ALTER TABLE book ADD COLUMN "creatorId" int`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: common.NamingColumnConventionMismatch,
+ Title: "naming.column",
+ Content: "\"book\".\"creatorId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ },
+ },
+ },
+ {
+ Statement: `CREATE TABLE book(id int, creator_id int);
+ ALTER TABLE book ADD COLUMN "creator" int`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: common.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: `CREATE TABLE book(id int, creator_id int);
+ ALTER TABLE book RENAME COLUMN creator_id TO "creatorId"`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: common.NamingColumnConventionMismatch,
+ Title: "naming.column",
+ Content: "\"book\".\"creatorId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ },
+ },
+ },
+ {
+ Statement: `CREATE TABLE book(id int, creator_id int);
+ ALTER TABLE book RENAME COLUMN creator_id TO "creator"`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: common.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ }
+ payload, err := json.Marshal(advisor.NamingRulePayload{
+ Format: "^[a-z]+(_[a-z]+)*$",
+ })
+ require.NoError(t, err)
+ advisor.RunSchemaReviewRuleTests(t, tests, &NamingColumnConventionAdvisor{}, &advisor.SchemaReviewRule{
+ Type: advisor.SchemaRuleColumnNaming,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: string(payload),
+ }, &advisor.MockCatalogService{})
+}
diff --git a/plugin/advisor/pg/advisor_naming_table.go b/plugin/advisor/pg/advisor_naming_table.go
index 2f13781e5b..6743d063c1 100644
--- a/plugin/advisor/pg/advisor_naming_table.go
+++ b/plugin/advisor/pg/advisor_naming_table.go
@@ -46,11 +46,11 @@ func (adv *NamingTableConventionAdvisor) Check(ctx advisor.Context, statement st
}
walker := &walk.AstWalker{
- Fn: check,
+ Fn: checker.check,
}
for _, stmt := range stmts {
- if _, err := walker.Walk(parser.Statements{stmt}, checker); err != nil {
+ if _, err := walker.Walk(parser.Statements{stmt}, nil); err != nil {
return nil, err
}
}
@@ -73,12 +73,7 @@ type namingTableConventionChecker struct {
format *regexp.Regexp
}
-func check(ctx interface{}, node interface{}) (stop bool) {
- checker, ok := ctx.(*namingTableConventionChecker)
- if !ok {
- return true
- }
-
+func (checker *namingTableConventionChecker) check(ctx interface{}, node interface{}) (stop bool) {
var tableNames []string
switch n := node.(type) {
diff --git a/plugin/advisor/schema_review.go b/plugin/advisor/schema_review.go
index 1b845635c8..a77aac4dcd 100644
--- a/plugin/advisor/schema_review.go
+++ b/plugin/advisor/schema_review.go
@@ -305,6 +305,8 @@ func getAdvisorTypeByRule(ruleType SchemaReviewRuleType, engine db.Type) (Type,
switch engine {
case db.MySQL, db.TiDB:
return MySQLNamingColumnConvention, nil
+ case db.Postgres:
+ return PostgreSQLNamingColumnConvention, nil
}
case SchemaRuleRequiredColumn:
switch engine {