o
    h+i                     @  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) erd d#lm*Z* d d$l+m,Z, d d%l-m.Z. d d&l-m/Z/ d d'l0m1Z1 d d(lm2Z2 d d)l3m4Z4 dd*l5m6Z6 G d+d, d,Z7G d-d. d.Z8d/S )0    )annotations)Any)Dict)List)Optional)Tuple)TYPE_CHECKING)Union)CheckConstraint)Column)ForeignKeyConstraint)Index)MetaData)PrimaryKeyConstraintschema)select)Table)types)SchemaEventTarget)OrderedDict)topological   )exc)_columns_for_constraint)_copy)_copy_expression)_ensure_scope_for_ddl)_fk_is_self_referential)_idx_table_bound_expressions)_is_type_bound)_remove_column_from_collection)_resolve_for_variant)constraint_name_defined)constraint_name_string)Literal)Dialect)ColumnClause)quoted_name)Function)
Constraint)
TypeEngine)DefaultImplc                   @  s   e Zd Zdd Zed1ddZed2dd	Zd3ddZd4ddZd4ddZ	d4ddZ
d4ddZd5ddZd5ddZdd Zd6d"d#Zd6d$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0S )7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'.	listeners column_reflect)
operations
table_namer   
ValueErrorrecreate	copy_from
table_argsdicttable_kwargsreflect_argsreflect_kwargs
setdefaultlistgetappendimplautogen_column_reflectnaming_conventionpartial_reorderingbatch)selfr4   r5   r   r7   r8   r9   r;   r<   r=   rD   rE   r2   r2   l/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/alembic/operations/batch.py__init__7   s,   




zBatchOperationsImpl.__init__returnr&   c                 C  s
   | j jjS N)r4   rB   dialectrG   r2   r2   rH   rL   \   s   
zBatchOperationsImpl.dialectr,   c                 C  s   | j jS rK   )r4   rB   rM   r2   r2   rH   rB   `   s   zBatchOperationsImpl.implboolc                 C  s*   | j dkr| jj| S | j dkrdS dS )Nr.   r/   TF)r7   r4   rB   requires_recreate_in_batchrM   r2   r2   rH   _should_recreated   s
   

z$BatchOperationsImpl._should_recreateNonec           
   	   C  s\  |   }t| jj |s%| jD ]\}}}t| jj|}||i | qnw| jr/t| jd}nt }| j	d ur=| j	}d}n.| jj
jrSt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    d S W d    d S 1 sw   Y  d S )	N)rD   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   autoload_withT)rE   )rP   r   rB   
connectionrF   getattrr4   rD   r   r8   migration_contextas_sqlr   CommandErrorrL   namer5   r   r<   r   get_bindr=   ApplyBatchImplr9   r;   rE   _create)
rG   should_recreateopnameargkwfnm1existing_table	reflected
batch_implr2   r2   rH   flushl   s`   



