import asyncio import sys import os # Fix PYTHONPATH: Add project root to sys.path sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from sqlalchemy import text from core.database import async_session_factory async def migrate_enum_type(): """ Create the native PG enum type for EscalationAction and migrate the column. This resolves the 'type "escalationaction" does not exist' error. """ async with async_session_factory() as db: print("--- DB MIGRATION: ENUM TYPE ---") # 1. Create Type (if not exists) # 2. Alter column to use the new type # Note: We use raw SQL because Alembic isn't being run manually here. commands = [ # Check if type exists first or just try-catch "DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'escalationaction') THEN CREATE TYPE escalationaction AS ENUM ('NOTIFY_LEAD', 'REASSIGN', 'AUTO_RESOLVE'); END IF; END $$;", "ALTER TABLE escalation_rules ALTER COLUMN action TYPE escalationaction USING action::escalationaction;" ] for cmd in commands: try: print(f"Executing: {cmd}") await db.execute(text(cmd)) await db.commit() print("SUCCESS.") except Exception as e: await db.rollback() print(f"SKIPPED/FAILED: {str(e)}") print("--- DB MIGRATION: COMPLETE ---") if __name__ == "__main__": asyncio.run(migrate_enum_type())