Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 18, 2025

The default value handling logic unnecessarily dropped and re-added defaults for all type changes, even when PostgreSQL could implicitly convert types (e.g., integer → bigint).

Changes

  • Modified internal/diff/column.go to evaluate needsUsingClause() once and use it to determine when defaults must be dropped/re-added
  • DROP/SET DEFAULT now only executes when a USING clause is required (e.g., text → enum)
  • Implicit conversions (integer → bigint) now preserve defaults automatically

Example

For a column changing from integer DEFAULT 1 to bigint DEFAULT 1:

Before:

ALTER TABLE t ALTER COLUMN id DROP DEFAULT;
ALTER TABLE t ALTER COLUMN id TYPE bigint;
ALTER TABLE t ALTER COLUMN id SET DEFAULT 1;

After:

ALTER TABLE t ALTER COLUMN id TYPE bigint;

For text DEFAULT 'pending' to action_type DEFAULT 'pending', the DROP/SET DEFAULT sequence is still correctly generated since USING is required.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: tianzhou <230323+tianzhou@users.noreply.github.com>
Copilot AI changed the title [WIP] WIP address feedback on USING clause for text-to-enum conversions Optimize default value handling during type changes Dec 18, 2025
Copilot AI requested a review from tianzhou December 18, 2025 11:38
Copy link
Contributor

@tianzhou tianzhou left a comment

Choose a reason for hiding this comment

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

LGTM

@tianzhou tianzhou marked this pull request as ready for review December 18, 2025 14:40
@tianzhou tianzhou merged commit af37a98 into change_column_type Dec 18, 2025
1 check passed
tianzhou added a commit that referenced this pull request Dec 18, 2025
* fix: add USING clause for text-to-enum type conversions (#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 (#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 (#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>
@tianzhou tianzhou deleted the copilot/sub-pr-209 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.

2 participants