99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

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類型-創新互聯
文章分享:http://www.yijiale78.com/article10/ceecgo.html

成都網站建設公司_創新互聯,為您提供網站改版關鍵詞優化網站制作網站排名網頁設計公司自適應網站

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站建設網站維護公司