
    +h+i                       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r0d 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,      Z7 G d- d.      Z8y/)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                      e Zd Zd Zedd       Zedd       ZddZddZddZ	ddZ
ddZdd	Zdd
Zd ZddZddZd Zd Zd Zd Zd Zy)BatchOperationsImplc           	        || _         || _        || _        |dvrt        d      || _        || _        || _        t        |      | _        || _	        t        |	      | _
        | j                  j                  dt        | j                  j                  dd                   | j                  d   j                  d|j                  j                   f       |
| _        || _        g | _        y )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)selfr7   r8   r   r:   r;   r<   r>   r?   r@   rG   rH   s               K/var/www/html/venv/lib/python3.12/site-packages/alembic/operations/batch.py__init__zBatchOperationsImpl.__init__7   s     %$66F  !"$ .(">2&&d1155k2FG	
 	K(//zEEF	
 "3"4
    c                B    | j                   j                  j                  S N)r7   rE   dialectrJ   s    rK   rP   zBatchOperationsImpl.dialect\   s    ##+++rM   c                .    | j                   j                  S rO   )r7   rE   rQ   s    rK   rE   zBatchOperationsImpl.impl`   s    ###rM   c                    | j                   dk(  r%| j                  j                  j                  |       S | j                   dk(  ryy)Nr1   r2   TF)r:   r7   rE   requires_recreate_in_batchrQ   s    rK   _should_recreatez$BatchOperationsImpl._should_recreated   s;    ==F"??''BB4HH]]h&rM   c           	        | j                         }t        | j                  j                        5  |s?| j                  D ].  \  }}}t        | j                  j                  |      } ||i | 0 n| j                  rt        | j                        }n
t               }| j                  | j                  }d}n| j                  j                  j                  rNt        j                  d| j                  j                  j                  j                   d| j                    d      t#        | j                   |g| j$                  | j&                  | j                  j)                         d| j*                  }d}t-        | j                  || j.                  | j0                  || j2                        }	| j                  D ]  \  }}}t        |	|      } ||i |  |	j5                  | j                         d d d        y # 1 sw Y   y xY w)	N)rG   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)rH   )rU   r   rE   
connectionrI   getattrr7   rG   r   r;   migration_contextas_sqlr   CommandErrorrP   namer8   r   r?   r   get_bindr@   ApplyBatchImplr<   r>   rH   _create)
rJ   should_recreateopnameargkwfnm1existing_table	reflected
batch_impls
             rK   flushzBatchOperationsImpl.flushl   s   //1"499#7#78 4	."'+zz #OFC !5!5v>BNrN# ))!D4J4JKB!B>>-%)^^N %I88??!..7#@@HHMMN O559__4E F9	
9  &+&
 **&  ${{&*oo&>&>&@	& --&N !%I+II"OO%%'+'>'>
 (,zz #OFC V4BNrN# ""499-i4	. 4	. 4	.s   G
HHc                @    | j                   j                  d||f       y )Nalter_columnrI   rD   rJ   rc   rd   s      rK   rl   z BatchOperationsImpl.alter_column       

>334rM   c                    d|v sd|v r%| j                         st        j                  d      | j                  j	                  d||f       y )Ninsert_beforeinsert_afterz^Can't specify insert_before or insert_after when using ALTER; please specify recreate='always'
add_column)rU   r   r\   rI   rD   rn   s      rK   rs   zBatchOperationsImpl.add_column   sP    r!^r%9'')"":  	

<b12rM   c                @    | j                   j                  d||f       y )Ndrop_columnrm   rn   s      rK   ru   zBatchOperationsImpl.drop_column   s    

=#r23rM   c                B    | j                   j                  d|fi f       y )Nadd_constraintrm   rJ   consts     rK   rw   z"BatchOperationsImpl.add_constraint   s    

+eXr:;rM   c                B    | j                   j                  d|fi f       y )Ndrop_constraintrm   rx   s     rK   r{   z#BatchOperationsImpl.drop_constraint   s    

,uh;<rM   c                @    | j                   j                  d||f       y )Nrename_tablerm   rn   s      rK   r}   z BatchOperationsImpl.rename_table   ro   rM   c                B    | j                   j                  d|f|f       y )Ncreate_indexrm   rJ   idxrd   s      rK   r   z BatchOperationsImpl.create_index   s    

>C6267rM   c                B    | j                   j                  d|f|f       y )N
drop_indexrm   r   s      rK   r   zBatchOperationsImpl.drop_index   s    

