a
    g	g ]                     @  s8  U d dl mZ d dlmZ d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ ddlmZ ddlmZ ddlmZ erd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z# d dl"m$Z$ d dl"m%Z% d d l&m'Z' d d!l&m(Z( d d"l&m)Z) d d#l&m*Z* d d$l&m+Z+ d d%l&m,Z, d d&l-m.Z. d d'l/m0Z0 dd(lm1Z1 dd)l2m3Z3 dd*l4m5Z5 dd+l4m6Z6 G d,d- d-e7Z8i Z9d.e:d/< ed0g d1Z;G d2d3 d3e8d4Z<d5d6 Z=dS )7    )annotations)
namedtupleN)Any)Callable)Dict)List)Optional)Sequence)Set)Tuple)Type)TYPE_CHECKING)Union)castschema)text   )base   )util)sqla_compat)Literal)TextIO)
Connection)Dialect)CursorResult)	Inspector)ClauseElement)ColumnElement)quoted_name)Column)
Constraint)ForeignKeyConstraint)Index)Table)UniqueConstraint)TableClause)
TypeEngine)_ServerDefault)AutogenContext)ApplyBatchImpl)BatchOperationsImplc                   @  s   e Zd ZddddddZdS )ImplMetastrzTuple[Type[DefaultImpl]]Dict[str, Any])	classnamebasesdict_c                 C  s(   t | |||}d|v r$| t|d < |S )N__dialect__)type__init___impls)clsr0   r1   r2   Znewtype r8   S/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/alembic/ddl/impl.pyr5   4   s    zImplMeta.__init__N)__name__
__module____qualname__r5   r8   r8   r8   r9   r-   3   s   r-   zDict[str, Type[DefaultImpl]]r6   Params)token0tokensargskwargsc                   @  s  e Zd ZU dZdZdZdZddhfZded< d	Z	d
ed< dZ
ded< ddddddddddZedddddZddddd Zd!dd"d#d$Zd%d&dd'd(d)Zedd*d+d,Zd-d	e fd.d/d0d1d2d3d4d5Zdd.ddd6d7d8Zddddd9d:d;d:dd<d:d;d=ddd>dd?d@dAZdddBdCddDdEdFZdddBd:ddDdGdHZd>ddIdJdKZdLddIdMdNZdddOdCddPdQdRZd&ddSdTdUZd&ddSdVdWZdXddYdZd[Zd&ddSd\d]Z d&ddSd^d_Z!d`ddadbdcZ"dXddYdddeZ#ddgdhdddidjdkZ$dBdldadmdnZ%dldlddodpdqZ&dldlddrdsdtZ'dBdBddudvdwZ(dxdy Z)dzd{dzd{dd|d}d~Z*dd Z+dddd>ddddZ,dddddZ-ddddddZ.dd Z/dd*ddZ0dd*ddZ1dd*ddZ2ddddddZ3dd Z4dXddYddZ5dd Z6d-S )DefaultImpla  Provide the entrypoint for major migration operations,
    including database-specific behavioral variances.

    While individual SQL/DDL constructs already provide
    for database-specific implementations, variances here
    allow for entirely different sequences of operations
    to take place for a particular migration, such as
    SQL Server's special 'IDENTITY INSERT' step for
    bulk inserts.

    defaultF;ZNUMERICZDECIMALzTuple[Set[str], ...]type_synonymsr8   zSequence[str]type_arg_extract)Zon_nullzTuple[str, ...]identity_attrs_ignorer   zOptional[Connection]boolzOptional[bool]zOptional[TextIO]r/   None)dialect
