Skip to content

Conversation

@tianzhou
Copy link
Contributor

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:

  • 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

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>
Copilot AI review requested due to automatic review settings December 18, 2025 11:06
Copy link
Contributor

Copilot AI left a 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 IsBuiltInType and IsTextLikeType helper 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>
Copy link
Contributor

Copilot AI commented Dec 18, 2025

@tianzhou I've opened a new pull request, #210, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Contributor

Copilot AI commented Dec 18, 2025

@tianzhou I've opened a new pull request, #211, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Contributor

Copilot AI commented Dec 18, 2025

@tianzhou I've opened a new pull request, #212, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 3 commits December 18, 2025 06:40
* 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>
@tianzhou tianzhou merged commit cc00ae3 into main Dec 18, 2025
1 check passed
@tianzhou tianzhou deleted the change_column_type branch January 5, 2026 14:12
alecthomas pushed a commit to alecthomas/pgschema that referenced this pull request Jan 26, 2026
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Column type change to enum fails: missing USING clause and existing default can't be cast

2 participants