Skip to content

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

feat(advisor): migrate column_type_disallow_list and comment_convention to ANTLR (#17825)

h3n4l975ffacb150f
diff --git a/backend/plugin/advisor/pgantlr/advisor_column_type_disallow_list.go b/backend/plugin/advisor/pgantlr/advisor_column_type_disallow_list.go
new file mode 100644
index 0000000000..3b520f0a5d
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/advisor_column_type_disallow_list.go
@@ -0,0 +1,163 @@
+package pgantlr
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/antlr4-go/antlr/v4"
+
+ parser "github.com/bytebase/parser/postgresql"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/parser/pg"
+)
+
+var (
+ _ advisor.Advisor = (*ColumnTypeDisallowListAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, advisor.SchemaRuleColumnTypeDisallowList, &ColumnTypeDisallowListAdvisor{})
+}
+
+// ColumnTypeDisallowListAdvisor is the advisor checking for column type restriction.
+type ColumnTypeDisallowListAdvisor struct {
+}
+
+// Check checks for column type restriction.
+func (*ColumnTypeDisallowListAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
+ tree, err := getANTLRTree(checkCtx)
+ if err != nil {
+ return nil, err
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ payload, err := advisor.UnmarshalStringArrayTypeRulePayload(checkCtx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+
+ // Convert disallowed types to lowercase for case-insensitive comparison
+ typeRestriction := make(map[string]bool)
+ for _, tp := range payload.List {
+ typeRestriction[strings.ToLower(tp)] = true
+ }
+
+ checker := &columnTypeDisallowListChecker{
+ BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ typeRestriction: typeRestriction,
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+
+ return checker.adviceList, nil
+}
+
+type columnTypeDisallowListChecker struct {
+ *parser.BasePostgreSQLParserListener
+
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ typeRestriction map[string]bool
+}
+
+func (c *columnTypeDisallowListChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ qualifiedNames := ctx.AllQualified_name()
+ if len(qualifiedNames) == 0 {
+ return
+ }
+
+ tableName := extractTableName(qualifiedNames[0])
+ if tableName == "" {
+ return
+ }
+
+ // Check all columns for disallowed types
+ if ctx.Opttableelementlist() != nil && ctx.Opttableelementlist().Tableelementlist() != nil {
+ allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()
+ for _, elem := range allElements {
+ if elem.ColumnDef() != nil {
+ colDef := elem.ColumnDef()
+ if colDef.Colid() != nil && colDef.Typename() != nil {
+ columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
+ c.checkType(tableName, columnName, colDef.Typename(), elem.GetStart().GetLine())
+ }
+ }
+ }
+ }
+}
+
+func (c *columnTypeDisallowListChecker) EnterAltertablestmt(ctx *parser.AltertablestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ if ctx.Relation_expr() == nil || ctx.Relation_expr().Qualified_name() == nil {
+ return
+ }
+
+ tableName := extractTableName(ctx.Relation_expr().Qualified_name())
+ if tableName == "" {
+ return
+ }
+
+ // Check ALTER TABLE ADD COLUMN
+ if ctx.Alter_table_cmds() != nil {
+ allCmds := ctx.Alter_table_cmds().AllAlter_table_cmd()
+ for _, cmd := range allCmds {
+ // ADD COLUMN
+ if cmd.ADD_P() != nil && cmd.ColumnDef() != nil {
+ colDef := cmd.ColumnDef()
+ if colDef.Colid() != nil && colDef.Typename() != nil {
+ columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
+ c.checkType(tableName, columnName, colDef.Typename(), ctx.GetStart().GetLine())
+ }
+ }
+
+ // ALTER COLUMN TYPE
+ if cmd.ALTER() != nil && cmd.TYPE_P() != nil && cmd.Typename() != nil {
+ allColids := cmd.AllColid()
+ if len(allColids) > 0 {
+ columnName := pg.NormalizePostgreSQLColid(allColids[0])
+ c.checkType(tableName, columnName, cmd.Typename(), ctx.GetStart().GetLine())
+ }
+ }
+ }
+ }
+}
+
+func (c *columnTypeDisallowListChecker) checkType(tableName, columnName string, typename parser.ITypenameContext, line int) {
+ if typename == nil {
+ return
+ }
+
+ // Get the type text and normalize to lowercase for comparison
+ typeText := strings.ToLower(typename.GetText())
+
+ // Check if this type is in the disallow list
+ if c.typeRestriction[typeText] {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.DisabledColumnType.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Disallow column type %s but column %q.%q is", strings.ToUpper(typeText), tableName, columnName),
+ StartPosition: &storepb.Position{
+ Line: int32(line),
+ Column: 0,
+ },
+ })
+ }
+}
diff --git a/backend/plugin/advisor/pgantlr/advisor_comment_convention.go b/backend/plugin/advisor/pgantlr/advisor_comment_convention.go
new file mode 100644
index 0000000000..e160b845f7
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/advisor_comment_convention.go
@@ -0,0 +1,88 @@
+package pgantlr
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/antlr4-go/antlr/v4"
+
+ parser "github.com/bytebase/parser/postgresql"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+)
+
+var (
+ _ advisor.Advisor = (*CommentConventionAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, advisor.SchemaRuleCommentLength, &CommentConventionAdvisor{})
+}
+
+// CommentConventionAdvisor is the advisor checking for comment length.
+type CommentConventionAdvisor struct {
+}
+
+// Check checks for comment length.
+func (*CommentConventionAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
+ tree, err := getANTLRTree(checkCtx)
+ if err != nil {
+ return nil, err
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ payload, err := advisor.UnmarshalNumberTypeRulePayload(checkCtx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &commentConventionChecker{
+ BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ maxLength: payload.Number,
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+
+ return checker.adviceList, nil
+}
+
+type commentConventionChecker struct {
+ *parser.BasePostgreSQLParserListener
+
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ maxLength int
+}
+
+func (c *commentConventionChecker) EnterCommentstmt(ctx *parser.CommentstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Extract comment text
+ if ctx.Comment_text() != nil && ctx.Comment_text().Sconst() != nil {
+ comment := extractStringConstant(ctx.Comment_text().Sconst())
+
+ // Check length
+ if c.maxLength > 0 && len(comment) > c.maxLength {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.CommentTooLong.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("The length of comment should be within %d characters", c.maxLength),
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+}
diff --git a/backend/plugin/advisor/pgantlr/pgantlr_test.go b/backend/plugin/advisor/pgantlr/pgantlr_test.go
index b7af2f2adf..9ffd6065c8 100644
--- a/backend/plugin/advisor/pgantlr/pgantlr_test.go
+++ b/backend/plugin/advisor/pgantlr/pgantlr_test.go
@@ -18,6 +18,8 @@ func TestPostgreSQLANTLRRules(t *testing.T) {
advisor.SchemaRuleColumnMaximumCharacterLength, // Migrated from legacy
advisor.SchemaRuleColumnNotNull, // Migrated from legacy
advisor.SchemaRuleColumnRequireDefault, // Migrated from legacy
+ advisor.SchemaRuleColumnTypeDisallowList, // Migrated from legacy
+ advisor.SchemaRuleCommentLength, // Migrated from legacy
advisor.SchemaRuleRequiredColumn, // Migrated from legacy
// Add real rules here as you migrate them from legacy pg/ folder
// Example:
diff --git a/backend/plugin/advisor/pgantlr/test/column_type_disallow_list.yaml b/backend/plugin/advisor/pgantlr/test/column_type_disallow_list.yaml
new file mode 100644
index 0000000000..c24eefb0e3
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/test/column_type_disallow_list.yaml
@@ -0,0 +1,26 @@
+- statement: CREATE TABLE t(a char(5));
+ changeType: 1
+- statement: CREATE TABLE t(a int, b JSON);
+ changeType: 1
+ want:
+ - status: 2
+ code: 411
+ title: column.type-disallow-list
+ content: Disallow column type JSON but column "t"."b" is
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: |-
+ CREATE TABLE t(d char(5));
+ ALTER TABLE t add COLUMN a JSON;
+ changeType: 1
+ want:
+ - status: 2
+ code: 411
+ title: column.type-disallow-list
+ content: Disallow column type JSON but column "t"."a" is
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/pgantlr/test/system_comment_length.yaml b/backend/plugin/advisor/pgantlr/test/system_comment_length.yaml
new file mode 100644
index 0000000000..1718cdfdb7
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/test/system_comment_length.yaml
@@ -0,0 +1,26 @@
+- statement: comment on column public.name is 'is a column name'
+ changeType: 1
+- statement: comment on column public.name is 'is a to long column name'
+ changeType: 1
+ want:
+ - status: 2
+ code: 1301
+ title: system.comment.length
+ content: The length of comment should be within 20 characters
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: comment on table public is 'is a table name'
+ changeType: 1
+- statement: comment on table public is 'is a to long table name long long long long long long long'
+ changeType: 1
+ want:
+ - status: 2
+ code: 1301
+ title: system.comment.length
+ content: The length of comment should be within 20 characters
+ startposition:
+ line: 1
+ column: 0
+ endposition: null