"""fix enum clinic doctor Revision ID: 0ce7107c1910 Revises: a3a9b7d17bdd Create Date: 2025-05-13 12:20:49.384154 """ from typing import Sequence, Union from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import postgresql from sqlalchemy import text # revision identifiers, used by Alembic. revision: str = '0ce7107c1910' down_revision: Union[str, None] = 'a3a9b7d17bdd' branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: """Upgrade schema.""" # ### commands auto generated by Alembic - please adjust! ### # Check if the enum type exists before creating it conn = op.get_bind() result = conn.execute(text("SELECT EXISTS(SELECT 1 FROM pg_type WHERE typname = 'clinicdoctortype')")) enum_exists = result.scalar() if not enum_exists: # Create the new enum type if it doesn't exist op.execute(text("CREATE TYPE clinicdoctortype AS ENUM ('DOCTOR', 'NURSE')")) # Check if the new_role column already exists inspector = sa.inspect(conn) columns = inspector.get_columns('clinic_doctors') column_names = [column['name'] for column in columns] if 'new_role' not in column_names: # Add a temporary column with the new type op.add_column('clinic_doctors', sa.Column('new_role', postgresql.ENUM('DOCTOR', 'NURSE', name='clinicdoctortype'), nullable=True)) # Update the temporary column with values based on the old column op.execute(text("UPDATE clinic_doctors SET new_role = 'DOCTOR' WHERE role = 'DIRECTOR'")) op.execute(text("UPDATE clinic_doctors SET new_role = 'NURSE' WHERE role = 'PRACTICE_MANAGER'")) # Drop the old column and rename the new one op.drop_column('clinic_doctors', 'role') op.alter_column('clinic_doctors', 'new_role', new_column_name='role') # We need to handle the users table that depends on the clinicuserroles enum # First check if the users table has a clinicRole column that uses the enum has_clinic_role = False try: user_columns = inspector.get_columns('users') for column in user_columns: if column['name'] == 'clinicRole': has_clinic_role = True break except: # Table might not exist pass if has_clinic_role: # We need to update the users table to not use the enum before dropping it # First, create a temporary column with a string type op.add_column('users', sa.Column('temp_clinic_role', sa.String(), nullable=True)) # Copy the values - use double quotes to preserve case sensitivity in PostgreSQL op.execute(text('UPDATE users SET temp_clinic_role = "clinicRole"::text')) # Drop the old column and rename the new one op.drop_column('users', 'clinicRole') op.alter_column('users', 'temp_clinic_role', new_column_name='clinicRole') # Now we can safely drop the old enum type op.execute(text("DROP TYPE IF EXISTS clinicuserroles")) # ### end Alembic commands ### def downgrade() -> None: """Downgrade schema.""" # ### commands auto generated by Alembic - please adjust! ### # Create the old enum type op.execute(text("CREATE TYPE clinicuserroles AS ENUM ('DIRECTOR', 'PRACTICE_MANAGER')")) # Add a temporary column with the old type op.add_column('clinic_doctors', sa.Column('old_role', postgresql.ENUM('DIRECTOR', 'PRACTICE_MANAGER', name='clinicuserroles'), nullable=True)) # Update the temporary column with values based on the new column op.execute(text("UPDATE clinic_doctors SET old_role = 'DIRECTOR' WHERE role = 'DOCTOR'")) op.execute(text("UPDATE clinic_doctors SET old_role = 'PRACTICE_MANAGER' WHERE role = 'NURSE'")) # Drop the new column and rename the old one op.drop_column('clinic_doctors', 'role') op.alter_column('clinic_doctors', 'old_role', new_column_name='role') # If we modified the users table in the upgrade, we need to restore it conn = op.get_bind() inspector = sa.inspect(conn) has_clinic_role = False try: user_columns = inspector.get_columns('users') for column in user_columns: if column['name'] == 'clinicRole': has_clinic_role = True break except: pass if has_clinic_role: # Create a temporary column with the enum type op.add_column('users', sa.Column('temp_clinic_role', postgresql.ENUM('DIRECTOR', 'PRACTICE_MANAGER', name='clinicuserroles'), nullable=True)) # Copy the values (with appropriate conversion) - use double quotes to preserve case sensitivity op.execute(text('UPDATE users SET temp_clinic_role = "clinicRole"::clinicuserroles')) # Drop the old column and rename the new one op.drop_column('users', 'clinicRole') op.alter_column('users', 'temp_clinic_role', new_column_name='clinicRole') # Drop the new enum type op.execute(text("DROP TYPE IF EXISTS clinicdoctortype")) # ### end Alembic commands ### # ### end Alembic commands ###