<#45rM   c                B    | j                   j                  d|fi f       y )Ncreate_table_commentrm   rJ   tables     rK   r   z(BatchOperationsImpl.create_table_comment   s    

1E8R@ArM   c                B    | j                   j                  d|fi f       y )Ndrop_table_commentrm   r   s     rK   r   z&BatchOperationsImpl.drop_table_comment   s    

/%2>?rM   c                    t        d      )Nz Can't create table in batch modeNotImplementedErrorr   s     rK   create_tablez BatchOperationsImpl.create_table   s    !"DEErM   c                    t        d      )NzCan't drop table in batch moder   r   s     rK   
drop_tablezBatchOperationsImpl.drop_table   s    !"BCCrM   c                B    | j                   j                  d|fi f       y )Ncreate_column_commentrm   rJ   columns     rK   r   z)BatchOperationsImpl.create_column_comment   s    

2VIrBCrM   N)returnr'   )r   r-   )r   boolr   Nonery   r+   r   r   )r   r   rd   r   r   r   )__name__
__module____qualname__rL   propertyrP   rE   rU   rj   rl   rs   ru   rw   r{   r}   r   r   r   r   r   r   r   r5   rM   rK   r/   r/   6   s    #J , , $ $7.r534<=586B@FDDrM   r/   c                  <   e Zd Z	 d	 	 	 	 	 	 	 	 	 	 	 	 	 ddZedd       ZddZddZddZddZ		 	 	 	 	 	 ddZ
ddZ	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd
Z	 	 	 	 	 	 	 	 ddZ	 	 d 	 	 	 	 	 	 	 	 	 d!dZ	 	 	 	 	 	 d"dZd Zd Zd Zd#dZd#dZd$dZd$dZd Zy	)%r_   c                ^   || _         || _        || _        || _        | j	                  |j
                        | _        d | _        || _        d| _	        t        d | j                  j                  D              | _        t        | j                        | _        || _        | j!                          y )Nr5   c              3  <   K   | ]  }|j                   d |if  ywexprN)r]   ).0cs     rK   	<genexpr>z*ApplyBatchImpl.__init__.<locals>.<genexpr>   s"      ,
&'QVVfa[!,
s   )rE   r   r<   r>   _calc_temp_namer]   temp_table_name	new_tablerH   add_col_orderingr   r   column_transfersrB   existing_orderingrh   _grab_table_elements)rJ   rE   r   r<   r>   rh   rH   s          rK   rL   zApplyBatchImpl.__init__   s     	
$(#33EJJ?*."4  	 !, ,
+/::<<,
 !
 "&d&;&;!<"!!#rM   c                    d|z  dd S )Nz_alembic_tmp_%sr   2   r5   )cls	tablenames     rK   r   zApplyBatchImpl._calc_temp_name   s    !I-q44rM   c                   | j                   j                  }t               | _        | j                   j                  D ]j  }t        ||      }dx|_        |_        t        |j                  t              r|j                  |j                  usJ || j                  |j                  <   l i | _        g | _        i | _        i | _        i | _        | j                   j"                  D ]  }t%        |      r| j&                  rt        |t(              r|j                  s8t+        |j                        r|| j                  |j                  <   g| j                  j-                  |        | j&                  sT| j                   j                  D ];  }|j"                  D ]*  }|j                  s||f| j                  |j                  <   , = | j                   j                  D ]  }|| j                  |j                  <    | j                   j.                  D ]5  }| j0                  j3                  || j                   j.                  |          7 y )Nr   F)r   r   r   columnsr   r   uniqueindex
isinstancetyper   r]   named_constraintsunnamed_constraintscol_named_constraintsindexesnew_indexesconstraintsr!   rh   r   r%   rD   kwargsr>   rA   )rJ   r   r   c_copyry   colr   ks           rK   r   z#ApplyBatchImpl._grab_table_elements   s   ""/:} 	*A1V,F+00FMFL !&&"34{{!&&000#)DLL 	* 9;#% %'")+-/ZZ++ 	7Ee$uo6


 '

