Package: alembic-postgresql-enum
alembic-postgresql-enumAlembic autogenerate support for creation, alteration and deletion of enumsAlembic will now automatically:Create enums that currently are not in postgres schemaRemove/add/alter enum valuesReorder enum valuesDelete unused enums from schemaIf you are curious to know about analogs and reasons for this library to exist seealternatives and motivationUsageInstall library:pip install alembic-postgresql-enumAdd the line:# env.pyimportalembic_postgresql_enum...To the top of your migrations/env.py file.FeaturesCreation of enumsDeletion of unreferenced enumsCreation of new enum valuesDeletion of enums valuesRenaming of enum valuesCreation of enumWhen table is createdclassMyEnum(enum.Enum):one=1two=2three=3classExampleTable(BaseModel):test_field=Column(Integer,primary_key=True,autoincrement=False)enum_field=Column(postgresql.ENUM(MyEnum))This code will generate migration given below:defupgrade():# ### commands auto generated by Alembic - please adjust! #### this line is generated by our librarysa.Enum('one','two','three',name='myenum').create(op.get_bind())op.create_table('example_table',sa.Column('test_field',sa.Integer(),nullable=False),# create_type=False argument is now present on postgresql.ENUM as library takes care of enum creationsa.Column('enum_field',postgresql.ENUM('one','two','three',name='myenum',create_type=False),nullable=True),sa.PrimaryKeyConstraint('test_field'))# ### end Alembic commands ###defdowngrade():# ### commands auto generated by Alembic - please adjust! #### drop_table does not drop enum by alembicop.drop_table('example_table')# It is dropped by ussa.Enum('one','two','three',name='myenum').drop(op.get_bind())# ### end Alembic commands ###When column is addedclassMyEnum(enum.Enum):one=1two=2three=3classExampleTable(BaseModel):test_field=Column(Integer,primary_key=True,autoincrement=False)# this column has just been addedenum_field=Column(postgresql.ENUM(MyEnum))This code will generate migration given below:defupgrade():# ### commands auto generated by Alembic - please adjust! #### this line is generated by our librarysa.Enum('one','two','three',name='myenum').create(op.get_bind())# create_type=False argument is now present on postgresql.ENUM as library takes care of enum creationop.add_column('example_table',sa.Column('enum_field',postgresql.ENUM('one','two','three',name='myenum',create_type=False),nullable=False))# ### end Alembic commands ###defdowngrade():# ### commands auto generated by Alembic - please adjust! ###op.drop_column('example_table','enum_field')# enum is explicitly dropped as it is no longer usedsa.Enum('one','two','three',name='myenum').drop(op.get_bind())# ### end Alembic commands ###Deletion of unreferenced enumIf enum is defined in postgres schema, but its mentions removed from code - It will be automatically removedclassExampleTable(BaseModel):test_field=Column(Integer,primary_key=True,autoincrement=False)# enum_field is removed from tabledefupgrade():# ### commands auto generated by Alembic - please adjust! ###op.drop_column('example_table','enum_field')sa.Enum('one','two','four',name='myenum').drop(op.get_bind())# ### end Alembic commands ###defdowngrade():# ### commands auto generated by Alembic - please adjust! ###sa.Enum('one','two','four',name='myenum').create(op.get_bind())op.add_column('example_table',sa.Column('enum_field',postgresql.ENUM('one','two','four',name='myenum',create_type=False),autoincrement=False,nullable=True))# ### end Alembic commands ###Creation of new enum valuesIf new enum value is defined sync_enum_values function call will be added to migration to account for itclassMyEnum(enum.Enum):one=1two=2three=3four=4# New enum valuedefupgrade():# ### commands auto generated by Alembic - please adjust! ###op.sync_enum_values('public','myenum',['one','two','three','four'],[('example_table','enum_field')],enum_values_to_rename=[])# ### end Alembic commands ###defdowngrade():# ### commands auto generated by Alembic - please adjust! ###op.sync_enum_values('public','myenum',['one','two','three'],[('example_table','enum_field')],enum_values_to_rename=[])# ### end Alembic commands ###Deletion of enums valuesIf enum value is removed it also will be detectedclassMyEnum(enum.Enum):one=1two=2# three = 3 removeddefupgrade():# ### commands auto generated by Alembic - please adjust! ###op.sync_enum_values('public','myenum',['one','two'],[('example_table','enum_field')],enum_values_to_rename=[])# ### end Alembic commands ###defdowngrade():# ### commands auto generated by Alembic - please adjust! ###op.sync_enum_values('public','myenum',['one','two','three'],[('example_table','enum_field')],enum_values_to_rename=[])# ### end Alembic commands ###Rename enum valueIn this case you must manually edit migrationclassMyEnum(enum.Enum):one=1two=2three=3# renamed from `tree`This code will generate this migration:defupgrade():# ### commands auto generated by Alembic - please adjust! ###op.sync_enum_values('public','myenum',['one','two','three'],[('example_table','enum_field')],enum_values_to_rename=[])# ### end Alembic commands ###defdowngrade():# ### commands auto generated by Alembic - please adjust! ###op.sync_enum_values('public','myenum',['one','two','tree'],[('example_table','enum_field')],enum_values_to_rename=[])# ### end Alembic commands ###This migration will cause problems with existing rows that references MyEnumSo adjust migration like thatdefupgrade():op.sync_enum_values('public','myenum',['one','two','three'],[('example_table','enum_field')],enum_values_to_rename=[('tree','three')])defdowngrade():op.sync_enum_values('public','myenum',['one','two','tree'],[('example_table','enum_field')],enum_values_to_rename=[('three','tree')])Do not forget to switch places old and new values for downgradeAll defaults in postgres will be renamed automatically as well