Alembic autogenerate support for creation, alteration and deletion of enums
Project description
alembic-postgresql-enum
Alembic autogenerate support for creation, alteration and deletion of enums
Alembic will now automatically:
- Create enums that currently are not in postgres schema
- Remove/add/alter enum values
- Reorder enum values
- Delete unused enums from schema
Usage
Install library:
pip install alembic-postgresql-enum
Add the line:
# env.py
import alembic_postgresql_enum
...
To the top of your migrations/env.py file.
Features
- Creation of enums
- Deletion of unreferenced enums
- Creation of new enum values
- Deletion of enums values
- Renaming of enum values
Creation of enum
class MyEnum(enum.Enum):
one = 1
two = 2
three = 3
class ExampleTable(BaseModel):
test_field = Column(Integer, primary_key=True, autoincrement=False)
enum_field = Column(sqlalchemy.Enum(MyEnum))
This code will generate migration given below:
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
sa.Enum('one', 'two', 'three', name='myenum').create(op.get_bind())
op.add_column('example_table', sa.Column('enum_field', sa.Enum('one', 'two', 'three', name='myenum'), nullable=False))
op.add_column('example_table', sa.Column('third_field', sa.Float(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('example_table', 'third_field')
op.drop_column('example_table', 'enum_field')
sa.Enum('one', 'two', 'three', name='myenum').drop(op.get_bind())
# ### end Alembic commands ###
Deletion of unreferenced enum
If enum is defined in postgres schema, but its mentions removed from code - It will be automatically removed
class ExampleTable(BaseModel):
test_field = Column(Integer, primary_key=True, autoincrement=False)
# enum_field is removed
def upgrade():
# ### 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 ###
def downgrade():
# ### 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'), autoincrement=False, nullable=True))
# ### end Alembic commands ###
Creation of new enum values
If new enum value is defined sync_enum_values function call will be added to migration to account for it
class MyEnum(enum.Enum):
one = 1
two = 2
three = 3
four = 4 # New enum value
def upgrade():
# ### 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 ###
def downgrade():
# ### 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 values
If enum value is removed it also will be detected
class MyEnum(enum.Enum):
one = 1
two = 2
# three = 3 removed
def upgrade():
# ### 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 ###
def downgrade():
# ### 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 value
In this case you must manually edit migration
class MyEnum(enum.Enum):
one = 1
two = 2
three = 3 # renamed from `tree`
This code will generate this migration:
def upgrade():
# ### 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 ###
def downgrade():
# ### 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 MyEnum
So adjust migration like that
def upgrade():
op.sync_enum_values('public', 'myenum', ['one', 'two', 'three'],
[('example_table', 'enum_field')],
enum_values_to_rename=[('tree', 'three')])
def downgrade():
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 downgrade
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for alembic_postgresql_enum-0.1.5a0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e060be7e453da6b07c7926ba88eb58e29c16a93a371f6259885287c2d95e3b8a |
|
MD5 | 36fbfdfc6d76007b5385c14ae4bc1d54 |
|
BLAKE2b-256 | 05e5f21acdc5b7777a34f89ec8595800edf2cf61c68bc7c96476e21ec5afd7ce |
Hashes for alembic_postgresql_enum-0.1.5a0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27371b70a72b6ee5c62bed8bad26c4ebc538c21a8ec9c3e80cb3f8924c303f8e |
|
MD5 | 231fd5dcc5e88feae88ab3cd6f155414 |
|
BLAKE2b-256 | bfbf9fefa5f3fbc991bad783f628e540e6c6c04ac673f55c2e905d0fd4b28365 |