Openstack数据库管理工具alembic更新Enum类型
admin
2023-05-25 08:21:32
0

        在使用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 sa

old_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 --version
psql (PostgreSQL) 9.3.10


相关内容

热门资讯

凯美瑞空调传感器故障 凯美瑞的空调传感器是一个非常重要的元件,它负责控制车内的温度,确保车内空气的清新和舒适。然而,由于使...
电脑的护眼模式在哪里设置 如何... 1、电脑桌面右击,选择个性化 2、选择窗口颜色 3、挑选绿色的颜色,绿色比较护眼 4、点...
大逆转!全球拼命建核电站,什么... 文|凯风AI的尽头是算力,算力的尽头是电力。01全球,正在拼命建核电站。日前,国常会一次性核准浙江金...
电脑上书名号怎么打 怎样在电... 电脑上打书名号的步骤分为以下三步:   第一步:在电脑上建立一个文档或聊天窗口,打开此文档; ...
电脑上的时间怎么设置自动更新 1、先点一下电脑屏幕右下角的时间,左键单击一下。 2、然后左键点击【更改日期和时间设置】...
在电脑上怎么截屏 电脑上截屏的... 电脑怎么截屏呢?具体操作如下:1、点击桌面右下角通知图标。2、在弹出的菜单栏中选择屏幕截图。3、框选...
十亿分之一米尺度上,材料有了新... 纳米是非常小的长度单位,1纳米等于十亿分之一米。如果把直径为1纳米的小球放到乒乓球上,就好似把乒乓球...
原创 老... 炒菜机器人,对于家电厂商来说,并不陌生。今年以来,之所以这一品类再次走热,本质上还是AI技术的赋能,...
首次!祝贺中国科学家斩获国际大... 当地时间8月3日,在意大利佛罗伦萨举办的第46届国际空间研究委员会(COSPAR)科学大会上, 中国...
苏泊尔AI广告“女生洗澡大叔帮... 封面来源 | 视频截图近日,苏泊尔官方认证店铺账号“苏泊尔个护电器旗舰店”在推销清洁机产品时,接连发...