a
    g	gh                     @  s  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 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.m0Z0 d d(l1m2Z2 d d)l3m4Z4 d d*l5m6Z6 dd+l7m8Z8 G d,d- d-Z9G d.d/ d/Z:d0S )1    )annotations)Any)Dict)List)Optional)Tuple)TYPE_CHECKING)Union)CheckConstraint)Column)ForeignKeyConstraint)Index)MetaData)PrimaryKeyConstraintschema)Table)types)SchemaEventTarget)OrderedDict)topological   )exc)_columns_for_constraint)_copy)_copy_expression)_ensure_scope_for_ddl)_fk_is_self_referential)_idx_table_bound_expressions)_insert_inline)_is_type_bound)_remove_column_from_collection)_resolve_for_variant)_select)constraint_name_defined)constraint_name_string)Literal)Dialect)ColumnClause)quoted_name)Function)
Constraint)
TypeEngine)DefaultImplc                   @  s   e Zd Zdd ZeddddZedddd	Zd
dddZddddZddddZ	ddddZ
ddddZdddddZdddddZdd Zdddd d!Zdddd"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.S )/BatchOperationsImplc                 C  s   || _ || _|| _|dvr"td|| _|| _|| _t|| _|| _	t|	| _
| j
dt| j
dd | j
d d|jjf |
| _|| _g | _d S )N)autoalwaysneverz4recreate may be one of 'auto', 'always', or 'never'.Z	listeners Zcolumn_reflect)
operations
table_namer   
ValueErrorrecreate	copy_from
table_argsdicttable_kwargsreflect_argsreflect_kwargs
setdefaultlistgetappendimplZautogen_column_reflectnaming_conventionpartial_reorderingbatch)selfr3   r4   r   r6   r7   r8   r:   r;   r<   rB   rC   r2   r2   [/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/alembic/operations/batch.py__init__5   s,    



zBatchOperationsImpl.__init__r'   returnc                 C  s
   | j jjS N)r3   rA   dialectrE   r2   r2   rF   rK   Z   s    zBatchOperationsImpl.dialectr-   c                 C  s   | j jS rJ   )r3   rA   rL   r2   r2   rF   rA   ^   s    zBatchOperationsImpl.implboolc                 C  s.   | j dkr| jj| S | j dkr&dS dS d S )Nr/   r0   TF)r6   r3   rA   Zrequires_recreate_in_batchrL   r2   r2   rF   _should_recreateb   s
    

