
    h                         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Z
de	deeee	   f   fd	Zddee	   ddfd
Zy)zCModule for checking differences between Prisma schema and database.    N)ListOptionalTuple)verbose_loggerdiff_outputreturnc                    | j                  d      D cg c]#  }|j                         s|j                         % }}g }d}d}|D ]  }|j                  d      r(|r#|r!|j                  |j                                d}d}<|s?|j	                  d      r)||z  }|j                  |j                                d}d}y||dz   z  } |r|j                  |j                                |S c c}w )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.
    
 Fz-- T; )splitstrip
startswithappendendswith)r   linelinessql_commandscurrent_commandin_sql_blocks         \/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/db/check_migration.pyextract_sql_commandsr   
   s     '2&7&7&=NdTZZ\NENLOL??5!##O$9$9$;<"$L}}S!4'##O$9$9$;<"$$4#:-   O11343 Os
   C,C,db_urlc           
      z   t        j                  d       	 t        j                  dddd| dddgd	d	d	
      }t	        |j
                        }|r,t        d       t        d       |D ]  }t        |        d	|fS dg fS # t        j                  $ r(}d|j                   }t        |       dg fcY d}~S d}~ww xY 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...prisma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_messages         r   check_prisma_schema_diff_helperr-   /   s     =>'!	  
" ,FMM:12*+'g (%%"9(( DQXXJOmbys$   A#A? ;A? ?B:B5/B:5B:c                     | "t        j                  d      } | t        d      t        |       \  }}|r%t	        j
                  dj                  |             yy)z2Main function to run the Prisma schema diff check.N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   has_diffmessages      r   check_prisma_schema_diffr7   \   s\    ~>*>2337?Hg  v}}	
     )N)__doc__r0   r#   typingr   r   r   litellm._loggingr   strr   boolr-   r7    r8   r   <module>r?      sg    I 	  ( ( +"c "d3i "J*C *E$S	/4J *Z
Xc] 
d 
r8   