Skip to content

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

feat: migrate some postgresql advisors (#17817)

h3n4lb55b62b3e2b6
diff --git a/backend/plugin/advisor/pgantlr/advisor_column_require_default.go b/backend/plugin/advisor/pgantlr/advisor_column_require_default.go
new file mode 100644
index 0000000000..b28e892e11
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/advisor_column_require_default.go
@@ -0,0 +1,190 @@
+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 = (*ColumnRequireDefaultAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, advisor.SchemaRuleColumnRequireDefault, &ColumnRequireDefaultAdvisor{})
+}
+
+// ColumnRequireDefaultAdvisor is the advisor checking for column default requirement.
+type ColumnRequireDefaultAdvisor struct {
+}
+
+// Check checks for column default requirement.
+func (*ColumnRequireDefaultAdvisor) 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
+ }
+
+ checker := &columnRequireDefaultChecker{
+ BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+
+ return checker.adviceList, nil
+}
+
+type columnRequireDefaultChecker struct {
+ *parser.BasePostgreSQLParserListener
+
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+}
+
+func (c *columnRequireDefaultChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ qualifiedNames := ctx.AllQualified_name()
+ if len(qualifiedNames) == 0 {
+ return
+ }
+
+ tableName := c.extractTableName(qualifiedNames[0])
+ if tableName == "" {
+ return
+ }
+
+ // Check all columns for DEFAULT clause
+ 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 {
+ columnName := colDef.Colid().GetText()
+ // Check if column has DEFAULT
+ if !c.hasDefault(colDef) {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.NoDefault.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Column %q.%q in schema %q doesn't have DEFAULT", tableName, columnName, "public"),
+ StartPosition: &storepb.Position{
+ Line: int32(elem.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+ }
+ }
+ }
+}
+
+func (c *columnRequireDefaultChecker) EnterAltertablestmt(ctx *parser.AltertablestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ if ctx.Relation_expr() == nil || ctx.Relation_expr().Qualified_name() == nil {
+ return
+ }
+
+ tableName := c.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 {
+ columnName := colDef.Colid().GetText()
+ // Check if column has DEFAULT
+ if !c.hasDefault(colDef) {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.NoDefault.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Column %q.%q in schema %q doesn't have DEFAULT", tableName, columnName, "public"),
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+ }
+ }
+ }
+}
+
+func (*columnRequireDefaultChecker) extractTableName(qualifiedNameCtx parser.IQualified_nameContext) string {
+ if qualifiedNameCtx == nil {
+ return ""
+ }
+
+ text := qualifiedNameCtx.GetText()
+ parts := splitIdentifier(text)
+ if len(parts) == 0 {
+ return ""
+ }
+
+ // Return the last part (table name)
+ return parts[len(parts)-1]
+}
+
+// hasDefault checks if a column definition has a DEFAULT clause
+// or uses a type that implicitly includes a default (like serial, bigserial, smallserial)
+func (*columnRequireDefaultChecker) hasDefault(colDef parser.IColumnDefContext) bool {
+ // Check if the type is serial/bigserial/smallserial (which have implicit defaults)
+ if colDef.Typename() != nil && colDef.Typename().Simpletypename() != nil {
+ simpleType := colDef.Typename().Simpletypename()
+ typeText := simpleType.GetText()
+ // serial, bigserial, smallserial all have implicit DEFAULT nextval()
+ if typeText == "serial" || typeText == "bigserial" || typeText == "smallserial" {
+ return true
+ }
+ }
+
+ // Check for explicit DEFAULT constraint
+ if colDef.Colquallist() == nil {
+ return false
+ }
+
+ allConstraints := colDef.Colquallist().AllColconstraint()
+ for _, constraint := range allConstraints {
+ if constraint.Colconstraintelem() == nil {
+ continue
+ }
+
+ elem := constraint.Colconstraintelem()
+
+ // Check for DEFAULT constraint
+ if elem.DEFAULT() != nil {
+ return true
+ }
+ }
+
+ return false
+}
diff --git a/backend/plugin/advisor/pgantlr/advisor_column_required.go b/backend/plugin/advisor/pgantlr/advisor_column_required.go
new file mode 100644
index 0000000000..77baa698a3
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/advisor_column_required.go
@@ -0,0 +1,227 @@
+package pgantlr
+
+import (
+ "context"
+ "fmt"
+ "slices"
+ "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"
+)
+
+var (
+ _ advisor.Advisor = (*ColumnRequirementAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, advisor.SchemaRuleRequiredColumn, &ColumnRequirementAdvisor{})
+}
+
+type columnSet map[string]bool
+
+// ColumnRequirementAdvisor is the advisor checking for column requirement.
+type ColumnRequirementAdvisor struct {
+}
+
+// Check checks for the column requirement.
+func (*ColumnRequirementAdvisor) 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
+ }
+
+ columnList, err := advisor.UnmarshalRequiredColumnList(checkCtx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+
+ // Convert to map for O(1) lookup
+ requiredColumnsMap := make(columnSet)
+ for _, col := range columnList {
+ requiredColumnsMap[col] = true
+ }
+
+ checker := &columnRequirementChecker{
+ BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ requiredColumnsMap: requiredColumnsMap,
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+
+ return checker.adviceList, nil
+}
+
+type columnRequirementChecker struct {
+ *parser.BasePostgreSQLParserListener
+
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ requiredColumnsMap columnSet // Map of all required columns (from config)
+ requiredColumns columnSet // Temp map for checking CREATE TABLE
+}
+
+func (c *columnRequirementChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Initialize required columns for this statement (copy from config map)
+ c.requiredColumns = make(columnSet)
+ for column := range c.requiredColumnsMap {
+ c.requiredColumns[column] = true
+ }
+
+ qualifiedNames := ctx.AllQualified_name()
+ if len(qualifiedNames) == 0 {
+ return
+ }
+
+ tableName := c.extractTableName(qualifiedNames[0])
+ if tableName == "" {
+ return
+ }
+
+ // Mark columns as present
+ if ctx.Opttableelementlist() != nil && ctx.Opttableelementlist().Tableelementlist() != nil {
+ allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()
+ for _, elem := range allElements {
+ if elem.ColumnDef() != nil && elem.ColumnDef().Colid() != nil {
+ columnName := elem.ColumnDef().Colid().GetText()
+ delete(c.requiredColumns, columnName)
+ }
+ }
+ }
+
+ // Check if any required columns are missing
+ if len(c.requiredColumns) > 0 {
+ var missingColumns []string
+ for column := range c.requiredColumns {
+ missingColumns = append(missingColumns, column)
+ }
+ slices.Sort(missingColumns)
+
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.NoRequiredColumn.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Table %q requires columns: %s", tableName, strings.Join(missingColumns, ", ")),
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+}
+
+func (c *columnRequirementChecker) EnterAltertablestmt(ctx *parser.AltertablestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ if ctx.Relation_expr() == nil || ctx.Relation_expr().Qualified_name() == nil {
+ return
+ }
+
+ tableName := c.extractTableName(ctx.Relation_expr().Qualified_name())
+ if tableName == "" {
+ return
+ }
+
+ // Check ALTER TABLE commands
+ if ctx.Alter_table_cmds() != nil {
+ allCmds := ctx.Alter_table_cmds().AllAlter_table_cmd()
+ for _, cmd := range allCmds {
+ // DROP COLUMN (note: COLUMN keyword is optional in PostgreSQL)
+ if cmd.DROP() != nil {
+ allColids := cmd.AllColid()
+ if len(allColids) > 0 {
+ columnName := allColids[0].GetText()
+ // Check if this is a required column (O(1) lookup)
+ if c.requiredColumnsMap[columnName] {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.NoRequiredColumn.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Table %q requires columns: %s", tableName, columnName),
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+ }
+ }
+ }
+}
+
+func (c *columnRequirementChecker) EnterRenamestmt(ctx *parser.RenamestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Check if this is RENAME COLUMN
+ if ctx.Opt_column() == nil || ctx.Opt_column().COLUMN() == nil {
+ return
+ }
+
+ // Get table name
+ var tableName string
+ if ctx.Relation_expr() != nil && ctx.Relation_expr().Qualified_name() != nil {
+ tableName = c.extractTableName(ctx.Relation_expr().Qualified_name())
+ }
+ if tableName == "" {
+ return
+ }
+
+ // Get old and new column names
+ allNames := ctx.AllName()
+ if len(allNames) < 2 {
+ return
+ }
+
+ oldName := allNames[0].GetText()
+ newName := allNames[1].GetText()
+
+ // Check if renaming away from a required column name (O(1) lookup)
+ if c.requiredColumnsMap[oldName] && oldName != newName {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.NoRequiredColumn.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("Table %q requires columns: %s", tableName, oldName),
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+}
+
+func (*columnRequirementChecker) extractTableName(qualifiedNameCtx parser.IQualified_nameContext) string {
+ if qualifiedNameCtx == nil {
+ return ""
+ }
+
+ text := qualifiedNameCtx.GetText()
+ parts := splitIdentifier(text)
+ if len(parts) == 0 {
+ return ""
+ }
+
+ // Return the last part (table name)
+ return parts[len(parts)-1]
+}
diff --git a/backend/plugin/advisor/pgantlr/pgantlr_test.go b/backend/plugin/advisor/pgantlr/pgantlr_test.go
index 8c86570299..b7af2f2adf 100644
--- a/backend/plugin/advisor/pgantlr/pgantlr_test.go
+++ b/backend/plugin/advisor/pgantlr/pgantlr_test.go
@@ -17,6 +17,8 @@ func TestPostgreSQLANTLRRules(t *testing.T) {
advisor.SchemaRuleColumnDisallowChangeType, // Migrated from legacy
advisor.SchemaRuleColumnMaximumCharacterLength, // Migrated from legacy
advisor.SchemaRuleColumnNotNull, // Migrated from legacy
+ advisor.SchemaRuleColumnRequireDefault, // Migrated from legacy
+ advisor.SchemaRuleRequiredColumn, // Migrated from legacy
// Add real rules here as you migrate them from legacy pg/ folder
// Example:
// advisor.SchemaRuleStatementDisallowCommit,
diff --git a/backend/plugin/advisor/pgantlr/test/column_require_default.yaml b/backend/plugin/advisor/pgantlr/test/column_require_default.yaml
new file mode 100644
index 0000000000..65691f6aba
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/test/column_require_default.yaml
@@ -0,0 +1,62 @@
+- statement: |-
+ CREATE TABLE t(
+ a int not null,
+ b text,
+ primary key (a)
+ );
+ changeType: 1
+ want:
+ - status: 2
+ code: 420
+ title: column.require-default
+ content: Column "t"."a" in schema "public" doesn't have DEFAULT
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+ - status: 2
+ code: 420
+ title: column.require-default
+ content: Column "t"."b" in schema "public" doesn't have DEFAULT
+ startposition:
+ line: 3
+ column: 0
+ endposition: null
+- statement: CREATE TABLE t(a serial primary key, b int default 1)
+ changeType: 1
+- statement: |-
+ CREATE TABLE t(
+ a int,
+ b int default 1
+ )
+ changeType: 1
+ want:
+ - status: 2
+ code: 420
+ title: column.require-default
+ content: Column "t"."a" in schema "public" doesn't have DEFAULT
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+- statement: |-
+ ALTER TABLE tech_book ADD COLUMN a BLOB;
+ ALTER TABLE tech_book ADD COLUMN b timestamp;
+ changeType: 1
+ want:
+ - status: 2
+ code: 420
+ title: column.require-default
+ content: Column "tech_book"."a" in schema "public" doesn't have DEFAULT
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+ - status: 2
+ code: 420
+ title: column.require-default
+ content: Column "tech_book"."b" in schema "public" doesn't have DEFAULT
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/pgantlr/test/column_required.yaml b/backend/plugin/advisor/pgantlr/test/column_required.yaml
new file mode 100644
index 0000000000..e0ed57553b
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/test/column_required.yaml
@@ -0,0 +1,45 @@
+- statement: CREATE TABLE book(id int)
+ changeType: 1
+ want:
+ - status: 2
+ code: 401
+ title: column.required
+ content: 'Table "book" requires columns: created_ts, creator_id, updated_ts, updater_id'
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: |-
+ CREATE TABLE book(
+ id int,
+ creator_id int,
+ created_ts timestamp,
+ updater_id int,
+ updated_ts timestamp)
+ changeType: 1
+- statement: |-
+ ALTER TABLE tech_book ADD COLUMN creator_id int;
+ ALTER TABLE tech_book RENAME COLUMN creator_id TO creator;
+ changeType: 1
+ want:
+ - status: 2
+ code: 401
+ title: column.required
+ content: 'Table "tech_book" requires columns: creator_id'
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+- statement: |-
+ ALTER TABLE tech_book ADD COLUMN creator_id int;
+ ALTER TABLE tech_book DROP COLUMN creator_id;
+ changeType: 1
+ want:
+ - status: 2
+ code: 401
+ title: column.required
+ content: 'Table "tech_book" requires columns: creator_id'
+ startposition:
+ line: 2
+ column: 0
+ endposition: null