跳转到主要内容

支持使用Alembic迁移PostgreSQL枚举类型

项目描述

Alembic Enums

example workflow

支持使用Alembic迁移PostgreSQL枚举类型

该包不会检测枚举变化或自动生成迁移代码,但提供了一个辅助类来在Alembic迁移脚本中运行枚举迁移。

问题陈述

当你使用SQLAlchemy定义枚举列时,初始迁移定义了一个自定义的枚举类型

一旦枚举类型创建,ALTER TYPE 允许你添加新值或重命名现有值,但不能删除它们。

如果你需要从枚举中删除一个值,你必须创建一个新的枚举类型并将所有列迁移到使用新的类型。

安装

pip install alembic-enums

使用方法

假设你决定将 state 枚举值 activeinactive 重命名为 enableddisabled

 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)

要迁移数据库,我们创建一个新的空迁移,使用 alembic revision -m "Rename enum values" 并将以下代码添加到生成的迁移脚本中

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")

在内部,EnumMigration 类创建一个新的枚举类型,更新目标列以使用新的枚举类型,并删除旧的枚举类型。

更改列默认值

要更改列默认值,将相应值传递给Column构造函数的新_server_default和old_server_default参数。new_server_default用于升级,old_server_default用于降级。

重要提示:将服务器默认值设置为None将删除列的默认值。如果你想保留默认值不变,将old_server_default和new_server_default设置为相同的值。

例如,将state列的默认值从enabled更改为active

from alembic_enums import Column

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

API参考

EnumMigration

Alembic迁移脚本中运行枚举迁移的辅助类。

构造函数参数

  • op:一个alembic.operations.Operations实例
  • enum_name:枚举类型的名称
  • old_options:旧枚举值列表
  • new_options:新枚举值列表
  • columns:使用枚举类型的Column实例列表
  • schema:枚举的可选模式

方法

  • upgrade_ctx():创建新的枚举类型,更新目标列以使用新的枚举类型,并删除旧枚举类型的上下文管理器
  • downgrade_ctx():执行相反操作的上下文管理器。
  • update_value(column, old_value, new_value):一个辅助方法,用于将column的值更新为new_value,在之前它是old_value。这在upgrade_ctxdowngrade_ctx上下文管理器中的升级和降级操作中很有用。
  • upgrade():是with upgrade_ctx(): pass的简写。
  • downgrade():是with downgrade_ctx(): pass的简写。

Column

用于定义枚举迁移的目标列的数据类。

构造函数参数

  • table_name:表的名称
  • column_name:列的名称
  • old_server_default:旧server_default值。当设置为None时,在降级时将删除server_default值。
  • new_server_default:新server_default值。当设置为None时,在升级时将删除server_default值。
  • schema:表的可选模式

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

alembic_enums-0.4.1.tar.gz (5.5 kB 查看散列)

上传时间

构建分布

alembic_enums-0.4.1-py3-none-any.whl (6.1 kB 查看散列)

上传时间 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面