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 env.py.
Examples
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 - I 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 ###
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')])
# ### 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')])
# ### end Alembic commands ###
It is fine if you do not have any data in your database. But in most cases it is not.
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')])
Expand old values with new one, update all old values with new one, remove old enum value
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.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c925e9cb0936b50fede69ec8f1d7dc3753ec8af0b4943c44eab1a79ba78a6cc2 |
|
MD5 | d7a9f2951dbd5bf2ed23fd9582e00fe0 |
|
BLAKE2b-256 | 23cadac21d206f8c04c0c53200ad71e765f7b27cabd1a364724200e684a04b0a |
Hashes for alembic_postgresql_enum-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1eb6c6634e3fa146011d1941100ef5be0759fb81ac11753dbff9d4746ba812c |
|
MD5 | 75d1c8c791583d1e5163a549c21adfe8 |
|
BLAKE2b-256 | 296b72b4417ee2421b665f94f7ee5b8c0097bf8a463842532642623fe81c83c4 |