
    g?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) dd#lm*Z* er0d 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-      Z8 G d. d/      Z9y0)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                      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)selfr8   r9   r   r;   r<   r=   r?   r@   rA   rH   rI   s               W/var/www/html/AdoubleTech/venv/lib/python3.12/site-packages/alembic/operations/batch.py__init__zBatchOperationsImpl.__init__8   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)r8   rF   dialectrK   s    rL   rQ   zBatchOperationsImpl.dialect]   s    ##+++rN   c                .    | j                   j                  S rP   )r8   rF   rR   s    rL   rF   zBatchOperationsImpl.impla   s    ###rN   c                    | j                   dk(  r%| j                  j                  j                  |       S | j                   dk(  ryy)Nr2   r3   TF)r;   r8   rF   requires_recreate_in_batchrR   s    rL   _should_recreatez$BatchOperationsImpl._should_recreatee   s;    ==F"??''BB4HH]]h&rN   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)rH   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)rI   )rV   r   rF   
connectionrJ   getattrr8   rH   r   r<   migration_contextas_sqlr   CommandErrorrQ   namer9   r   r@   r   get_bindrA   ApplyBatchImplr=   r?   rI   _create)
rK   should_recreateopnameargkwfnm1existing_table	reflected
batch_impls
             rL   flushzBatchOperationsImpl.flushm   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rJ   rE   rK   rd   re   s      rL   rm   z BatchOperationsImpl.alter_column       

>334rN   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)rV   r   r]   rJ   rE   ro   s      rL   rt   zBatchOperationsImpl.add_column   sP    r!^r%9'')"":  	

<b12rN   c                @    | j                   j                  d||f       y )Ndrop_columnrn   ro   s      rL   rv   zBatchOperationsImpl.drop_column   s    

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

+eXr:;rN   c                B    | j                   j                  d|fi f       y )Ndrop_constraintrn   ry   s     rL   r|   z#BatchOperationsImpl.drop_constraint   s    

,uh;<rN   c                @    | j                   j                  d||f       y )Nrename_tablern   ro   s      rL   r~   z BatchOperationsImpl.rename_table   rp   rN   c                B    | j                   j                  d|f|f       y )Ncreate_indexrn   rK   idxre   s      rL   r   z BatchOperationsImpl.create_index   s    

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

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

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

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

