Skip to main content

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

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', 'tree', 'three'], [('example_table', 'enum_field')])
    op.execute("""UPDATE example_table SET enum_field = 'three' WHERE enum_field = 'tree'""")
    op.sync_enum_values('public', 'myenum', ['one', 'two', 'three'], [('example_table', 'enum_field')])


def downgrade():
    op.sync_enum_values('public', 'myenum', ['one', 'two', 'tree', 'three'], [('example_table', 'enum_field')])
    op.execute("""UPDATE example_table SET enum_field = 'tree' WHERE enum_field = 'three'""")
    op.sync_enum_values('public', 'myenum', ['one', 'two', 'tree'], [('example_table', 'enum_field')])

Expand old values with new one, update all old values with new one, remove old enum value

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alembic_postgresql_enum-0.1.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alembic_postgresql_enum-0.1.2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file alembic_postgresql_enum-0.1.2.tar.gz.

File metadata

  • Download URL: alembic_postgresql_enum-0.1.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.11.0rc2 Linux/5.19.0-42-generic

File hashes

Hashes for alembic_postgresql_enum-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d92e2aa7a2841082d898a733fb3aec26a5dd24a910b0f5b4c5f194a5ea40bdab
MD5 d523293ac9c60b45c09c958a83a35365
BLAKE2b-256 659848785c4694f7e5189c7f7bc08ce1c90235b07858e1e3b0e53540670a4397

See more details on using hashes here.

File details

Details for the file alembic_postgresql_enum-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for alembic_postgresql_enum-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1454709341286283a72ce9c62d32d0f7731b18c81198d977585123fb6bc931e4
MD5 f931ab6d14ccf3188de6c7965834ccd7
BLAKE2b-256 8f52f56fd50e9f6a8ac1cae4bc999bb2c8edd51304a7a895714cf7af9adbabe1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page