Spaces:
3SCError404
/
Paused

File size: 1,573 Bytes
b7980f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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())