
    gl                     z    d Z ddlZddlZddlmZmZ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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         U/var/www/openai/venv/lib/python3.12/site-packages/litellm/proxy/db/check_migration.pyextract_sql_commandsr      s     '2&7&7&=N&=dTZZ\&=E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           
      P   	 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.
    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)
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%=B B% B%c                     | "t        j                  d      } | t        d      t        |       \  }}|rt        dj	                  |            y)z2Main function to run the Prisma schema diff check.NDATABASE_URLzDATABASE_URL not setz[prisma schema out of sync with db. Consider running these sql_commands to sync the two - {})osgetenv	Exceptionr+   format)r   has_diffmessages      r   check_prisma_schema_diffr4   Y   s[    ~>*>2337?Hgipp
 	
     )N)__doc__r.   r!   typingr   r   r   strr   boolr+   r4    r5   r   <module>r;      sd    I 	  ( ("c "d3i "J)C )E$S	/4J )X
Xc] 
d 
r5   