Files
notifications-api/migrations/versions/0404_expire_invites.py
T

42 lines
1.2 KiB
Python
Raw Normal View History

2023-11-10 16:14:03 -05:00
"""
Revision ID: 0404_expire_invites
Revises: 0403_add_carrier
Create Date: 2023-11-10 15:52:07.348485
"""
2024-04-01 15:12:33 -07:00
2023-11-10 16:14:03 -05:00
from re import I
2023-12-08 21:43:52 -05:00
2023-11-10 16:14:03 -05:00
import sqlalchemy as sa
2023-12-08 21:43:52 -05:00
from alembic import op
2023-11-10 16:14:03 -05:00
2023-11-14 11:47:34 -05:00
# Copied pattern for adjusting a enum as defined in 0359_more_permissions
2023-11-10 16:14:03 -05:00
revision = "0404_expire_invites"
down_revision = "0403_add_carrier"
2023-11-14 11:47:34 -05:00
enum_name = "invited_users_status_types"
tmp_name = "tmp_" + enum_name
old_options = ("pending", "accepted", "cancelled")
old_type = sa.Enum(*old_options, name=enum_name)
2023-11-10 16:14:03 -05:00
def upgrade():
2023-11-14 11:47:34 -05:00
# ALTER TYPE must be run outside of a transaction block (see link below for details)
# https://alembic.sqlalchemy.org/en/latest/api/runtime.html#alembic.runtime.migration.MigrationContext.autocommit_block
with op.get_context().autocommit_block():
op.execute(f"ALTER TYPE {enum_name} ADD VALUE 'expired'")
2023-11-10 16:14:03 -05:00
def downgrade():
2023-11-14 12:28:18 -05:00
op.execute(f"DELETE FROM invited_users WHERE status in ('expired')")
2023-11-14 11:47:34 -05:00
op.execute(f"ALTER TYPE {enum_name} RENAME TO {tmp_name}")
old_type.create(op.get_bind())
op.execute(
f"ALTER TABLE invited_users ALTER COLUMN status TYPE {enum_name} using status::text::{enum_name}"
)
op.execute(f"DROP TYPE {tmp_name}")