35:&&uzz2((//6	7" ~~zz|| N __ NEzzBEu225::>NN
 ::%% 	)C%(DLL"	) "" 	BA((DJJ,=,=a,@A	BrM   c                x    t               }t         j                        } j                  rD j                  D ]4  }t	        |      D ]$  \  }}|dkD  s|j                  ||dz
     |f       & 6 n<t	         j                        D ]$  \  }}|dkD  s|j                  ||dz
     |f       & |j                   j                         |D cg c]  }|d   |d   k7  s| }}t        t        j                  ||d            }t         fd|D               _        t         fd|D               _        y c c}w )Nr      T)deterministic_orderc              3  @   K   | ]  }|j                   |   f  y wrO   )r   r   r   rJ   s     rK   r   zMApplyBatchImpl._adjust_self_columns_for_partial_reordering.<locals>.<genexpr>=  s     "IAAt||A#7"I   c              3  @   K   | ]  }|j                   |   f  y wrO   )r   r   s     rK   r   zMApplyBatchImpl._adjust_self_columns_for_partial_reordering.<locals>.<genexpr>>  s&      ,
./Q%%a(),
r   )setrB   r   rH   	enumerateaddr   updater   r   sortr   r   )	rJ   pairs
col_by_idxtuple_r   elemp
pairs_listsorted_s	   `        rK   +_adjust_self_columns_for_partial_reorderingz:ApplyBatchImpl._adjust_self_columns_for_partial_reordering$  s6   $,,'
""11 =#,V#4 =KE4qy		6%!)#4d";<==
  ))?)?@ =t19IIz%!)4d;<= 	T**+
 "'7A!A$!A$,a7
7ZN
 #"I"II + ,
3:,
 !
 8s   D7D7c                x   | j                   J d       t               }| j                  j                  }| j                  s| j
                  r| j                          t        | j                  |gt        | j                  j                               t        | j                        z   d|i| j                  x| _         }t        | j                  j                               | j                  z   D ]  }t!        |      D ch c]  }|j"                   }}|j%                  | j&                        sAt)        |t*              r2t-        |      rt/        ||| j                        }nt/        ||      }nt/        |||      }t)        |t*              r| j1                  ||       |j3                  |        y c c}w )NzCan only create new table oncer   )r   target_tabler   )r   r   r   r   rH   r   r   r   r   rB   r   valuesr<   r>   r   r   r   keyissubsetr   r   r   r   r   _setup_referentappend_constraint)rJ   mr   r   ry   r   const_columns
const_copys           rK   _transfer_elements_to_new_tablez.ApplyBatchImpl._transfer_elements_to_new_tableB  s   ~~%G'GG%J""""d&;&;<<>%*  &
 4<<&&()D,AA&
 	&

 &
 	
 ''..01D4L4LL	4E -DE,JKqQUUKMK ))$*?*?@ %!56*51 "'f4::"J "'uV!<J"&y
 %!56$$Q.''
3?	4 Ls    F7c                   | j                   J g }| j                  j                         D ]  }|j                  rt	        |j
                  gt        |      D cg c]  }t        || j                          c}|j                  | j                   d|j                  }|j                  |        | j                  j                         D ]}  }|j                  t	        |j
                  g|j                  j                         D cg c]  }| j                   j                  |    c}d|j                  i|j                          |S c c}w c c}w )N)r   _tabler   )r   r   r   _column_flagr   r]   r    r   r   r   rD   r   r   keysr   )rJ   r   idx_existingr   idx_copyr   r   s          rK    _gather_indexes_from_both_tablesz/ApplyBatchImpl._gather_indexes_from_both_tablest  sN   ~~))) LL//1 	!L ((!!	
 !=\ J %T4>>:	#**
 ~~	 %%	H JJx !	!$ %%,,. 	EJJJJ 8=}}7I7I7KLdnn&&s+L << ll		 
% Ms   E Ec           
        |j                   d   j                         }|j                  d      }|d   }t        |      dk(  r|d   }nd }|| j                  k7  rt        j                  ||      }dd}||j                  v rs|j                  |   }	|j                   D ]T  }
 ||
      j                  d      d   }||	j                  vs,|	j                  t        |t        j                               V y t        ||g|j                   D 
cg c]  }
 ||
      j                  d      d    c}
D cg c]  }t        |t        j                         c}d|i y y c c}
w c c}w )	Nr   .   c                "    | j                         S rO   )_get_colspec)r   s    rK   colspecz/ApplyBatchImpl._setup_referent.<locals>.colspec  s    ((**rM   r   )r   r   )elementsr   splitlenr   
sql_schema_get_table_keytablesr   append_columnr   sqltypesNULLTYPEr   )rJ   metadata
constraintspecpartstnamereferent_schemar   r   tr   colnamens                rK   r   zApplyBatchImpl._setup_referent  sb    ""1%224

3b	u:?#AhO"OD(((++E?CC+ hoo%OOC(&// LD%dm11#6r:Gacc)w8I8I(JKL
  )3(;(;" $ $DM//4R8" q("3"34 + )$"s   !E
1!E"
c                   | j                          |j                  | | j                         | j                  J |j	                  | j                         	 |j                  | j                  j                         j                         j                  t        d | j                  j                         D              t        | j                  j                         D cg c]  }d|v r|d    c}              |j                  | j                         |j                  | j                   | j                  j"                  | j                  j$                         | j                  j"                  | j                  _        	 | j'                         D ]  }|j)                  |        	 | j                   | j                  _        y c c}w # | j                   | j                  _        w xY w#  |j                  | j                          xY w)Nc              3  .   K   | ]  \  }}d |v r|  ywr   r5   )r   r   transfers      rK   r   z)ApplyBatchImpl._create.<locals>.<genexpr>  s$      'Ax!X- s   r   r   )r   prep_table_for_batchr   r   r   _execinsertinlinefrom_selectrB   r   itemsr   r   r   r}   r   r]   r   r   r   )rJ   op_implr  r   s       rK   r`   zApplyBatchImpl._create  s   ,,.$$T4::6~~)))T^^, 	;MM%%' +/+@+@+F+F+H 
  -1,A,A,H,H,J (%1 %V,$ tzz*
   $$djjoodjj>O>O !  #'**//DNN;@@B .C((-. '+&:&:#+* '+&:&:#	t~~.s+   BG( G-'G(  &G G( G%(HNc	                   | j                   |   }
| j                  |   }|||k7  r||
_        ||d<   |	j                  dd       }|ret	        |	d   | j
                  j                        }t        |t              r2|j                  r&| j                  j                  |j                  d        |ut        j                  |      }t        |
