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

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

分享题目:Openstack数据库管理工具alembic更新Enum类型-创新互联
文章分享:https://www.cdcxhl.com/article10/ceecgo.html

成都网站建设公司_创新互联,为您提供网站改版关键词优化网站制作网站排名网页设计公司自适应网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司