o
    ưi                     @   s   d Z ddlZddlZddlmZmZmZ ddlmZ de	dee	 fddZ
d	e	deeee	 f fd
dZdd	ee	 ddfddZdS )zCModule for checking differences between Prisma schema and database.    N)ListOptionalTuple)verbose_loggerdiff_outputreturnc                 C   s   dd |  dD }g }d}d}|D ]4}|dr)|r&|r&||  d}d}q|rF|dr@||7 }||  d}d}q||d	 7 }q|rP||  |S )
z
    Extract SQL commands from the Prisma migrate diff output.
    Args:
        diff_output (str): The full output from prisma migrate diff.
    Returns:
        List[str]: A list of SQL commands extracted from the diff output.
    c                 S   s   g | ]
}|  r|  qS  )strip).0liner   r   W/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/db/check_migration.py
<listcomp>   s    z(extract_sql_commands.<locals>.<listcomp>
 Fz-- T; )split
startswithappendr	   endswith)r   linessql_commandsZcurrent_commandZin_sql_blockr   r   r   r   extract_sql_commands
   s*   	

r   db_urlc              
   C   s   t d z3tjdddd| dddgd	d	d	d
}t|j}|r4td td |D ]}t| q(d	|fW S dg fW S  tjyY } zd|j }t| dg fW  Y d}~S d}~ww )a  Checks for differences between current database and Prisma schema.
    Returns:
        A tuple containing:
        - A boolean indicating if differences were found (True) or not (False).
        - A string with the diff output or error message.
    Raises:
        subprocess.CalledProcessError: If the Prisma command fails.
        Exception: For any other errors during execution.
    z"Checking for Prisma schema diff...ZprismaZmigratediffz
--from-urlz--to-schema-datamodelz./schema.prismaz--scriptT)capture_outputtextcheckzChanges to DB Schema detectedzRequired SQL commands:Fz*Failed to generate migration diff. Error: N)	r   debug
subprocessrunr   stdoutprintCalledProcessErrorstderr)r   resultr   commandeerror_messager   r   r   check_prisma_schema_diff_helper/   s<   






r)   c                 C   sJ   | du rt d} | du rtdt| \}}|r#td| dS dS )z2Main function to run the Prisma schema diff check.NZDATABASE_URLzDATABASE_URL not setuh   🚨🚨🚨 prisma schema out of sync with db. Consider running these sql_commands to sync the two - {})osgetenv	Exceptionr)   r   	exceptionformat)r   Zhas_diffmessager   r   r   check_prisma_schema_diff\   s   
r0   )N)__doc__r*   r   typingr   r   r   Zlitellm._loggingr   strr   boolr)   r0   r   r   r   r   <module>   s    %-