-
Notifications
You must be signed in to change notification settings - Fork 29
fix: add USING clause for text-to-enum type conversions #209
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
When converting columns from text-like types (text, varchar, char) to custom types like ENUMs, PostgreSQL requires an explicit USING clause. Additionally, columns with default values need special handling during type changes: drop default, alter type, then re-add default. Changes: - Add USING clause when converting from text-like to non-built-in types - Handle default values during type change (drop -> alter -> set) - Add IsBuiltInType and IsTextLikeType helpers to ir/normalize.go - Add test cases for text-to-enum conversion with and without defaults 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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 an issue where PostgreSQL migrations fail when converting columns from text-like types (text, varchar, char) to custom types like ENUMs. PostgreSQL requires an explicit USING clause for such conversions because there's no implicit cast. Additionally, columns with default values need special handling: the default must be dropped before the type change, then re-added afterward to avoid casting errors.
- Added
IsBuiltInTypeandIsTextLikeTypehelper functions to determine when USING clauses are needed - Enhanced column type change logic to handle defaults with drop → alter → set sequence
- Added comprehensive test case covering text-to-enum conversions with and without defaults
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| testdata/diff/create_table/alter_column_types/old.sql | Added test columns action and status with text type to test enum conversion |
| testdata/diff/create_table/alter_column_types/new.sql | Added enum type and converted test columns to use it, with and without defaults |
| testdata/diff/create_table/alter_column_types/plan.txt | Expected plan output showing USING clauses and default handling sequence |
| testdata/diff/create_table/alter_column_types/plan.sql | Expected SQL output with proper USING clauses and default drop/set statements |
| testdata/diff/create_table/alter_column_types/plan.json | Expected JSON plan structure with all migration steps |
| testdata/diff/create_table/alter_column_types/diff.sql | Expected diff SQL matching plan.sql output |
| ir/normalize.go | Added builtInTypes map, IsBuiltInType, and IsTextLikeType helper functions for type classification |
| internal/diff/column.go | Enhanced generateColumnSQL to handle defaults during type changes and added needsUsingClause function |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Initial plan * Optimize default value handling for type changes without USING clause Co-authored-by: tianzhou <230323+tianzhou@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tianzhou <230323+tianzhou@users.noreply.github.com>
…#211) * Initial plan * fix: handle array types in IsTextLikeType function Co-authored-by: tianzhou <230323+tianzhou@users.noreply.github.com> * refactor: consolidate array type tests into existing alter_column_types test Co-authored-by: tianzhou <230323+tianzhou@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tianzhou <230323+tianzhou@users.noreply.github.com>
* fix: add USING clause for text-to-enum type conversions (pgplex#190) When converting columns from text-like types (text, varchar, char) to custom types like ENUMs, PostgreSQL requires an explicit USING clause. Additionally, columns with default values need special handling during type changes: drop default, alter type, then re-add default. Changes: - Add USING clause when converting from text-like to non-built-in types - Handle default values during type change (drop -> alter -> set) - Add IsBuiltInType and IsTextLikeType helpers to ir/normalize.go - Add test cases for text-to-enum conversion with and without defaults 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Update internal/diff/column.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Optimize default value handling during type changes (pgplex#210) * Initial plan * Optimize default value handling for type changes without USING clause Co-authored-by: tianzhou <230323+tianzhou@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tianzhou <230323+tianzhou@users.noreply.github.com> * Fix array type handling in IsTextLikeType for USING clause generation (pgplex#211) * Initial plan * fix: handle array types in IsTextLikeType function Co-authored-by: tianzhou <230323+tianzhou@users.noreply.github.com> * refactor: consolidate array type tests into existing alter_column_types test Co-authored-by: tianzhou <230323+tianzhou@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tianzhou <230323+tianzhou@users.noreply.github.com> * chore: add missing updated plan --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Fix #190
When converting columns from text-like types (text, varchar, char) to custom types like ENUMs, PostgreSQL requires an explicit USING clause. Additionally, columns with default values need special handling during type changes: drop default, alter type, then re-add default.
Changes:
🤖 Generated with Claude Code