Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Openstack database management tool alembic updates the Enum type

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

When using alembic to develop and manage the database, you will encounter a troublesome problem, that is, to change the enumeration type of a column. In fact, it is quite simple to use the sql command to change. An alter can be executed:

ALTER TYPE status ADD value 'output_limit_exceeded' after' timed_out';# delete DROP TYPE status

However, this can not meet the original intention of alembic management, nor can it achieve downgrade.

Use the google search keyword "alembic enum type", and the first thing that appears is a post from 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)

This method provides a solution to the problem, but it is slightly cumbersome. We can do a simple encapsulation to generate a general function:

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)

In this way, the upgrade or downgrade operation can be performed directly by passing parameters.

Operating environment:

Alembic-0.8.2-bash-4.2$ psql-- versionpsql (PostgreSQL) 9.3.10

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report