fix: emit DROP+ADD COLUMN for incompatible type changes#4
Open
andyc-valstro wants to merge 1 commit into
Open
Conversation
When a column's type changes between incompatible types (e.g. double precision → enum, or enum → integer), ALTER COLUMN ... TYPE ... USING fails because PostgreSQL has no cast path. Detect these transitions and emit DROP COLUMN + ADD COLUMN instead. Text-like types can still be cast to custom types via USING and remain on the existing ALTER TYPE path. Co-authored-by: Cursor <cursoragent@cursor.com>
🔍 General Code Quality Feedback🔍 Comprehensive Code ReviewConsolidated Feedback
Overall Assessment: The pull request introduces a significant change to handle incompatible type transitions in PostgreSQL migrations. While the solution is well thought out, there are areas for improvement in maintainability, documentation, and testing coverage. Critical Issues:
Improvements:
Positive Notes:
Next Steps:
By addressing these issues and suggestions, the code will be more maintainable, easier to understand, and less prone to future bugs. 🤖 Generated by Wellcode.ai |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a column's type changes from a built-in numeric type (like
double precision) to a custom enum type, pgschema generates:This fails at execution time because PostgreSQL has no cast from
double precisionto an enum. The same issue occurs for other incompatible transitions likeboolean→ enum or enum →integer.Solution
Added a
requiresDropAdd(oldType, newType)check ingenerateColumnSQL()that detects type transitions where no PostgreSQL cast path exists. When detected, the migration emits:The detection logic: if one side is a non-text-like built-in type and the other is a custom type (enum/composite/domain), there is no valid cast. Text-like types (text, varchar, char) can still be cast to enums via
USING col::enum_typeand continue using the existingALTER TYPE ... USINGapproach.Note: This is a data-destructive operation by nature — the column data is lost. However, this is the only correct approach when types are truly incompatible, and mirrors what a DBA would do manually.
Test plan
alter_column_incompatible_typecoveringdouble precision→ enum and enum →integeralter_column_typestest still passes (text → enum uses ALTER TYPE USING)TestPlanAndApply) passesMade with Cursor