"""Add spam status Revision ID: 8b9b2791b0ba Revises: c94b0fdfe996 Create Date: 2020-03-30 18:12:38.934796 """ from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. revision = '8b9b2791b0ba' down_revision = 'c94b0fdfe996' branch_labels = None depends_on = None old_options = ('new', 'confirmed', 'allocated', 'fulfilled', 'rejected') new_options = sorted(old_options + ('spam',)) 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'spam') .values(status='rejected')) # 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)