covid-formity/migrations/versions/b0c9f82e386e_add_delegated_...

55 lines
2.1 KiB
Python

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