Skip to content

feat(postgres): add native enum type support#2412

Open
ErfanMomeniii wants to merge 4 commits into
cakephp:0.xfrom
ErfanMomeniii:0.x
Open

feat(postgres): add native enum type support#2412
ErfanMomeniii wants to merge 4 commits into
cakephp:0.xfrom
ErfanMomeniii:0.x

Conversation

@ErfanMomeniii
Copy link
Copy Markdown

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 inline ENUM('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.

Operation Behavior
createTable() Emits CREATE TYPE before CREATE TABLE
addColumn() Emits CREATE TYPE before ALTER TABLE
dropColumn() Drops the associated enum type after column removal
dropTable() Drops all associated enum types after table removal
getColumns() Round-trips enums back as PHINX_TYPE_ENUM with values from pg_enum

Usage

$this->table('orders')
      ->addColumn('status', 'enum', [
          'values'  => ['pending', 'shipped', 'delivered'],
          'default' => 'pending',
      ])
      ->create();

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

@ErfanMomeniii
Copy link
Copy Markdown
Author

Hi @dereuromark , could you please review it?

@dereuromark
Copy link
Copy Markdown
Member

@MasterOdin took over maintainance.

Copy link
Copy Markdown

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

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_ENUM support to PostgresAdapter (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)),
));
Comment thread src/Phinx/Db/Adapter/PostgresAdapter.php
Comment thread src/Phinx/Db/Adapter/PostgresAdapter.php Outdated
Comment thread src/Phinx/Db/Adapter/PostgresAdapter.php
Comment thread tests/Phinx/Db/Adapter/PostgresAdapterTest.php
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.

3 participants