connectionas_sqltransactional_ddloutput_buffercontext_optsreturnc                 C  sZ   || _ || _|| _|dd| _|| _i | _|| _|d ur@|| _| jrV| jsVt	
dd S )Nliteral_bindsFz3Can't use literal_binds setting without as_sql mode)rJ   rK   rL   getrQ   rN   memorO   rM   r   CommandError)selfrJ   rK   rL   rM   rN   rO   r8   r8   r9   r5   \   s    	zDefaultImpl.__init__zType[DefaultImpl])rJ   rP   c                 C  s
   t |j S N)r6   name)r7   rJ   r8   r8   r9   get_by_dialectv   s    zDefaultImpl.get_by_dialectr.   )r   rP   c                 C  s,   | j d usJ | j |d  | j   d S )Nz

)rN   writeflush)rU   r   r8   r8   r9   static_outputz   s    zDefaultImpl.static_outputr,   )batch_oprP   c                 C  s   dS )zReturn True if the given :class:`.BatchOperationsImpl`
        would need the table to be recreated and copied in order to
        proceed.

        Normally, only returns True on SQLite when operations other
        than add_column are present.

        Fr8   )rU   r\   r8   r8   r9   requires_recreate_in_batch   s    z&DefaultImpl.requires_recreate_in_batchr+   r%   )
batch_impltablerP   c                 C  s   dS )zperform any operations needed on a table before a new
        one is created to replace it in batch mode.

        the PG dialect uses this to drop constraints on the table
        before the new one uses those same names.

        Nr8   )rU   r^   r_   r8   r8   r9   prep_table_for_batch   s    z DefaultImpl.prep_table_for_batch)rP   c                 C  s   | j S rV   )rK   rU   r8   r8   r9   bind   s    zDefaultImpl.bindNzUnion[ClauseElement, str]zOptional[dict]zSequence[dict]zDict[str, int]zOptional[CursorResult])	constructexecution_optionsmultiparamsparamsrP   c                 C  s   t |trt|}| jr|s |r(td| jrJt |tjsJtddid}ni }|j	f d| j
i|}| t|dd | j  d S | j}|d usJ |r|jf i |}|rt |tsJ ||f7 }|||S d S )Nz+Execution arguments not allowed with as_sqlrQ   T)compile_kwargsrJ   	z    )
isinstancer.   r   rL   	ExceptionrQ   r   Z
DDLElementdictcompilerJ   r[   replacestripcommand_terminatorrK   rd   tupleexecute)rU   rc   rd   re   rf   
compile_kwZcompiledconnr8   r8   r9   _exec   s>    

zDefaultImpl._exec)sqlrd   rP   c                 C  s   |  || d S rV   )rt   )rU   ru   rd   r8   r8   r9   rq      s    zDefaultImpl.executez%Union[_ServerDefault, Literal[False]]zOptional[str]zOptional[TypeEngine]z$Optional[Union[str, Literal[False]]]zOptional[_ServerDefault]r   )
table_namecolumn_namenullableserver_defaultrW   type_r   autoincrementcommentexisting_commentexisting_typeexisting_server_defaultexisting_nullableexisting_autoincrementkwrP   c                 K  s*  |d us|d urt jddd |d urF| tj||||||||
d |duri }t||rftj}n"t||rtj	}| |d< ntj
}| ||||f|||||
d| |d ur| tj||||||||
d |	dur| tj|||	|||||
d |d ur&| tj|||||||d d S )NzBautoincrement and existing_autoincrement only make sense for MySQL   )
stacklevel)r   r~   r   r   r}   Fimpl)r   r~   r   r   )r   warnrt   r   ZColumnNullabler   Z_server_default_is_computedZComputedColumnDefaultZ_server_default_is_identityZIdentityColumnDefaultZColumnDefaultZ
ColumnTypeZColumnCommentZ
ColumnName)rU   rv   rw   rx   ry   rW   rz   r   r{   r|   r}   r~   r   r   r   r   Zcls_r8   r8   r9   alter_column   s    
	
