feat(postgres): add native enum type support#2412
Open
ErfanMomeniii wants to merge 4 commits into
Open
Conversation
Author
|
Hi @dereuromark , could you please review it? |
Member
|
@MasterOdin took over maintainance. |
There was a problem hiding this comment.
Pull request overview
Adds native PostgreSQL enum support to Phinx by introducing a managed enum-type lifecycle in PostgresAdapter (create type before table/column creation, drop type on column/table removal) and round-tripping enums back to PHINX_TYPE_ENUM via catalog inspection.
Changes:
- Add
PHINX_TYPE_ENUMsupport toPostgresAdapter(DDL emission + column introspection for enum labels). - Update shared type constants/comments to reflect enum support beyond MySQL.
- Add Postgres integration tests covering enum create/add/drop and round-tripping behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
src/Phinx/Db/Adapter/PostgresAdapter.php |
Implements enum type creation, drop cleanup, and enum detection/round-trip in getColumns(). |
src/Phinx/Db/Table/Column.php |
Updates Column::ENUM documentation to reflect Postgres support. |
src/Phinx/Db/Adapter/AdapterInterface.php |
Reclassifies PHINX_TYPE_ENUM as supported by MySQL + PostgreSQL. |
tests/Phinx/Db/Adapter/PostgresAdapterTest.php |
Adds integration tests validating enum lifecycle and getColumns() round-tripping. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+311
to
+321
| if ($column->getType() === static::PHINX_TYPE_ENUM) { | ||
| $values = $column->getValues(); | ||
| if (!empty($values)) { | ||
| $typeName = $this->getEnumTypeName($table->getName(), $column->getName()); | ||
| $queries[] = sprintf( | ||
| 'CREATE TYPE %s AS ENUM (%s)', | ||
| $this->quoteColumnName($typeName), | ||
| implode(', ', array_map(fn($v) => $this->getConnection()->quote($v), $values)), | ||
| ); | ||
| } | ||
| } |
Comment on lines
+590
to
+594
| $this->execute(sprintf( | ||
| 'CREATE TYPE %s AS ENUM (%s)', | ||
| $this->quoteColumnName($typeName), | ||
| implode(', ', array_map(fn($v) => $this->getConnection()->quote($v), $values)), | ||
| )); |
…ma-aware enum helpers
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.
Adds native PostgreSQL enum type support to
PostgresAdapter.Problem
PostgreSQL requires a two-step process for enum columns — first
CREATE TYPE ... AS ENUM (...), then referencing that type in column definitions. Phinx only supported MySQL's inlineENUM('a','b')syntax.See: #2258
Solution
Handle the full enum lifecycle automatically with the naming convention
{table}_{column}(e.g.orders_status) to prevent cross-table collisions.createTable()CREATE TYPEbeforeCREATE TABLEaddColumn()CREATE TYPEbeforeALTER TABLEdropColumn()dropTable()getColumns()PHINX_TYPE_ENUMwith values frompg_enumUsage
Rollbacks work automatically — dropping a column or table cleans up the associated PostgreSQL type.
Tests
10 new integration tests covering create, add, drop column, drop table, round-trip, cross-table isolation, and schema-qualified tables.
Fixes #2258