Openstack数据库管理工具alembic更新Enum类型
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,在使用alembic开发管理数据库时,会遇到一个比较麻烦的问题,就是变更某列的枚举类型,事实上使用sql命令变更相当的简单,一条alter的执行即可:ALTER TYPE status ADD val
千家信息网最后更新 2025年11月08日Openstack数据库管理工具alembic更新Enum类型
在使用alembic开发管理数据库时,会遇到一个比较麻烦的问题,就是变更某列的枚举类型,事实上使用sql命令变更相当的简单,一条alter的执行即可:
ALTER TYPE status ADD value 'output_limit_exceeded' after 'timed_out';#删除DROP TYPE status
但这样并不能满足alembic管理的初衷,也无法实现downgrade。
使用google搜索关键字"alembic enum type",第一个出现的是stackflow的一个帖子Altering an Enum field using Alembic。
from alembic import opimport sqlalchemy as saold_options = ('nonexistent_executable', 'signal', 'success', 'timed_out')new_options = sorted(old_options + ('output_limit_exceeded',))old_type = sa.Enum(*old_options, name='status')new_type = sa.Enum(*new_options, name='status')tmp_type = sa.Enum(*new_options, name='_status')tcr = sa.sql.table('testcaseresult', sa.Column('status', new_type, nullable=False)) def upgrade(): # Create a tempoary "_status" type, convert and drop the "old" type tmp_type.create(op.get_bind(), checkfirst=False) op.execute('ALTER TABLE testcaseresult ALTER COLUMN status TYPE _status' ' USING status::text::_status') old_type.drop(op.get_bind(), checkfirst=False) # Create and convert to the "new" status type new_type.create(op.get_bind(), checkfirst=False) op.execute('ALTER TABLE testcaseresult ALTER COLUMN status TYPE status' ' USING status::text::status') tmp_type.drop(op.get_bind(), checkfirst=False) def downgrade(): # Convert 'output_limit_exceeded' status into 'timed_out' op.execute(tcr.update().where(tcr.c.status==u'output_limit_exceeded') .values(status='timed_out')) # Create a tempoary "_status" type, convert and drop the "new" type tmp_type.create(op.get_bind(), checkfirst=False) op.execute('ALTER TABLE testcaseresult ALTER COLUMN status TYPE _status' ' USING status::text::_status') new_type.drop(op.get_bind(), checkfirst=False) # Create and convert to the "old" status type old_type.create(op.get_bind(), checkfirst=False) op.execute('ALTER TABLE testcaseresult ALTER COLUMN status TYPE status' ' USING status::text::status') tmp_type.drop(op.get_bind(), checkfirst=False)这个方法提供了解决了问题,但稍显繁琐。我们可以做一下简单的封装,生成一个通用函数:
def upgrade_enum(table, column_name, enum_name, old_options, new_options): old_type = sa.Enum(*old_options, name=enum_name) new_type = sa.Enum(*new_options, name=enum_name) tmp_type = sa.Enum(*new_options, name="_" + enum_name) # Create a temporary type, convert and drop the "old" type tmp_type.create(op.get_bind(), checkfirst=False) op.execute( u'ALTER TABLE {0} ALTER COLUMN {1} TYPE _{2}' u' USING {1}::text::_{2}'.format( table, column_name, enum_name ) ) old_type.drop(op.get_bind(), checkfirst=False) # Create and convert to the "new" type new_type.create(op.get_bind(), checkfirst=False) op.execute( u'ALTER TABLE {0} ALTER COLUMN {1} TYPE {2}' u' USING {1}::text::{2}'.format( table, column_name, enum_name ) ) tmp_type.drop(op.get_bind(), checkfirst=False)这样就可以直接通过传参直接来执行升级或降级操作了。
操作环境:
alembic-0.8.2-bash-4.2$ psql --versionpsql (PostgreSQL) 9.3.10
管理
问题
数据
数据库
类型
繁琐
事实
事实上
关键
关键字
函数
初衷
命令
就是
帖子
方法
环境
麻烦
升级
封装
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
恐龙岛6分钟一个点服务器
电脑服务器由什么配件组成
如何搭建网关服务器
计算机网络技术考研容易吗
深圳中兴通讯的软件开发职位好吗
网络技术专业定义
数据库索引思维导图
易安信服务器固件
在线教育老师网络安全
软件开发公司商标
寻找软件开发项目
胶州路租房软件开发
诸城网络技术公司
广联达软件开发
数据库数数据不要重复的
手机网络聊天软件开发
给我找一个网络安全知识的手抄报
服务器登录界面在哪里
出售淘宝店铺云服务器
地基基础基础软件开发
数据库自然连接是什么
成都公司内部网络安全电话
平谷区品牌软件开发技术指导
扫黄打非网络安全宣传资料
数据库创建表的语句
网络安全 考公务员
乘风破浪的网络安全吗
中冶南方自动化软件开发笔试
服务器远程管理设备
浪潮信息发布元宇宙服务器