covid-formity/migrations/versions/fc0fbabc5fb6_add_pickuppend...

57 lines
2.3 KiB
Python

"""Add pickuppending/shippingpending status
Revision ID: fc0fbabc5fb6
Revises: 6e57deac23f2
Create Date: 2020-04-09 16:21:00.191895
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'fc0fbabc5fb6'
down_revision = '6e57deac23f2'
branch_labels = None
depends_on = None
old_options = ('new', 'confirmed', 'allocated', 'fulfilled', 'rejected', 'spam', 'delegated', 'intransit')
new_options = sorted(old_options + ('shippingpending','pickuppending'))
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'shippingpending')
.values(status='intransit'))
op.execute(fsr.update().where(fsr.c.status==u'pickupppending')
.values(status='allocated'))
# 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)