j                  t              r"dx|
j                  _        |
j                  _        | j
                  j!                  |
||       ||
_        |||
_        |dur.|d |
_        n$t'        j(                  |      j+                  |
       |t-        |      |
_        |dur||
_        y y )Nr]   existing_typeF)r   r   r]   rC   r#   rE   rP   r   r   r   popr   to_instancer   _create_eventscreate_constraintcast_for_batch_migratenullableserver_defaultr   DefaultClause_set_parentr   autoincrementcomment)rJ   r8   column_namer  r  r]   type_r  r  rd   existingexisting_transferr  resolved_existing_types                 rK   rl   zApplyBatchImpl.alter_column  s    <<,,0,A,A+,N 3 !HM(,f%FF?D9M)='):):*& 57HI.33**...33
 ((/E (--):; ,MM3 II,,+U "HM  (H&%*.'(("+h'$%)-%8H"%&H  rM   c                F   | j                   }t        |      D ci c]  \  }}||
 }}}| j                  sz|r:|s8||v r||   dz   }|t        |      k  r||   }nt	        | j
                        |   }|r<|s:||v r||   dz
  }|dk\  r)||   }n#| j
                  D 	
ci c]  \  }	}
|
|	
 c}
}	|   }|r| xj
                  ||ffz  c_        |r| xj
                  ||ffz  c_        | j                  s%|s"|s|r| xj
                  |d   |ffz  c_        y y y y y c c}}w c c}
}	w )Nr   r   r   )r   r   rH   r   r=   r   )rJ   r   rq   rr   
index_colsir]   col_indexesr   abs              rK   "_setup_dependencies_for_add_columnz1ApplyBatchImpl._setup_dependencies_for_add_column.  sk    ++
.7
.CD71dtQwDD&&$#{2),7!;Z0,6sOM )-T-B-B(C() #$3)-81<!8+5c?L .2-B-B(%)QAqD('() !!w&>%@@!!!|W&=%??! ''! !!z"~w&?%AA!  ! " (E E0(s   DDc                    | j                  |j                  ||       t        || j                  j                        | j
                  |j                  <   i | j                  |j                  <   y )Nr   )r"  r]   r   r   r   r   r   )rJ   r8   r   rq   rr   rd   s         rK   rs   zApplyBatchImpl.add_column^  sV     	//KK	

 %*&9J9J$KV[[!-/fkk*rM   c                   |j                   | j                  j                  j                  v r*t	        | j                  j                  j                  |       | j                  |j                   = | j
                  |j                   = | j                  j                  |j                          d|v rNt        |d   t              r:|d   j                   r*| j                  j                  |d   j                   d        y y y y )Nr  )r]   r   primary_keyr   r"   r   r   remover   r   r   r  )rJ   r8   r   rd   s       rK   ru   zApplyBatchImpl.drop_columnn  s     ;;$**00888*

