o
    Df?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) 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)lm3Z3 d d*l4m5Z5 dd+l6m7Z7 G d,d- d-Z8G d.d/ d/Z9d0S )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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)selfr5   r6   r   r8   r9   r:   r<   r=   r>   rE   rF   r3   r3   Q/home/ubuntu/webapp/venv/lib/python3.10/site-packages/alembic/operations/batch.py__init__8   s,   




zBatchOperationsImpl.__init__returnr'   c                 C  s
   | j jjS N)r5   rC   dialectrH   r3   r3   rI   rM   ]   s   
zBatchOperationsImpl.dialectr-   c                 C  s   | j jS rL   )r5   rC   rN   r3   r3   rI   rC   a   s   zBatchOperationsImpl.implboolc                 C  s*   | j dkr| jj| S | j dkrdS dS )Nr/   r0   TF)r8   r5   rC   requires_recreate_in_batchrN   r3   r3   rI   _should_recreatee   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)rE   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)rF   )rQ   r   rC   
connectionrG   getattrr5   rE   r   r9   migration_contextas_sqlr   CommandErrorrM   namer6   r   r=   r   get_bindr>   ApplyBatchImplr:   r<   rF   _create)
rH   should_recreateopnameargkwfnm1existing_table	reflected
batch_implr3   r3   rI   flushm   s`   



"zBatchOperationsImpl.flushc                 O     | j d||f d S )Nalter_columnrG   rB   rH   r_   r`   r3   r3   rI   rh         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)rQ   r   rX   rG   rB   rj   r3   r3   rI   rn      s   zBatchOperationsImpl.add_columnc                 O  rg   )Ndrop_columnri   rj   r3   r3   rI   ro      rk   zBatchOperationsImpl.drop_columnconstr+   c                 C     | j d|fi f d S )Nadd_constraintri   rH   rp   r3   r3   rI   rr         z"BatchOperationsImpl.add_constraintc                 C  rq   )Ndrop_constraintri   rs   r3   r3   rI   ru      rt   z#BatchOperationsImpl.drop_constraintc                 O  rg   )Nrename_tableri   rj   r3   r3   rI   rv      rk   z BatchOperationsImpl.rename_tableidxr   r`   r   c                 K     | j d|f|f d S )Ncreate_indexri   rH   rw   r`   r3   r3   rI   ry      rt   z BatchOperationsImpl.create_indexc                 K  rx   )N
drop_indexri   rz   r3   r3   rI   r{      rt   zBatchOperationsImpl.drop_indexc                 C  rq   )Ncreate_table_commentri   rH   tabler3   r3   rI   r|      rt   z(BatchOperationsImpl.create_table_commentc                 C  rq   )Ndrop_table_commentri   r}   r3   r3   rI   r      rt   z&BatchOperationsImpl.drop_table_commentc                 C     t d)Nz Can't create table in batch modeNotImplementedErrorr}   r3   r3   rI   create_table      z BatchOperationsImpl.create_tablec                 C  r   )NzCan't drop table in batch moder   r}   r3   r3   rI   
drop_table   r   zBatchOperationsImpl.drop_tablec                 C  rq   )Ncreate_column_commentri   rH   columnr3   r3   rI   r      rt   z)BatchOperationsImpl.create_column_commentN)rK   r'   )rK   r-   )rK   rO   rK   rR   rp   r+   rK   rR   )rw   r   r`   r   rK   rR   )__name__
__module____qualname__rJ   propertyrM   rC   rQ   rf   rh   rn   ro   rr   ru   rv   ry   r{   r|   r   r   r   r   r3   r3   r3   rI   r.   7   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 )ir[   r3   rC   r-   r~   r   r:   tupler<   Dict[str, Any]rd   rO   rF   rK   rR   c                 C  sn   || _ || _|| _|| _| |j| _d | _|| _d| _	t
dd | jjD | _t| j| _|| _|   d S )Nr3   c                 s  s    | ]
}|j d |ifV  qdS exprN)rY   .0cr3   r3   rI   	<genexpr>       
z*ApplyBatchImpl.__init__.<locals>.<genexpr>)rC   r~   r:   r<   _calc_temp_namerY   temp_table_name	new_tablerF   add_col_orderingr   r   column_transfersr@   existing_orderingrd   _grab_table_elements)rH   rC   r~   r:   r<   rd   rF   r3   r3   rI   rJ      s   	
zApplyBatchImpl.__init__	tablenameUnion[quoted_name, str]strc                 C  s   d| dd S )Nz_alembic_tmp_%sr   2   r3   )clsr   r3   r3   rI   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   rY   named_constraintsunnamed_constraintscol_named_constraintsindexesnew_indexesconstraintsr    rd   r
   r%   rB   kwargsr<   r?   )rH   r   r   c_copyrp   colrw   kr3   r3   rI   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   r3   )r   pr3   r3   rI   