zDefaultImpl.alter_columnr!   z!Optional[Union[str, quoted_name]])rv   columnr   rP   c                 C  s   |  tj|||d d S Nr   )rt   r   Z	AddColumn)rU   rv   r   r   r8   r8   r9   
add_column<  s    zDefaultImpl.add_columnc                 K  s   |  tj|||d d S r   )rt   r   Z
DropColumn)rU   rv   r   r   r   r8   r8   r9   drop_columnD  s    zDefaultImpl.drop_column)constrP   c                 C  s(   |j d u s| | r$| t| d S rV   )Z_create_rulert   r   ZAddConstraintrU   r   r8   r8   r9   add_constraintM  s    zDefaultImpl.add_constraintr"   c                 C  s   |  t| d S rV   )rt   r   ZDropConstraintr   r8   r8   r9   drop_constraintQ  s    zDefaultImpl.drop_constraintzUnion[str, quoted_name])old_table_namenew_table_namer   rP   c                 C  s   |  tj|||d d S r   )rt   r   ZRenameTable)rU   r   r   r   r8   r8   r9   rename_tableT  s    zDefaultImpl.rename_table)r_   rP   c                 C  s   |j j|| jd| d | t| |j j|| jd| d |jD ]}| t| qB| j	j
oh| j	j }|j}|r|r| | |jD ]}|j}|r|r| | qd S NF)Z
checkfirstZ_ddl_runner)dispatchZbefore_createrK   rt   r   ZCreateTableZafter_createZindexesCreateIndexrJ   Zsupports_commentsZinline_commentsr|   create_table_commentcolumnscreate_column_comment)rU   r_   indexZwith_commentr|   r   r8   r8   r9   create_table^  s$    




zDefaultImpl.create_tablec                 C  s@   |j j|| jd| d | t| |j j|| jd| d d S r   )r   Zbefore_droprK   rt   r   Z	DropTableZ
after_droprU   r_   r8   r8   r9   
drop_tableu  s    

zDefaultImpl.drop_tabler$   )r   rP   c                 C  s   |  t| d S rV   )rt   r   r   rU   r   r8   r8   r9   create_index~  s    zDefaultImpl.create_indexc                 C  s   |  t| d S rV   )rt   r   ZSetTableCommentr   r8   r8   r9   r     s    z DefaultImpl.create_table_commentc                 C  s   |  t| d S rV   )rt   r   ZDropTableCommentr   r8   r8   r9   drop_table_comment  s    zDefaultImpl.drop_table_commentr   )r   rP   c                 C  s   |  t| d S rV   )rt   r   ZSetColumnComment)rU   r   r8   r8   r9   r     s    z!DefaultImpl.create_column_commentc                 C  s   |  t| d S rV   )rt   r   Z	DropIndexr   r8   r8   r9   
drop_index  s    zDefaultImpl.drop_indexTzUnion[TableClause, Table]z
List[dict])r_   rowsmultiinsertrP   c              	     s   t |tstdn|r.t |d ts.td| jrn|D ]2}| t jf i  fdd|	 D  q8nD|r|r| jt |d n&|D ] }| t jf i | qd S )NzList expectedr   zList of dictionaries expectedc                   s:   i | ]2\}}|t |tjs2tj|| j| jd n|qS ))rz   )ri   r   Z_literal_bindparamcr4   ).0kvr_   r8   r9   
<dictcomp>  s   z+DefaultImpl.bulk_insert.<locals>.<dictcomp>)re   )
ri   list	TypeErrorrk   rL   rt   r   Z_insert_inlinevaluesitems)rU   r_   r   r   rowr8   r   r9   bulk_insert  s,    



zDefaultImpl.bulk_insertr=   c                 C  s   | j j|j }td|}g }d }|D ] }td|rB|}q,|| q,t	|d |dd  g i }|rtd|D ]>}d|v r|
d\}	}
|
 |j|	 < qx|j|  qx|S )Nz[\w\-_]+|\(.+?\)z^\(.*\)$r   r   z[^(),]+=)rJ   Ztype_compilerprocessr4   lowerrefindallmatchappendr=   splitrn   rA   r@   )rU   r   Z
