phinx icon indicating copy to clipboard operation
phinx copied to clipboard

ENUM column type on PostgreSQL adapter

Open bhean opened this issue 8 years ago • 12 comments

Hi,

I'm tryin to migrate some of my projects to Phinx (its great!), but I cannot complete the migration because Phinx don't support ENUM column types on PostgreSQL adapter...

Are there any reason for that, or I'm doing something wrong?

Regards!!

bhean avatar Jul 07 '16 21:07 bhean

any update here from the team?

dereuromark avatar Oct 08 '17 17:10 dereuromark

Side note: Afaik only few DBs (like MySQL) have enum support. That is also why CakePHP does not have native support for it, as it is a snowflake kind of thing. I recommend using integer columns and some low level framework or project PHP abstraction to simulate enums. This is then also DB agostic.

dereuromark avatar Oct 08 '17 17:10 dereuromark

Sure, but in Phinx there are a lot of things only applies for one DB adapter (signedcolumns for some data types in MySQL, timezoneon timestamp data types in PostgreSQL, etc) so I think "DB agnostic" is not a good reason to don't support some interesting and usefull features (like enum types, in my opinion)

bhean avatar Oct 08 '17 20:10 bhean

Fair enough Are you able to provide a pull request with your suggested changes?

dereuromark avatar Oct 08 '17 21:10 dereuromark

I'd love to do it, but I'm not sure if I can. I'll try it...

bhean avatar Oct 08 '17 21:10 bhean

@bhean Did you manage to do this?

acanthis avatar Feb 15 '18 08:02 acanthis

I'm not sure how one would integrate this into Phinx. To use enums in PostgreSQL, you have to do CREATE TYPE identifier AS ENUM ('value1', 'value2', 'value3', ...);

The example they give on their docs is:

CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE person (
    name text,
    current_mood mood
);

I'm guessing it'd be something like when in createTable(), loop through all columns and if any are enums, generate the CREATE TYPE statement for it, issue that, and then continue to generating the create table statement, substituting out the 'enum' type for the name we used in the CREATE TYPE (and in changeColumn, if the new type is enum, then do the CREATE TYPE business like above). Would you want an additional option name so that people could name their enums instead of having Phinx auto-generate the name?

MasterOdin avatar Feb 15 '18 13:02 MasterOdin

@MasterOdin Yes I need it

acanthis avatar Feb 15 '18 13:02 acanthis

enums have tricky requirements.

Closing due to lack of activity or out of scope. If there is new interest, please open a PR.

othercorey avatar May 09 '20 01:05 othercorey

@othercorey can this be re-opened. It's annoying to do, but something phinx should probably support at some point.

MasterOdin avatar May 11 '20 02:05 MasterOdin

Is anyone working on this, because i just hit this today having to support enums in pgsql ?

darxmac avatar Jun 23 '20 12:06 darxmac

This is a quick hack for anyone who want to add 'enum' field to a postgresql table.

Use execute method to create enum type and then alter table:

$this->execute("create type foo as enum ('bar1', 'bar2')");
$this->execute('alter table table_name add column foo_column foo');

armezit avatar Feb 06 '21 15:02 armezit