2VIrBCrN   N)returnr(   )r   r.   )r   boolr   Nonerz   r,   r   r   )r   r   re   r   r   r   )__name__
__module____qualname__rM   propertyrQ   rF   rV   rk   rm   rt   rv   rx   r|   r~   r   r   r   r   r   r   r   r6   rN   rL   r0   r0   7   s    #J , , $ $7.r534<=586B@FDDrN   r0   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 )Nr6   c              3  <   K   | ]  }|j                   d |if  ywexprN)r^   ).0cs     rL   	<genexpr>z*ApplyBatchImpl.__init__.<locals>.<genexpr>   s"      ,
&'QVVfa[!,
s   )rF   r   r=   r?   _calc_temp_namer^   temp_table_name	new_tablerI   add_col_orderingr   r   column_transfersrC   existing_orderingri   _grab_table_elements)rK   rF   r   r=   r?   ri   rI   s          rL   rM   zApplyBatchImpl.__init__   s     	
$(#33EJJ?*."4  	 !, ,
+/::<<,
 !
 "&d&;&;!<"!!#rN   c                    d|z  dd S )Nz_alembic_tmp_%sr   2   r6   )cls	tablenames     rL   r   zApplyBatchImpl._calc_temp_name   s    !I-q44rN   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!   ri   r   r&   rE   kwargsr?   rB   )rK   r   r   c_copyrz   colr   ks           rL   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rN   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rP   )r   r   r   rK   s     rL   r   zMApplyBatchImpl._adjust_self_columns_for_partial_reordering.<locals>.<genexpr>>  s     "IAAt||A#7"I   c              3  @   K   | ]  }|j                   |   f  y wrP   )r   r   s     rL   r   zMApplyBatchImpl._adjust_self_columns_for_partial_reordering.<locals>.<genexpr>?  s&      ,
./Q%%a(),
r   )setrC   r   rI   	enumerateaddr   updater   r   sortr   r   )	rK   pairs
col_by_idxtuple_r   elemp
pairs_listsorted_s	   `        rL   +_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   rI   r   r   r   r   rC   r   valuesr=   r?   r   r   r   keyissubsetr   r   r   r   r   _setup_referentappend_constraint)rK   mr   r   rz   r   const_columns
const_copys           rL   _transfer_elements_to_new_tablez.ApplyBatchImpl._transfer_elements_to_new_tableC  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   rE   r   r   keysr   )rK   r   idx_existingr   idx_copyr   r   s          rL    _gather_indexes_from_both_tablesz/ApplyBatchImpl._gather_indexes_from_both_tablesu  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 rP   )_get_colspec)r   s    rL   colspecz/ApplyBatchImpl._setup_referent.<locals>.colspec  s    ((**rN   r   )r   r   )elementsr   splitlenr   
sql_schema_get_table_keytablesr   append_columnr   sqltypesNULLTYPEr   )rK   metadata
constraintspecpartstnamereferent_schemar   r   tr   colnamens                rL   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                  t        | 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   r6   )r   r   transfers      rL   r   z)ApplyBatchImpl._create.<locals>.<genexpr>  s$      'Ax!X- s   r   r   )r   prep_table_for_batchr   r   r   _execr    from_selectrC   r   itemsr$   r   r   r~   r   r^   r   r   r   )rK   op_implr  r   s       rL   ra   zApplyBatchImpl._create  s   ,,.$$T4::6~~)))T^^,	;MMt~~.:: +/+@+@+F+F+H 
  -1,A,A,H,H,J (%1 %V,  tzz*
   $$djjoodjj>O>O !  #'**//DNN;@@B .C((-. '+&:&:#+* '+&:&:#	t~~.s+   A3G 
F0'G -&F5 0G 5GG3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^   rD   r#   rF   rQ   r   r   r   popr   to_instancer   _create_eventscreate_constraintcast_for_batch_migratenullableserver_defaultr   DefaultClause_set_parentr   autoincrementcomment)rK   r9   column_namer  r  r^   type_r  r  re   existingexisting_transferr
  resolved_existing_types                 rL   rm   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  rN   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   rI   r   r>   r   )rK   r   rr   rs   
index_colsir^   col_indexesr   abs              rL   "_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   )rK   r9   r   rr   rs   re   s         rL   rt   zApplyBatchImpl.add_column]  sV     	//KK	

 %*&9J9J$KV[[!-/fkk*rN   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  )rK   r9   r   re   s       rL   rv   zApplyBatchImpl.drop_columnm  s     ;;$**00888*

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

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

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

        Nr6   r   s     rL   r   z!ApplyBatchImpl.drop_table_comment  r'  rN   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^   r:   r   r   r   r   r$  r   r%  r&   r   rE   ry   s     rL   rx   zApplyBatchImpl.add_constraint  s    &uzz2:;;eZ<<=zz%%)A)AA((//

0F0FG!%**-16D""5::.$$++E2rN   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^   r:   r   r  rC   r   r   r%  r&   r   r   r   r   r$  KeyErrorr!   )rK   rz   r   	col_consts       rL   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 rP   )r   r^   rK   r   s     rL   r   zApplyBatchImpl.create_index  s    %("rN   c                ~    	 | j                   |j                  = y # t        $ r t        d|j                  z        w xY w)NzNo such index: '%s')r   r^   r-  r:   r0  s     rL   r   zApplyBatchImpl.drop_index  s;    	?SXX& 	?2SXX=>>	?s    "<c                    t        d      )NTODOr   ro   s      rL   r~   zApplyBatchImpl.rename_table  s    !&))rN   )r6   )rF   r.   r   r   r=   tupler?   zDict[str, Any]ri   r   rI   r4  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)r9   r5  r  r5  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   r5  rr   r6  rs   r6  r   r   )NN)
r9   r5  r   zColumn[Any]rr   r6  rs   r6  r   r   )r9   r5  r   z%Union[ColumnClause[Any], Column[Any]]r   r   r   )r   r   r   r   )r   r   r   rM   classmethodr   r   r   r   r   r   ra   rm   r!  rt   rv   r   r   r   rx   r|   r   r   r~   r6   rN   rL   r`   r`      s    %'$$ $ 	$
 %$ $ "$ 
$: 5 5,B\
<04dB# #.B#	#J%;V $(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)?*rN   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.   r0   r`   r6   rN   rL   <module>rD     s    #         &  +   + +  ( 3 ' '  6 $ / 4 6 ; - - = 3 & 6 5)43102&[D [D|x* x*rN   