definitionr?   Zterm_tokensZ
paren_termtokenrf   Ztermkeyvalr8   r8   r9   _tokenize_column_type  s     z!DefaultImpl._tokenize_column_type)inspector_paramsmetadata_paramsrP   c                 C  s|   |j |j krdS dd | jD }d|j g|j }d|j g|j }|D ]*}||h|sp|j |j h|rL dS qLdS )NTc                 S  s   g | ]}d d |D qS )c                 S  s   h | ]}|  qS r8   )r   )r   tr8   r8   r9   	<setcomp>      z=DefaultImpl._column_types_match.<locals>.<listcomp>.<setcomp>r8   )r   batchr8   r8   r9   
<listcomp>  r   z3DefaultImpl._column_types_match.<locals>.<listcomp> F)r>   rE   joinr?   issubset)rU   r   r   ZsynonymsZinspector_all_termsZmetadata_all_termsr   r8   r8   r9   _column_types_match  s$    zDefaultImpl._column_types_match)inspected_paramsmeta_paramsrP   c                 C  s   t |jt |jkr$|j|jkr$dS t |jt |jkrH|j|jkrHdS d|j }d|j }| jD ]>}t||}t||}|rn|rn|d|dkrn dS qndS )a  We want to compare column parameters. However, we only want
        to compare parameters that are set. If they both have `collation`,
        we want to make sure they are the same. However, if only one
        specifies it, dont flag it for being less specific
        Fr   r   T)	lenr?   r@   r   r   rF   r   searchgroup)rU   r   r   Zinspmetaregmimmr8   r8   r9   _column_args_match  s$    



zDefaultImpl._column_args_match)inspector_columnmetadata_columnrP   c                 C  s8   |  |}|  |}| ||s$dS | ||s4dS dS )zReturns True if there ARE differences between the types of the two
        columns. Takes impl.type_synonyms into account between retrospected
        and metadata types
        TF)r   r   r   )rU   r   r   r   r   r8   r8   r9   compare_type  s    

zDefaultImpl.compare_typec                 C  s   ||kS rV   r8   )rU   r   r   Zrendered_metadata_defaultZrendered_inspector_defaultr8   r8   r9   compare_server_default   s    z"DefaultImpl.compare_server_defaultzSet[UniqueConstraint]z
Set[Index])conn_uniquesconn_indexesmetadata_unique_constraintsmetadata_indexesrP   c                 C  s   d S rV   r8   )rU   r   r   r   r   r8   r8   r9   correct_for_autogen_constraints)  s    z+DefaultImpl.correct_for_autogen_constraintsc                 C  s$   |j j|jur t|d ||d< d S )Nexpr)r4   Z_type_affinityr   )rU   existingZexisting_transfernew_typer8   r8   r9   cast_for_batch_migrate2  s    z"DefaultImpl.cast_for_batch_migrater   )r   is_server_defaultr   rP   c                 K  s(   ddddi}t |jf d| ji|S )zRender a SQL expression that is typically a server default,
        index expression, etc.

        .. versionadded:: 1.0.11

        rg   TF)rQ   Zinclude_tablerJ   )r.   rl   rJ   )rU   r   r   r   rr   r8   r8   r9   render_ddl_sql_expr8  s    
