More attempts with migration fun.

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2023-11-14 11:47:34 -05:00
parent 8350408512
commit e4c5fdb013

View File

@@ -9,14 +9,31 @@ from re import I
from alembic import op
import sqlalchemy as sa
# Copied pattern for adjusting a enum as defined in 0359_more_permissions
revision = "0404_expire_invites"
down_revision = "0403_add_carrier"
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)
def upgrade():
op.execute("insert into invite_status_type values ('expired')")
# 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'")
def downgrade():
op.execute("delete from invite_status_type where name = 'expired'")
op.execute(f"DELETE FROM invited_users WHERE status = 'expired'")
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}")