Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions internal/ir/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,25 +518,9 @@ func (i *Inspector) buildConstraints(ctx context.Context, schema *IR, targetSche
dbSchema := schema.getOrCreateSchema(key.schema)
table, exists := dbSchema.Tables[key.table]
if exists {
// Sort constraint columns by their position to preserve original order from database
// This ensures constraints maintain the correct column order as defined in PostgreSQL
if requiresPositionSorting(constraint.Type) {
sort.Slice(constraint.Columns, func(i, j int) bool {
return constraint.Columns[i].Position < constraint.Columns[j].Position
})

// Also sort referenced columns for foreign keys
if constraint.Type == ConstraintTypeForeignKey && len(constraint.ReferencedColumns) > 0 {
sort.Slice(constraint.ReferencedColumns, func(i, j int) bool {
return constraint.ReferencedColumns[i].Position < constraint.ReferencedColumns[j].Position
})
}
}

table.Constraints[key.name] = constraint

// For partitioned tables, ensure primary key columns are ordered with partition key first
Copy link

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment on line 524 references 'position-based sorting' but the position-based sorting code has been removed. This comment should be updated to reflect the current behavior or removed entirely.

Suggested change
// For partitioned tables, ensure primary key columns are ordered with partition key first
// For partitioned tables, handle primary key columns as needed

Copilot uses AI. Check for mistakes.
// This special handling overrides the position-based sorting for partitioned tables
if constraint.Type == ConstraintTypePrimaryKey && table.IsPartitioned && table.PartitionKey != "" {
i.sortPrimaryKeyColumnsForPartitionedTable(constraint, table.PartitionKey)
}
Expand All @@ -553,16 +537,6 @@ func (i *Inspector) buildConstraints(ctx context.Context, schema *IR, targetSche
return nil
}

// requiresPositionSorting returns true if the constraint type requires columns to be sorted by position
func requiresPositionSorting(constraintType ConstraintType) bool {
switch constraintType {
case ConstraintTypeUnique, ConstraintTypePrimaryKey, ConstraintTypeForeignKey:
return true
default:
return false
}
}

// buildPartitionMapping builds a mapping from partition table names to their parent's partition keys
func (i *Inspector) buildPartitionMapping(ctx context.Context, schema *IR, targetSchema string) map[string]string {
partitionMapping := make(map[string]string)
Expand Down