-
Notifications
You must be signed in to change notification settings - Fork 29
Fix normalization of custom array type #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes the normalization of custom array types (enums, composites, domains) by converting PostgreSQL's internal _typename format to the SQL standard typename[] syntax. This ensures that DDL statements and schema comparisons use the correct array notation.
Key changes:
- Added logic to detect and convert underscore-prefixed custom array types to bracket notation
- Added test case for enum array columns to verify the fix
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| ir/normalize.go | Added custom array type normalization logic to convert _typename to typename[] format |
| testdata/diff/create_table/enum_array_column/old.sql | Test fixture: schema before adding enum array column |
| testdata/diff/create_table/enum_array_column/new.sql | Test fixture: schema after adding enum array column |
| testdata/diff/create_table/enum_array_column/diff.sql | Expected DDL output showing status[] array syntax |
| testdata/diff/create_table/enum_array_column/plan.sql | Expected plan SQL with normalized array type |
| testdata/diff/create_table/enum_array_column/plan.txt | Human-readable plan output |
| testdata/diff/create_table/enum_array_column/plan.json | Machine-readable plan output in JSON format |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Handle custom array types (internal PostgreSQL array notation) | ||
| // e.g., _my_enum_type -> my_enum_type[] | ||
| if strings.HasPrefix(typeName, "_") { | ||
| return typeName[1:] + "[]" | ||
| } |
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The custom array type normalization only handles direct type names but doesn't handle type casts. When custom array types appear in expressions with type casts (e.g., value::_my_enum), they won't be normalized to value::my_enum[].
Consider adding normalization for custom array types in the type cast handling section (lines 756-759). For example:
// Replace PostgreSQL internal type names with standard SQL types in type casts
for pgType, sqlType := range typeMap {
expr = strings.ReplaceAll(expr, "::"+pgType, "::"+sqlType)
}
// Handle custom array types in type casts
// e.g., ::_my_enum -> ::my_enum[]
expr = regexp.MustCompile(`::(pg_catalog\.)?_(\w+)`).ReplaceAllString(expr, "::$2[]")| @@ -780,6 +780,12 @@ func normalizePostgreSQLType(input string) string { | |||
| return after | |||
Copilot
AI
Nov 14, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pg_catalog prefix removal returns immediately without checking if the result is a custom array type. For example, pg_catalog._my_enum would be returned as _my_enum instead of being further normalized to my_enum[].
The logic should continue to check for the underscore prefix after removing pg_catalog:
// Remove pg_catalog prefix for unmapped types
if after, found := strings.CutPrefix(typeName, "pg_catalog."); found {
typeName = after
}
// Handle custom array types (internal PostgreSQL array notation)
// e.g., _my_enum_type -> my_enum_type[]
if strings.HasPrefix(typeName, "_") {
return typeName[1:] + "[]"
}
return typeName| return after | |
| typeName = after |
tianzhou
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.
LGTM overall. I will merge this and make some refinement in a followup PR.
|
Followup #164 |
Fix custom type arrays (enums, composites, domains) incorrectly outputting PostgreSQL's internal
_typenameformat instead of SQL standardtypename[]syntax.