"""Add intransit status Revision ID: b269beca8128 Revises: 62f8f7e5b9e6 Create Date: 2020-04-07 16:55:48.773104 """ from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. revision = 'b269beca8128' down_revision = '62f8f7e5b9e6' branch_labels = None depends_on = None old_options = ('new', 'confirmed', 'allocated', 'fulfilled', 'rejected', 'spam', 'delegated') new_options = sorted(old_options + ('intransit',)) old_type = sa.Enum(*old_options, name='status') new_type = sa.Enum(*new_options, name='status') tmp_type = sa.Enum(*new_options, name='_status') fsr = sa.sql.table('faceshield_request', 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 faceshield_request ALTER COLUMN status DROP DEFAULT, 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 faceshield_request ALTER COLUMN status TYPE status' ' USING status::text::status, ALTER COLUMN status SET DEFAULT \'new\'::status') tmp_type.drop(op.get_bind(), checkfirst=False) def downgrade(): op.execute(fsr.update().where(fsr.c.status==u'intransit') .values(status='fulfilled')) # Create a tempoary "_status" type, convert and drop the "new" type tmp_type.create(op.get_bind(), checkfirst=False) op.execute('ALTER TABLE faceshield_request ALTER COLUMN status DROP DEFAULT, 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 faceshield_request ALTER COLUMN status TYPE status' ' USING status::text::status, ALTER COLUMN status SET DEFAULT \'new\'::status') tmp_type.drop(op.get_bind(), checkfirst=False)