Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor(pg): migrate SDL checkers from ANTLR to omni parser (#19757)
rebelice9be8d1af0aaf
diff --git a/backend/plugin/advisor/pg/generic_checker.go b/backend/plugin/advisor/pg/generic_checker.godeleted file mode 100644index f9d8467236..0000000000--- a/backend/plugin/advisor/pg/generic_checker.go+++ /dev/null@@ -1,121 +0,0 @@-package pg--import (- "fmt"- "reflect"- "strings"-- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/postgresql"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store"-)--// Rule defines the interface for individual SQL validation rules.-// Each rule implements specific checking logic without embedding the base listener.-type Rule interface {- // OnEnter is called when entering a parse tree node- OnEnter(ctx antlr.ParserRuleContext, nodeType string) error-- // OnExit is called when exiting a parse tree node- OnExit(ctx antlr.ParserRuleContext, nodeType string) error-- // Name returns the rule name for logging/debugging- Name() string-- // GetAdviceList returns the accumulated advice from this rule- GetAdviceList() []*storepb.Advice-}--// GenericChecker embeds the base PostgreSQL parser listener and dispatches events to registered rules.-// This design ensures only one copy of the listener type metadata in the binary.-type GenericChecker struct {- *parser.BasePostgreSQLParserListener-- rules []Rule- baseLine int-}--// NewGenericChecker creates a new instance of GenericChecker with the given rules.-func NewGenericChecker(rules []Rule) *GenericChecker {- return &GenericChecker{- BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},- rules: rules,- }-}--// SetBaseLine sets the base line number for error reporting.-func (g *GenericChecker) SetBaseLine(baseLine int) {- g.baseLine = baseLine-}--// EnterEveryRule is called when any rule is entered.-// It dispatches the event to all registered rules.-func (g *GenericChecker) EnterEveryRule(ctx antlr.ParserRuleContext) {- nodeType := g.getNodeType(ctx)- for _, rule := range g.rules {- if err := rule.OnEnter(ctx, nodeType); err != nil {- // Log error if needed- fmt.Printf("Rule %s error on enter %s: %v\n", rule.Name(), nodeType, err)- }- }-}--// ExitEveryRule is called when any rule is exited.-// It dispatches the event to all registered rules.-func (g *GenericChecker) ExitEveryRule(ctx antlr.ParserRuleContext) {- nodeType := g.getNodeType(ctx)- for _, rule := range g.rules {- if err := rule.OnExit(ctx, nodeType); err != nil {- // Log error if needed- fmt.Printf("Rule %s error on exit %s: %v\n", rule.Name(), nodeType, err)- }- }-}--// GetAdviceList collects and returns all advice from registered rules.-func (g *GenericChecker) GetAdviceList() []*storepb.Advice {- var allAdvice []*storepb.Advice- for _, rule := range g.rules {- allAdvice = append(allAdvice, rule.GetAdviceList()...)- }- return allAdvice-}--// getNodeType returns the type name of the parse tree node.-func (*GenericChecker) getNodeType(ctx antlr.ParserRuleContext) string {- t := reflect.TypeOf(ctx)- if t.Kind() == reflect.Ptr {- t = t.Elem()- }- name := t.Name()- // Remove "Context" suffix if present- name = strings.TrimSuffix(name, "Context")- return name-}--// BaseRule provides common functionality for rules.-// Other rules can embed this struct to get common behavior.-type BaseRule struct {- adviceList []*storepb.Advice- baseLine int-}--// SetBaseLine sets the base line for the rule.-func (r *BaseRule) SetBaseLine(baseLine int) {- r.baseLine = baseLine-}--// GetAdviceList returns the accumulated advice.-func (r *BaseRule) GetAdviceList() []*storepb.Advice {- return r.adviceList-}--// AddAdvice adds a new advice to the list.-// Automatically adds baseLine offset to the line number.-func (r *BaseRule) AddAdvice(advice *storepb.Advice) {- if advice.StartPosition != nil {- advice.StartPosition.Line += int32(r.baseLine)- }- r.adviceList = append(r.adviceList, advice)-}diff --git a/backend/plugin/advisor/pg/sdl_check.go b/backend/plugin/advisor/pg/sdl_check.goindex 4bc3adeeb7..b2fc1ab395 100644--- a/backend/plugin/advisor/pg/sdl_check.go+++ b/backend/plugin/advisor/pg/sdl_check.go@@ -1,12 +1,12 @@package pgimport (+ "errors""fmt"- "github.com/bytebase/bytebase/backend/plugin/advisor/code"+ "github.com/bytebase/omni/pg/ast"- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/postgresql"+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"storepb "github.com/bytebase/bytebase/backend/generated-go/store"pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"@@ -23,44 +23,64 @@ import (//// For integrity checks (foreign key validation, duplicate detection, etc.), use CheckSDLIntegrity from sdl_integrity_check.go.func CheckSDLStyle(statement string) ([]*storepb.Advice, error) {- parseResults, err := pgparser.ParsePostgreSQL(statement)- if err != nil {+ if err := validateParenBalance(statement); err != nil {return nil, err}- // Run style/convention checks on all statements- styleChecker := &sdlChecker{- BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},+ stmts, err := pgparser.ParsePg(statement)+ if err != nil {+ return nil, err}- for _, parseResult := range parseResults {- antlr.ParseTreeWalkerDefault.Walk(styleChecker, parseResult.Tree)- }+ var adviceList []*storepb.Advice- return styleChecker.adviceList, nil-}+ for _, stmt := range stmts {+ if stmt.AST == nil {+ continue+ }+ line := int32(stmt.Start.Line)++ switch n := stmt.AST.(type) {+ case *ast.CreateStmt:+ adviceList = checkCreateStmt(statement, n, line, adviceList)+ case *ast.IndexStmt:+ adviceList = checkIndexStmt(statement, n, line, adviceList)+ case *ast.ViewStmt:+ adviceList = checkViewStmt(n, line, adviceList)+ case *ast.CreateSeqStmt:+ adviceList = checkCreateSeqStmt(n, line, adviceList)+ case *ast.CreateFunctionStmt:+ adviceList = checkCreateFunctionStmt(n, line, adviceList)+ case *ast.AlterSeqStmt:+ adviceList = checkAlterSeqStmt(n, line, adviceList)+ default:+ }+ }-type sdlChecker struct {- *parser.BasePostgreSQLParserListener- adviceList []*storepb.Advice+ return adviceList, nil}-// EnterCreatestmt handles CREATE TABLE statements.-func (c *sdlChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {- if !isTopLevel(ctx.GetParent()) {- return+func nodeLineOrDefault(statement string, locStart int, defaultLine int32) int32 {+ if locStart >= 0 {+ pos := pgparser.ByteOffsetToRunePosition(statement, locStart)+ if pos.Line > 0 {+ return pos.Line+ }}+ return defaultLine+}- var tableName, schemaName string- allQualifiedNames := ctx.AllQualified_name()- if len(allQualifiedNames) > 0 {- schemaName = extractSchemaName(allQualifiedNames[0])- tableName = extractTableName(allQualifiedNames[0])+func checkCreateStmt(statement string, n *ast.CreateStmt, stmtLine int32, adviceList []*storepb.Advice) []*storepb.Advice {+ tableName := ""+ schemaName := ""+ if n.Relation != nil {+ tableName = n.Relation.Relname+ schemaName = n.Relation.Schemaname}- // Check 3: Require explicit schema name+ // Check: Require explicit schema nameif schemaName == "" {- c.adviceList = append(c.adviceList, &storepb.Advice{+ adviceList = append(adviceList, &storepb.Advice{Status: storepb.Advice_ERROR,Code: code.SDLRequireSchemaName.Int32(),Title: "sdl.require-schema-name",@@ -74,270 +94,67 @@ func (c *sdlChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {tableName, tableName, tableName,),StartPosition: &storepb.Position{- Line: int32(ctx.GetStart().GetLine()),+ Line: stmtLine,Column: 0,},})}- // Check all column definitions and table constraints- if ctx.Opttableelementlist() != nil && ctx.Opttableelementlist().Tableelementlist() != nil {- allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()- for _, elem := range allElements {- // Check 1: Disallow column-level constraints- if elem.ColumnDef() != nil {- c.checkColumnConstraints(elem.ColumnDef(), tableName)- }- // Check 2: Require constraint names for table constraints- if elem.Tableconstraint() != nil {- c.checkTableConstraintName(elem.Tableconstraint(), tableName)- // Check 3: Check foreign key reference tables have schema names- c.checkForeignKeyReferenceSchema(elem.Tableconstraint(), tableName)- }- }- }-}--// EnterIndexstmt handles CREATE INDEX-func (c *sdlChecker) EnterIndexstmt(ctx *parser.IndexstmtContext) {- if !isTopLevel(ctx.GetParent()) {- return- }-- // Get index name if available- var indexName string- if ctx.Name() != nil && ctx.Name().Colid() != nil {- indexName = pgparser.NormalizePostgreSQLColid(ctx.Name().Colid())- }+ cols, tableConstraints := omniTableElements(n)- // Check 1: Disallow unnamed indexes- if indexName == "" {- c.adviceList = append(c.adviceList, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SDLRequireIndexName.Int32(),- Title: "sdl.require-index-name",- Content: "Index is created without an explicit name.\n\n" +- "SDL requires all indexes to have explicit names.\n\n" +- "Example - Instead of:\n" +- " CREATE INDEX ON table_name (column);\n\n" +- "Use:\n" +- " CREATE INDEX idx_table_column ON table_name (column);",- StartPosition: &storepb.Position{- Line: int32(ctx.GetStart().GetLine()),- Column: 0,- },- })- }+ // Check column-level constraints+ for _, col := range cols {+ columnName := col.Colname+ constraints := omniColumnConstraints(col)- // Check 2: Require schema name on table- if ctx.Relation_expr() != nil && ctx.Relation_expr().Qualified_name() != nil {- schemaName := extractSchemaName(ctx.Relation_expr().Qualified_name())- tableName := extractTableName(ctx.Relation_expr().Qualified_name())+ var disallowedTypes []string+ var firstConstraintLine int32- if schemaName == "" {- var content string- if indexName != "" {- content = fmt.Sprintf(- "Index '%s' is created on table '%s' without explicit schema name.\n\n"+- "SDL requires all objects to specify their schema explicitly.\n\n"+- "Example - Instead of:\n"+- " CREATE INDEX %s ON %s (...);\n\n"+- "Use:\n"+- " CREATE INDEX %s ON schema_name.%s (...);",- indexName, tableName,- indexName, tableName,- indexName, tableName,- )- } else {- // Unnamed index (already reported above, but still check table schema)- content = fmt.Sprintf(- "Index is created on table '%s' without explicit schema name.\n\n"+- "SDL requires all objects to specify their schema explicitly.\n\n"+- "Example - Use:\n"+- " CREATE INDEX idx_name ON schema_name.%s (...);",- tableName,- tableName,- )+ for _, c := range constraints {+ typeName := constraintTypeName(c.Contype)+ if typeName == "" {+ continue}-- c.adviceList = append(c.adviceList, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SDLRequireSchemaName.Int32(),- Title: "sdl.require-schema-name",- Content: content,- StartPosition: &storepb.Position{- Line: int32(ctx.GetStart().GetLine()),- Column: 0,- },- })- }- }-}--// EnterViewstmt handles CREATE VIEW-func (c *sdlChecker) EnterViewstmt(ctx *parser.ViewstmtContext) {- if !isTopLevel(ctx.GetParent()) {- return- }-- if ctx.Qualified_name() != nil {- schemaName := extractSchemaName(ctx.Qualified_name())- viewName := extractTableName(ctx.Qualified_name())- if schemaName == "" {- c.adviceList = append(c.adviceList, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SDLRequireSchemaName.Int32(),- Title: "sdl.require-schema-name",- Content: fmt.Sprintf(- "View '%s' is created without explicit schema name.\n\n"+- "SDL requires all objects to specify their schema explicitly.\n\n"+- "Example - Instead of:\n"+- " CREATE VIEW %s AS ...;\n\n"+- "Use:\n"+- " CREATE VIEW schema_name.%s AS ...;",- viewName, viewName, viewName,- ),- StartPosition: &storepb.Position{- Line: int32(ctx.GetStart().GetLine()),- Column: 0,- },- })- }- }-}--// EnterCreateseqstmt handles CREATE SEQUENCE-func (c *sdlChecker) EnterCreateseqstmt(ctx *parser.CreateseqstmtContext) {- if !isTopLevel(ctx.GetParent()) {- return- }-- if ctx.Qualified_name() != nil {- schemaName := extractSchemaName(ctx.Qualified_name())- sequenceName := extractTableName(ctx.Qualified_name())- if schemaName == "" {- c.adviceList = append(c.adviceList, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SDLRequireSchemaName.Int32(),- Title: "sdl.require-schema-name",- Content: fmt.Sprintf(- "Sequence '%s' is created without explicit schema name.\n\n"+- "SDL requires all objects to specify their schema explicitly.\n\n"+- "Example - Instead of:\n"+- " CREATE SEQUENCE %s ...;\n\n"+- "Use:\n"+- " CREATE SEQUENCE schema_name.%s ...;",- sequenceName, sequenceName, sequenceName,- ),- StartPosition: &storepb.Position{- Line: int32(ctx.GetStart().GetLine()),- Column: 0,- },- })- }- }-}--// EnterCreatefunctionstmt handles CREATE FUNCTION-func (c *sdlChecker) EnterCreatefunctionstmt(ctx *parser.CreatefunctionstmtContext) {- if !isTopLevel(ctx.GetParent()) {- return- }-- if ctx.Func_name() != nil {- schemaName, functionName := c.extractSchemaAndNameFromFuncName(ctx.Func_name())- if schemaName == "" {- c.adviceList = append(c.adviceList, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SDLRequireSchemaName.Int32(),- Title: "sdl.require-schema-name",- Content: fmt.Sprintf(- "Function '%s' is created without explicit schema name.\n\n"+- "SDL requires all objects to specify their schema explicitly.\n\n"+- "Example - Instead of:\n"+- " CREATE FUNCTION %s(...) ...;\n\n"+- "Use:\n"+- " CREATE FUNCTION schema_name.%s(...) ...;",- functionName, functionName, functionName,- ),- StartPosition: &storepb.Position{- Line: int32(ctx.GetStart().GetLine()),- Column: 0,- },- })- }- }-}--// EnterAlterseqstmt handles ALTER SEQUENCE-func (c *sdlChecker) EnterAlterseqstmt(ctx *parser.AlterseqstmtContext) {- if !isTopLevel(ctx.GetParent()) {- return- }-- if ctx.Qualified_name() != nil {- schemaName := extractSchemaName(ctx.Qualified_name())- sequenceName := extractTableName(ctx.Qualified_name())- if schemaName == "" {- c.adviceList = append(c.adviceList, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SDLRequireSchemaName.Int32(),- Title: "sdl.require-schema-name",- Content: fmt.Sprintf(- "Sequence '%s' is altered without explicit schema name.\n\n"+- "SDL requires all objects to specify their schema explicitly.\n\n"++ if disallowedTypes == nil {+ firstConstraintLine = nodeLineOrDefault(statement, c.Loc.Start, stmtLine)+ }+ disallowedTypes = append(disallowedTypes, typeName)++ // Also check FK reference schema for column-level FK+ if c.Contype == ast.CONSTR_FOREIGN && c.Pktable != nil && c.Pktable.Schemaname == "" {+ refTableName := c.Pktable.Relname+ content := fmt.Sprintf(+ "Column '%s' in table '%s' has a foreign key reference to table '%s' without explicit schema name.\n\n"++ "SDL requires all table references in foreign keys to specify their schema explicitly.\n"++ "Note: Foreign keys should be defined at table level, not column level.\n\n"+"Example - Instead of:\n"+- " ALTER SEQUENCE %s ...;\n\n"+- "Use:\n"+- " ALTER SEQUENCE schema_name.%s ...;",- sequenceName, sequenceName, sequenceName,- ),- StartPosition: &storepb.Position{- Line: int32(ctx.GetStart().GetLine()),- Column: 0,- },- })- }- }-}--// checkColumnConstraints checks a column definition for disallowed constraints.-// SDL allows NOT NULL, DEFAULT, GENERATED at column level.-// SDL disallows PRIMARY KEY, UNIQUE, CHECK, FOREIGN KEY at column level.-func (c *sdlChecker) checkColumnConstraints(columnDef parser.IColumnDefContext, tableName string) {- if columnDef == nil {- return- }-- var columnName string- if columnDef.Colid() != nil {- columnName = pgparser.NormalizePostgreSQLColid(columnDef.Colid())- }-- // Check if column has any disallowed constraints- if columnDef.Colquallist() != nil {- allConstraints := columnDef.Colquallist().AllColconstraint()- var disallowedConstraints []parser.IColconstraintContext- var disallowedConstraintTypes []string-- for _, constraint := range allConstraints {- if constraint.Colconstraintelem() != nil {- elem := constraint.Colconstraintelem()- // Check if this is a disallowed constraint type- if c.isDisallowedColumnConstraint(elem) {- disallowedConstraints = append(disallowedConstraints, constraint)- disallowedConstraintTypes = append(disallowedConstraintTypes, c.getConstraintTypeName(elem))- }- // Also check FK reference schema even though FK is disallowed at column level- // This provides a more specific error message- c.checkColumnForeignKeyReferenceSchema(elem, columnName, tableName)+ " CREATE TABLE %s (\n"++ " %s INTEGER REFERENCES %s(id)\n"++ " );\n\n"++ "Use table-level constraint with schema:\n"++ " CREATE TABLE %s (\n"++ " %s INTEGER,\n"++ " CONSTRAINT fk_name FOREIGN KEY (%s) REFERENCES schema_name.%s(id)\n"++ " );",+ columnName, tableName, refTableName,+ tableName, columnName, refTableName,+ tableName, columnName, columnName, refTableName,+ )+ adviceList = append(adviceList, &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.SDLRequireSchemaName.Int32(),+ Title: "sdl.require-schema-name",+ Content: content,+ StartPosition: &storepb.Position{+ Line: nodeLineOrDefault(statement, c.Loc.Start, stmtLine),+ Column: 0,+ },+ })}}- if len(disallowedConstraints) > 0 {- firstConstraint := disallowedConstraints[0]- exampleConstraint := disallowedConstraintTypes[0]-+ if len(disallowedTypes) > 0 {+ exampleConstraint := disallowedTypes[0]content := fmt.Sprintf("Column '%s' in table '%s' has disallowed column-level constraint(s): %v.\n\n"+"SDL only allows NOT NULL, DEFAULT, and GENERATED constraints at the column level.\n"+@@ -351,115 +168,64 @@ func (c *sdlChecker) checkColumnConstraints(columnDef parser.IColumnDefContext," %s INTEGER,\n"+" CONSTRAINT constraint_name %s (%s)\n"+" );",- columnName, tableName, disallowedConstraintTypes,+ columnName, tableName, disallowedTypes,tableName, columnName, exampleConstraint,tableName, columnName, exampleConstraint, columnName,)- c.adviceList = append(c.adviceList, &storepb.Advice{+ adviceList = append(adviceList, &storepb.Advice{Status: storepb.Advice_ERROR,Code: code.SDLDisallowColumnConstraint.Int32(),Title: "sdl.disallow-column-constraint",Content: content,StartPosition: &storepb.Position{- Line: int32(firstConstraint.GetStart().GetLine()),+ Line: firstConstraintLine,Column: 0,},})}}-}-// isDisallowedColumnConstraint returns true if the constraint is not allowed at column level in SDL.-// Allowed: NOT NULL, NULL, DEFAULT, GENERATED-// Disallowed: PRIMARY KEY, UNIQUE, CHECK, FOREIGN KEY-func (*sdlChecker) isDisallowedColumnConstraint(elem parser.IColconstraintelemContext) bool {- if elem == nil {- return false- }- // Disallowed constraints- if elem.PRIMARY() != nil && elem.KEY() != nil {- return true // PRIMARY KEY- }- if elem.UNIQUE() != nil {- return true // UNIQUE- }- if elem.CHECK() != nil {- return true // CHECK- }- if elem.REFERENCES() != nil {- return true // FOREIGN KEY- }- // Allowed constraints: NOT NULL, NULL, DEFAULT, GENERATED- return false-}+ // Check table-level constraints+ for _, c := range tableConstraints {+ // Check unnamed constraint+ if c.Conname == "" {+ constraintType := tableConstraintTypeName(c.Contype)+ constraintExample := tableConstraintExample(c.Contype)-// checkTableConstraintName checks if a table constraint has an explicit name.-func (c *sdlChecker) checkTableConstraintName(constraint parser.ITableconstraintContext, tableName string) {- if constraint == nil {- return- }+ content := fmt.Sprintf(+ "Table '%s' has a %s constraint without an explicit name.\n\n"++ "SDL requires all table constraints to have explicit names using the CONSTRAINT keyword.\n\n"++ "Example - Instead of:\n"++ " CREATE TABLE %s (\n"++ " id INTEGER,\n"++ " %s\n"++ " );\n\n"++ "Use named constraint:\n"++ " CREATE TABLE %s (\n"++ " id INTEGER,\n"++ " CONSTRAINT constraint_name %s\n"++ " );",+ tableName, constraintType,+ tableName, constraintExample,+ tableName, constraintExample,+ )- // Check if constraint has a name using CONSTRAINT keyword- hasConstraintName := false- if constraint.CONSTRAINT() != nil {- if constraint.Name() != nil && constraint.Name().Colid() != nil {- hasConstraintName = true+ adviceList = append(adviceList, &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.SDLRequireConstraintName.Int32(),+ Title: "sdl.require-constraint-name",+ Content: content,+ StartPosition: &storepb.Position{+ Line: nodeLineOrDefault(statement, c.Loc.Start, stmtLine),+ Column: 0,+ },+ })}- }- if !hasConstraintName {- constraintType := c.getTableConstraintType(constraint)-- content := fmt.Sprintf(- "Table '%s' has a %s constraint without an explicit name.\n\n"+- "SDL requires all table constraints to have explicit names using the CONSTRAINT keyword.\n\n"+- "Example - Instead of:\n"+- " CREATE TABLE %s (\n"+- " id INTEGER,\n"+- " %s\n"+- " );\n\n"+- "Use named constraint:\n"+- " CREATE TABLE %s (\n"+- " id INTEGER,\n"+- " CONSTRAINT constraint_name %s\n"+- " );",- tableName, constraintType,- tableName, c.getConstraintExample(constraint, false),- tableName, c.getConstraintExample(constraint, true),- )-- c.adviceList = append(c.adviceList, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SDLRequireConstraintName.Int32(),- Title: "sdl.require-constraint-name",- Content: content,- StartPosition: &storepb.Position{- Line: int32(constraint.GetStart().GetLine()),- Column: 0,- },- })- }-}--// checkForeignKeyReferenceSchema checks if a FOREIGN KEY constraint references a table with explicit schema name.-func (c *sdlChecker) checkForeignKeyReferenceSchema(constraint parser.ITableconstraintContext, tableName string) {- if constraint == nil || constraint.Constraintelem() == nil {- return- }-- elem := constraint.Constraintelem()- // Check if this is a FOREIGN KEY constraint- if elem.FOREIGN() == nil || elem.KEY() == nil {- return- }-- // Check the referenced table (REFERENCES clause)- if elem.Qualified_name() != nil {- refSchemaName := extractSchemaName(elem.Qualified_name())- refTableName := extractTableName(elem.Qualified_name())-- if refSchemaName == "" {+ // Check FK reference schema+ if c.Contype == ast.CONSTR_FOREIGN && c.Pktable != nil && c.Pktable.Schemaname == "" {+ refTableName := c.Pktable.Relnamecontent := fmt.Sprintf("Foreign key constraint in table '%s' references table '%s' without explicit schema name.\n\n"+"SDL requires all table references in foreign keys to specify their schema explicitly.\n\n"+@@ -478,165 +244,328 @@ func (c *sdlChecker) checkForeignKeyReferenceSchema(constraint parser.ITableconstableName, refTableName,)- c.adviceList = append(c.adviceList, &storepb.Advice{+ adviceList = append(adviceList, &storepb.Advice{Status: storepb.Advice_ERROR,Code: code.SDLRequireSchemaName.Int32(),Title: "sdl.require-schema-name",Content: content,StartPosition: &storepb.Position{- Line: int32(constraint.GetStart().GetLine()),+ Line: nodeLineOrDefault(statement, c.Loc.Start, stmtLine),Column: 0,},})}}++ return adviceList}-// checkColumnForeignKeyReferenceSchema checks if a column-level FOREIGN KEY references a table with explicit schema name.-func (c *sdlChecker) checkColumnForeignKeyReferenceSchema(elem parser.IColconstraintelemContext, columnName, tableName string) {- if elem == nil {- return- }+func checkIndexStmt(_ string, n *ast.IndexStmt, stmtLine int32, adviceList []*storepb.Advice) []*storepb.Advice {+ indexName := n.Idxname- // Check if this is a FOREIGN KEY constraint (REFERENCES)- if elem.REFERENCES() == nil {- return+ // Check: Disallow unnamed indexes+ if indexName == "" {+ adviceList = append(adviceList, &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.SDLRequireIndexName.Int32(),+ Title: "sdl.require-index-name",+ Content: "Index is created without an explicit name.\n\n" ++ "SDL requires all indexes to have explicit names.\n\n" ++ "Example - Instead of:\n" ++ " CREATE INDEX ON table_name (column);\n\n" ++ "Use:\n" ++ " CREATE INDEX idx_table_column ON table_name (column);",+ StartPosition: &storepb.Position{+ Line: stmtLine,+ Column: 0,+ },+ })}- // Check the referenced table- if elem.Qualified_name() != nil {- refSchemaName := extractSchemaName(elem.Qualified_name())- refTableName := extractTableName(elem.Qualified_name())+ // Check: Require schema name on table+ if n.Relation != nil {+ schemaName := n.Relation.Schemaname+ tableName := n.Relation.Relname- if refSchemaName == "" {- content := fmt.Sprintf(- "Column '%s' in table '%s' has a foreign key reference to table '%s' without explicit schema name.\n\n"+- "SDL requires all table references in foreign keys to specify their schema explicitly.\n"+- "Note: Foreign keys should be defined at table level, not column level.\n\n"+- "Example - Instead of:\n"+- " CREATE TABLE %s (\n"+- " %s INTEGER REFERENCES %s(id)\n"+- " );\n\n"+- "Use table-level constraint with schema:\n"+- " CREATE TABLE %s (\n"+- " %s INTEGER,\n"+- " CONSTRAINT fk_name FOREIGN KEY (%s) REFERENCES schema_name.%s(id)\n"+- " );",- columnName, tableName, refTableName,- tableName, columnName, refTableName,- tableName, columnName, columnName, refTableName,- )+ if schemaName == "" {+ var content string+ if indexName != "" {+ content = fmt.Sprintf(+ "Index '%s' is created on table '%s' without explicit schema name.\n\n"++ "SDL requires all objects to specify their schema explicitly.\n\n"++ "Example - Instead of:\n"++ " CREATE INDEX %s ON %s (...);\n\n"++ "Use:\n"++ " CREATE INDEX %s ON schema_name.%s (...);",+ indexName, tableName,+ indexName, tableName,+ indexName, tableName,+ )+ } else {+ content = fmt.Sprintf(+ "Index is created on table '%s' without explicit schema name.\n\n"++ "SDL requires all objects to specify their schema explicitly.\n\n"++ "Example - Use:\n"++ " CREATE INDEX idx_name ON schema_name.%s (...);",+ tableName,+ tableName,+ )+ }- c.adviceList = append(c.adviceList, &storepb.Advice{+ adviceList = append(adviceList, &storepb.Advice{Status: storepb.Advice_ERROR,Code: code.SDLRequireSchemaName.Int32(),Title: "sdl.require-schema-name",Content: content,StartPosition: &storepb.Position{- Line: int32(elem.GetStart().GetLine()),+ Line: stmtLine,Column: 0,},})}}-}-// Helper functions+ return adviceList+}-func (*sdlChecker) getConstraintTypeName(elem parser.IColconstraintelemContext) string {- if elem == nil {- return "UNKNOWN"+func checkViewStmt(n *ast.ViewStmt, stmtLine int32, adviceList []*storepb.Advice) []*storepb.Advice {+ if n.View == nil {+ return adviceList}- if elem.PRIMARY() != nil && elem.KEY() != nil {- return "PRIMARY KEY"- }- if elem.UNIQUE() != nil {- return "UNIQUE"+ schemaName := n.View.Schemaname+ viewName := n.View.Relname+ if schemaName == "" {+ adviceList = append(adviceList, &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.SDLRequireSchemaName.Int32(),+ Title: "sdl.require-schema-name",+ Content: fmt.Sprintf(+ "View '%s' is created without explicit schema name.\n\n"++ "SDL requires all objects to specify their schema explicitly.\n\n"++ "Example - Instead of:\n"++ " CREATE VIEW %s AS ...;\n\n"++ "Use:\n"++ " CREATE VIEW schema_name.%s AS ...;",+ viewName, viewName, viewName,+ ),+ StartPosition: &storepb.Position{+ Line: stmtLine,+ Column: 0,+ },+ })}- if elem.NOT() != nil && elem.NULL_P() != nil {- return "NOT NULL"+ return adviceList+}++func checkCreateSeqStmt(n *ast.CreateSeqStmt, stmtLine int32, adviceList []*storepb.Advice) []*storepb.Advice {+ if n.Sequence == nil {+ return adviceList}- if elem.NULL_P() != nil {- return "NULL"+ schemaName := n.Sequence.Schemaname+ sequenceName := n.Sequence.Relname+ if schemaName == "" {+ adviceList = append(adviceList, &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.SDLRequireSchemaName.Int32(),+ Title: "sdl.require-schema-name",+ Content: fmt.Sprintf(+ "Sequence '%s' is created without explicit schema name.\n\n"++ "SDL requires all objects to specify their schema explicitly.\n\n"++ "Example - Instead of:\n"++ " CREATE SEQUENCE %s ...;\n\n"++ "Use:\n"++ " CREATE SEQUENCE schema_name.%s ...;",+ sequenceName, sequenceName, sequenceName,+ ),+ StartPosition: &storepb.Position{+ Line: stmtLine,+ Column: 0,+ },+ })}- if elem.CHECK() != nil {- return "CHECK"+ return adviceList+}++func checkCreateFunctionStmt(n *ast.CreateFunctionStmt, stmtLine int32, adviceList []*storepb.Advice) []*storepb.Advice {+ schemaName := omniExtractSchemaFromFuncname(n.Funcname)+ parts := omniListStrings(n.Funcname)+ functionName := ""+ if len(parts) > 0 {+ functionName = parts[len(parts)-1]}- if elem.DEFAULT() != nil {- return "DEFAULT"+ if schemaName == "" {+ adviceList = append(adviceList, &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.SDLRequireSchemaName.Int32(),+ Title: "sdl.require-schema-name",+ Content: fmt.Sprintf(+ "Function '%s' is created without explicit schema name.\n\n"++ "SDL requires all objects to specify their schema explicitly.\n\n"++ "Example - Instead of:\n"++ " CREATE FUNCTION %s(...) ...;\n\n"++ "Use:\n"++ " CREATE FUNCTION schema_name.%s(...) ...;",+ functionName, functionName, functionName,+ ),+ StartPosition: &storepb.Position{+ Line: stmtLine,+ Column: 0,+ },+ })}- if elem.REFERENCES() != nil {- return "FOREIGN KEY"+ return adviceList+}++func checkAlterSeqStmt(n *ast.AlterSeqStmt, stmtLine int32, adviceList []*storepb.Advice) []*storepb.Advice {+ if n.Sequence == nil {+ return adviceList}- if elem.GENERATED() != nil {- return "GENERATED"+ schemaName := n.Sequence.Schemaname+ sequenceName := n.Sequence.Relname+ if schemaName == "" {+ adviceList = append(adviceList, &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.SDLRequireSchemaName.Int32(),+ Title: "sdl.require-schema-name",+ Content: fmt.Sprintf(+ "Sequence '%s' is altered without explicit schema name.\n\n"++ "SDL requires all objects to specify their schema explicitly.\n\n"++ "Example - Instead of:\n"++ " ALTER SEQUENCE %s ...;\n\n"++ "Use:\n"++ " ALTER SEQUENCE schema_name.%s ...;",+ sequenceName, sequenceName, sequenceName,+ ),+ StartPosition: &storepb.Position{+ Line: stmtLine,+ Column: 0,+ },+ })}- return "CONSTRAINT"+ return adviceList}-func (*sdlChecker) getTableConstraintType(constraint parser.ITableconstraintContext) string {- if constraint.Constraintelem() == nil {- return "CONSTRAINT"- }- elem := constraint.Constraintelem()- if elem.PRIMARY() != nil && elem.KEY() != nil {+// constraintTypeName returns the display name for disallowed column-level constraints.+// Returns empty string for allowed constraints (NOT NULL, DEFAULT, GENERATED, NULL).+func constraintTypeName(contype ast.ConstrType) string {+ switch contype {+ case ast.CONSTR_PRIMARY:return "PRIMARY KEY"- }- if elem.UNIQUE() != nil {+ case ast.CONSTR_UNIQUE:return "UNIQUE"- }- if elem.CHECK() != nil {+ case ast.CONSTR_CHECK:return "CHECK"- }- if elem.FOREIGN() != nil && elem.KEY() != nil {+ case ast.CONSTR_FOREIGN:return "FOREIGN KEY"+ default:+ return ""}- if elem.EXCLUDE() != nil {- return "EXCLUDE"- }- return "CONSTRAINT"}-func (*sdlChecker) getConstraintExample(constraint parser.ITableconstraintContext, _ bool) string {- if constraint.Constraintelem() == nil {- return "..."+// tableConstraintTypeName returns the display name for a table-level constraint type.+func tableConstraintTypeName(contype ast.ConstrType) string {+ switch contype {+ case ast.CONSTR_PRIMARY:+ return "PRIMARY KEY"+ case ast.CONSTR_UNIQUE:+ return "UNIQUE"+ case ast.CONSTR_CHECK:+ return "CHECK"+ case ast.CONSTR_FOREIGN:+ return "FOREIGN KEY"+ default:+ return "CONSTRAINT"}- elem := constraint.Constraintelem()- if elem.PRIMARY() != nil && elem.KEY() != nil {+}++// tableConstraintExample returns an example SQL snippet for a given constraint type.+func tableConstraintExample(contype ast.ConstrType) string {+ switch contype {+ case ast.CONSTR_PRIMARY:return "PRIMARY KEY (id)"- }- if elem.UNIQUE() != nil {+ case ast.CONSTR_UNIQUE:return "UNIQUE (column)"- }- if elem.CHECK() != nil {+ case ast.CONSTR_CHECK:return "CHECK (condition)"- }- if elem.FOREIGN() != nil && elem.KEY() != nil {+ case ast.CONSTR_FOREIGN:return "FOREIGN KEY (column) REFERENCES other_table(id)"+ default:+ return "..."}- return "..."}-func (*sdlChecker) extractSchemaAndNameFromFuncName(funcName parser.IFunc_nameContext) (string, string) {- if funcName.Type_function_name() != nil {- return "", funcName.Type_function_name().GetText()- }- if funcName.Indirection() != nil {- parts := []string{}- if funcName.Colid() != nil {- parts = append(parts, funcName.Colid().GetText())- }- for _, attr := range funcName.Indirection().AllIndirection_el() {- if attr.Attr_name() != nil {- parts = append(parts, attr.Attr_name().GetText())+// validateParenBalance checks for unmatched parentheses in SQL, skipping string+// literals, dollar-quoted strings, and comments. This catches syntax errors that+// the omni parser may recover from silently.+func validateParenBalance(sql string) error {+ depth := 0+ i := 0+ for i < len(sql) {+ ch := sql[i]+ switch {+ case ch == '-' && i+1 < len(sql) && sql[i+1] == '-':+ // Line comment: skip to end of line+ for i < len(sql) && sql[i] != '\n' {+ i++}+ case ch == '/' && i+1 < len(sql) && sql[i+1] == '*':+ // Block comment: skip to */+ i += 2+ for i+1 < len(sql) && (sql[i] != '*' || sql[i+1] != '/') {+ i+++ }+ if i+1 < len(sql) {+ i += 2+ }+ case ch == '\'':+ // Single-quoted string: skip to closing quote (handle escaped quotes)+ i+++ for i < len(sql) {+ if sql[i] == '\'' {+ if i+1 < len(sql) && sql[i+1] == '\'' {+ i += 2+ continue+ }+ break+ }+ i+++ }+ i+++ case ch == '$':+ // Dollar-quoted string: find tag and skip to closing tag+ j := i + 1+ for j < len(sql) && (sql[j] == '_' || (sql[j] >= 'a' && sql[j] <= 'z') || (sql[j] >= 'A' && sql[j] <= 'Z') || (sql[j] >= '0' && sql[j] <= '9')) {+ j+++ }+ if j < len(sql) && sql[j] == '$' {+ tag := sql[i : j+1]+ i = j + 1+ // Find closing tag+ for i+len(tag) <= len(sql) {+ if sql[i] == '$' && sql[i:i+len(tag)] == tag {+ i += len(tag)+ break+ }+ i+++ }+ } else {+ i+++ }+ case ch == '(':+ depth+++ i+++ case ch == ')':+ depth--+ if depth < 0 {+ return errors.New("syntax error: unmatched closing parenthesis")+ }+ i+++ default:+ i++}- if len(parts) == 1 {- return "", parts[0]- }- if len(parts) >= 2 {- return parts[0], parts[1]- }}- return "", ""+ if depth != 0 {+ return errors.New("syntax error: unmatched opening parenthesis")+ }+ return nil}diff --git a/backend/plugin/advisor/pg/sdl_integrity_check.go b/backend/plugin/advisor/pg/sdl_integrity_check.goindex b7b2b3be44..a1c069efb6 100644--- a/backend/plugin/advisor/pg/sdl_integrity_check.go+++ b/backend/plugin/advisor/pg/sdl_integrity_check.go@@ -2,11 +2,10 @@ package pgimport ("fmt"- "strconv""strings"- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/postgresql"+ omnipg "github.com/bytebase/omni/pg"+ "github.com/bytebase/omni/pg/ast"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor/code"@@ -32,9 +31,8 @@ func CheckSDLIntegrity(files map[string]string) (map[string][]*storepb.Advice, e// Parse each file and build individual symbol tablesfileCheckers := make([]*fileSymbolTable, 0, len(files))for filePath, statement := range files {- parseResults, err := pgparser.ParsePostgreSQL(statement)- if err != nil {- // Return parse error for this file+ // Validate syntax using ANTLR parser first (stricter than omni for some edge cases)+ if _, err := pgparser.ParsePostgreSQL(statement); err != nil {return map[string][]*storepb.Advice{filePath: {{Status: storepb.Advice_ERROR,@@ -45,14 +43,23 @@ func CheckSDLIntegrity(files map[string]string) (map[string][]*storepb.Advice, e}, nil}- checker := &sdlIntegrityChecker{- BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},- symbolTable: newSymbolTable(),+ stmts, err := pgparser.ParsePg(statement)+ if err != nil {+ return map[string][]*storepb.Advice{+ filePath: {{+ Status: storepb.Advice_ERROR,+ Code: code.StatementSyntaxError.Int32(),+ Title: "SQL syntax error",+ Content: fmt.Sprintf("Failed to parse SQL in file '%s': %v", filePath, err),+ }},+ }, nil}- // Build symbol table for this file - process all statements- for _, parseResult := range parseResults {- antlr.ParseTreeWalkerDefault.Walk(checker, parseResult.Tree)+ st, advices := buildFileSymbolTable(stmts, statement)++ checker := &sdlIntegrityChecker{+ symbolTable: st,+ adviceList: advices,}fileCheckers = append(fileCheckers, &fileSymbolTable{@@ -365,7 +372,7 @@ func (c *sdlIntegrityChecker) validateForeignKeysWithMergedTable(merged *mergedS"Foreign key constraint '%s' on table '%s.%s' references table '%s.%s' which does not exist in any SDL file.\n\n"+"Foreign key columns: %s\n\n"+"Make sure the referenced table is defined in one of the SDL files.",- c.getConstraintName(constraint),+ getConstraintName(constraint),table.schemaName, table.tableName,constraint.fkReferencedSchema, constraint.fkReferencedTable,formatColumnList(constraint.fkColumns),@@ -400,18 +407,17 @@ func (c *sdlIntegrityChecker) validateForeignKeysWithMergedTable(merged *mergedS" Source column: %s.%s.%s\n"+" Referenced column: %s.%s.%s (DOES NOT EXIST)\n\n"+"Available columns in '%s.%s': %s",- c.getConstraintName(constraint),+ getConstraintName(constraint),table.schemaName, table.tableName,refCol,constraint.fkReferencedSchema, constraint.fkReferencedTable,table.schemaName, table.tableName, fkCol,constraint.fkReferencedSchema, constraint.fkReferencedTable, refCol,constraint.fkReferencedSchema, constraint.fkReferencedTable,- c.getColumnList(refTable),+ getColumnList(refTable),),StartPosition: &storepb.Position{Line: int32(constraint.line)},})- continue}}}@@ -421,12 +427,11 @@ func (c *sdlIntegrityChecker) validateForeignKeysWithMergedTable(merged *mergedS// validateViewDependenciesWithMergedTable validates view dependencies using merged symbol tablefunc (c *sdlIntegrityChecker) validateViewDependenciesWithMergedTable(merged *mergedSymbolTable) {for _, view := range c.symbolTable.views {- if view.selectStmt == nil {+ if view.selectQuery == nil {continue}- // Extract dependencies using the same approach as generate_migration- dependencies := c.getViewDependenciesFromAST(view.selectStmt, view.schemaName)+ dependencies := getViewDependencies(view.selectQuery, view.schemaName)for _, dep := range dependencies {// Skip system schemas@@ -476,9 +481,8 @@ func formatColumnList(columns []string) string {return result}-// sdlIntegrityChecker performs multi-pass integrity checking+// sdlIntegrityChecker holds the symbol table and advices for integrity checkingtype sdlIntegrityChecker struct {- *parser.BasePostgreSQLParserListenersymbolTable *symbolTableadviceList []*storepb.Advice}@@ -536,11 +540,10 @@ type indexDef struct {}type viewDef struct {- schemaName string- viewName string- // Store the SELECT statement context for dependency analysis- selectStmt parser.ISelectstmtContext- line int+ schemaName string+ viewName string+ selectQuery ast.Node // the SELECT query AST node+ line int}type constraintDef struct {@@ -554,8 +557,6 @@ type constraintDef struct {fkReferencedTable stringfkColumns []stringfkReferencedColumns []string- // For CHECK constraints- checkExpr parser.IA_exprContext}func newSymbolTable() *symbolTable {@@ -597,13 +598,13 @@ func (st *symbolTable) addIndex(schema, index, table string, line int) {}}-func (st *symbolTable) addView(schema, view string, selectStmt parser.ISelectstmtContext, line int) {+func (st *symbolTable) addView(schema, view string, selectQuery ast.Node, line int) {key := qualifiedName(schema, view)st.views[key] = &viewDef{- schemaName: schema,- viewName: view,- selectStmt: selectStmt,- line: line,+ schemaName: schema,+ viewName: view,+ selectQuery: selectQuery,+ line: line,}}@@ -642,29 +643,49 @@ func qualifiedName(schema, name string) string {return schema + "." + name}-// Pass 1: Collect definitions+// buildFileSymbolTable iterates omni statements and builds the symbol table.+func buildFileSymbolTable(stmts []omnipg.Statement, fileText string) (*symbolTable, []*storepb.Advice) {+ st := newSymbolTable()+ var advices []*storepb.Advice+ for _, stmt := range stmts {+ switch n := stmt.AST.(type) {+ case *ast.CreateStmt:+ processCreateStmt(n, fileText, st, &advices)+ case *ast.IndexStmt:+ processIndexStmt(n, fileText, st, &advices)+ case *ast.ViewStmt:+ processViewStmt(n, fileText, st)+ default:+ }+ }+ return st, advices+}-func (c *sdlIntegrityChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {- if !isTopLevel(ctx.GetParent()) {- return+// locToLine converts a byte offset to a 1-based line number.+func locToLine(fileText string, loc ast.Loc) int {+ if loc.Start < 0 || fileText == "" {+ return 1}+ pos := pgparser.ByteOffsetToRunePosition(fileText, loc.Start)+ return int(pos.Line)+}- var tableName, schemaName string- allQualifiedNames := ctx.AllQualified_name()- if len(allQualifiedNames) > 0 {- schemaName = extractSchemaName(allQualifiedNames[0])- tableName = extractTableName(allQualifiedNames[0])+func processCreateStmt(n *ast.CreateStmt, fileText string, st *symbolTable, advices *[]*storepb.Advice) {+ if n.Relation == nil {+ return}+ tableName := n.Relation.Relname+ schemaName := n.Relation.Schemanameif schemaName == "" {schemaName = "public"}- line := ctx.GetStart().GetLine()+ line := locToLine(fileText, n.Loc)// Check for duplicate table name- if existing := c.symbolTable.getTable(schemaName, tableName); existing != nil {- c.adviceList = append(c.adviceList, &storepb.Advice{+ if existing := st.getTable(schemaName, tableName); existing != nil {+ *advices = append(*advices, &storepb.Advice{Status: storepb.Advice_ERROR,Code: code.SDLDuplicateTableName.Int32(),Title: "Duplicate table name",@@ -682,153 +703,144 @@ func (c *sdlIntegrityChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {return}- // Add table to symbol table- table := c.symbolTable.addTable(schemaName, tableName, line)+ table := st.addTable(schemaName, tableName, line)++ // Process columns from TableElts+ position := 0+ if n.TableElts != nil {+ for _, item := range n.TableElts.Items {+ switch elem := item.(type) {+ case *ast.ColumnDef:+ processColumnDef(elem, fileText, schemaName, tableName, table, &position, advices)+ case *ast.Constraint:+ processTableConstraint(elem, fileText, table, st, advices)+ default:+ }+ }+ }- // Collect column definitions and constraints- if ctx.Opttableelementlist() != nil && ctx.Opttableelementlist().Tableelementlist() != nil {- allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()+ // Process Constraints list+ if n.Constraints != nil {+ for _, item := range n.Constraints.Items {+ if c, ok := item.(*ast.Constraint); ok {+ processTableConstraint(c, fileText, table, st, advices)+ }+ }+ }+}- position := 0- for _, elem := range allElements {- // Collect column definitions- if elem.ColumnDef() != nil {- colDef := elem.ColumnDef()- colName := colDef.Colid().GetText()+func processColumnDef(col *ast.ColumnDef, fileText, schemaName, tableName string, table *tableDef, position *int, advices *[]*storepb.Advice) {+ colName := col.Colname- // Check for duplicate column name- if _, exists := table.columns[colName]; exists {- c.adviceList = append(c.adviceList, &storepb.Advice{- Status: storepb.Advice_ERROR,- Code: code.SDLDuplicateColumnName.Int32(),- Title: "Duplicate column name",- Content: fmt.Sprintf(- "Column '%s' is defined multiple times in table '%s.%s'.\n\n"+- "Each column can only be defined once per table.",- colName, schemaName, tableName,- ),- StartPosition: &storepb.Position{- Line: int32(colDef.GetStart().GetLine()),- },- })- continue- }+ colLine := locToLine(fileText, col.Loc)- // Extract column data type- dataType := c.extractDataType(colDef.Typename())-- // Check for NOT NULL constraint- notNull := false- if colDef.Colquallist() != nil {- for _, qual := range colDef.Colquallist().AllColconstraint() {- if qual.Colconstraintelem() != nil {- elem := qual.Colconstraintelem()- if elem.NOT() != nil && elem.NULL_P() != nil {- notNull = true- }- }- }- }+ // Check for duplicate column name+ if _, exists := table.columns[colName]; exists {+ *advices = append(*advices, &storepb.Advice{+ Status: storepb.Advice_ERROR,+ Code: code.SDLDuplicateColumnName.Int32(),+ Title: "Duplicate column name",+ Content: fmt.Sprintf(+ "Column '%s' is defined multiple times in table '%s.%s'.\n\n"++ "Each column can only be defined once per table.",+ colName, schemaName, tableName,+ ),+ StartPosition: &storepb.Position{+ Line: int32(colLine),+ },+ })+ return+ }- table.columns[colName] = &columnDef{- name: colName,- dataType: dataType,- notNull: notNull,- position: position,- line: colDef.GetStart().GetLine(),- }- position++- }+ dt := extractOmniDataType(col.TypeName)- // Collect table constraints- if elem.Tableconstraint() != nil {- c.collectTableConstraint(elem.Tableconstraint(), table)+ // Check for NOT NULL: either via IsNotNull flag or via column constraints+ notNull := col.IsNotNull+ if !notNull && col.Constraints != nil {+ for _, cItem := range col.Constraints.Items {+ if c, ok := cItem.(*ast.Constraint); ok && c.Contype == ast.CONSTR_NOTNULL {+ notNull = true+ break}}}++ table.columns[colName] = &columnDef{+ name: colName,+ dataType: dt,+ notNull: notNull,+ position: *position,+ line: colLine,+ }+ *position++}-func (c *sdlIntegrityChecker) collectTableConstraint(ctx parser.ITableconstraintContext, table *tableDef) {+func processTableConstraint(c *ast.Constraint, fileText string, table *tableDef, st *symbolTable, advices *[]*storepb.Advice) {constraint := &constraintDef{schemaName: table.schemaName,tableName: table.tableName,- line: ctx.GetStart().GetLine(),+ line: locToLine(fileText, c.Loc),+ name: c.Conname,}- // Get constraint name if present- if ctx.CONSTRAINT() != nil {- if ctx.Name() != nil && ctx.Name().Colid() != nil {- constraint.name = ctx.Name().Colid().GetText()- }- }-- if ctx.Constraintelem() == nil {- return- }-- elem := ctx.Constraintelem()-- // Determine constraint type and extract details- if elem.PRIMARY() != nil && elem.KEY() != nil {+ switch c.Contype {+ case ast.CONSTR_PRIMARY:constraint.constraintType = "PRIMARY KEY"table.primaryKeys = append(table.primaryKeys, constraint)- } else if elem.UNIQUE() != nil {+ case ast.CONSTR_UNIQUE:constraint.constraintType = "UNIQUE"- } else if elem.CHECK() != nil {+ case ast.CONSTR_CHECK:constraint.constraintType = "CHECK"- // Extract CHECK expression- if elem.A_expr() != nil {- constraint.checkExpr = elem.A_expr()- }- } else if elem.FOREIGN() != nil && elem.KEY() != nil {+ case ast.CONSTR_FOREIGN:constraint.constraintType = "FOREIGN KEY"// Extract FK columns- if elem.Columnlist() != nil {- for _, col := range elem.Columnlist().AllColumnElem() {- if col.Colid() != nil {- constraint.fkColumns = append(constraint.fkColumns, col.Colid().GetText())+ if c.FkAttrs != nil {+ for _, item := range c.FkAttrs.Items {+ if s, ok := item.(*ast.String); ok {+ constraint.fkColumns = append(constraint.fkColumns, s.Str)}}}- // Extract referenced table and columns- if elem.Qualified_name() != nil {- constraint.fkReferencedSchema = extractSchemaName(elem.Qualified_name())- constraint.fkReferencedTable = extractTableName(elem.Qualified_name())+ // Extract referenced table+ if c.Pktable != nil {+ constraint.fkReferencedTable = c.Pktable.Relname+ constraint.fkReferencedSchema = c.Pktable.Schemanameif constraint.fkReferencedSchema == "" {constraint.fkReferencedSchema = "public"}}- if elem.Opt_column_list() != nil && elem.Opt_column_list().Columnlist() != nil {- for _, col := range elem.Opt_column_list().Columnlist().AllColumnElem() {- if col.Colid() != nil {- constraint.fkReferencedColumns = append(constraint.fkReferencedColumns, col.Colid().GetText())+ // Extract referenced columns+ if c.PkAttrs != nil {+ for _, item := range c.PkAttrs.Items {+ if s, ok := item.(*ast.String); ok {+ constraint.fkReferencedColumns = append(constraint.fkReferencedColumns, s.Str)}}}+ default:+ return}// Check for duplicate constraint name- // PRIMARY KEY and UNIQUE: must be unique within schema- // CHECK and FOREIGN KEY: must be unique within tableif constraint.name != "" {- if !c.symbolTable.addConstraintName(table.schemaName, table.tableName, constraint.name, constraint) {+ if !st.addConstraintName(table.schemaName, table.tableName, constraint.name, constraint) {isPKOrUK := constraint.constraintType == "PRIMARY KEY" || constraint.constraintType == "UNIQUE"var existing *constraintDefvar scope stringvar scopeDetail stringif isPKOrUK {- existing = c.symbolTable.schemaConstraintNames[table.schemaName][constraint.name]+ existing = st.schemaConstraintNames[table.schemaName][constraint.name]scope = "schema"scopeDetail = "PostgreSQL requires PRIMARY KEY and UNIQUE constraint names to be unique within a schema."} else {tableKey := qualifiedName(table.schemaName, table.tableName)- existing = c.symbolTable.tableConstraintNames[tableKey][constraint.name]+ existing = st.tableConstraintNames[tableKey][constraint.name]scope = "table"scopeDetail = "PostgreSQL requires CHECK and FOREIGN KEY constraint names to be unique within a table."}- c.adviceList = append(c.adviceList, &storepb.Advice{+ *advices = append(*advices, &storepb.Advice{Status: storepb.Advice_ERROR,Code: code.SDLDuplicateConstraintName.Int32(),Title: "Duplicate constraint name",@@ -853,76 +865,14 @@ func (c *sdlIntegrityChecker) collectTableConstraint(ctx parser.ITableconstrainttable.constraints = append(table.constraints, constraint)}-func (*sdlIntegrityChecker) extractDataType(typeCtx parser.ITypenameContext) *dataType {- if typeCtx == nil {- return &dataType{baseType: "UNKNOWN"}- }-- dt := &dataType{}-- // Use GetText() to extract the full type string- typeText := strings.ToUpper(typeCtx.GetText())-- // Parse the type to separate base type from modifiers- // e.g., "VARCHAR(100)" -> baseType="VARCHAR", length=100- // e.g., "NUMERIC(10,2)" -> baseType="NUMERIC", precision=10, scale=2-- // Find opening parenthesis- parenIdx := strings.Index(typeText, "(")- if parenIdx == -1 {- // No modifiers, just the base type- dt.baseType = typeText- } else {- // Extract base type- dt.baseType = typeText[:parenIdx]-- // Extract modifiers (numbers inside parentheses)- modifiers := typeText[parenIdx+1:]- if closeIdx := strings.Index(modifiers, ")"); closeIdx != -1 {- modifiers = modifiers[:closeIdx]- }-- // Split by comma for precision/scale- parts := strings.Split(modifiers, ",")- if len(parts) >= 1 {- if length, err := strconv.Atoi(strings.TrimSpace(parts[0])); err == nil {- dt.length = length- dt.precision = length // For NUMERIC types, this is precision- }- }- if len(parts) >= 2 {- if scale, err := strconv.Atoi(strings.TrimSpace(parts[1])); err == nil {- dt.scale = scale- }- }- }-- // Check for array notation [] and strip it from base type- if strings.HasSuffix(typeText, "[]") {- dt.baseType = strings.TrimSuffix(dt.baseType, "[]")- }-- return dt-}--func (c *sdlIntegrityChecker) EnterIndexstmt(ctx *parser.IndexstmtContext) {- if !isTopLevel(ctx.GetParent()) {- return- }-- // Get index name- var indexName string- if ctx.Name() != nil && ctx.Name().Colid() != nil {- indexName = ctx.Name().Colid().GetText()- }+func processIndexStmt(n *ast.IndexStmt, fileText string, st *symbolTable, advices *[]*storepb.Advice) {+ indexName := n.Idxname- // Get table name and schemavar schemaName, tableName string- if ctx.Relation_expr() != nil && ctx.Relation_expr().Qualified_name() != nil {- schemaName = extractSchemaName(ctx.Relation_expr().Qualified_name())- tableName = extractTableName(ctx.Relation_expr().Qualified_name())+ if n.Relation != nil {+ schemaName = n.Relation.Schemaname+ tableName = n.Relation.Relname}-if schemaName == "" {schemaName = "public"}@@ -931,12 +881,12 @@ func (c *sdlIntegrityChecker) EnterIndexstmt(ctx *parser.IndexstmtContext) {return // Unnamed index check is handled by sdl_check.go}- line := ctx.GetStart().GetLine()+ line := locToLine(fileText, n.Loc)// Check for duplicate index namekey := qualifiedName(schemaName, indexName)- if existing, exists := c.symbolTable.indexes[key]; exists {- c.adviceList = append(c.adviceList, &storepb.Advice{+ if existing, exists := st.indexes[key]; exists {+ *advices = append(*advices, &storepb.Advice{Status: storepb.Advice_ERROR,Code: code.SDLDuplicateIndexName.Int32(),Title: "Duplicate index name",@@ -954,33 +904,115 @@ func (c *sdlIntegrityChecker) EnterIndexstmt(ctx *parser.IndexstmtContext) {return}- c.symbolTable.addIndex(schemaName, indexName, tableName, line)+ st.addIndex(schemaName, indexName, tableName, line)}-func (c *sdlIntegrityChecker) EnterViewstmt(ctx *parser.ViewstmtContext) {- if !isTopLevel(ctx.GetParent()) {+func processViewStmt(n *ast.ViewStmt, fileText string, st *symbolTable) {+ if n.View == nil {return}- var viewName, schemaName string- if ctx.Qualified_name() != nil {- schemaName = extractSchemaName(ctx.Qualified_name())- viewName = extractTableName(ctx.Qualified_name())- }-+ viewName := n.View.Relname+ schemaName := n.View.Schemanameif schemaName == "" {schemaName = "public"}- line := ctx.GetStart().GetLine()+ line := locToLine(fileText, n.Loc)++ st.addView(schemaName, viewName, n.Query, line)+}++// extractOmniDataType extracts data type information from an omni TypeName.+func extractOmniDataType(tn *ast.TypeName) *dataType {+ if tn == nil {+ return &dataType{baseType: "UNKNOWN"}+ }++ dt := &dataType{}++ // Build the type name from Names list+ typeName := omniTypeNameStr(tn)+ dt.baseType = strings.ToUpper(typeName)++ // Extract length/precision from Typmods+ if tn.Typmods != nil {+ for i, item := range tn.Typmods.Items {+ val := extractIntValue(item)+ if val < 0 {+ continue+ }+ if i == 0 {+ dt.length = val+ dt.precision = val+ }+ if i == 1 {+ dt.scale = val+ }+ }+ }++ // Check for array type+ if tn.ArrayBounds != nil && len(tn.ArrayBounds.Items) > 0 {+ dt.baseType = strings.TrimSuffix(dt.baseType, "[]")+ }++ return dt+}++// extractIntValue extracts an integer value from an AST node.+func extractIntValue(item ast.Node) int {+ switch v := item.(type) {+ case *ast.Integer:+ return int(v.Ival)+ case *ast.A_Const:+ if iv, ok := v.Val.(*ast.Integer); ok {+ return int(iv.Ival)+ }+ default:+ }+ return -1+}++// omniTypeNameStr builds a type name string from a TypeName node.+func omniTypeNameStr(tn *ast.TypeName) string {+ if tn == nil || tn.Names == nil {+ return "UNKNOWN"+ }++ var parts []string+ for _, item := range tn.Names.Items {+ if s, ok := item.(*ast.String); ok {+ parts = append(parts, s.Str)+ }+ }++ if len(parts) == 0 {+ return "UNKNOWN"+ }++ // The Names list typically contains [catalog, typename] e.g. ["pg_catalog", "int4"]+ // We want just the last part (the actual type name).+ typeName := parts[len(parts)-1]++ // Map common internal type names to SQL type names+ typeMap := map[string]string{+ "int2": "SMALLINT",+ "int4": "INTEGER",+ "int8": "BIGINT",+ "float4": "REAL",+ "float8": "DOUBLE PRECISION",+ "bool": "BOOLEAN",+ "varchar": "VARCHAR",+ "bpchar": "CHARACTER",+ "numeric": "NUMERIC",+ }- // Get the SELECT statement- var selectStmt parser.ISelectstmtContext- if ctx.Selectstmt() != nil {- selectStmt = ctx.Selectstmt()+ if mapped, ok := typeMap[typeName]; ok {+ return mapped}- c.symbolTable.addView(schemaName, viewName, selectStmt, line)+ return typeName}// Pass 2: Validate references@@ -1023,116 +1055,69 @@ type tableReference struct {tableName string}-// getViewDependenciesFromAST extracts view dependencies using ExtractAccessTables-// This is the same approach used in generate_migration.go for consistent behavior-func (c *sdlIntegrityChecker) getViewDependenciesFromAST(selectStmt parser.ISelectstmtContext, schemaName string) []tableReference {- if selectStmt == nil {- return []tableReference{}+// getViewDependencies extracts view dependencies by walking the AST for RangeVar references.+func getViewDependencies(query ast.Node, defaultSchema string) []tableReference {+ if query == nil {+ return nil}- // Extract CTE names from WITH clause first- cteNames := c.extractCTENames(selectStmt)+ cteNames := extractCTENames(query)+ var refs []tableReference+ seen := make(map[string]bool)- // Get the SELECT statement text from the AST- var selectStatement string- if tokenStream := selectStmt.GetParser().GetTokenStream(); tokenStream != nil {- start := selectStmt.GetStart()- stop := selectStmt.GetStop()- if start != nil && stop != nil {- selectStatement = tokenStream.GetTextFromTokens(start, stop)+ ast.Inspect(query, func(n ast.Node) bool {+ rv, ok := n.(*ast.RangeVar)+ if !ok {+ return true+ }+ if rv.Relname == "" {+ return true}- }-- if selectStatement == "" {- return []tableReference{}- }-- // Use ExtractAccessTables to get all table/view references- accessTables, err := pgparser.ExtractAccessTables(selectStatement, pgparser.ExtractAccessTablesOption{- DefaultDatabase: "",- DefaultSchema: schemaName,- SkipMetadataValidation: true,- })- if err != nil {- return []tableReference{}- }-- // Convert to tableReference and deduplicate, filtering out CTEs- refMap := make(map[string]tableReference)- for _, resource := range accessTables {// Skip CTE references- if cteNames[resource.Table] {- continue+ if cteNames[rv.Relname] {+ return true}- resourceSchema := resource.Schema- if resourceSchema == "" {- resourceSchema = schemaName+ schema := rv.Schemaname+ if schema == "" {+ schema = defaultSchema}-- key := qualifiedName(resourceSchema, resource.Table)- if _, exists := refMap[key]; !exists {- refMap[key] = tableReference{- schemaName: resourceSchema,- tableName: resource.Table,- }+ key := schema + "." + rv.Relname+ if !seen[key] {+ seen[key] = true+ refs = append(refs, tableReference{schemaName: schema, tableName: rv.Relname})}- }-- refs := make([]tableReference, 0, len(refMap))- for _, ref := range refMap {- refs = append(refs, ref)- }+ return true+ })return refs}-// extractCTENames extracts all CTE names from a SELECT statement's WITH clause-func (*sdlIntegrityChecker) extractCTENames(selectStmt parser.ISelectstmtContext) map[string]bool {+// extractCTENames extracts all CTE names from a query's WITH clause.+func extractCTENames(query ast.Node) map[string]bool {cteNames := make(map[string]bool)-- if selectStmt == nil {+ sel, ok := query.(*ast.SelectStmt)+ if !ok || sel == nil || sel.WithClause == nil || sel.WithClause.Ctes == nil {return cteNames}-- // Get the select_no_parens from selectstmt- selectNoParens := selectStmt.Select_no_parens()- if selectNoParens == nil {- return cteNames- }-- // Get the WITH clause- withClause := selectNoParens.With_clause()- if withClause == nil {- return cteNames- }-- // Extract CTE names from cte_list- cteList := withClause.Cte_list()- if cteList == nil {- return cteNames- }-- for _, cte := range cteList.AllCommon_table_expr() {- if cte.Name() != nil {- cteName := pgparser.NormalizePostgreSQLName(cte.Name())- cteNames[cteName] = true+ for _, item := range sel.WithClause.Ctes.Items {+ if cte, ok := item.(*ast.CommonTableExpr); ok {+ cteNames[cte.Ctename] = true}}-return cteNames}// Helper functions-func (*sdlIntegrityChecker) getConstraintName(constraint *constraintDef) string {+func getConstraintName(constraint *constraintDef) string {if constraint.name != "" {return constraint.name}return "<unnamed>"}-func (*sdlIntegrityChecker) getColumnList(table *tableDef) string {+func getColumnList(table *tableDef) string {cols := make([]string, 0, len(table.columns))for colName := range table.columns {cols = append(cols, colName)diff --git a/backend/plugin/advisor/pg/utils.go b/backend/plugin/advisor/pg/utils.goindex 52c6bade14..00715f47da 100644--- a/backend/plugin/advisor/pg/utils.go+++ b/backend/plugin/advisor/pg/utils.go@@ -5,123 +5,9 @@ import ("strconv""strings"- "github.com/antlr4-go/antlr/v4"- parser "github.com/bytebase/parser/postgresql""github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/plugin/parser/pg")-// appendSessionPreExecutionStatements tracks top-level SET statements that affect-// how subsequent DML statements should be planned/executed in the same script.-func appendSessionPreExecutionStatements(preExecutions []string, tokens *antlr.CommonTokenStream, ctx *parser.VariablesetstmtContext) []string {- if ctx == nil {- return preExecutions- }-- if !isRoleOrSearchPathSetStatement(ctx) {- return preExecutions- }-- return append(preExecutions, getTextFromTokens(tokens, ctx))-}--func isRoleOrSearchPathSetStatement(ctx *parser.VariablesetstmtContext) bool {- if ctx == nil {- return false- }-- setRest := ctx.Set_rest()- if setRest == nil {- return false- }- setRestMore := setRest.Set_rest_more()- if setRestMore == nil {- return false- }-- // Covers SET ROLE / SET SESSION ROLE syntax.- if setRestMore.ROLE() != nil {- return true- }-- genericSet := setRestMore.Generic_set()- if genericSet == nil {- return false- }- varName := genericSet.Var_name()- if varName == nil || len(varName.AllColid()) != 1 {- return false- }-- name := pg.NormalizePostgreSQLColid(varName.Colid(0))- return strings.EqualFold(name, "role") || strings.EqualFold(name, "search_path")-}--// isTopLevel checks if the context is at the top level of the parse tree.-// Top level contexts are: RootContext, StmtblockContext, StmtmultiContext, or StmtContext.-func isTopLevel(ctx antlr.Tree) bool {- if ctx == nil {- return true- }-- switch ctx := ctx.(type) {- case *parser.RootContext, *parser.StmtblockContext:- return true- case *parser.StmtmultiContext, *parser.StmtContext:- return isTopLevel(ctx.GetParent())- default:- return false- }-}--// getTextFromTokens extracts the original text for a rule context from the token stream.-// Uses GetTextFromRuleContext to include hidden channel tokens (whitespace, comments).-// Returns clean text without leading/trailing whitespace.-func getTextFromTokens(tokens *antlr.CommonTokenStream, ctx antlr.ParserRuleContext) string {- if tokens == nil || ctx == nil {- return ""- }- text := tokens.GetTextFromRuleContext(ctx)- return strings.TrimSpace(text)-}--// extractTableName extracts the table name (last component) from a qualified name.-// Handles both "schema.table" and "table" formats.-func extractTableName(ctx parser.IQualified_nameContext) string {- parts := pg.NormalizePostgreSQLQualifiedName(ctx)- if len(parts) == 0 {- return ""- }- return parts[len(parts)-1]-}--// extractSchemaName extracts the schema name (first component) from a qualified name.-// Returns empty string if only table name is provided (implying default schema).-func extractSchemaName(ctx parser.IQualified_nameContext) string {- parts := pg.NormalizePostgreSQLQualifiedName(ctx)- if len(parts) <= 1 {- return ""- }- return parts[0]-}--// nolint:unused-// extractStringConstant extracts a string value from an Sconst context.-// Removes surrounding quotes and handles basic escape sequences.-func extractStringConstant(ctx parser.ISconstContext) string {- if ctx == nil {- return ""- }-- text := ctx.GetText()- // Remove surrounding single quotes- if len(text) >= 2 && text[0] == '\'' && text[len(text)-1] == '\'' {- return text[1 : len(text)-1]- }- return text-}-// getTemplateRegexp generates a regex pattern by replacing tokens in the template with actual values.// Used by naming convention advisors to dynamically build patterns based on metadata.func getTemplateRegexp(template string, templateList []string, tokens map[string]string) (*regexp.Regexp, error) {diff --git a/backend/plugin/advisor/pg/utils_test.go b/backend/plugin/advisor/pg/utils_test.goindex 8581044f42..9cb7a54b02 100644--- a/backend/plugin/advisor/pg/utils_test.go+++ b/backend/plugin/advisor/pg/utils_test.go@@ -1,27 +1,18 @@package pgimport (+ "strings""testing"- "github.com/antlr4-go/antlr/v4"- pgparser "github.com/bytebase/parser/postgresql"+ "github.com/bytebase/omni/pg/ast""github.com/stretchr/testify/require"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/parser/base"+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg")-type sessionPreExecutionCollector struct {- *pgparser.BasePostgreSQLParserListener- tokens *antlr.CommonTokenStream- preExecutions []string-}--func (l *sessionPreExecutionCollector) EnterVariablesetstmt(ctx *pgparser.VariablesetstmtContext) {- l.preExecutions = appendSessionPreExecutionStatements(l.preExecutions, l.tokens, ctx)-}--func TestAppendSessionPreExecutionStatements(t *testing.T) {+func TestOmniIsRoleOrSearchPathSet(t *testing.T) {tests := []struct {name stringsql string@@ -59,13 +50,15 @@ func TestAppendSessionPreExecutionStatements(t *testing.T) {if stmt.AST == nil {continue}-- antlrAST, ok := base.GetANTLRAST(stmt.AST)- require.True(t, ok)-- collector := &sessionPreExecutionCollector{tokens: antlrAST.Tokens}- antlr.ParseTreeWalkerDefault.Walk(collector, antlrAST.Tree)- preExecutions = append(preExecutions, collector.preExecutions...)+ node, ok := pgparser.GetOmniNode(stmt.AST)+ if !ok {+ continue+ }+ if vs, ok := node.(*ast.VariableSetStmt); ok {+ if omniIsRoleOrSearchPathSet(vs) {+ preExecutions = append(preExecutions, strings.TrimRight(strings.TrimSpace(stmt.Text), ";"))+ }+ }}require.Equal(t, tc.want, preExecutions)