z$BatchOperationsImpl._should_recreateNonec           
   	   C  sN  |   }t| jj" |sL| jD ]&\}}}t| jj|}||i | q"n| jr`t| jd}nt }| j	d ur|| j	}d}n\| jj
jrtd| jj
jj d| j dt| j|g| jR | j| j d| j}d}t| j|| j| j|| jd}	| jD ]"\}}}t|	|}||i | q|	| j W d    n1 s@0    Y  d S )	N)rB   FzEThis operation cannot proceed in --sql mode; batch mode with dialect zF requires a live database connection with which to reflect the table "z". To generate a batch SQL migration script using table "move and copy", a complete Table object should be passed to the "copy_from" argument of the batch_alter_table() method so that table reflection can be skipped.)r   Zautoload_withT)rC   )rN   r   rA   
connectionrD   getattrr3   rB   r   r7   Zmigration_contextZas_sqlr   CommandErrorrK   namer4   r   r;   r   Zget_bindr<   ApplyBatchImplr8   r:   rC   _create)
rE   Zshould_recreateopnameargkwfnm1Zexisting_table	reflectedZ
batch_implr2   r2   rF   flushj   sX    



zBatchOperationsImpl.flushc                 O  s   | j d||f d S )Nalter_columnrD   r@   rE   rW   rX   r2   r2   rF   r]      s    z BatchOperationsImpl.alter_columnc                 O  s8   d|v sd|v r"|   s"td| jd||f d S )Ninsert_beforeinsert_afterz^Can't specify insert_before or insert_after when using ALTER; please specify recreate='always'
add_column)rN   r   rR   rD   r@   r_   r2   r2   rF   rb      s    zBatchOperationsImpl.add_columnc                 O  s   | j d||f d S )Ndrop_columnr^   r_   r2   r2   rF   rc      s    zBatchOperationsImpl.drop_columnr+   constrI   c                 C  s   | j d|fi f d S )Nadd_constraintr^   rE   re   r2   r2   rF   rf      s    z"BatchOperationsImpl.add_constraintc                 C  s   | j d|fi f d S )Ndrop_constraintr^   rg   r2   r2   rF   rh      s    z#BatchOperationsImpl.drop_constraintc                 O  s   | j d||f d S )Nrename_tabler^   r_   r2   r2   rF   ri      s    z BatchOperationsImpl.rename_tabler   idxrI   c                 C  s   | j d|fi f d S )Ncreate_indexr^   rE   rk   r2   r2   rF   rl      s    z BatchOperationsImpl.create_indexc                 C  s   | j d|fi f d S )N
drop_indexr^   rm   r2   r2   rF   rn      s    zBatchOperationsImpl.drop_indexc                 C  s   | j d|fi f d S )Ncreate_table_commentr^   rE   tabler2   r2   rF   ro      s    z(BatchOperationsImpl.create_table_commentc                 C  s   | j d|fi f d S )Ndrop_table_commentr^   rp   r2   r2   rF   rr      s    z&BatchOperationsImpl.drop_table_commentc                 C  s   t dd S )Nz Can't create table in batch modeNotImplementedErrorrp   r2   r2   rF   create_table   s    z BatchOperationsImpl.create_tablec                 C  s   t dd S )NzCan't drop table in batch moders   rp   r2   r2   rF   
drop_table   s    zBatchOperationsImpl.drop_tablec                 C  s   | j d|fi f d S )Ncreate_column_commentr^   rE   columnr2   r2   rF   rw      s    z)BatchOperationsImpl.create_column_commentN)__name__
__module____qualname__rG   propertyrK   rA   rN   r\   r]   rb   rc   rf   rh   ri   rl   rn   ro   rr   ru   rv   rw   r2   r2   r2   rF   r.   4   s(   %9
r.   c                   @  sT  e Zd ZdKddddddddd	d
ZedddddZddddZddddZddddZddddZ	ddddddZ
dddd d!ZdLddd$d%d&d'dd(dd)	d*d+Zdd&d&dd,d-d.ZdMdd/d&d&dd0d1d2Zdd3dd4d5d6Zd7d8 Zd9d: Zd;d< Zd=dd>d?d@Zd=dd>dAdBZdCddDdEdFZdCddDdGdHZdIdJ Zd"S )NrT   r2   r-   r   tuplezDict[str, Any]rM   rO   )rA   rq   r8   r:   r[   rC   rI   c                 C  sn   || _ || _|| _|| _| |j| _d | _|| _d| _	t
dd | jjD | _t| j| _|| _|   d S )Nr2   c                 s  s   | ]}|j d |ifV  qdS exprN)rS   .0cr2   r2   rF   	<genexpr>   s   z*ApplyBatchImpl.__init__.<locals>.<genexpr>)rA   rq   r8   r:   _calc_temp_namerS   temp_table_name	new_tablerC   add_col_orderingr   r   column_transfersr>   existing_orderingr[   _grab_table_elements)rE   rA   rq   r8   r:   r[   rC   r2   r2   rF   rG      s    	
zApplyBatchImpl.__init__zUnion[quoted_name, str]str)	tablenamerI   c                 C  s   d| dd S )Nz_alembic_tmp_%sr   2   r2   )clsr   r2   r2   rF   r      s    zApplyBatchImpl._calc_temp_namerH   c                 C  sP  | j j}t | _| j jD ]D}t||d}d |_|_t|j	t
rP|j	|j	usPJ || j|j< qi | _g | _i | _i | _i | _| j jD ]L}t|rqq| jrt|tr|jsqt|jr|| j|j< q| j| q| js
| j jD ]&}|jD ]}|jr||f| j|j< qq| j jD ]}|| j|j< q| j jD ]}| j|| j j|  q.d S )Nr   F)rq   r   r   columnsr   r   uniqueindex
isinstancetyper   rS   named_constraintsunnamed_constraintscol_named_constraintsindexesnew_indexesconstraintsr    r[   r
   r%   r@   kwargsr:   r=   )rE   r   r   Zc_copyre   colrk   kr2   r2   rF   r      sF    

z#ApplyBatchImpl._grab_table_elementsc                   s   t  }t j} jrT jD ]4}t|D ]&\}}|dkr(|||d  |f q(qn2t jD ]&\}}|dkr^|||d  |f q^| j dd |D }tt	j
||dd}t fdd|D  _t fd	d|D  _d S )
Nr      c                 S  s    g | ]}|d  |d kr|qS )r   r   r2   )r   pr2   r2   rF   
<listcomp>6      zNApplyBatchImpl._adjust_self_columns_for_partial_reordering.<locals>.<listcomp>T)Zdeterministic_orderc                 3  s   | ]}| j | fV  qd S rJ   )r   r   r   rL   r2   rF   r   ;  r   zMApplyBatchImpl._adjust_self_columns_for_partial_reordering.<locals>.<genexpr>c                 3  s   | ]}| j | fV  qd S rJ   )r   r   rL   r2   rF   r   <  s   )setr>   r   rC   	enumerateaddr   updater   r   sortr   r   )rE   pairsZ
col_by_idxtuple_r   elemZ
pairs_listZsorted_r2   rL   rF   +_adjust_self_columns_for_partial_reordering"  s&    

z:ApplyBatchImpl._adjust_self_columns_for_partial_reorderingc                 C  s  | j d u sJ dt }| jj}| js,| jr4|   t| j|gt	| j
 t	| j R d|i| j | _ }t	| j | j D ]}dd t|D }|| jsqt|trt|rt||| jd}qt||d}nt|||d}t|tr| || || qd S )NzCan only create new table oncer   c                 S  s   h | ]
}|j qS r2   )keyr   r2   r2   rF   	<setcomp>U  r   zAApplyBatchImpl._transfer_elements_to_new_table.<locals>.<setcomp>)r   Ztarget_tabler   )r   r   rq   r   rC   r   r   r   r   r>   r   valuesr8   r:   r   r   r   issubsetr   r   r   r   r   _setup_referentZappend_constraint)rE   mr   r   re   Zconst_columnsZ
const_copyr2   r2   rF   _transfer_elements_to_new_table@  s@    	

z.ApplyBatchImpl._transfer_elements_to_new_tablezList[Index]c                   s    j d usJ g } j D ]L}|jr(qt|jg fddt|D R |j j d|j}|	| q j
 D ]>}|	t|jg fdd|j D R d|ji|j qt|S )Nc                   s   g | ]}t | jqS r2   )r   r   )r   r   rL   r2   rF   r     s   zCApplyBatchImpl._gather_indexes_from_both_tables.<locals>.<listcomp>)r   Z_tablec                   s   g | ]} j j| qS r2   )r   r   )r   r   rL   r2   rF   r     r   r   )r   r   r   Z_column_flagr   rS   r   r   r   r@   r   r   keys)rE   rk   Zidx_existingZidx_copyr   r2   rL   rF    _gather_indexes_from_both_tabless  s>    

z/ApplyBatchImpl._gather_indexes_from_both_tablesr   r   )metadata
constraintrI   c                   s   |j d  }|d}|d }t|dkr6|d }nd }|| jkrt||}dddd ||jv r|j| }|j D ]2}	 |	dd	 }
|
|jvrx|	t
|
tj qxn2t||gd
d  fdd|j D D R d|i d S )Nr   .   r   r   c                 S  s   |   S rJ   )_get_colspecr   r2   r2   rF   colspec  s    z/ApplyBatchImpl._setup_referent.<locals>.colspecc                 S  s   g | ]}t |tjqS r2   )r   sqltypesNULLTYPE)r   nr2   r2   rF   r     s   z2ApplyBatchImpl._setup_referent.<locals>.<listcomp>c                   s   g | ]} | d d qS )r   r   )split)r   r   r   r2   rF   r     s   r   )elementsr   r   lenr   
sql_schemaZ_get_table_keyZtablesr   Zappend_columnr   r   r   r   )rE   r   r   specpartsZtnameZreferent_schemar   tr   colnamer2   r   rF   r     s:    








zApplyBatchImpl._setup_referent)op_implrI   c              	   C  s   |    || | j | jd us$J || j zP|t| jtdd | j	
 D tdd | j	 D   || j W n   || j  Y n\0 |j| j| jj| jjd | jj| j_z&|  D ]}|| qW | j| j_n| j| j_0 d S )Nc                 s  s   | ]\}}d |v r|V  qdS r   r2   )r   r   transferr2   r2   rF   r     s   z)ApplyBatchImpl._create.<locals>.<genexpr>c                 S  s   g | ]}d |v r|d  qS )r   r2   )r   r   r2   r2   rF   r     s   z*ApplyBatchImpl._create.<locals>.<listcomp>r   )r   Zprep_table_for_batchrq   r   ru   _execr   Zfrom_selectr>   r   itemsr#   r   rv   ri   r   rS   r   r   rl   )rE   r   rk   r2   r2   rF   rU     s:    
zApplyBatchImpl._createNFzOptional[bool]z)Optional[Union[Function[Any], str, bool]]zOptional[str]zOptional[TypeEngine]zUnion[str, Literal[False]])	r4   column_namenullableserver_defaultrS   type_autoincrementcommentrI   c	                 K  s  | j | }
| j| }|d urt||krt||
_||d< |	dd }|rtt|	d | jj}t|trt|jrt| j	
|jd  |d urt|}t|
jtrd |
j_|
j_| j|
|| ||
_|d ur||
_|dur|d u rd |
_nt||
 |d urt||
_|dur||
_d S )NrS   existing_typeF)r   r   rS   r?   r"   rA   rK   r   r   r   popr   Zto_instancer   Z_create_eventsZcreate_constraintZcast_for_batch_migrater   r   r   ZDefaultClauseZ_set_parentrM   r   r   )rE   r4   r   r   r   rS   r   r   r   rX   existingZexisting_transferr   Zresolved_existing_typer2   r2   rF   r]     sT    





zApplyBatchImpl.alter_column)r   r`   ra   rI   c                 C  s   | j }dd t|D }| js|r^|s^||v rP|| d }|t|k r^|| }nt| j| }|r|s||v r|| d }|dkr|| }ndd | jD | }|r|  j||ff7  _|r|  j||ff7  _| js|s|s|r|  j|d |ff7  _d S )Nc                 S  s   i | ]\}}||qS r2   r2   )r   irS   r2   r2   rF   
<dictcomp>6  r   zEApplyBatchImpl._setup_dependencies_for_add_column.<locals>.<dictcomp>r   r   c                 S  s   i | ]\}}||qS r2   r2   )r   abr2   r2   rF   r   N  s   r   )r   r   rC   r   r9   r   )rE   r   r`   ra   Z
index_colsZcol_indexesrk   r2   r2   rF   "_setup_dependencies_for_add_column/  sH    

z1ApplyBatchImpl._setup_dependencies_for_add_columnr   )r4   ry   r`   ra   rI   c                 K  s8   |  |j|| t|| jjd| j|j< i | j|j< d S )Nr   )r   rS   r   rq   r   r   r   )rE   r4   ry   r`   ra   rX   r2   r2   rF   rb   _  s
    zApplyBatchImpl.add_columnzUnion[ColumnClause, Column])r4   ry   rI   c                 K  sz   |j | jjjv r t| jjj| | j|j = | j|j = | j|j  d|v rvt|d t	rv|d j rv| j
|d j d  d S )Nr   )rS   rq   primary_keyr   r!   r   r   remover   r   r   r   )rE   r4   ry   rX   r2   r2   rF   rc   o  s     



zApplyBatchImpl.drop_columnc                 C  s   dS )a^  the batch table creation function will issue create_column_comment
        on the real "impl" as part of the create table process.

        That is, the Column object will have the comment on it already,
        so when it is received by add_column() it will be a normal part of
        the CREATE TABLE and doesn't need an extra step here.

        Nr2   rx   r2   r2   rF   rw     s    z$ApplyBatchImpl.create_column_commentc                 C  s   dS )zthe batch table creation function will issue create_table_comment
        on the real "impl" as part of the create table process.

        Nr2   rp   r2   r2   rF   ro     s    z#ApplyBatchImpl.create_table_commentc                 C  s   dS )zthe batch table creation function will issue drop_table_comment
        on the real "impl" as part of the create table process.

        Nr2   rp   r2   r2   rF   rr     s    z!ApplyBatchImpl.drop_table_commentr+   rd   c                 C  sd   t |jstdt|tjr<| jj| jv r<| j	| jj t
|jrT|| j|j< n| j| d S )NConstraint must have a name)r$   rS   r5   r   r   r   rq   r   r   r   r%   r   r@   rg   r2   r2   rF   rf     s    

zApplyBatchImpl.add_constraintc                 C  s   |j stdz|j | jv rh| j|j \}}t| j|j  jD ]$}|j |j kr@| j|j  j| q@n0t|j r| j	|j }n|| j
v r| j
| W n. ty   t|rY d S td|j  Y n&0 t|tr|jD ]}d| j|j  _qd S )Nr   zNo such constraint: '%s'F)rS   r5   r   r   r>   r   r   r   r%   r   r   KeyErrorr    r   r   r   )rE   re   r   Z	col_constr2   r2   rF   rh     s&    



zApplyBatchImpl.drop_constraintr   rj   c                 C  s   || j |j< d S rJ   )r   rS   rm   r2   r2   rF   rl     s    zApplyBatchImpl.create_indexc                 C  s4   z| j |j= W n  ty.   td|j Y n0 d S )NzNo such index: '%s')r   rS   r   r5   rm   r2   r2   rF   rn     s    zApplyBatchImpl.drop_indexc                 O  s   t dd S )NZTODOrs   r_   r2   r2   rF   ri     s    zApplyBatchImpl.rename_table)r2   )NFNNNF)NN)rz   r{   r|   rG   classmethodr   r   r   r   r   r   rU   r]   r   rb   rc   rw   ro   rr   rf   rh   rl   rn   ri   r2   r2   r2   rF   rT      s<    .3!'+       M4  
rT   N);
__future__r   typingr   r   r   r   r   r   r	   Z
sqlalchemyr
   r   r   r   r   r   r   r   r   r   r   Zsqlalchemy.eventsr   Zsqlalchemy.utilr   r   utilr   Zutil.sqla_compatr   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   Zsqlalchemy.enginer'   Zsqlalchemy.sql.elementsr(   r)   Zsqlalchemy.sql.functionsr*   Zsqlalchemy.sql.schemar+   Zsqlalchemy.sql.type_apir,   Zddl.implr-   r.   rT   r2   r2   r2   rF   <module>   sZ    