Skip to content

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

chore: add integrity checks for pg sdl (#17974)

rebelice363d4568170a
diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.go
index 395cc408c8..e41305c3d2 100644
--- a/backend/api/v1/release_service_check.go
+++ b/backend/api/v1/release_service_check.go
@@ -381,6 +381,20 @@ func (s *ReleaseService) checkReleaseDeclarative(ctx context.Context, files []*v
}
}
+ // Perform SDL integrity checks for PostgreSQL (handles cross-file validation)
+ var sdlIntegrityAdvices map[string][]*storepb.Advice
+ if engine == storepb.Engine_POSTGRES {
+ fileContents := make(map[string]string)
+ for _, file := range files {
+ fileContents[file.Path] = string(file.Statement)
+ }
+ var err error
+ sdlIntegrityAdvices, err = advisorpg.CheckSDLIntegrity(fileContents)
+ if err != nil {
+ return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to check SDL integrity"))
+ }
+ }
+
// Perform syntax check and statement type check for declarative files
for _, file := range files {
checkResult := &v1pb.CheckReleaseResponse_CheckResult{
@@ -441,18 +455,10 @@ func (s *ReleaseService) checkReleaseDeclarative(ctx context.Context, files []*v
}
}
- // Run SDL-specific checks (only for PostgreSQL)
+ // Add SDL integrity check results for this file (PostgreSQL only)
if engine == storepb.Engine_POSTGRES && len(checkResult.Advices) == 0 {
- sdlAdvices, err := s.runSDLChecksForPostgreSQL(ctx, statement)
- if err != nil {
- checkResult.Advices = append(checkResult.Advices, &v1pb.Advice{
- Status: v1pb.Advice_ERROR,
- Code: advisor.Internal.Int32(),
- Title: "Failed to run SDL checks",
- Content: err.Error(),
- })
- } else {
- for _, advice := range sdlAdvices {
+ if advices, exists := sdlIntegrityAdvices[file.Path]; exists {
+ for _, advice := range advices {
checkResult.Advices = append(checkResult.Advices, convertToV1Advice(advice))
}
}
@@ -668,12 +674,3 @@ func getStatementTypesWithPositionsForEngine(engine storepb.Engine, asts any) ([
return []statementTypeWithPosition{}, nil
}
}
-
-// runSDLChecksForPostgreSQL runs SDL-specific checks for PostgreSQL declarative files.
-// It checks for:
-// 1. Column-level constraints (not allowed, only table-level)
-// 2. Table constraints without explicit names
-// 3. Objects without explicit schema names
-func (*ReleaseService) runSDLChecksForPostgreSQL(_ context.Context, statement string) ([]*storepb.Advice, error) {
- return advisorpg.CheckSDL(statement)
-}
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index 931f44bbca..6ce40cc60a 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -68,6 +68,17 @@ const (
SDLRequireConstraintName Code = 241
SDLRequireSchemaName Code = 242
SDLRequireIndexName Code = 243
+ SDLForeignKeyTableNotFound Code = 244
+ SDLForeignKeyColumnNotFound Code = 245
+ SDLForeignKeyTypeMismatch Code = 246
+ SDLCheckConstraintInvalidColumn Code = 247
+ SDLCheckConstraintCrossTableReference Code = 248
+ SDLDuplicateTableName Code = 249
+ SDLDuplicateIndexName Code = 250
+ SDLDuplicateConstraintName Code = 251
+ SDLDuplicateColumnName Code = 252
+ SDLMultiplePrimaryKey Code = 253
+ SDLViewDependencyNotFound Code = 254
// 301 ~ 399 naming error code
// 301 table naming advisor error code.
diff --git a/backend/plugin/advisor/pg/sdl_check.go b/backend/plugin/advisor/pg/sdl_check.go
index f7c2287c68..ccde78d1ef 100644
--- a/backend/plugin/advisor/pg/sdl_check.go
+++ b/backend/plugin/advisor/pg/sdl_check.go
@@ -12,25 +12,46 @@ import (
)
// CheckSDL performs SDL-specific checks on PostgreSQL declarative schema files.
-// It checks for:
+// It runs both style/convention checks and integrity checks:
+//
+// Style Checks (sdl_check.go):
// 1. Disallowed column-level constraints (PRIMARY KEY, UNIQUE, CHECK, FOREIGN KEY must be table-level; NOT NULL, DEFAULT, GENERATED are allowed)
// 2. Table constraints without explicit names
// 3. Objects without explicit schema names (tables, indexes, views, sequences, functions)
// 4. Foreign key references without explicit schema names
// 5. Indexes without explicit names (unnamed indexes are not allowed)
+//
+// Integrity Checks (sdl_integrity_check.go):
+// 1. Foreign key references to non-existent tables/columns
+// 2. Foreign key type mismatches
+// 3. CHECK constraints referencing non-existent columns
+// 4. CHECK constraints referencing other tables (not allowed)
+// 5. Duplicate object names (tables, indexes, constraints, columns)
+// 6. Multiple primary keys on same table
+// 7. View dependencies on non-existent tables
func CheckSDL(statement string) ([]*storepb.Advice, error) {
tree, err := pgparser.ParsePostgreSQL(statement)
if err != nil {
return nil, err
}
- checker := &sdlChecker{
+ var allAdvices []*storepb.Advice
+
+ // Run style/convention checks
+ styleChecker := &sdlChecker{
BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
}
+ antlr.ParseTreeWalkerDefault.Walk(styleChecker, tree.Tree)
+ allAdvices = append(allAdvices, styleChecker.adviceList...)
- antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+ // Run integrity checks
+ integrityAdvices, err := checkSingleStatement(statement)
+ if err != nil {
+ return allAdvices, err // Return style checks even if integrity check fails
+ }
+ allAdvices = append(allAdvices, integrityAdvices...)
- return checker.adviceList, nil
+ return allAdvices, nil
}
type sdlChecker struct {
diff --git a/backend/plugin/advisor/pg/sdl_check_test.go b/backend/plugin/advisor/pg/sdl_check_test.go
index 7ffbc349a8..76c2a122d4 100644
--- a/backend/plugin/advisor/pg/sdl_check_test.go
+++ b/backend/plugin/advisor/pg/sdl_check_test.go
@@ -124,7 +124,11 @@ func TestCheckSDL(t *testing.T) {
},
{
name: "Error - Table-level FOREIGN KEY without name",
- statement: `CREATE TABLE public.orders (
+ statement: `CREATE TABLE public.users (
+ id INTEGER,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+ );
+ CREATE TABLE public.orders (
id INTEGER,
user_id INTEGER,
FOREIGN KEY (user_id) REFERENCES public.users(id)
@@ -232,14 +236,16 @@ func TestCheckSDL(t *testing.T) {
wantCodes: []advisor.Code{advisor.SDLRequireIndexName},
},
{
- name: "Valid SDL - CREATE VIEW with schema name",
- statement: `CREATE VIEW public.active_users AS SELECT * FROM public.users WHERE active = true;`,
+ name: "Valid SDL - CREATE VIEW with schema name",
+ statement: `CREATE TABLE public.users (id INTEGER, active BOOLEAN, CONSTRAINT pk_users PRIMARY KEY (id));
+CREATE VIEW public.active_users AS SELECT * FROM public.users WHERE active = true;`,
wantCount: 0,
wantCodes: nil,
},
{
- name: "Error - CREATE VIEW without schema name",
- statement: `CREATE VIEW active_users AS SELECT * FROM public.users WHERE active = true;`,
+ name: "Error - CREATE VIEW without schema name",
+ statement: `CREATE TABLE public.users (id INTEGER, active BOOLEAN, CONSTRAINT pk_users PRIMARY KEY (id));
+CREATE VIEW active_users AS SELECT * FROM public.users WHERE active = true;`,
wantCount: 1,
wantCodes: []advisor.Code{advisor.SDLRequireSchemaName},
},
@@ -322,7 +328,15 @@ func TestCheckSDL(t *testing.T) {
},
{
name: "Valid SDL - Complex table with multiple named constraints",
- statement: `CREATE TABLE public.orders (
+ statement: `CREATE TABLE public.users (
+ id INTEGER,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+ );
+ CREATE TABLE public.products (
+ id INTEGER,
+ CONSTRAINT pk_products PRIMARY KEY (id)
+ );
+ CREATE TABLE public.orders (
id INTEGER,
user_id INTEGER,
product_id INTEGER,
@@ -340,7 +354,11 @@ func TestCheckSDL(t *testing.T) {
},
{
name: "Error - FK reference without schema name (table-level)",
- statement: `CREATE TABLE public.orders (
+ statement: `CREATE TABLE public.users (
+ id INTEGER,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+ );
+ CREATE TABLE public.orders (
id INTEGER,
user_id INTEGER,
CONSTRAINT pk_orders PRIMARY KEY (id),
@@ -363,7 +381,15 @@ func TestCheckSDL(t *testing.T) {
},
{
name: "Error - Multiple FK references without schema",
- statement: `CREATE TABLE public.orders (
+ statement: `CREATE TABLE public.users (
+ id INTEGER,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+ );
+ CREATE TABLE public.products (
+ id INTEGER,
+ CONSTRAINT pk_products PRIMARY KEY (id)
+ );
+ CREATE TABLE public.orders (
id INTEGER,
user_id INTEGER,
product_id INTEGER,
@@ -378,7 +404,15 @@ func TestCheckSDL(t *testing.T) {
},
{
name: "Valid SDL - Mix of FK with and without errors",
- statement: `CREATE TABLE public.orders (
+ statement: `CREATE TABLE public.users (
+ id INTEGER,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+ );
+ CREATE TABLE public.products (
+ id INTEGER,
+ CONSTRAINT pk_products PRIMARY KEY (id)
+ );
+ CREATE TABLE public.orders (
id INTEGER,
user_id INTEGER,
product_id INTEGER,
diff --git a/backend/plugin/advisor/pg/sdl_integrity_check.go b/backend/plugin/advisor/pg/sdl_integrity_check.go
new file mode 100644
index 0000000000..b4b95a6684
--- /dev/null
+++ b/backend/plugin/advisor/pg/sdl_integrity_check.go
@@ -0,0 +1,1142 @@
+package pg
+
+import (
+ "fmt"
+ "strconv"
+ "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"
+ pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
+)
+
+// CheckSDLIntegrity performs comprehensive integrity checks across SDL files.
+// This function always handles multiple files to properly validate cross-file references
+// (foreign keys, views) and detect duplicate definitions across files.
+//
+// Parameters:
+// - files: map[filePath]sqlContent - All SDL files to check together.
+// For single-file checking, use a map with one entry.
+//
+// Returns:
+// - map[filePath][]*storepb.Advice - Per-file advice list
+// - error - Parse or system errors
+func CheckSDLIntegrity(files map[string]string) (map[string][]*storepb.Advice, error) {
+ if len(files) == 0 {
+ return make(map[string][]*storepb.Advice), nil
+ }
+
+ // Parse each file and build individual symbol tables
+ fileCheckers := make([]*fileSymbolTable, 0, len(files))
+ for filePath, statement := range files {
+ tree, err := pgparser.ParsePostgreSQL(statement)
+ if err != nil {
+ // Return parse error for this file
+ return map[string][]*storepb.Advice{
+ filePath: {{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.StatementSyntaxError.Int32(),
+ Title: "SQL syntax error",
+ Content: fmt.Sprintf("Failed to parse SQL in file '%s': %v", filePath, err),
+ }},
+ }, nil
+ }
+
+ checker := &sdlIntegrityChecker{
+ BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
+ symbolTable: newSymbolTable(),
+ }
+
+ // Build symbol table for this file
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+
+ fileCheckers = append(fileCheckers, &fileSymbolTable{
+ filePath: filePath,
+ checker: checker,
+ adviceList: make([]*storepb.Advice, 0),
+ })
+ }
+
+ // Merge all symbol tables
+ merged := mergeSymbolTables(fileCheckers)
+
+ // Detect cross-file duplicates
+ crossFileDuplicates := detectCrossFileDuplicates(fileCheckers)
+ for filePath, advices := range crossFileDuplicates {
+ for _, fc := range fileCheckers {
+ if fc.filePath == filePath {
+ fc.adviceList = append(fc.adviceList, advices...)
+ break
+ }
+ }
+ }
+
+ // Validate references with merged symbol table
+ for _, fc := range fileCheckers {
+ fc.checker.validateReferencesWithMergedTable(merged)
+ fc.adviceList = append(fc.adviceList, fc.checker.adviceList...)
+ }
+
+ // Aggregate results per file
+ results := make(map[string][]*storepb.Advice)
+ for _, fc := range fileCheckers {
+ results[fc.filePath] = fc.adviceList
+ }
+
+ return results, nil
+}
+
+// checkSingleStatement is a helper for backward compatibility with single-statement checks.
+// It wraps the statement in a single-file map and extracts the results.
+func checkSingleStatement(statement string) ([]*storepb.Advice, error) {
+ const defaultFileName = "statement.sql"
+ results, err := CheckSDLIntegrity(map[string]string{
+ defaultFileName: statement,
+ })
+ if err != nil {
+ return nil, err
+ }
+ return results[defaultFileName], nil
+}
+
+// fileSymbolTable tracks the symbol table and advices for a single file
+type fileSymbolTable struct {
+ filePath string
+ checker *sdlIntegrityChecker
+ adviceList []*storepb.Advice
+}
+
+// mergedSymbolTable provides a global view of all objects across all files
+type mergedSymbolTable struct {
+ allTables map[string]*tableDef
+ allIndexes map[string]*indexDef
+ allViews map[string]*viewDef
+ allSchemaConstraintNames map[string]map[string]*constraintDef // schema -> constraint_name (for PK/UK)
+ allTableConstraintNames map[string]map[string]*constraintDef // schema.table -> constraint_name (for CHECK/FK)
+ objectSources map[string]string // qualifiedName -> filePath
+}
+
+// mergeSymbolTables merges symbol tables from all files into a unified view
+func mergeSymbolTables(fileCheckers []*fileSymbolTable) *mergedSymbolTable {
+ merged := &mergedSymbolTable{
+ allTables: make(map[string]*tableDef),
+ allIndexes: make(map[string]*indexDef),
+ allViews: make(map[string]*viewDef),
+ allSchemaConstraintNames: make(map[string]map[string]*constraintDef),
+ allTableConstraintNames: make(map[string]map[string]*constraintDef),
+ objectSources: make(map[string]string),
+ }
+
+ for _, fc := range fileCheckers {
+ // Merge tables
+ for key, table := range fc.checker.symbolTable.tables {
+ if _, exists := merged.allTables[key]; !exists {
+ merged.allTables[key] = table
+ merged.objectSources[key] = fc.filePath
+ }
+ }
+
+ // Merge indexes
+ for key, index := range fc.checker.symbolTable.indexes {
+ if _, exists := merged.allIndexes[key]; !exists {
+ merged.allIndexes[key] = index
+ merged.objectSources["index:"+key] = fc.filePath
+ }
+ }
+
+ // Merge views
+ for key, view := range fc.checker.symbolTable.views {
+ if _, exists := merged.allViews[key]; !exists {
+ merged.allViews[key] = view
+ merged.objectSources["view:"+key] = fc.filePath
+ }
+ }
+
+ // Merge schema-level constraint names (PRIMARY KEY and UNIQUE)
+ for schema, constraints := range fc.checker.symbolTable.schemaConstraintNames {
+ if merged.allSchemaConstraintNames[schema] == nil {
+ merged.allSchemaConstraintNames[schema] = make(map[string]*constraintDef)
+ }
+ for name, constraint := range constraints {
+ if _, exists := merged.allSchemaConstraintNames[schema][name]; !exists {
+ merged.allSchemaConstraintNames[schema][name] = constraint
+ merged.objectSources["constraint:"+schema+"."+name] = fc.filePath
+ }
+ }
+ }
+
+ // Merge table-level constraint names (CHECK and FOREIGN KEY)
+ for tableKey, constraints := range fc.checker.symbolTable.tableConstraintNames {
+ if merged.allTableConstraintNames[tableKey] == nil {
+ merged.allTableConstraintNames[tableKey] = make(map[string]*constraintDef)
+ }
+ for name, constraint := range constraints {
+ if _, exists := merged.allTableConstraintNames[tableKey][name]; !exists {
+ merged.allTableConstraintNames[tableKey][name] = constraint
+ merged.objectSources["constraint:"+tableKey+"."+name] = fc.filePath
+ }
+ }
+ }
+ }
+
+ return merged
+}
+
+// objectFirstSeen tracks where an object was first defined
+type objectFirstSeen struct {
+ filePath string
+ line int
+ obj any // *tableDef, *indexDef, *viewDef, or *constraintDef
+}
+
+// detectCrossFileDuplicates finds objects defined in multiple files
+func detectCrossFileDuplicates(fileCheckers []*fileSymbolTable) map[string][]*storepb.Advice {
+ advices := make(map[string][]*storepb.Advice)
+
+ // Track first occurrence of each object
+ seenTables := make(map[string]*objectFirstSeen)
+ seenIndexes := make(map[string]*objectFirstSeen)
+ seenViews := make(map[string]*objectFirstSeen)
+ seenConstraints := make(map[string]map[string]*objectFirstSeen)
+
+ for _, fc := range fileCheckers {
+ // Check tables
+ for key, table := range fc.checker.symbolTable.tables {
+ if first, exists := seenTables[key]; exists {
+ advice := &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLDuplicateTableName.Int32(),
+ Title: "Duplicate table name across files",
+ Content: fmt.Sprintf(
+ "Table '%s.%s' is defined in multiple SDL files.\n\n"+
+ "First definition: %s (line %d)\n"+
+ "Duplicate definition: %s (line %d)\n\n"+
+ "Each table must be defined in exactly one file in the SDL project.",
+ table.schemaName, table.tableName,
+ first.filePath, first.line,
+ fc.filePath, table.line,
+ ),
+ StartPosition: &storepb.Position{Line: int32(table.line)},
+ }
+ advices[fc.filePath] = append(advices[fc.filePath], advice)
+ } else {
+ seenTables[key] = &objectFirstSeen{
+ filePath: fc.filePath,
+ line: table.line,
+ obj: table,
+ }
+ }
+ }
+
+ // Check indexes
+ for key, index := range fc.checker.symbolTable.indexes {
+ if first, exists := seenIndexes[key]; exists {
+ advice := &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLDuplicateIndexName.Int32(),
+ Title: "Duplicate index name across files",
+ Content: fmt.Sprintf(
+ "Index '%s.%s' is defined in multiple SDL files.\n\n"+
+ "First definition: %s (line %d)\n"+
+ "Duplicate definition: %s (line %d)\n\n"+
+ "Each index must be defined in exactly one file in the SDL project.",
+ index.schemaName, index.indexName,
+ first.filePath, first.line,
+ fc.filePath, index.line,
+ ),
+ StartPosition: &storepb.Position{Line: int32(index.line)},
+ }
+ advices[fc.filePath] = append(advices[fc.filePath], advice)
+ } else {
+ seenIndexes[key] = &objectFirstSeen{
+ filePath: fc.filePath,
+ line: index.line,
+ obj: index,
+ }
+ }
+ }
+
+ // Check views
+ for key, view := range fc.checker.symbolTable.views {
+ if first, exists := seenViews[key]; exists {
+ advice := &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLDuplicateTableName.Int32(),
+ Title: "Duplicate view name across files",
+ Content: fmt.Sprintf(
+ "View '%s.%s' is defined in multiple SDL files.\n\n"+
+ "First definition: %s (line %d)\n"+
+ "Duplicate definition: %s (line %d)\n\n"+
+ "Each view must be defined in exactly one file in the SDL project.",
+ view.schemaName, view.viewName,
+ first.filePath, first.line,
+ fc.filePath, view.line,
+ ),
+ StartPosition: &storepb.Position{Line: int32(view.line)},
+ }
+ advices[fc.filePath] = append(advices[fc.filePath], advice)
+ } else {
+ seenViews[key] = &objectFirstSeen{
+ filePath: fc.filePath,
+ line: view.line,
+ obj: view,
+ }
+ }
+ }
+
+ // Check schema-level constraint names (PRIMARY KEY and UNIQUE)
+ for schema, constraints := range fc.checker.symbolTable.schemaConstraintNames {
+ if seenConstraints[schema] == nil {
+ seenConstraints[schema] = make(map[string]*objectFirstSeen)
+ }
+
+ for name, constraint := range constraints {
+ if first, exists := seenConstraints[schema][name]; exists {
+ advice := &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLDuplicateConstraintName.Int32(),
+ Title: "Duplicate constraint name across files",
+ Content: fmt.Sprintf(
+ "Constraint '%s' in schema '%s' is defined in multiple SDL files.\n\n"+
+ "First definition: %s on table '%s' in %s (line %d)\n"+
+ "Duplicate definition: %s on table '%s' in %s (line %d)\n\n"+
+ "PostgreSQL requires PRIMARY KEY and UNIQUE constraint names to be unique within a schema.\n"+
+ "Each constraint must be defined in exactly one file in the SDL project.",
+ name, schema,
+ first.obj.(*constraintDef).constraintType, first.obj.(*constraintDef).tableName, first.filePath, first.line,
+ constraint.constraintType, constraint.tableName, fc.filePath, constraint.line,
+ ),
+ StartPosition: &storepb.Position{Line: int32(constraint.line)},
+ }
+ advices[fc.filePath] = append(advices[fc.filePath], advice)
+ } else {
+ seenConstraints[schema][name] = &objectFirstSeen{
+ filePath: fc.filePath,
+ line: constraint.line,
+ obj: constraint,
+ }
+ }
+ }
+ }
+
+ // Check table-level constraint names (CHECK and FOREIGN KEY)
+ // These are already scoped to table, so duplicates across files are allowed if on different tables
+ // No cross-file check needed for table-level constraints
+ }
+
+ return advices
+}
+
+// validateReferencesWithMergedTable validates references using the merged symbol table
+func (c *sdlIntegrityChecker) validateReferencesWithMergedTable(merged *mergedSymbolTable) {
+ // Check for multiple primary keys
+ c.checkMultiplePrimaryKeys()
+
+ // Validate foreign key references with merged table
+ c.validateForeignKeysWithMergedTable(merged)
+
+ // Validate view dependencies with merged table
+ c.validateViewDependenciesWithMergedTable(merged)
+}
+
+// validateForeignKeysWithMergedTable validates FK references using merged symbol table
+func (c *sdlIntegrityChecker) validateForeignKeysWithMergedTable(merged *mergedSymbolTable) {
+ for _, table := range c.symbolTable.tables {
+ for _, constraint := range table.constraints {
+ if constraint.constraintType != "FOREIGN KEY" {
+ continue
+ }
+
+ // Look up referenced table in merged symbol table
+ refKey := qualifiedName(constraint.fkReferencedSchema, constraint.fkReferencedTable)
+ refTable := merged.allTables[refKey]
+
+ if refTable == nil {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLForeignKeyTableNotFound.Int32(),
+ Title: "Foreign key references non-existent table",
+ Content: fmt.Sprintf(
+ "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),
+ table.schemaName, table.tableName,
+ constraint.fkReferencedSchema, constraint.fkReferencedTable,
+ formatColumnList(constraint.fkColumns),
+ ),
+ StartPosition: &storepb.Position{Line: int32(constraint.line)},
+ })
+ continue
+ }
+
+ // Validate columns and types
+ if len(constraint.fkColumns) != len(constraint.fkReferencedColumns) {
+ continue
+ }
+
+ for i, fkCol := range constraint.fkColumns {
+ refCol := constraint.fkReferencedColumns[i]
+
+ sourceCol := table.columns[fkCol]
+ if sourceCol == nil {
+ continue
+ }
+
+ targetCol := refTable.columns[refCol]
+ if targetCol == nil {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLForeignKeyColumnNotFound.Int32(),
+ Title: "Foreign key references non-existent column",
+ Content: fmt.Sprintf(
+ "Foreign key constraint '%s' on table '%s.%s' references column '%s' in table '%s.%s', but this column does not exist.\n\n"+
+ "Foreign key definition:\n"+
+ " 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),
+ 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),
+ ),
+ StartPosition: &storepb.Position{Line: int32(constraint.line)},
+ })
+ continue
+ }
+ }
+ }
+ }
+}
+
+// validateViewDependenciesWithMergedTable validates view dependencies using merged symbol table
+func (c *sdlIntegrityChecker) validateViewDependenciesWithMergedTable(merged *mergedSymbolTable) {
+ for _, view := range c.symbolTable.views {
+ if view.selectStmt == nil {
+ continue
+ }
+
+ // Extract dependencies using the same approach as generate_migration
+ dependencies := c.getViewDependenciesFromAST(view.selectStmt, view.schemaName)
+
+ for _, dep := range dependencies {
+ // Skip system schemas
+ if pgparser.IsSystemSchema(dep.schemaName) {
+ continue
+ }
+ // Skip system tables/views by name
+ if pgparser.IsSystemTable(dep.tableName) || pgparser.IsSystemView(dep.tableName) {
+ continue
+ }
+
+ refKey := qualifiedName(dep.schemaName, dep.tableName)
+ // Check if the reference exists in either tables or views
+ if merged.allTables[refKey] == nil && merged.allViews[refKey] == nil {
+ objectType := "table or view"
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLViewDependencyNotFound.Int32(),
+ Title: "View references non-existent table or view",
+ Content: fmt.Sprintf(
+ "View '%s.%s' (line %d) references %s '%s.%s' which does not exist in any SDL file.\n\n"+
+ "Views must reference tables or views that are defined in the SDL project.\n\n"+
+ "Fix: Define %s '%s.%s' in one of the SDL files, or remove the view if the object is external.",
+ view.schemaName, view.viewName, view.line,
+ objectType, dep.schemaName, dep.tableName,
+ objectType, dep.schemaName, dep.tableName,
+ ),
+ StartPosition: &storepb.Position{Line: int32(view.line)},
+ })
+ }
+ }
+ }
+}
+
+// formatColumnList formats a list of columns for display
+func formatColumnList(columns []string) string {
+ if len(columns) == 0 {
+ return "<none>"
+ }
+ result := ""
+ for i, col := range columns {
+ if i > 0 {
+ result += ", "
+ }
+ result += col
+ }
+ return result
+}
+
+// sdlIntegrityChecker performs multi-pass integrity checking
+type sdlIntegrityChecker struct {
+ *parser.BasePostgreSQLParserListener
+ symbolTable *symbolTable
+ adviceList []*storepb.Advice
+}
+
+// symbolTable stores all defined objects for cross-referencing
+type symbolTable struct {
+ // schema.table -> TableDef
+ tables map[string]*tableDef
+ // schema.index -> IndexDef
+ indexes map[string]*indexDef
+ // schema.view -> ViewDef
+ views map[string]*viewDef
+ // Schema-level constraint names (for PRIMARY KEY and UNIQUE constraints)
+ // schema -> constraint_name -> constraintDef
+ schemaConstraintNames map[string]map[string]*constraintDef
+ // Table-level constraint names (for CHECK and FOREIGN KEY constraints)
+ // schema.table -> constraint_name -> constraintDef
+ tableConstraintNames map[string]map[string]*constraintDef
+}
+
+type tableDef struct {
+ schemaName string
+ tableName string
+ columns map[string]*columnDef // column_name -> ColumnDef
+ // Track primary keys to detect duplicates
+ primaryKeys []*constraintDef
+ // Track all constraints for validation
+ constraints []*constraintDef
+ line int
+}
+
+type columnDef struct {
+ name string
+ dataType *dataType
+ notNull bool
+ position int
+ line int
+}
+
+type dataType struct {
+ // Base type name: INTEGER, TEXT, VARCHAR, NUMERIC, TIMESTAMP, etc.
+ baseType string
+ // For VARCHAR(n), CHAR(n)
+ length int
+ // For NUMERIC(p,s)
+ precision int
+ scale int
+}
+
+type indexDef struct {
+ schemaName string
+ indexName string
+ tableName string
+ line int
+}
+
+type viewDef struct {
+ schemaName string
+ viewName string
+ // Store the SELECT statement context for dependency analysis
+ selectStmt parser.ISelectstmtContext
+ line int
+}
+
+type constraintDef struct {
+ name string
+ constraintType string // "PRIMARY KEY", "UNIQUE", "CHECK", "FOREIGN KEY"
+ schemaName string
+ tableName string
+ line int
+ // For FK constraints
+ fkReferencedSchema string
+ fkReferencedTable string
+ fkColumns []string
+ fkReferencedColumns []string
+ // For CHECK constraints
+ checkExpr parser.IA_exprContext
+}
+
+func newSymbolTable() *symbolTable {
+ return &symbolTable{
+ tables: make(map[string]*tableDef),
+ indexes: make(map[string]*indexDef),
+ views: make(map[string]*viewDef),
+ schemaConstraintNames: make(map[string]map[string]*constraintDef),
+ tableConstraintNames: make(map[string]map[string]*constraintDef),
+ }
+}
+
+func (st *symbolTable) addTable(schema, table string, line int) *tableDef {
+ key := qualifiedName(schema, table)
+ if _, exists := st.tables[key]; !exists {
+ st.tables[key] = &tableDef{
+ schemaName: schema,
+ tableName: table,
+ columns: make(map[string]*columnDef),
+ primaryKeys: make([]*constraintDef, 0),
+ constraints: make([]*constraintDef, 0),
+ line: line,
+ }
+ }
+ return st.tables[key]
+}
+
+func (st *symbolTable) getTable(schema, table string) *tableDef {
+ return st.tables[qualifiedName(schema, table)]
+}
+
+func (st *symbolTable) addIndex(schema, index, table string, line int) {
+ key := qualifiedName(schema, index)
+ st.indexes[key] = &indexDef{
+ schemaName: schema,
+ indexName: index,
+ tableName: table,
+ line: line,
+ }
+}
+
+func (st *symbolTable) addView(schema, view string, selectStmt parser.ISelectstmtContext, line int) {
+ key := qualifiedName(schema, view)
+ st.views[key] = &viewDef{
+ schemaName: schema,
+ viewName: view,
+ selectStmt: selectStmt,
+ line: line,
+ }
+}
+
+func (st *symbolTable) addConstraintName(schema, table, constraintName string, constraint *constraintDef) bool {
+ // PRIMARY KEY and UNIQUE constraints must be unique within schema
+ // CHECK and FOREIGN KEY constraints must be unique within table
+ isPKOrUK := constraint.constraintType == "PRIMARY KEY" || constraint.constraintType == "UNIQUE"
+
+ if isPKOrUK {
+ // Schema-level uniqueness for PK/UK
+ if st.schemaConstraintNames[schema] == nil {
+ st.schemaConstraintNames[schema] = make(map[string]*constraintDef)
+ }
+ if _, exists := st.schemaConstraintNames[schema][constraintName]; exists {
+ return false // Duplicate
+ }
+ st.schemaConstraintNames[schema][constraintName] = constraint
+ } else {
+ // Table-level uniqueness for CHECK/FK
+ tableKey := qualifiedName(schema, table)
+ if st.tableConstraintNames[tableKey] == nil {
+ st.tableConstraintNames[tableKey] = make(map[string]*constraintDef)
+ }
+ if _, exists := st.tableConstraintNames[tableKey][constraintName]; exists {
+ return false // Duplicate
+ }
+ st.tableConstraintNames[tableKey][constraintName] = constraint
+ }
+ return true
+}
+
+func qualifiedName(schema, name string) string {
+ if schema == "" {
+ schema = "public"
+ }
+ return schema + "." + name
+}
+
+// Pass 1: Collect definitions
+
+func (c *sdlIntegrityChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ var tableName, schemaName string
+ allQualifiedNames := ctx.AllQualified_name()
+ if len(allQualifiedNames) > 0 {
+ schemaName = extractSchemaName(allQualifiedNames[0])
+ tableName = extractTableName(allQualifiedNames[0])
+ }
+
+ if schemaName == "" {
+ schemaName = "public"
+ }
+
+ line := ctx.GetStart().GetLine()
+
+ // Check for duplicate table name
+ if existing := c.symbolTable.getTable(schemaName, tableName); existing != nil {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLDuplicateTableName.Int32(),
+ Title: "Duplicate table name",
+ Content: fmt.Sprintf(
+ "Table '%s.%s' is defined multiple times in the SDL.\n\n"+
+ "First definition at line %d\n"+
+ "Duplicate definition at line %d\n\n"+
+ "Each table can only be defined once per schema.",
+ schemaName, tableName, existing.line, line,
+ ),
+ StartPosition: &storepb.Position{
+ Line: int32(line),
+ },
+ })
+ return
+ }
+
+ // Add table to symbol table
+ table := c.symbolTable.addTable(schemaName, tableName, line)
+
+ // Collect column definitions and constraints
+ if ctx.Opttableelementlist() != nil && ctx.Opttableelementlist().Tableelementlist() != nil {
+ allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()
+
+ position := 0
+ for _, elem := range allElements {
+ // Collect column definitions
+ if elem.ColumnDef() != nil {
+ colDef := elem.ColumnDef()
+ colName := colDef.Colid().GetText()
+
+ // Check for duplicate column name
+ if _, exists := table.columns[colName]; exists {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.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
+ }
+
+ // 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
+ }
+ }
+ }
+ }
+
+ table.columns[colName] = &columnDef{
+ name: colName,
+ dataType: dataType,
+ notNull: notNull,
+ position: position,
+ line: colDef.GetStart().GetLine(),
+ }
+ position++
+ }
+
+ // Collect table constraints
+ if elem.Tableconstraint() != nil {
+ c.collectTableConstraint(elem.Tableconstraint(), table)
+ }
+ }
+ }
+}
+
+func (c *sdlIntegrityChecker) collectTableConstraint(ctx parser.ITableconstraintContext, table *tableDef) {
+ constraint := &constraintDef{
+ schemaName: table.schemaName,
+ tableName: table.tableName,
+ line: ctx.GetStart().GetLine(),
+ }
+
+ // 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 {
+ constraint.constraintType = "PRIMARY KEY"
+ table.primaryKeys = append(table.primaryKeys, constraint)
+ } else if elem.UNIQUE() != nil {
+ constraint.constraintType = "UNIQUE"
+ } else if elem.CHECK() != nil {
+ constraint.constraintType = "CHECK"
+ // Extract CHECK expression
+ if elem.A_expr() != nil {
+ constraint.checkExpr = elem.A_expr()
+ }
+ } else if elem.FOREIGN() != nil && elem.KEY() != nil {
+ 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())
+ }
+ }
+ }
+ // Extract referenced table and columns
+ if elem.Qualified_name() != nil {
+ constraint.fkReferencedSchema = extractSchemaName(elem.Qualified_name())
+ constraint.fkReferencedTable = extractTableName(elem.Qualified_name())
+ if 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())
+ }
+ }
+ }
+ }
+
+ // Check for duplicate constraint name
+ // PRIMARY KEY and UNIQUE: must be unique within schema
+ // CHECK and FOREIGN KEY: must be unique within table
+ if constraint.name != "" {
+ if !c.symbolTable.addConstraintName(table.schemaName, table.tableName, constraint.name, constraint) {
+ isPKOrUK := constraint.constraintType == "PRIMARY KEY" || constraint.constraintType == "UNIQUE"
+ var existing *constraintDef
+ var scope string
+ var scopeDetail string
+
+ if isPKOrUK {
+ existing = c.symbolTable.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]
+ scope = "table"
+ scopeDetail = "PostgreSQL requires CHECK and FOREIGN KEY constraint names to be unique within a table."
+ }
+
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLDuplicateConstraintName.Int32(),
+ Title: "Duplicate constraint name",
+ Content: fmt.Sprintf(
+ "Constraint '%s' is defined multiple times in %s '%s'.\n\n"+
+ "First definition: %s on table '%s' (line %d)\n"+
+ "Duplicate definition: %s on table '%s' (line %d)\n\n"+
+ "%s\n"+
+ "Use different names or let PostgreSQL generate unique names automatically.",
+ constraint.name, scope, table.schemaName,
+ existing.constraintType, existing.tableName, existing.line,
+ constraint.constraintType, table.tableName, constraint.line,
+ scopeDetail,
+ ),
+ StartPosition: &storepb.Position{
+ Line: int32(constraint.line),
+ },
+ })
+ }
+ }
+
+ table.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()
+ }
+
+ // Get table name and schema
+ var 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 schemaName == "" {
+ schemaName = "public"
+ }
+
+ if indexName == "" {
+ return // Unnamed index check is handled by sdl_check.go
+ }
+
+ line := ctx.GetStart().GetLine()
+
+ // Check for duplicate index name
+ key := qualifiedName(schemaName, indexName)
+ if existing, exists := c.symbolTable.indexes[key]; exists {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLDuplicateIndexName.Int32(),
+ Title: "Duplicate index name",
+ Content: fmt.Sprintf(
+ "Index '%s.%s' is defined multiple times in the SDL.\n\n"+
+ "First definition at line %d\n"+
+ "Duplicate definition at line %d\n\n"+
+ "Each index name must be unique within a schema.",
+ schemaName, indexName, existing.line, line,
+ ),
+ StartPosition: &storepb.Position{
+ Line: int32(line),
+ },
+ })
+ return
+ }
+
+ c.symbolTable.addIndex(schemaName, indexName, tableName, line)
+}
+
+func (c *sdlIntegrityChecker) EnterViewstmt(ctx *parser.ViewstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ var viewName, schemaName string
+ if ctx.Qualified_name() != nil {
+ schemaName = extractSchemaName(ctx.Qualified_name())
+ viewName = extractTableName(ctx.Qualified_name())
+ }
+
+ if schemaName == "" {
+ schemaName = "public"
+ }
+
+ line := ctx.GetStart().GetLine()
+
+ // Get the SELECT statement
+ var selectStmt parser.ISelectstmtContext
+ if ctx.Selectstmt() != nil {
+ selectStmt = ctx.Selectstmt()
+ }
+
+ c.symbolTable.addView(schemaName, viewName, selectStmt, line)
+}
+
+// Pass 2: Validate references
+
+func (c *sdlIntegrityChecker) checkMultiplePrimaryKeys() {
+ for _, table := range c.symbolTable.tables {
+ if len(table.primaryKeys) > 1 {
+ var pkDescriptions []string
+ for i, pk := range table.primaryKeys {
+ name := pk.name
+ if name == "" {
+ name = "<unnamed>"
+ }
+ pkDescriptions = append(pkDescriptions, fmt.Sprintf(" %d. %s at line %d", i+1, name, pk.line))
+ }
+
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: storepb.Advice_ERROR,
+ Code: advisor.SDLMultiplePrimaryKey.Int32(),
+ Title: "Multiple primary keys defined",
+ Content: fmt.Sprintf(
+ "Table '%s.%s' has multiple PRIMARY KEY constraints defined.\n\n"+
+ "Found %d primary key constraints:\n%s\n\n"+
+ "A table can only have one PRIMARY KEY constraint.\n"+
+ "If you need to enforce uniqueness on multiple column combinations, use UNIQUE constraints instead.",
+ table.schemaName, table.tableName,
+ len(table.primaryKeys),
+ strings.Join(pkDescriptions, "\n"),
+ ),
+ StartPosition: &storepb.Position{
+ Line: int32(table.primaryKeys[1].line), // Point to second PK
+ },
+ })
+ }
+ }
+}
+
+type tableReference struct {
+ schemaName string
+ 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{}
+ }
+
+ // Extract CTE names from WITH clause first
+ cteNames := c.extractCTENames(selectStmt)
+
+ // 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)
+ }
+ }
+
+ 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
+ }
+
+ resourceSchema := resource.Schema
+ if resourceSchema == "" {
+ resourceSchema = schemaName
+ }
+
+ key := qualifiedName(resourceSchema, resource.Table)
+ if _, exists := refMap[key]; !exists {
+ refMap[key] = tableReference{
+ schemaName: resourceSchema,
+ tableName: resource.Table,
+ }
+ }
+ }
+
+ refs := make([]tableReference, 0, len(refMap))
+ for _, ref := range refMap {
+ refs = append(refs, ref)
+ }
+
+ return refs
+}
+
+// extractCTENames extracts all CTE names from a SELECT statement's WITH clause
+func (*sdlIntegrityChecker) extractCTENames(selectStmt parser.ISelectstmtContext) map[string]bool {
+ cteNames := make(map[string]bool)
+
+ if selectStmt == 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
+ }
+ }
+
+ return cteNames
+}
+
+// Helper functions
+
+func (*sdlIntegrityChecker) getConstraintName(constraint *constraintDef) string {
+ if constraint.name != "" {
+ return constraint.name
+ }
+ return "<unnamed>"
+}
+
+func (*sdlIntegrityChecker) getColumnList(table *tableDef) string {
+ cols := make([]string, 0, len(table.columns))
+ for colName := range table.columns {
+ cols = append(cols, colName)
+ }
+ if len(cols) == 0 {
+ return "<no columns>"
+ }
+ return strings.Join(cols, ", ")
+}
diff --git a/backend/plugin/advisor/pg/sdl_integrity_check_multifile_test.go b/backend/plugin/advisor/pg/sdl_integrity_check_multifile_test.go
new file mode 100644
index 0000000000..8c68bc00c3
--- /dev/null
+++ b/backend/plugin/advisor/pg/sdl_integrity_check_multifile_test.go
@@ -0,0 +1,778 @@
+package pg
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+)
+
+func TestCheckSDLIntegrity_ValidCrossFileReferences(t *testing.T) {
+ files := map[string]string{
+ "users.sql": `
+ CREATE TABLE public.users (
+ id SERIAL PRIMARY KEY,
+ name TEXT NOT NULL,
+ email TEXT UNIQUE
+ );
+ `,
+ "orders.sql": `
+ CREATE TABLE public.orders (
+ id SERIAL PRIMARY KEY,
+ user_id INTEGER NOT NULL,
+ total DECIMAL(10,2),
+ CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES public.users(id)
+ );
+ `,
+ "order_items.sql": `
+ CREATE TABLE public.order_items (
+ id SERIAL PRIMARY KEY,
+ order_id INTEGER NOT NULL,
+ product_name TEXT,
+ FOREIGN KEY (order_id) REFERENCES public.orders(id)
+ );
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have no errors - all references are valid
+ for filePath, advices := range results {
+ require.Empty(t, advices, "File %s should have no errors, got: %v", filePath, advices)
+ }
+}
+
+func TestCheckSDLIntegrity_CrossFileViewDependencies(t *testing.T) {
+ files := map[string]string{
+ "tables.sql": `
+ CREATE TABLE public.products (
+ id SERIAL PRIMARY KEY,
+ name TEXT NOT NULL,
+ price DECIMAL(10,2)
+ );
+
+ CREATE TABLE public.sales (
+ id SERIAL PRIMARY KEY,
+ product_id INTEGER REFERENCES public.products(id),
+ quantity INTEGER,
+ sale_date DATE
+ );
+ `,
+ "views.sql": `
+ CREATE VIEW public.product_sales AS
+ SELECT p.name, s.quantity, s.sale_date
+ FROM public.products p
+ JOIN public.sales s ON p.id = s.product_id;
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have no errors - view references tables from other file
+ for filePath, advices := range results {
+ require.Empty(t, advices, "File %s should have no errors, got: %v", filePath, advices)
+ }
+}
+
+func TestCheckSDLIntegrity_DuplicateTableAcrossFiles(t *testing.T) {
+ files := map[string]string{
+ "file1.sql": `
+ CREATE TABLE public.users (
+ id SERIAL PRIMARY KEY,
+ name TEXT
+ );
+ `,
+ "file2.sql": `
+ CREATE TABLE public.users (
+ id INTEGER PRIMARY KEY,
+ email TEXT
+ );
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have exactly one error total (in either file1.sql or file2.sql)
+ totalErrors := len(results["file1.sql"]) + len(results["file2.sql"])
+ require.Equal(t, 1, totalErrors, "Should have exactly 1 error total")
+
+ // Find the error (it will be in one of the files)
+ var advice *storepb.Advice
+ if len(results["file1.sql"]) > 0 {
+ advice = results["file1.sql"][0]
+ } else {
+ advice = results["file2.sql"][0]
+ }
+
+ require.Equal(t, storepb.Advice_ERROR, advice.Status)
+ require.Equal(t, advisor.SDLDuplicateTableName.Int32(), advice.Code)
+ require.Contains(t, advice.Content, "Table 'public.users' is defined in multiple SDL files")
+}
+
+func TestCheckSDLIntegrity_DuplicateIndexAcrossFiles(t *testing.T) {
+ files := map[string]string{
+ "table1.sql": `
+ CREATE TABLE public.employees (
+ id SERIAL PRIMARY KEY,
+ email TEXT NOT NULL
+ );
+ CREATE INDEX idx_employee_email ON public.employees(email);
+ `,
+ "table2.sql": `
+ CREATE TABLE public.customers (
+ id SERIAL PRIMARY KEY,
+ email TEXT NOT NULL
+ );
+ CREATE INDEX idx_employee_email ON public.customers(email);
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have exactly one error total (in either file)
+ totalErrors := len(results["table1.sql"]) + len(results["table2.sql"])
+ require.Equal(t, 1, totalErrors, "Should have exactly 1 error total")
+
+ // Find the error
+ var advice *storepb.Advice
+ if len(results["table1.sql"]) > 0 {
+ advice = results["table1.sql"][0]
+ } else {
+ advice = results["table2.sql"][0]
+ }
+
+ require.Equal(t, storepb.Advice_ERROR, advice.Status)
+ require.Equal(t, advisor.SDLDuplicateIndexName.Int32(), advice.Code)
+ require.Contains(t, advice.Content, "Index 'public.idx_employee_email' is defined in multiple SDL files")
+}
+
+func TestCheckSDLIntegrity_DuplicateConstraintAcrossFiles(t *testing.T) {
+ files := map[string]string{
+ "orders.sql": `
+ CREATE TABLE public.orders (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ CONSTRAINT pk_common PRIMARY KEY (id),
+ CONSTRAINT uk_email UNIQUE (email)
+ );
+ `,
+ "invoices.sql": `
+ CREATE TABLE public.invoices (
+ id INTEGER NOT NULL,
+ code TEXT NOT NULL,
+ CONSTRAINT pk_common PRIMARY KEY (id),
+ CONSTRAINT uk_code UNIQUE (code)
+ );
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have duplicate PRIMARY KEY constraint error (schema-level uniqueness)
+ hasError := false
+ for _, advices := range results {
+ for _, advice := range advices {
+ if advice.Code == advisor.SDLDuplicateConstraintName.Int32() {
+ require.Contains(t, advice.Content, "Constraint 'pk_common' in schema 'public' is defined in multiple SDL files")
+ require.Contains(t, advice.Content, "PostgreSQL requires PRIMARY KEY and UNIQUE constraint names to be unique within a schema")
+ hasError = true
+ }
+ }
+ }
+ require.True(t, hasError, "Should have duplicate constraint error")
+}
+
+func TestCheckSDLIntegrity_SameCheckFKNameAcrossTablesAllowed(t *testing.T) {
+ files := map[string]string{
+ "orders.sql": `
+ CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+ );
+
+ CREATE TABLE public.orders (
+ id INTEGER NOT NULL,
+ user_id INTEGER NOT NULL,
+ amount NUMERIC NOT NULL,
+ CONSTRAINT pk_orders PRIMARY KEY (id),
+ CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES public.users(id),
+ CONSTRAINT chk_positive CHECK (amount > 0)
+ );
+ `,
+ "invoices.sql": `
+ CREATE TABLE public.invoices (
+ id INTEGER NOT NULL,
+ user_id INTEGER NOT NULL,
+ total NUMERIC NOT NULL,
+ CONSTRAINT pk_invoices PRIMARY KEY (id),
+ CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES public.users(id),
+ CONSTRAINT chk_positive CHECK (total > 0)
+ );
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have NO errors - CHECK and FK constraints are table-scoped
+ for filePath, advices := range results {
+ require.Empty(t, advices, "File %s should have no errors, got: %v", filePath, advices)
+ }
+}
+
+func TestCheckSDLIntegrity_ForeignKeyTableNotFound(t *testing.T) {
+ files := map[string]string{
+ "orders.sql": `
+ CREATE TABLE public.orders (
+ id SERIAL PRIMARY KEY,
+ user_id INTEGER NOT NULL,
+ FOREIGN KEY (user_id) REFERENCES public.users(id)
+ );
+ `,
+ "products.sql": `
+ CREATE TABLE public.products (
+ id SERIAL PRIMARY KEY,
+ name TEXT
+ );
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have FK table not found error in orders.sql
+ require.Greater(t, len(results["orders.sql"]), 0, "orders.sql should have errors")
+
+ var foundError bool
+ for _, advice := range results["orders.sql"] {
+ if advice.Code == advisor.SDLForeignKeyTableNotFound.Int32() {
+ require.Contains(t, advice.Content, "references table 'public.users' which does not exist in any SDL file")
+ foundError = true
+ }
+ }
+ require.True(t, foundError, "Should have FK table not found error")
+}
+
+func TestCheckSDLIntegrity_ViewReferencesNonExistentTable(t *testing.T) {
+ files := map[string]string{
+ "products.sql": `
+ CREATE TABLE public.products (
+ id SERIAL PRIMARY KEY,
+ name TEXT
+ );
+ `,
+ "views.sql": `
+ CREATE VIEW public.order_summary AS
+ SELECT o.id, o.total, p.name
+ FROM public.orders o
+ JOIN public.products p ON o.product_id = p.id;
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have view dependency error in views.sql
+ var foundError bool
+ for _, advice := range results["views.sql"] {
+ if advice.Code == advisor.SDLViewDependencyNotFound.Int32() {
+ require.Contains(t, advice.Content, "View 'public.order_summary'")
+ require.Contains(t, advice.Content, "references table or view 'public.orders' which does not exist")
+ foundError = true
+ }
+ }
+ require.True(t, foundError, "Should have view dependency error")
+}
+
+func TestCheckSDLIntegrity_ViewReferencesView(t *testing.T) {
+ files := map[string]string{
+ "tables.sql": `
+ CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ name TEXT NOT NULL,
+ active BOOLEAN NOT NULL DEFAULT true,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+ );
+ `,
+ "views.sql": `
+ -- Base view
+ CREATE VIEW public.active_users AS
+ SELECT id, name FROM public.users WHERE active = true;
+
+ -- View referencing another view (should be valid)
+ CREATE VIEW public.user_summary AS
+ SELECT id, name FROM public.active_users;
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have NO errors - views can reference other views
+ for filePath, advices := range results {
+ require.Empty(t, advices, "File %s should have no errors, got: %v", filePath, advices)
+ }
+}
+
+func TestCheckSDLIntegrity_ViewWithCTE(t *testing.T) {
+ files := map[string]string{
+ "tables.sql": `
+ CREATE TABLE public.orders (
+ id INTEGER NOT NULL,
+ user_id INTEGER NOT NULL,
+ total NUMERIC NOT NULL,
+ CONSTRAINT pk_orders PRIMARY KEY (id)
+ );
+ `,
+ "views.sql": `
+ -- View using CTE
+ CREATE VIEW public.order_summary AS
+ WITH high_value_orders AS (
+ SELECT id, user_id, total
+ FROM public.orders
+ WHERE total > 1000
+ )
+ SELECT user_id, COUNT(*) as order_count, SUM(total) as total_amount
+ FROM high_value_orders
+ GROUP BY user_id;
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have NO errors - CTE 'high_value_orders' is not a real table
+ for filePath, advices := range results {
+ require.Empty(t, advices, "File %s should have no errors, got: %v", filePath, advices)
+ }
+}
+
+func TestCheckSDLIntegrity_ViewWithMultipleCTEs(t *testing.T) {
+ files := map[string]string{
+ "tables.sql": `
+ CREATE TABLE public.orders (
+ id INTEGER NOT NULL,
+ user_id INTEGER NOT NULL,
+ product_id INTEGER NOT NULL,
+ total NUMERIC NOT NULL,
+ CONSTRAINT pk_orders PRIMARY KEY (id)
+ );
+
+ CREATE TABLE public.products (
+ id INTEGER NOT NULL,
+ name TEXT NOT NULL,
+ price NUMERIC NOT NULL,
+ CONSTRAINT pk_products PRIMARY KEY (id)
+ );
+ `,
+ "views.sql": `
+ -- View using multiple CTEs
+ CREATE VIEW public.sales_summary AS
+ WITH high_value_orders AS (
+ SELECT id, user_id, product_id, total
+ FROM public.orders
+ WHERE total > 1000
+ ),
+ product_stats AS (
+ SELECT p.name, COUNT(h.id) as order_count, SUM(h.total) as total_revenue
+ FROM high_value_orders h
+ JOIN public.products p ON h.product_id = p.id
+ GROUP BY p.name
+ )
+ SELECT * FROM product_stats WHERE order_count > 5;
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have NO errors - both CTEs should be filtered out
+ for filePath, advices := range results {
+ require.Empty(t, advices, "File %s should have no errors, got: %v", filePath, advices)
+ }
+}
+
+func TestCheckSDLIntegrity_ViewWithCTEAndMissingTable(t *testing.T) {
+ files := map[string]string{
+ "views.sql": `
+ -- View using CTE but referencing non-existent table
+ CREATE VIEW public.order_summary AS
+ WITH valid_cte AS (
+ SELECT id, total
+ FROM public.orders
+ WHERE total > 100
+ )
+ SELECT * FROM valid_cte;
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have error for missing 'orders' table, but NOT for 'valid_cte'
+ require.Greater(t, len(results["views.sql"]), 0, "Should have error for missing table")
+
+ var foundOrdersError bool
+ var foundCTEError bool
+ for _, advice := range results["views.sql"] {
+ if advice.Code == advisor.SDLViewDependencyNotFound.Int32() {
+ if strings.Contains(advice.Content, "'public.orders'") {
+ foundOrdersError = true
+ }
+ if strings.Contains(advice.Content, "valid_cte") {
+ foundCTEError = true
+ }
+ }
+ }
+
+ require.True(t, foundOrdersError, "Should have error for missing 'orders' table")
+ require.False(t, foundCTEError, "Should NOT have error for CTE 'valid_cte'")
+}
+
+func TestCheckSDLIntegrity_ViewReferencesSystemTables(t *testing.T) {
+ files := map[string]string{
+ "tables.sql": `
+ CREATE TABLE public.users (
+ id SERIAL PRIMARY KEY,
+ name TEXT
+ );
+ `,
+ "system_views.sql": `
+ -- View referencing pg_* system tables (should not report error)
+ CREATE VIEW public.table_sizes AS
+ SELECT
+ schemaname,
+ tablename,
+ pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size
+ FROM pg_tables
+ WHERE schemaname = 'public';
+
+ -- View referencing information_schema (should not report error)
+ CREATE VIEW public.column_info AS
+ SELECT table_name, column_name, data_type
+ FROM information_schema.columns
+ WHERE table_schema = 'public';
+
+ -- View referencing pg_catalog (should not report error)
+ CREATE VIEW public.index_info AS
+ SELECT i.relname AS index_name, t.relname AS table_name
+ FROM pg_catalog.pg_index idx
+ JOIN pg_catalog.pg_class i ON i.oid = idx.indexrelid
+ JOIN pg_catalog.pg_class t ON t.oid = idx.indrelid;
+
+ -- Mixed: system tables and user tables (should not report error for system tables)
+ CREATE VIEW public.user_stats AS
+ SELECT u.name, COUNT(*) as table_count
+ FROM public.users u
+ CROSS JOIN pg_tables pt
+ WHERE pt.schemaname = 'public'
+ GROUP BY u.name;
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have no errors - all system table references should be ignored
+ for filePath, advices := range results {
+ require.Empty(t, advices, "File %s should have no errors, got: %v", filePath, advices)
+ }
+}
+
+func TestCheckSDLIntegrity_ViewReferencesMixedTablesWithError(t *testing.T) {
+ files := map[string]string{
+ "tables.sql": `
+ CREATE TABLE public.products (
+ id SERIAL PRIMARY KEY,
+ name TEXT
+ );
+ `,
+ "views.sql": `
+ -- This view references both system tables (ok) and non-existent user table (error)
+ CREATE VIEW public.order_analysis AS
+ SELECT o.id, o.total, p.name, pg_size_pretty(pg_table_size('orders')) as table_size
+ FROM public.orders o
+ JOIN public.products p ON o.product_id = p.id;
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have error for 'orders' table but not for pg_table_size or pg_size_pretty
+ var foundOrdersError bool
+ for _, advice := range results["views.sql"] {
+ if advice.Code == advisor.SDLViewDependencyNotFound.Int32() {
+ require.Contains(t, advice.Content, "public.orders")
+ require.NotContains(t, advice.Content, "pg_")
+ foundOrdersError = true
+ }
+ }
+ require.True(t, foundOrdersError, "Should have error for orders table")
+}
+
+func TestCheckSDLIntegrity_MultipleErrorsInSameFile(t *testing.T) {
+ files := map[string]string{
+ "base.sql": `
+ CREATE TABLE public.users (
+ id SERIAL PRIMARY KEY,
+ name TEXT
+ );
+ `,
+ "errors.sql": `
+ CREATE TABLE public.users (
+ id INTEGER PRIMARY KEY
+ );
+
+ CREATE TABLE public.orders (
+ id SERIAL PRIMARY KEY,
+ user_id BIGINT,
+ customer_id INTEGER,
+ FOREIGN KEY (user_id) REFERENCES public.users(id),
+ FOREIGN KEY (customer_id) REFERENCES public.customers(id)
+ );
+
+ CREATE VIEW public.bad_view AS
+ SELECT * FROM public.non_existent_table;
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Collect all error codes from both files
+ errorCodes := make(map[int32]int)
+ for _, advices := range results {
+ for _, advice := range advices {
+ errorCodes[advice.Code]++
+ }
+ }
+
+ // Should have: duplicate table, FK table not found, view dependency not found
+ require.Greater(t, errorCodes[advisor.SDLDuplicateTableName.Int32()], 0, "Should have duplicate table")
+ require.Greater(t, errorCodes[advisor.SDLForeignKeyTableNotFound.Int32()], 0, "Should have FK table not found")
+ require.Greater(t, errorCodes[advisor.SDLViewDependencyNotFound.Int32()], 0, "Should have view dependency error")
+
+ // Should have at least 3 total errors
+ totalErrors := 0
+ for _, advices := range results {
+ totalErrors += len(advices)
+ }
+ require.GreaterOrEqual(t, totalErrors, 3, "Should have at least 3 errors total")
+}
+
+func TestCheckSDLIntegrity_EmptyFiles(t *testing.T) {
+ files := map[string]string{
+ "empty1.sql": "",
+ "empty2.sql": " \n\n ",
+ "valid.sql": `
+ CREATE TABLE public.test (id SERIAL PRIMARY KEY);
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Empty files should not cause errors
+ for filePath, advices := range results {
+ require.Empty(t, advices, "File %s should have no errors", filePath)
+ }
+}
+
+func TestCheckSDLIntegrity_SyntaxErrorInOneFile(t *testing.T) {
+ files := map[string]string{
+ "valid.sql": `
+ CREATE TABLE public.users (id SERIAL PRIMARY KEY);
+ `,
+ "invalid.sql": `
+ CREATE TABLE public.orders (
+ id SERIAL PRIMARY KEY,
+ user_id INTEGER
+ FOREIGN KEY (user_id) REFERENCES public.users(id) -- Missing comma
+ );
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Syntax error should be reported for invalid.sql
+ require.Greater(t, len(results["invalid.sql"]), 0, "invalid.sql should have errors")
+
+ var foundSyntaxError bool
+ for _, advice := range results["invalid.sql"] {
+ if advice.Code == advisor.StatementSyntaxError.Int32() {
+ foundSyntaxError = true
+ }
+ }
+ require.True(t, foundSyntaxError, "Should have syntax error")
+
+ // valid.sql should have no errors
+ require.Empty(t, results["valid.sql"], "valid.sql should have no errors")
+}
+
+func TestCheckSDLIntegrity_ComplexMultiFileSchema(t *testing.T) {
+ files := map[string]string{
+ "01_base.sql": `
+ CREATE TABLE public.organizations (
+ id SERIAL PRIMARY KEY,
+ name TEXT NOT NULL
+ );
+
+ CREATE TABLE public.users (
+ id SERIAL PRIMARY KEY,
+ org_id INTEGER NOT NULL,
+ email TEXT UNIQUE,
+ FOREIGN KEY (org_id) REFERENCES public.organizations(id)
+ );
+ `,
+ "02_business.sql": `
+ CREATE TABLE public.projects (
+ id SERIAL PRIMARY KEY,
+ org_id INTEGER NOT NULL,
+ name TEXT NOT NULL,
+ owner_id INTEGER,
+ FOREIGN KEY (org_id) REFERENCES public.organizations(id),
+ FOREIGN KEY (owner_id) REFERENCES public.users(id)
+ );
+
+ CREATE TABLE public.tasks (
+ id SERIAL PRIMARY KEY,
+ project_id INTEGER NOT NULL,
+ assignee_id INTEGER,
+ title TEXT NOT NULL,
+ FOREIGN KEY (project_id) REFERENCES public.projects(id),
+ FOREIGN KEY (assignee_id) REFERENCES public.users(id)
+ );
+ `,
+ "03_analytics.sql": `
+ CREATE VIEW public.project_stats AS
+ SELECT
+ p.id as project_id,
+ p.name as project_name,
+ o.name as org_name,
+ COUNT(t.id) as task_count
+ FROM public.projects p
+ JOIN public.organizations o ON p.org_id = o.id
+ LEFT JOIN public.tasks t ON t.project_id = p.id
+ GROUP BY p.id, p.name, o.name;
+
+ CREATE VIEW public.user_workload AS
+ SELECT
+ u.id as user_id,
+ u.email,
+ COUNT(t.id) as assigned_tasks
+ FROM public.users u
+ LEFT JOIN public.tasks t ON t.assignee_id = u.id
+ GROUP BY u.id, u.email;
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // All files should have no errors
+ for filePath, advices := range results {
+ require.Empty(t, advices, "File %s should have no errors, got: %v", filePath, advices)
+ }
+}
+
+func TestCheckSDLIntegrity_ForeignKeyColumnNotFound(t *testing.T) {
+ files := map[string]string{
+ "users.sql": `
+ CREATE TABLE public.users (
+ id SERIAL PRIMARY KEY,
+ name TEXT
+ );
+ `,
+ "orders.sql": `
+ CREATE TABLE public.orders (
+ id SERIAL PRIMARY KEY,
+ user_id INTEGER NOT NULL,
+ FOREIGN KEY (user_id) REFERENCES public.users(user_uuid)
+ );
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have FK column not found error
+ var foundError bool
+ for _, advice := range results["orders.sql"] {
+ if advice.Code == advisor.SDLForeignKeyColumnNotFound.Int32() {
+ require.Contains(t, advice.Content, "references column 'user_uuid'")
+ require.Contains(t, advice.Content, "does not exist")
+ foundError = true
+ }
+ }
+ require.True(t, foundError, "Should have FK column not found error")
+}
+
+func TestCheckSDLIntegrity_DuplicateViewAcrossFiles(t *testing.T) {
+ files := map[string]string{
+ "tables.sql": `
+ CREATE TABLE public.products (id SERIAL PRIMARY KEY);
+ `,
+ "view1.sql": `
+ CREATE VIEW public.product_list AS SELECT * FROM public.products;
+ `,
+ "view2.sql": `
+ CREATE VIEW public.product_list AS SELECT id FROM public.products;
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should have duplicate view error
+ var foundError bool
+ for _, advices := range results {
+ for _, advice := range advices {
+ if advice.Code == advisor.SDLDuplicateTableName.Int32() && advice.Title == "Duplicate view name across files" {
+ require.Contains(t, advice.Content, "View 'public.product_list' is defined in multiple SDL files")
+ foundError = true
+ }
+ }
+ }
+ require.True(t, foundError, "Should have duplicate view error")
+}
+
+func TestCheckSDLIntegrity_SingleFile(t *testing.T) {
+ files := map[string]string{
+ "schema.sql": `
+ CREATE TABLE public.users (
+ id SERIAL PRIMARY KEY,
+ email TEXT UNIQUE
+ );
+
+ CREATE TABLE public.orders (
+ id SERIAL PRIMARY KEY,
+ user_id INTEGER NOT NULL,
+ FOREIGN KEY (user_id) REFERENCES public.users(id)
+ );
+ `,
+ }
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+
+ // Should work correctly with single file (backward compatibility)
+ require.Empty(t, results["schema.sql"], "Single file should have no errors")
+}
+
+func TestCheckSDLIntegrity_EmptyInput(t *testing.T) {
+ files := map[string]string{}
+
+ results, err := CheckSDLIntegrity(files)
+ require.NoError(t, err)
+ require.Empty(t, results, "Empty input should return empty results")
+}
diff --git a/backend/plugin/advisor/pg/sdl_integrity_check_test.go b/backend/plugin/advisor/pg/sdl_integrity_check_test.go
new file mode 100644
index 0000000000..5b3a9d291b
--- /dev/null
+++ b/backend/plugin/advisor/pg/sdl_integrity_check_test.go
@@ -0,0 +1,553 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+)
+
+func TestCheckSDLIntegrity(t *testing.T) {
+ tests := []struct {
+ name string
+ statement string
+ wantCount int
+ wantCodes []advisor.Code
+ }{
+ // ============================================================
+ // FOREIGN KEY VALIDATION TESTS
+ // ============================================================
+ {
+ name: "Valid FK - table and column exist with matching types",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);
+
+CREATE TABLE public.orders (
+ id BIGINT NOT NULL,
+ user_id INTEGER NOT NULL,
+ CONSTRAINT pk_orders PRIMARY KEY (id),
+ CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES public.users(id)
+);`,
+ wantCount: 0,
+ },
+ {
+ name: "Error - FK references non-existent table",
+ statement: `
+CREATE TABLE public.orders (
+ id BIGINT NOT NULL,
+ user_id INTEGER NOT NULL,
+ CONSTRAINT pk_orders PRIMARY KEY (id),
+ CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES public.users(id)
+);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLForeignKeyTableNotFound},
+ },
+ {
+ name: "Error - FK references non-existent column",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);
+
+CREATE TABLE public.orders (
+ id BIGINT NOT NULL,
+ user_id INTEGER NOT NULL,
+ CONSTRAINT pk_orders PRIMARY KEY (id),
+ CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES public.users(user_id)
+);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLForeignKeyColumnNotFound},
+ },
+ {
+ name: "Valid FK - Composite FK with matching types",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ tenant_id INTEGER NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id, tenant_id)
+);
+
+CREATE TABLE public.orders (
+ id BIGINT NOT NULL,
+ user_id INTEGER NOT NULL,
+ tenant_id INTEGER NOT NULL,
+ CONSTRAINT pk_orders PRIMARY KEY (id),
+ CONSTRAINT fk_orders_user FOREIGN KEY (user_id, tenant_id) REFERENCES public.users(id, tenant_id)
+);`,
+ wantCount: 0,
+ },
+ {
+ name: "Valid FK - VARCHAR types with different lengths are compatible",
+ statement: `
+CREATE TABLE public.users (
+ email VARCHAR(255) NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (email)
+);
+
+CREATE TABLE public.sessions (
+ id BIGINT NOT NULL,
+ user_email VARCHAR(100) NOT NULL,
+ CONSTRAINT pk_sessions PRIMARY KEY (id),
+ CONSTRAINT fk_sessions_user FOREIGN KEY (user_email) REFERENCES public.users(email)
+);`,
+ wantCount: 0,
+ },
+ {
+ name: "Valid FK - TEXT and VARCHAR are compatible",
+ statement: `
+CREATE TABLE public.users (
+ email TEXT NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (email)
+);
+
+CREATE TABLE public.sessions (
+ id BIGINT NOT NULL,
+ user_email VARCHAR(255) NOT NULL,
+ CONSTRAINT pk_sessions PRIMARY KEY (id),
+ CONSTRAINT fk_sessions_user FOREIGN KEY (user_email) REFERENCES public.users(email)
+);`,
+ wantCount: 0,
+ },
+
+ // ============================================================
+ // DUPLICATE NAME DETECTION TESTS
+ // ============================================================
+ {
+ name: "Error - Duplicate table name in same schema",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);
+
+CREATE TABLE public.users (
+ user_id BIGINT NOT NULL,
+ CONSTRAINT pk_users2 PRIMARY KEY (user_id)
+);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLDuplicateTableName},
+ },
+ {
+ name: "Valid - Same table name in different schemas",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);
+
+CREATE TABLE analytics.users (
+ id BIGINT NOT NULL,
+ CONSTRAINT pk_analytics_users PRIMARY KEY (id)
+);`,
+ wantCount: 0,
+ },
+ {
+ name: "Error - Duplicate index name in same schema",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);
+
+CREATE INDEX idx_users_email ON public.users(email);
+CREATE INDEX idx_users_email ON public.users(id);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLDuplicateIndexName},
+ },
+ {
+ name: "Error - Duplicate constraint name in same schema",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ CONSTRAINT pk_common PRIMARY KEY (id),
+ CONSTRAINT uk_users_email UNIQUE (email)
+);
+
+CREATE TABLE public.products (
+ id INTEGER NOT NULL,
+ code TEXT NOT NULL,
+ CONSTRAINT pk_common PRIMARY KEY (id),
+ CONSTRAINT uk_products_code UNIQUE (code)
+);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLDuplicateConstraintName},
+ },
+ {
+ name: "Error - Duplicate column name in same table",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ email TEXT NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLDuplicateColumnName},
+ },
+ {
+ name: "Valid - Same column name in different tables",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);
+
+CREATE TABLE public.products (
+ id INTEGER NOT NULL,
+ name TEXT NOT NULL,
+ CONSTRAINT pk_products PRIMARY KEY (id)
+);`,
+ wantCount: 0,
+ },
+
+ // ============================================================
+ // MULTIPLE PRIMARY KEY TESTS
+ // ============================================================
+ {
+ name: "Error - Multiple PRIMARY KEY constraints on same table",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ CONSTRAINT pk_users_id PRIMARY KEY (id),
+ CONSTRAINT pk_users_email PRIMARY KEY (email)
+);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLMultiplePrimaryKey},
+ },
+ {
+ name: "Error - Three PRIMARY KEY constraints on same table",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ username TEXT NOT NULL,
+ CONSTRAINT pk1 PRIMARY KEY (id),
+ CONSTRAINT pk2 PRIMARY KEY (email),
+ CONSTRAINT pk3 PRIMARY KEY (username)
+);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLMultiplePrimaryKey},
+ },
+ {
+ name: "Valid - One PRIMARY KEY with multiple columns (composite PK)",
+ statement: `
+CREATE TABLE public.user_roles (
+ user_id INTEGER NOT NULL,
+ role_id INTEGER NOT NULL,
+ CONSTRAINT pk_user_roles PRIMARY KEY (user_id, role_id)
+);`,
+ wantCount: 0,
+ },
+
+ // ============================================================
+ // VIEW DEPENDENCY TESTS
+ // ============================================================
+ {
+ name: "Valid VIEW - references existing table",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ active BOOLEAN NOT NULL DEFAULT true,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);
+
+CREATE VIEW public.active_users AS
+ SELECT id, email FROM public.users WHERE active = true;`,
+ wantCount: 0,
+ },
+ {
+ name: "Error - VIEW references non-existent table",
+ statement: `
+CREATE VIEW public.active_users AS
+ SELECT id, email FROM public.users WHERE active = true;`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLViewDependencyNotFound},
+ },
+ {
+ name: "Error - VIEW references multiple tables, one doesn't exist",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);
+
+CREATE VIEW public.user_orders AS
+ SELECT u.id, u.email, o.id as order_id
+ FROM public.users u
+ JOIN public.orders o ON u.id = o.user_id;`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLViewDependencyNotFound},
+ },
+ {
+ name: "Valid VIEW - references all existing tables",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);
+
+CREATE TABLE public.orders (
+ id BIGINT NOT NULL,
+ user_id INTEGER NOT NULL,
+ CONSTRAINT pk_orders PRIMARY KEY (id),
+ CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES public.users(id)
+);
+
+CREATE VIEW public.user_orders AS
+ SELECT u.id, u.email, o.id as order_id
+ FROM public.users u
+ JOIN public.orders o ON u.id = o.user_id;`,
+ wantCount: 0,
+ },
+
+ // ============================================================
+ // COMPLEX SCENARIOS - MULTIPLE ERRORS
+ // ============================================================
+ {
+ name: "Multiple errors - FK table not found + type mismatch",
+ statement: `
+CREATE TABLE public.orders (
+ id BIGINT NOT NULL,
+ user_id SMALLINT NOT NULL,
+ price NUMERIC NOT NULL,
+ CONSTRAINT pk_orders PRIMARY KEY (id),
+ CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES public.users(id)
+);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{
+ advisor.SDLForeignKeyTableNotFound,
+ },
+ },
+ {
+ name: "Multiple errors - Duplicate names + multiple PKs",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ email TEXT NOT NULL,
+ CONSTRAINT pk_common PRIMARY KEY (id),
+ CONSTRAINT pk_common2 PRIMARY KEY (email)
+);
+
+CREATE TABLE public.orders (
+ id BIGINT NOT NULL,
+ user_id INTEGER NOT NULL,
+ CONSTRAINT pk_common PRIMARY KEY (id)
+);`,
+ wantCount: 2,
+ wantCodes: []advisor.Code{
+ advisor.SDLDuplicateConstraintName, // Detected first during Pass 1
+ advisor.SDLMultiplePrimaryKey, // Detected during Pass 2
+ },
+ },
+
+ // ============================================================
+ // CONSTRAINT NAME SCOPING TESTS
+ // ============================================================
+ {
+ name: "Valid - Same FK name on different tables (table-level scoping)",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);
+
+CREATE TABLE public.orders (
+ id INTEGER NOT NULL,
+ user_id INTEGER NOT NULL,
+ CONSTRAINT pk_orders PRIMARY KEY (id),
+ CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES public.users(id)
+);
+
+CREATE TABLE public.invoices (
+ id INTEGER NOT NULL,
+ user_id INTEGER NOT NULL,
+ CONSTRAINT pk_invoices PRIMARY KEY (id),
+ CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES public.users(id)
+);`,
+ wantCount: 0, // FK constraints are table-scoped, same name allowed on different tables
+ },
+ {
+ name: "Valid - Same CHECK name on different tables (table-level scoping)",
+ statement: `
+CREATE TABLE public.products (
+ id INTEGER NOT NULL,
+ price NUMERIC NOT NULL,
+ CONSTRAINT pk_products PRIMARY KEY (id),
+ CONSTRAINT chk_positive CHECK (price > 0)
+);
+
+CREATE TABLE public.services (
+ id INTEGER NOT NULL,
+ price NUMERIC NOT NULL,
+ CONSTRAINT pk_services PRIMARY KEY (id),
+ CONSTRAINT chk_positive CHECK (price > 0)
+);`,
+ wantCount: 0, // CHECK constraints are table-scoped, same name allowed on different tables
+ },
+ {
+ name: "Error - Duplicate FK name in same table",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);
+
+CREATE TABLE public.posts (
+ id INTEGER NOT NULL,
+ author_id INTEGER NOT NULL,
+ editor_id INTEGER NOT NULL,
+ CONSTRAINT pk_posts PRIMARY KEY (id),
+ CONSTRAINT fk_user FOREIGN KEY (author_id) REFERENCES public.users(id),
+ CONSTRAINT fk_user FOREIGN KEY (editor_id) REFERENCES public.users(id)
+);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLDuplicateConstraintName},
+ },
+ {
+ name: "Error - Duplicate PRIMARY KEY name across tables (schema-level scoping)",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ CONSTRAINT pk_common PRIMARY KEY (id)
+);
+
+CREATE TABLE public.orders (
+ id INTEGER NOT NULL,
+ CONSTRAINT pk_common PRIMARY KEY (id)
+);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLDuplicateConstraintName},
+ },
+ {
+ name: "Error - Duplicate UNIQUE name across tables (schema-level scoping)",
+ statement: `
+CREATE TABLE public.users (
+ email TEXT NOT NULL,
+ CONSTRAINT uk_email UNIQUE (email)
+);
+
+CREATE TABLE public.customers (
+ email TEXT NOT NULL,
+ CONSTRAINT uk_email UNIQUE (email)
+);`,
+ wantCount: 1,
+ wantCodes: []advisor.Code{advisor.SDLDuplicateConstraintName},
+ },
+
+ // ============================================================
+ // EDGE CASES
+ // ============================================================
+ {
+ name: "Valid - Self-referencing FK (same table)",
+ statement: `
+CREATE TABLE public.employees (
+ id INTEGER NOT NULL,
+ manager_id INTEGER,
+ CONSTRAINT pk_employees PRIMARY KEY (id),
+ CONSTRAINT fk_employees_manager FOREIGN KEY (manager_id) REFERENCES public.employees(id)
+);`,
+ wantCount: 0,
+ },
+ {
+ name: "Valid - Empty SDL (no tables)",
+ statement: ``,
+ wantCount: 0,
+ },
+ {
+ name: "Valid - Comments only",
+ statement: `-- This is a comment
+-- Another comment`,
+ wantCount: 0,
+ },
+ {
+ name: "Valid - Table with no constraints",
+ statement: `
+CREATE TABLE public.logs (
+ id BIGINT,
+ message TEXT,
+ created_at TIMESTAMP
+);`,
+ wantCount: 0,
+ },
+ {
+ name: "Error - Unnamed FK constraint (detected by style check, not integrity)",
+ statement: `
+CREATE TABLE public.users (
+ id INTEGER NOT NULL,
+ CONSTRAINT pk_users PRIMARY KEY (id)
+);
+
+CREATE TABLE public.orders (
+ id BIGINT NOT NULL,
+ user_id INTEGER NOT NULL,
+ CONSTRAINT pk_orders PRIMARY KEY (id),
+ FOREIGN KEY (user_id) REFERENCES public.users(id)
+);`,
+ wantCount: 0, // Integrity check doesn't care about unnamed constraints
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ advices, err := checkSingleStatement(tc.statement)
+ require.NoError(t, err, "checkSingleStatement should not return error for valid SQL syntax")
+ require.Equal(t, tc.wantCount, len(advices), "Unexpected number of advices. Advices: %+v", advices)
+
+ if tc.wantCount > 0 {
+ require.NotNil(t, tc.wantCodes, "wantCodes should be specified when wantCount > 0")
+ require.Equal(t, len(tc.wantCodes), len(advices), "Number of advices should match wantCodes")
+
+ // Verify each advice has the expected error code and ERROR status
+ for i, advice := range advices {
+ require.Equal(t, storepb.Advice_ERROR, advice.Status, "All SDL integrity check advices should have ERROR status")
+ require.Equal(t, tc.wantCodes[i].Int32(), advice.Code, "Advice %d should have expected error code. Got: %s", i, advice.Title)
+ require.NotEmpty(t, advice.Title, "Advice should have a title")
+ require.NotEmpty(t, advice.Content, "Advice should have content")
+ require.NotNil(t, advice.StartPosition, "Advice should have start position")
+ require.Greater(t, advice.StartPosition.Line, int32(0), "Line number should be positive")
+ }
+ }
+ })
+ }
+}
+
+func TestCheckSDLIntegrity_InvalidSQL(t *testing.T) {
+ tests := []struct {
+ name string
+ statement string
+ }{
+ {
+ name: "Syntax error - missing parenthesis",
+ statement: `CREATE TABLE public.users (id INTEGER;`,
+ },
+ {
+ name: "Syntax error - invalid keyword",
+ statement: `CREATE TABLEX public.users (id INTEGER);`,
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ advices, err := checkSingleStatement(tc.statement)
+ // Syntax errors are returned as advices, not as errors
+ require.NoError(t, err)
+ require.Greater(t, len(advices), 0, "Should have syntax error advice")
+ require.Equal(t, advisor.StatementSyntaxError.Int32(), advices[0].Code)
+ })
+ }
+}