|
| 1 | +"""empty message |
| 2 | +
|
| 3 | +Revision ID: 16f917e0f7c7 |
| 4 | +Revises: 0b3e7aac77c7 |
| 5 | +Create Date: 2025-09-01 10:24:21.024944 |
| 6 | +
|
| 7 | +""" |
| 8 | +from alembic import op |
| 9 | +import sqlalchemy as sa |
| 10 | +from sqlalchemy.dialects import postgresql |
| 11 | + |
| 12 | +# revision identifiers, used by Alembic. |
| 13 | +revision = '16f917e0f7c7' |
| 14 | +down_revision = '0b3e7aac77c7' |
| 15 | +branch_labels = None |
| 16 | +depends_on = None |
| 17 | + |
| 18 | +# old_enum = postgresql.ENUM( |
| 19 | +# 'IN_PROGRESS', 'COMPLETED', |
| 20 | +# 'CANCELLED', 'ARTISAN_ARRIVED', |
| 21 | +# name='bookingstatusenum' |
| 22 | +# ) |
| 23 | +# new_enum = postgresql.ENUM( |
| 24 | +# 'IN_PROGRESS', 'COMPLETED', 'ARTISAN_CANCELLED', |
| 25 | +# 'CUSTOMER_CANCELLED', 'ARTISAN_ARRIVED', |
| 26 | +# name='bookingstatusenum' |
| 27 | +# ) |
| 28 | +old_enum_name = 'bookingstatusenum' |
| 29 | +new_enum_name = 'bookingstatusenum_new' |
| 30 | +old_enum_values = ('IN_PROGRESS', 'COMPLETED', 'CANCELLED', 'ARTISAN_ARRIVED') |
| 31 | +new_enum_values = ('IN_PROGRESS', 'COMPLETED', 'ARTISAN_CANCELLED', 'CUSTOMER_CANCELLED', 'ARTISAN_ARRIVED') |
| 32 | + |
| 33 | + |
| 34 | +def upgrade(): |
| 35 | + op.execute(f"CREATE TYPE {new_enum_name} AS ENUM ({', '.join(f'\'{v}\'' for v in new_enum_values)})") |
| 36 | + op.alter_column( |
| 37 | + 'booking', 'status', |
| 38 | + existing_type=postgresql.ENUM(*old_enum_values, name=old_enum_name), |
| 39 | + type_=postgresql.ENUM(*new_enum_values, name=new_enum_name), |
| 40 | + postgresql_using=f"status::text::{new_enum_name}" |
| 41 | + ) |
| 42 | + op.execute(f"DROP TYPE {old_enum_name}") |
| 43 | + op.execute(f"ALTER TYPE {new_enum_name} RENAME TO {old_enum_name}") |
| 44 | + op.execute(sa.text(f""" |
| 45 | + UPDATE booking |
| 46 | + SET status = CASE |
| 47 | + WHEN status::text = 'IN_PROGRESS' THEN '{new_enum_values[0]}'::{old_enum_name} |
| 48 | + WHEN status::text = 'COMPLETED' THEN '{new_enum_values[1]}'::{old_enum_name} |
| 49 | + ELSE status |
| 50 | + END |
| 51 | + """)) |
| 52 | + # ### commands auto generated by Alembic - please adjust! ### |
| 53 | + with op.batch_alter_table('booking', schema=None) as batch_op: |
| 54 | + batch_op.create_index(batch_op.f('ix_booking_artisan_id'), ['artisan_id'], unique=False) |
| 55 | + batch_op.create_index(batch_op.f('ix_booking_category_id'), ['category_id'], unique=False) |
| 56 | + batch_op.create_index(batch_op.f('ix_booking_contract_type'), ['contract_type'], unique=False) |
| 57 | + batch_op.create_index(batch_op.f('ix_booking_customer_id'), ['customer_id'], unique=False) |
| 58 | + batch_op.create_index(batch_op.f('ix_booking_payment_method'), ['payment_method'], unique=False) |
| 59 | + batch_op.create_index(batch_op.f('ix_booking_settlement_type'), ['settlement_type'], unique=False) |
| 60 | + # ### end Alembic commands ### |
| 61 | + |
| 62 | + |
| 63 | +def downgrade(): |
| 64 | + # ### commands auto generated by Alembic - please adjust! ### |
| 65 | + op.execute(f"CREATE TYPE {old_enum_name}_old AS ENUM ({', '.join(f'\'{v}\'' for v in old_enum_values)})") |
| 66 | + op.alter_column( |
| 67 | + 'booking', 'status', |
| 68 | + existing_type=postgresql.ENUM(*new_enum_values, name=old_enum_name), |
| 69 | + type_=postgresql.ENUM(*old_enum_values, name=f'{old_enum_name}_old'), |
| 70 | + postgresql_using=f"status::text::{old_enum_name}_old" |
| 71 | + ) |
| 72 | + op.execute(f"DROP TYPE {old_enum_name}") |
| 73 | + op.execute(f"ALTER TYPE {old_enum_name}_old RENAME TO {old_enum_name}") |
| 74 | + op.execute(sa.text(f""" |
| 75 | + UPDATE booking |
| 76 | + SET status = CASE |
| 77 | + WHEN status::text = 'IN_PROGRESS' THEN '{old_enum_values[0]}'::{old_enum_name} |
| 78 | + WHEN status::text = 'COMPLETED' THEN '{old_enum_values[1]}'::{old_enum_name} |
| 79 | + ELSE status |
| 80 | + END |
| 81 | + """)) |
| 82 | + with op.batch_alter_table('booking', schema=None) as batch_op: |
| 83 | + batch_op.drop_index(batch_op.f('ix_booking_settlement_type')) |
| 84 | + batch_op.drop_index(batch_op.f('ix_booking_payment_method')) |
| 85 | + batch_op.drop_index(batch_op.f('ix_booking_customer_id')) |
| 86 | + batch_op.drop_index(batch_op.f('ix_booking_contract_type')) |
| 87 | + batch_op.drop_index(batch_op.f('ix_booking_category_id')) |
| 88 | + batch_op.drop_index(batch_op.f('ix_booking_artisan_id')) |
| 89 | + # ### end Alembic commands ### |
0 commit comments