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 fix_escalation_rules(): """ Sanitize escalation_rules data to match the new EscalationAction enum. Legacy values like 'string' will be converted to 'NOTIFY_LEAD'. """ async with async_session_factory() as db: print("--- DB SANITIZATION: START ---") # 1. Update invalid actions to 'NOTIFY_LEAD' # We use a raw SQL execution to bypass the Enum validation that is breaking the ORM stmt = text(""" UPDATE escalation_rules SET action = 'NOTIFY_LEAD' WHERE action NOT IN ('NOTIFY_LEAD', 'REASSIGN', 'AUTO_RESOLVE'); """) try: result = await db.execute(stmt) await db.commit() print(f"SUCCESS: Updated {result.rowcount} records to 'NOTIFY_LEAD'.") except Exception as e: await db.rollback() print(f"FAILURE: Could not sanitize DB: {str(e)}") print("--- DB SANITIZATION: COMPLETE ---") if __name__ == "__main__": asyncio.run(fix_escalation_rules())