"zBatchOperationsImpl.flushc                 O     | j d||f d S )Nalter_columnrF   rA   rG   r^   r_   r2   r2   rH   rg         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)rP   r   rW   rF   rA   ri   r2   r2   rH   rm      s   zBatchOperationsImpl.add_columnc                 O  rf   )Ndrop_columnrh   ri   r2   r2   rH   rn      rj   zBatchOperationsImpl.drop_columnconstr*   c                 C     | j d|fi f d S )Nadd_constraintrh   rG   ro   r2   r2   rH   rq         z"BatchOperationsImpl.add_constraintc                 C  rp   )Ndrop_constraintrh   rr   r2   r2   rH   rt      rs   z#BatchOperationsImpl.drop_constraintc                 O  rf   )Nrename_tablerh   ri   r2   r2   rH   ru      rj   z BatchOperationsImpl.rename_tableidxr   r_   r   c                 K     | j d|f|f d S )Ncreate_indexrh   rG   rv   r_   r2   r2   rH   rx      rs   z BatchOperationsImpl.create_indexc                 K  rw   )N
drop_indexrh   ry   r2   r2   rH   rz      rs   zBatchOperationsImpl.drop_indexc                 C  rp   )Ncreate_table_commentrh   rG   tabler2   r2   rH   r{      rs   z(BatchOperationsImpl.create_table_commentc                 C  rp   )Ndrop_table_commentrh   r|   r2   r2   rH   r~      rs   z&BatchOperationsImpl.drop_table_commentc                 C     t d)Nz Can't create table in batch modeNotImplementedErrorr|   r2   r2   rH   create_table      z BatchOperationsImpl.create_tablec                 C  r   )NzCan't drop table in batch moder   r|   r2   r2   rH   
drop_table   r   zBatchOperationsImpl.drop_tablec                 C  rp   )Ncreate_column_commentrh   rG   columnr2   r2   rH   r      rs   z)BatchOperationsImpl.create_column_commentN)rJ   r&   )rJ   r,   )rJ   rN   rJ   rQ   ro   r*   rJ   rQ   )rv   r   r_   r   rJ   rQ   )__name__
__module____qualname__rI   propertyrL   rB   rP   re   rg   rm   rn   rq   rt   ru   rx   rz   r{   r~   r   r   r   r2   r2   r2   rH   r-   6   s*    %


9






r-   c                   @  s   e Zd Z	dZd[ddZed\ddZd]ddZd]ddZd]ddZd^ddZ	d_d#d$Z
d`d&d'Z	(	)	(	(	(	)dadbd8d9Zdcd=d>Z	(	(dddedAdBZdfdDdEZdFdG ZdHdI ZdJdK ZdgdNdOZdgdPdQZdhdTdUZdhdVdWZdXdY Zd(S )irZ   r2   rB   r,   r}   r   r9   tupler;   Dict[str, Any]rc   rN   rE   rJ   rQ   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)rX   .0cr2   r2   rH   	<genexpr>       
z*ApplyBatchImpl.__init__.<locals>.<genexpr>)rB   r}   r9   r;   _calc_temp_namerX   temp_table_name	new_tablerE   add_col_orderingr   r   column_transfersr?   existing_orderingrc   _grab_table_elements)rG   rB   r}   r9   r;   rc   rE   r2   r2   rH   rI      s   	
zApplyBatchImpl.__init__	tablenameUnion[quoted_name, str]strc                 C  s   d| dd S )Nz_alembic_tmp_%sr   2   r2   )clsr   r2   r2   rH   r      s   zApplyBatchImpl._calc_temp_namec                 C  sH  | j j}t | _| j jD ]"}t||d}d |_|_t|j	t
r(|j	|j	us(J || j|j< qi | _g | _i | _i | _i | _| j jD ]%}t|rIqB| jrUt|trU|jsUqBt|jra|| j|j< qB| j| qB| js| j jD ]}|jD ]}|jr||f| j|j< qtqo| j jD ]}|| j|j< q| j jD ]}| j|| j j|  qd S )Nr   F)r}   r   r   columnsr   r   uniqueindex
isinstancetyper   rX   named_constraintsunnamed_constraintscol_named_constraintsindexesnew_indexesconstraintsr    rc   r
   r$   rA   kwargsr;   r>   )rG   r   r   c_copyro   colrv   kr2   r2   rH   r      sL   

z#ApplyBatchImpl._grab_table_elementsc                   s   t  }t j} jr* jD ]}t|D ]\}}|dkr'|||d  |f qqnt jD ]\}}|dkrB|||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   rH   
<listcomp>8  s     zNApplyBatchImpl._adjust_self_columns_for_partial_reordering.<locals>.<listcomp>T)deterministic_orderc                 3      | ]
}| j | fV  qd S rK   )r   r   r   rM   r2   rH   r   =  s    zMApplyBatchImpl._adjust_self_columns_for_partial_reordering.<locals>.<genexpr>c                 3  r   rK   )r   r   rM   r2   rH   r   >  r   )setr?   r   rE   	enumerateaddr   updater   r   sortr   r   )rG   pairs
col_by_idxtuple_r   elem
pairs_listsorted_r2   rM   rH   +_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r|   t| j|gt	| j
 t	| j R d|i| j | _ }t	| j | j D ]B}dd t|D }|| jsSqAt|trlt|ret||| jd}nt||d}nt|||d}t|tr~| || || qAd S )NzCan only create new table oncer   c                 S  s   h | ]}|j qS r2   )keyr   r2   r2   rH   	<setcomp>V  s    zAApplyBatchImpl._transfer_elements_to_new_table.<locals>.<setcomp>)r   target_tabler   )r   r   r}   r   rE   r   r   r   r   r?   r   valuesr9   r;   r   r   r   issubsetr   r   r   r   r   _setup_referentappend_constraint)rG   mr   r   ro   const_columns
const_copyr2   r2   rH   _transfer_elements_to_new_tableB  sB   	

z.ApplyBatchImpl._transfer_elements_to_new_tableList[Index]c                   s    j d usJ g } j D ]&}|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 q:|S )Nc                   s   g | ]}t | jqS r2   )r   r   )r   r   rM   r2   rH   r         
zCApplyBatchImpl._gather_indexes_from_both_tables.<locals>.<listcomp>)r   _tablec                   s   g | ]} j j| qS r2   )r   r   )r   r   rM   r2   rH   r     s    r   )r   r   r   _column_flagr   rX   r   r   r   rA   r   r   keys)rG   rv   idx_existingidx_copyr   r2   rM   rH    _gather_indexes_from_both_tablest  s>   

z/ApplyBatchImpl._gather_indexes_from_both_tablesmetadatar   
constraintr   c                   s   |j d  }|d}|d }t|dkr|d }nd }|| jkrqt||}ddd ||jv rV|j| }|j D ]}	 |	dd	 }
|
|jvrS|	t
|
tj q:d S t||gd
d  fdd|j D D R d|i d S d S )Nr   .   r   r   c                 S  s   |   S rK   )_get_colspec)r   r2   r2   rH   colspec  r   z/ApplyBatchImpl._setup_referent.<locals>.colspecc                 S  s   g | ]}t |tjqS r2   )r   sqltypesNULLTYPE)r   nr2   r2   rH   r     r   z2ApplyBatchImpl._setup_referent.<locals>.<listcomp>c                   s   g | ]} | d d qS )r   r   )split)r   r   r   r2   rH   r     s    r   )r   r   )elementsr   r   lenr   
sql_schema_get_table_keytablesr   append_columnr   r   r   r   )rG   r   r   specpartstnamereferent_schemar   tr   colnamer2   r   rH   r     s<   










zApplyBatchImpl._setup_referentop_implc              	   C  s   |    || | j | jd usJ || j z*|| j  t	dd | j
 D tdd | j
 D   || j W n
   || j  |j| j| jj| jjd | jj| j_z|  D ]}|| qeW | j| j_d S | j| j_w )Nc                 s  s     | ]\}}d |v r|V  qdS r   r2   )r   r   transferr2   r2   rH   r     s    z)ApplyBatchImpl._create.<locals>.<genexpr>c                 S  s   g | ]
}d |v r|d  qS )r   r2   )r   r   r2   r2   rH   r     s
    z*ApplyBatchImpl._create.<locals>.<listcomp>r   )r   prep_table_for_batchr}   r   r   _execinsertinlinefrom_selectr?   r   itemsr   r   r   ru   r   rX   r   r   rx   )rG   r   rv   r2   r2   rH   r[     s@   zApplyBatchImpl._createNFr5   column_namenullableOptional[bool]server_default)Optional[Union[Function[Any], str, bool]]rX   Optional[str]type_Optional[TypeEngine]autoincrement&Optional[Union[bool, Literal['auto']]]commentUnion[str, Literal[False]]c	                 K  s  | j | }
| j| }|d ur:||kr:||
_||d< |	dd }|r:t|	d | jj}t|tr:|jr:| j	
|jd  |d ur\t|}t|
jtrQd |
j_|
j_| j|
|| ||
_|d urc||
_|durw|d u rod |
_nt||
 |d urt||
_|dur||
_d S d S )NrX   existing_typeF)r   r   rX   r@   r"   rB   rL   r   r   r   popr   to_instancer   _create_eventscreate_constraintcast_for_batch_migrater   r   r   DefaultClause_set_parentrN   r   r  )rG   r5   r   r   r   rX   r   r   r  r_   existingexisting_transferr  resolved_existing_typer2   r2   rH   rg     sT   




zApplyBatchImpl.alter_columnr   rk   rl   c                 C  s  | j }dd t|D }| jsP|r/|s/||v r(|| d }|t|k r'|| }nt| j| }|rP|sP||v rF|| d }|dkrE|| }n
dd | jD | }|r\|  j||ff7  _|rh|  j||ff7  _| js|s|s|r|  j|d |ff7  _d S d S d S d S d S )Nc                 S     i | ]\}}||qS r2   r2   )r   irX   r2   r2   rH   
<dictcomp>5  s    zEApplyBatchImpl._setup_dependencies_for_add_column.<locals>.<dictcomp>r   r   c                 S  r  r2   r2   )r   abr2   r2   rH   r  M  s    
r   )r   r   rE   r   r:   r   )rG   r   rk   rl   
index_colscol_indexesrv   r2   r2   rH   "_setup_dependencies_for_add_column.  sN   z1ApplyBatchImpl._setup_dependencies_for_add_columnr   Column[Any]c                 K  s8   |  |j|| t|| jjd| j|j< i | j|j< d S )Nr   )r  rX   r   r}   r   r   r   )rG   r5   r   rk   rl   r_   r2   r2   rH   rm   ^  s
   zApplyBatchImpl.add_column%Union[ColumnClause[Any], Column[Any]]c                 K  s   |j | jjjv rt| jjj| | j|j = | j|j = | j|j  d|v r=t|d t	r?|d j rA| j
|d j d  d S d S d S d S )Nr  )rX   r}   primary_keyr   r!   r   r   remover   r   r   r  )rG   r5   r   r_   r2   r2   rH   rn   n  s    



zApplyBatchImpl.drop_columnc                 C     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   r   r2   r2   rH   r         z$ApplyBatchImpl.create_column_commentc                 C  r  )zthe batch table creation function will issue create_table_comment
        on the real "impl" as part of the create table process.

        Nr2   r|   r2   r2   rH   r{     r  z#ApplyBatchImpl.create_table_commentc                 C  r  )zthe batch table creation function will issue drop_table_comment
        on the real "impl" as part of the create table process.

        Nr2   r|   r2   r2   rH   r~     r  z!ApplyBatchImpl.drop_table_commentro   r*   c                 C  sf   t |js	tdt|tjr| jj| jv r| j	| jj t
|jr+|| j|j< d S | j| d S )NConstraint must have a name)r#   rX   r6   r   r   r   r}   r  r   r  r$   r   rA   rr   r2   r2   rH   rq     s   

zApplyBatchImpl.add_constraintc                 C  s   |j stdzF|j | jv r4| j|j \}}t| j|j  jD ]}|j |j kr2| j|j  j| q nt|j rA| j	|j }n|| j
v rL| j
| W n tyb   t|r[Y d S td|j  w t|tru|jD ]}d| j|j  _qkd S d S )Nr  zNo such constraint: '%s'F)rX   r6   r   r  r?   r   r   r  r$   r   r   KeyErrorr    r   r   r  )rG   ro   r   	col_constr2   r2   rH   rt     s2   


	
zApplyBatchImpl.drop_constraintrv   r   c                 C  s   || j |j< d S rK   )r   rX   rG   rv   r2   r2   rH   rx     s   zApplyBatchImpl.create_indexc                 C  s.   z| j |j= W d S  ty   td|j w )NzNo such index: '%s')r   rX   r  r6   r   r2   r2   rH   rz     s
   zApplyBatchImpl.drop_indexc                 O  r   )NTODOr   ri   r2   r2   rH   ru     r   zApplyBatchImpl.rename_table)r2   )rB   r,   r}   r   r9   r   r;   r   rc   rN   rE   r   rJ   rQ   )r   r   rJ   r   r   )rJ   r   )r   r   r   r   rJ   rQ   )r   r,   rJ   rQ   )NFNNNF)r5   r   r   r   r   r   r   r   rX   r   r   r   r   r  r  r  rJ   rQ   )r   r   rk   r   rl   r   rJ   rQ   )NN)
r5   r   r   r  rk   r   rl   r   rJ   rQ   )r5   r   r   r  rJ   rQ   r   )rv   r   rJ   rQ   )r   r   r   rI   classmethodr   r   r   r   r   r   r[   rg   r  rm   rn   r   r{   r~   rq   rt   rx   rz   ru   r2   r2   r2   rH   rZ      s>    

.

2
!
%-
K4





rZ   N)9
__future__r   typingr   r   r   r   r   r   r	   
sqlalchemyr
   r   r   r   r   r   r   r   r   r   r   r   sqlalchemy.sql.schemar   sqlalchemy.utilr   r   utilr   util.sqla_compatr   r   r   r   r   r   r    r!   r"   r#   r$   r%   sqlalchemy.enginer&   sqlalchemy.sql.elementsr'   r(   sqlalchemy.sql.functionsr)   r*   sqlalchemy.sql.type_apir+   ddl.implr,   r-   rZ   r2   r2   r2   rH   <module>   sZ    