zDefaultImpl.render_ddl_sql_exprr   r   )	inspectorrP   c                 C  s   | j S rV   )autogen_column_reflect)rU   r   r8   r8   r9   _compat_autogen_column_reflectK  s    z*DefaultImpl._compat_autogen_column_reflectzSet[ForeignKeyConstraint])conn_fksmetadata_fksrP   c                 C  s   d S rV   r8   )rU   r   r   r8   r8   r9   correct_for_autogen_foreignkeysN  s    z+DefaultImpl.correct_for_autogen_foreignkeysc                 C  s   dS )zA hook that is attached to the 'column_reflect' event for when
        a Table is reflected from the database during the autogenerate
        process.

        Dialects can elect to modify the information gathered here.

        Nr8   )rU   r   r_   Zcolumn_infor8   r8   r9   r   U  s    z"DefaultImpl.autogen_column_reflectc                 C  s   dS )zA hook called when :meth:`.EnvironmentContext.run_migrations`
        is called.

        Implementations can set up per-migration-run state here.

        Nr8   ra   r8   r8   r9   start_migrations^  s    zDefaultImpl.start_migrationsc                 C  s   |  d| j  dS )zEmit the string ``BEGIN``, or the backend-specific
        equivalent, on the current connection context.

        This is used in offline mode and typically
        via :meth:`.EnvironmentContext.begin_transaction`.

        ZBEGINNr[   ro   ra   r8   r8   r9   
emit_beginf  s    zDefaultImpl.emit_beginc                 C  s   |  d| j  dS )zEmit the string ``COMMIT``, or the backend-specific
        equivalent, on the current connection context.

        This is used in offline mode and typically
        via :meth:`.EnvironmentContext.begin_transaction`.

        ZCOMMITNr   ra   r8   r8   r9   emit_commitp  s    zDefaultImpl.emit_commitr(   r*   zUnion[str, Literal[False]])type_objautogen_contextrP   c                 C  s   dS )NFr8   )rU   r   r   r8   r8   r9   render_typez  s    zDefaultImpl.render_typec                 C  st   t tj||t \}}t|dd }t|dd }t|t|krJ|d || j ||t|ppt|t|kfS )Nalways)	_compare_identity_optionsr   Z_identity_attrsZIdentitygetattrrH   adddifference_updaterG   )rU   Zmetadata_identityZinspector_identitydiffZignoredZmeta_alwaysZinspector_alwaysr8   r8   r9   _compare_identity_default  s    
z%DefaultImpl._compare_identity_defaultzTuple[Any, ...]c                 C  s   t dd |jD S )Nc                 s  s   | ]}|j V  qd S rV   rW   )r   colr8   r8   r9   	<genexpr>  r   z/DefaultImpl.create_index_sig.<locals>.<genexpr>)rp   r   r   r8   r8   r9   create_index_sig  s    zDefaultImpl.create_index_sigc              
   C  sh   dd |D }t |D ]L}|j|v r&qt|}|rtd|jd| jdtj d || qd S )Nc                 S  s   i | ]}|j |qS r8   r   )r   r   r8   r8   r9   r     r   z8DefaultImpl._skip_functional_indexes.<locals>.<dictcomp>z@autogenerate skipping metadata-specified expression-based index z
; dialect z under SQLAlchemy z6 can't reflect these indexes so they can't be compared)	r   rW   r   Zis_expression_indexr   r   r3   Zsqlalchemy_versiondiscard)rU   r   r   Zconn_indexes_by_nameidxZiexr8   r8   r9   _skip_functional_indexes  s     

z$DefaultImpl._skip_functional_indexes)N)NFNNNNFNNNNN)N)N)N)T)F)7r:   r;   r<   __doc__r3   rM   ro   rE   __annotations__rF   rG   r5   classmethodrX   r[   r]   r`   propertyrb   r   Zimmutabledictrt   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   r8   r8   r9   rB   E   s   
-             .t  	 
	 &&!		 	

rB   )	metaclassc           
      C  sh   t  }t  }| D ]N}t||d }t||d }t||d }	|	|kr||krT|| q|| q||fS rV   )setr   r   )

attributesZmetadata_ioZinspector_ioZ
default_ior   Zignored_attrattrZ
meta_valuedefault_valueZ
conn_valuer8   r8   r9   r     s    r   )>
__future__r   collectionsr   r   typingr   r   r   r   r   r	   r
   r   r   r   r   Z
sqlalchemyr   r   r    r   r   r   r   r   Zsqlalchemy.enginer   r   Zsqlalchemy.engine.cursorr   Zsqlalchemy.engine.reflectionr   Zsqlalchemy.sql.elementsr   r   r    Zsqlalchemy.sql.schemar!   r"   r#   r$   r%   r&   Zsqlalchemy.sql.selectabler'   Zsqlalchemy.sql.type_apir(   r)   Zautogenerate.apir*   Zoperations.batchr+   r,   r4   r-   r6   r   r=   rB   r   r8   r8   r8   r9   <module>   sd       q