支持使用Alembic迁移PostgreSQL枚举类型
项目描述
Alembic Enums
支持使用Alembic迁移PostgreSQL枚举类型
该包不会检测枚举变化或自动生成迁移代码,但提供了一个辅助类来在Alembic迁移脚本中运行枚举迁移。
问题陈述
当你使用SQLAlchemy定义枚举列时,初始迁移定义了一个自定义的枚举类型。
一旦枚举类型创建,ALTER TYPE 允许你添加新值或重命名现有值,但不能删除它们。
如果你需要从枚举中删除一个值,你必须创建一个新的枚举类型并将所有列迁移到使用新的类型。
安装
pip install alembic-enums
使用方法
假设你决定将 state
枚举值 active
和 inactive
重命名为 enabled
和 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)
要迁移数据库,我们创建一个新的空迁移,使用 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_ctx
和downgrade_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 查看散列)
关闭
alembic_enums-0.4.1.tar.gz的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 50a806e4b56c2ff94593ff64e45e02d2307b05ebedbc984c2140b6a3f8734a54 |
|
MD5 | 3496f2411cf2ce7318ce5ea86f34301e |
|
BLAKE2b-256 | e8de5182ac4b77c4a5413a31ea992b5eab03cba8af7b819afc9c3aca18769b3f |
关闭
alembic_enums-0.4.1-py3-none-any.whl的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 0ff2551efa0e4720deca80f2d35f681611522957ca9c8bd3290f6d55e24076ed |
|
MD5 | 2402782d251e750a09d7144859458dc1 |
|
BLAKE2b-256 | 052934603d930e27d3e75e100b1c0f108b6a8b9f690982c01df1305e4515693d |