&&.. LL%!!&++.%%fkk2 r!2o.0AB?#((""&&?#(($ ) C "rM   c                     y)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.

        Nr5   r   s     rK   r   z$ApplyBatchImpl.create_column_comment      rM   c                     y)zthe batch table creation function will issue create_table_comment
        on the real "impl" as part of the create table process.

        Nr5   r   s     rK   r   z#ApplyBatchImpl.create_table_comment  r(  rM   c                     y)zthe batch table creation function will issue drop_table_comment
        on the real "impl" as part of the create table process.

        Nr5   r   s     rK   r   z!ApplyBatchImpl.drop_table_comment  r(  rM   c                   t        |j                        st        d      t        |t        j
                        rQ| j                  j                  | j                  v r/| j                  j                  | j                  j                         t        |j                        r|| j                  |j                  <   y | j                  j                  |       y )NConstraint must have a name)r$   r]   r9   r   r   r   r   r%  r   r&  r%   r   rD   rx   s     rK   rw   zApplyBatchImpl.add_constraint  s    &uzz2:;;eZ<<=zz%%)A)AA((//

0F0FG!%**-16D""5::.$$++E2rM   c                \   |j                   st        d      	 |j                   | j                  v r| j                  j                  |j                         \  }}t	        | j
                  |j                      j                        D ]N  }|j                   |j                   k(  s| j
                  |j                      j                  j                  |       P ndt        |j                         r&| j                  j                  |j                         }n)|| j                  v r| j                  j                  |       t        |t              r0|j
                  D ]   }d| j
                  |j                      _        " y y # t        $ r& t        |      rY y t        d|j                   z        w xY w)Nr,  FzNo such constraint: '%s')r]   r9   r   r  rB   r   r   r&  r%   r   r   r   r   r%  KeyErrorr!   )rJ   ry   r   	col_consts       rK   r{   zApplyBatchImpl.drop_constraint  s]   zz:;;	?zzT777!77;;EJJG
U!%dll388&<&H&H!I MI ~~3SXX.::AA)LM (

3..225::>$222((//6 %!56 == ?C9>DLL*6? 7  	Fe$
 7%**DEE	Fs   BE< "BE< <F+F+c                6    || j                   |j                  <   y rO   )r   r]   rJ   r   s     rK   r   zApplyBatchImpl.create_index  s    %("rM   c                ~    	 | j                   |j                  = y # t        $ r t        d|j                  z        w xY w)NzNo such index: '%s')r   r]   r.  r9   r1  s     rK   r   zApplyBatchImpl.drop_index  s;    	?SXX& 	?2SXX=>>	?s    "<c                    t        d      )NTODOr   rn   s      rK   r}   zApplyBatchImpl.rename_table  s    !&))rM   )r5   )rE   r-   r   r   r<   tupler>   zDict[str, Any]rh   r   rH   r5  r   r   )r   zUnion[quoted_name, str]r   strr   )r   zList[Index])r   r   r   r   r   r   )r	  r-   r   r   )NFNNNF)r8   r6  r  r6  r  zOptional[bool]r  z)Optional[Union[Function[Any], str, bool]]r]   Optional[str]r  zOptional[TypeEngine]r  z&Optional[Union[bool, Literal['auto']]]r  zUnion[str, Literal[False]]r   r   )r   r6  rq   r7  rr   r7  r   r   )NN)
r8   r6  r   zColumn[Any]rq   r7  rr   r7  r   r   )r8   r6  r   z%Union[ColumnClause[Any], Column[Any]]r   r   r   )r   r   r   r   )r   r   r   rL   classmethodr   r   r   r   r   r   r`   rl   r"  rs   ru   r   r   r   rw   r{   r   r   r}   r5   rM   rK   r_   r_      s    %'$$ $ 	$
 %$ $ "$ 
$: 5 5,B\
<04dB# #.B#	#J';Z $(DI"&*@D.3I'I' I' !	I'
 BI' I' $I' >I' ,I' 
I'V.B.B %.B $	.B
 
.Bh (,&*00 0 %	0
 $0 
0  6
 
0
3?8)?*rM   r_   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/   r_   r5   rM   rK   <module>rE     s    #         &  +   + +   ( 3 ' '  6 $ / 4 6 ; - = 3 6 5)43102&[D [D|z* z*rM   