Skip to main content

Alembic Enums

example workflow

Support for migrating PostgreSQL enums with Alembic

The package doesn't detect enum changes or generate migration code automatically, but it provides a helper class to run the enum migrations in Alembic migration scripts.

Problem statement

When you define an enum column with SQLAlchemy, the initial migration defines a custom enum type.

Once the enum type is created, ALTER TYPE allows you to add new values or rename existing ones, but not delete them.

If you need to delete a value from an enum, you must create a new enum type and migrate all the columns to use the new type.

Installation

pip install alembic-enums

Usage

Assume you decided to rename the state enum values active and inactive to enabled and disabled:

 class Resource(Base):
     __tablename__ = "resources"
     id = Column(Integer, primary_key=True)
     name = Column(String(255), nullable=False)
-    state = Column(Enum("enabled", "disabled", name="resource_state"), nullable=False)
+    state = Column(Enum("active", "archived", name="resource_state"), nullable=False)

To migrate the database, we create a new empty migration with alembic revision -m "Rename enum values" and add the following code to the generated migration script:

from alembic import op

from alembic_enums import EnumMigration, Column

# Define a target column. As in PostgreSQL, the same enum can be used in multiple
# column definitions, you may have more than one target column.
# The constructor arguments are the table name, the column name, and the
# server_default values for the old and new enum types.
column = Column("resources", "state", old_server_default=None, new_server_default=None)

# Define an enum migration. It defines the old and new enum values
# for the enum, and the list of target columns.
enum_migration = EnumMigration(
    op=op,
    enum_name="resource_state",
    old_options=["enabled", "disabled"],
    new_options=["active", "archived"],
    columns=[column],
)

# Define upgrade and downgrade operations. Inside upgrade_ctx and downgrade_ctx
# context managers, you can update your data.

def upgrade():
    with enum_migration.upgrade_ctx():
        enum_migration.update_value(column, "enabled", "active")
        enum_migration.update_value(column, "disabled", "archived")


def downgrade():
    with enum_migration.downgrade_ctx():
        enum_migration.update_value(column, "active", "enabled")
        enum_migration.update_value(column, "archived", "disabled")

Under the hood, the EnumMigration class creates a new enum type, updates the target columns to use the new enum type, and deletes the old enum type.

Change column default values

To change the column default values, pass corresponding values to new_server_default and old_server_default arguments of the Column constructor. The new_server_default is used on upgrade, and the old_server_default is used on downgrade.

IMPORTANT: Setting the server_default value to None will remove the default value from the column. If you want to keep the default value as is, set old_server_default and new_server_default to the same value.

For example, to change the default value of the state column from enabled to active:

from alembic_enums import Column

column = Column(
    "resources",
    "state",
    old_server_default="enabled",
    new_server_default="active",
)

API reference

EnumMigration

A helper class to run enum migrations in Alembic migration scripts.

Constructor arguments:

  • op: an instance of alembic.operations.Operations
  • enum_name: the name of the enum type
  • old_options: a list of old enum values
  • new_options: a list of new enum values
  • columns: a list of Column instances that use the enum type
  • schema: the optional schema of the enum

Methods:

  • upgrade_ctx(): a context manager that creates a new enum type, updates the target columns to use the new enum type, and deletes the old enum type
  • downgrade_ctx(): a context manager that performs the opposite operations.
  • update_value(column, old_value, new_value): a helper method to update the value of the column to new_value where it was old_value before. It's useful to update the data in the upgrade and downgrade operations within the upgrade_ctx and downgrade_ctx context managers.
  • upgrade(): a shorthand for with upgrade_ctx(): pass.
  • downgrade(): a shorthand for with downgrade_ctx(): pass.

Column

A data class to define a target column for an enum migration.

Constructor arguments:

  • table_name: the name of the table
  • column_name: the name of the column
  • old_server_default: the old server_default value. When set to None, the server_default value is removed on downgrade.
  • new_server_default: the new server_default value. When set to None, the server_default value is removed on upgrade.
  • schema: the optional schema of the table

Release files for alembic-enums 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for alembic-enums 0.4.1
File Size Uploaded
alembic_enums-0.4.1.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for alembic-enums 0.4.1
File Interpreter ABI Platform
alembic_enums-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.6 kB

Release files / alembic_enums-0.4.1.tar.gz

Download URL alembic_enums-0.4.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
50a806e4b56c2ff94593ff64e45e02d2307b05ebedbc984c2140b6a3f8734a54
BLAKE2b-256 checksum
How to use checksums
e8de5182ac4b77c4a5413a31ea992b5eab03cba8af7b819afc9c3aca18769b3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.10.8 Darwin/21.6.0

Release files / alembic_enums-0.4.1-py3-none-any.whl

Download URL alembic_enums-0.4.1-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0ff2551efa0e4720deca80f2d35f681611522957ca9c8bd3290f6d55e24076ed
BLAKE2b-256 checksum
How to use checksums
052934603d930e27d3e75e100b1c0f108b6a8b9f690982c01df1305e4515693d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.10.8 Darwin/21.6.0

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page