Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding an enum db type doesn't seem to add the requisite op.drop_type() in downgrade #114

Closed
lolopinto opened this issue Aug 23, 2020 · 2 comments · Fixed by #117
Closed

Comments

@lolopinto
Copy link
Owner

adding an enum e.g. rainbow_type in tests at #113

generates the following code:

# Code generated by github.com/lolopinto/ent/ent, DO NOT edit. 

"""add accounts table

Revision ID: bcfa772af8d3
Revises: 
Create Date: 2020-08-22 08:37:36.040787+00:00

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'bcfa772af8d3'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('accounts',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('rainbow', sa.Enum('red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet', name='rainbow_type'), nullable=False),
    sa.PrimaryKeyConstraint('id', name='accounts_id_pkey')
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('accounts')
    # ### end Alembic commands ###

there's no explicit create type but the type is created.
however, since no explicit create type, there ends up not being a drop type either

lolopinto added a commit that referenced this issue Aug 25, 2020
the alembic autogenerate code was apparently only detecting when a new enum type was added when creating a table but not when a column with new enum was added to an existing table

this adds support for detecting that

this also drops the type when a column is removed with that type or a table is dropped

also fixes #114 where adding a new enum type didn't have the op.drop_type since we explicitly handle the type being created and add op.add_enum_type() before the enum is used
@lolopinto
Copy link
Owner Author

turns out this is intentional based on how the Enum type works:

https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/dialects/postgresql/base.py#L1395-L1469

When the builtin type :class:_types.Enum is used and the
:paramref:.Enum.native_enum flag is left at its default of
True, the PostgreSQL backend will use a :class:_postgresql.ENUM
type as the implementation, so the special create/drop rules
will be used.
The create/drop behavior of ENUM is necessarily intricate, due to the
awkward relationship the ENUM type has in relationship to the
parent table, in that it may be "owned" by just a single table, or
may be shared among many tables.
When using :class:_types.Enum or :class:_postgresql.ENUM
in an "inline" fashion, the CREATE TYPE and DROP TYPE is emitted
corresponding to when the :meth:_schema.Table.create and
:meth:_schema.Table.drop
methods are called::
table = Table('sometable', metadata,
Column('some_enum', ENUM('a', 'b', 'c', name='myenum'))
)
table.create(engine) # will emit CREATE ENUM and CREATE TABLE
table.drop(engine) # will emit DROP TABLE and DROP ENUM

lolopinto added a commit that referenced this issue Aug 25, 2020
the alembic autogenerate code was apparently only detecting when a new enum type was added when creating a table but not when a column with new enum was added to an existing table

this adds support for detecting that

this also drops the type when a column is removed with that type or a table is dropped

also fixes #114 where adding a new enum type didn't have the op.drop_type since we explicitly handle the type being created and add op.add_enum_type() before the enum is used
@lolopinto
Copy link
Owner Author

^ didn't work. probably/maybe because of how alembic calls the create types in autogenerate mode which is different from what the comment said. So fixed in #117 to explicitly create/drop types and not depend on the table

lolopinto added a commit that referenced this issue Aug 25, 2020
* detect enum type added when a new column is added

the alembic autogenerate code was apparently only detecting when a new enum type was added when creating a table but not when a column with new enum was added to an existing table

this adds support for detecting that

this also drops the type when a column is removed with that type or a table is dropped

also fixes #114 where adding a new enum type didn't have the op.drop_type since we explicitly handle the type being created and add op.add_enum_type() before the enum is used

* cleanup

* use postgresql  enum + change the rendering to use create_type=False when rendering the enum so that we don't get the create type and we handle both the create type and the drop type

* .
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 a pull request may close this issue.

1 participant