<listcomp>9  s     zNApplyBatchImpl._adjust_self_columns_for_partial_reordering.<locals>.<listcomp>T)deterministic_orderc                 3      | ]
}| j | fV  qd S rL   )r   r   r   rN   r3   rI   r   >  s    zMApplyBatchImpl._adjust_self_columns_for_partial_reordering.<locals>.<genexpr>c                 3  r   rL   )r   r   rN   r3   rI   r   ?  r   )setr@   r   rF   	enumerateaddr   updater   r   sortr   r   )rH   pairs
col_by_idxtuple_r   elem
pairs_listsorted_r3   rN   rI   +_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 r3   )keyr   r3   r3   rI   	<setcomp>W  s    zAApplyBatchImpl._transfer_elements_to_new_table.<locals>.<setcomp>)r   target_tabler   )r   r   r~   r   rF   r   r   r   r   r@   r   valuesr:   r<   r   r   r   issubsetr   r   r   r   r   _setup_referentappend_constraint)rH   mr   r   rp   const_columns
const_copyr3   r3   rI   _transfer_elements_to_new_tableC  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 r3   )r   r   )r   r   rN   r3   rI   r         
zCApplyBatchImpl._gather_indexes_from_both_tables.<locals>.<listcomp>)r   _tablec                   s   g | ]} j j| qS r3   )r   r   )r   r   rN   r3   rI   r     s    r   )r   r   r   _column_flagr   rY   r   r   r   rB   r   r   keys)rH   rw   idx_existingidx_copyr   r3   rN   rI    _gather_indexes_from_both_tablesu  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 rL   )_get_colspec)r   r3   r3   rI   colspec  r   z/ApplyBatchImpl._setup_referent.<locals>.colspecc                 S  s   g | ]}t |tjqS r3   )r   sqltypesNULLTYPE)r   nr3   r3   rI   r     r   z2ApplyBatchImpl._setup_referent.<locals>.<listcomp>c                   s   g | ]} | d d qS )r   r   )split)r   r   r   r3   rI   r     s    r   )r   r   )elementsr   r   lenr   
sql_schema_get_table_keytablesr   append_columnr   r   r   r   )rH   r   r   specpartstnamereferent_schemar   tr   colnamer3   r   rI   r     s<   










zApplyBatchImpl._setup_referentop_implc              	   C  s   |    || | j | jd usJ || j z(|t| 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 ]}|| qcW | j| j_d S | j| j_w )Nc                 s  s     | ]\}}d |v r|V  qdS r   r3   )r   r   transferr3   r3   rI   r     s    z)ApplyBatchImpl._create.<locals>.<genexpr>c                 S  s   g | ]
}d |v r|d  qS )r   r3   )r   r   r3   r3   rI   r     s
    z*ApplyBatchImpl._create.<locals>.<listcomp>r   )r   prep_table_for_batchr~   r   r   _execr   from_selectr@   r   itemsr#   r   r   rv   r   rY   r   r   ry   )rH   r   rw   r3   r3   rI   r\     s<   
zApplyBatchImpl._createNFr6   column_namenullableOptional[bool]server_default)Optional[Union[Function[Any], str, bool]]rY   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 )NrY   existing_typeF)r   r   rY   rA   r"   rC   rM   r   r   r   popr   to_instancer   _create_eventscreate_constraintcast_for_batch_migrater   r   r   DefaultClause_set_parentrO   r   r  )rH   r6   r   r   r   rY   r   r   r  r`   existingexisting_transferr  resolved_existing_typer3   r3   rI   rh     sT   




zApplyBatchImpl.alter_columnr   rl   rm   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 r3   r3   )r   irY   r3   r3   rI   
<dictcomp>4  s    zEApplyBatchImpl._setup_dependencies_for_add_column.<locals>.<dictcomp>r   r   c                 S  r  r3   r3   )r   abr3   r3   rI   r  L  s    
r   )r   r   rF   r   r;   r   )rH   r   rl   rm   
index_colscol_indexesrw   r3   r3   rI   "_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  rY   r   r~   r   r   r   )rH   r6   r   rl   rm   r`   r3   r3   rI   rn   ]  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  )rY   r~   primary_keyr   r!   r   r   remover   r   r   r  )rH   r6   r   r`   r3   r3   rI   ro   m  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.

        Nr3   r   r3   r3   rI   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.

        Nr3   r}   r3   r3   rI   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.

        Nr3   r}   r3   r3   rI   r     r  z!ApplyBatchImpl.drop_table_commentrp   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$   rY   r7   r   r   r   r~   r  r   r  r%   r   rB   rs   r3   r3   rI   rr     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)rY   r7   r   r  r@   r   r   r  r%   r   r   KeyErrorr    r   r   r  )rH   rp   r   	col_constr3   r3   rI   ru     s2   


	
zApplyBatchImpl.drop_constraintrw   r   c                 C  s   || j |j< d S rL   )r   rY   rH   rw   r3   r3   rI   ry     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   rY   r  r7   r  r3   r3   rI   r{     s
   zApplyBatchImpl.drop_indexc                 O  r   )NTODOr   rj   r3   r3   rI   rv     r   zApplyBatchImpl.rename_table)r3   )rC   r-   r~   r   r:   r   r<   r   rd   rO   rF   r   rK   rR   )r   r   rK   r   r   )rK   r   )r   r   r   r   rK   rR   )r   r-   rK   rR   )NFNNNF)r6   r   r   r   r   r   r   r   rY   r   r   r   r   r   r  r  rK   rR   )r   r   rl   r   rm   r   rK   rR   )NN)
r6   r   r   r  rl   r   rm   r   rK   rR   )r6   r   r   r  rK   rR   r   )rw   r   rK   rR   )r   r   r   rJ   classmethodr   r   r   r   r   r   r\   rh   r  rn   ro   r   r|   r   rr   ru   ry   r{   rv   r3   r3   r3   rI   r[      s>    

.

2
!
%+
K4





r[   N):
__future__r   typingr   r   r   r   r   r   r	   
sqlalchemyr
   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$   r%   r&   sqlalchemy.enginer'   sqlalchemy.sql.elementsr(   r)   sqlalchemy.sql.functionsr*   r+   sqlalchemy.sql.type_apir,   ddl.implr-   r.   r[   r3   r3   r3   rI   <module>   s\    