o
    hru                     @  s  U d dl mZ d dlZd dl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r,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/m0Z0 d d*l1m2Z2 d d+l1m3Z3 d d,l1m4Z4 d d-l1m5Z5 d d.l6m7Z7 d d/l8m9Z9 dd0lm:Z: dd1l;m<Z< dd2l=m>Z> dd3l=m?Z? e@eAZBG d4d5 d5eCZDi ZEd6eFd7< G d8d9 d9eDd:ZGG d;d< d<eZHdFdDdEZIdS )G    )annotationsN)Any)Callable)Dict)Iterable)List)Mapping)
NamedTuple)Optional)Sequence)Set)Tuple)Type)TYPE_CHECKING)Union)cast)Column)MetaData)PrimaryKeyConstraintschema)String)Table)text   )_autogen)base)_constraint_sig)ComparisonResult   )util)sqla_compat)Literal)TextIO)
Connection)Dialect)CursorResult)	Inspector)ClauseElement)
Executable)quoted_name)
Constraint)ForeignKeyConstraint)Index)UniqueConstraint)TableClause)
TypeEngine)_ServerDefault)AutogenContext)ApplyBatchImpl)BatchOperationsImplc                   @  s   e Zd Zd
ddZd	S )ImplMeta	classnamestrbasesTuple[Type[DefaultImpl]]dict_Dict[str, Any]c                 C  s(   t | |||}d|v r| t|d < |S )N__dialect__)type__init___impls)clsr6   r8   r:   newtype rB   d/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/alembic/ddl/impl.pyr>   B   s   zImplMeta.__init__N)r6   r7   r8   r9   r:   r;   )__name__
__module____qualname__r>   rB   rB   rB   rC   r5   A   s    r5   zDict[str, Type[DefaultImpl]]r?   c                   @  sZ  e Zd ZU dZdZdZdZddhfZded< d	Z	d
ed< dZ
ded< dddZedd d!Zdd$d%Zdd-d.Zdd1d2Zdd6d7Zedd8d9Zd:d:e fddDdEZ	:dddHdIZ	:		:	:	:	:		:	:	:	:	:ddd\d]Z	:dddadbZ	:dddcddZddfdgZddidjZ	:dddndoZddpdqZddrdsZddvdwZ ddxdyZ!ddzd{Z"dd|d}Z#dd~dZ$	ddddZ%dddZ&dddZ'dddZ(dddZ)dd Z*dddZ+dd Z,	ddddZ-dddZ.dddZ/dd Z0dddZ1dddZ2dddZ3dddZ4dd Z5dddƄZ6d dd̄Z7d dd΄Z8dddфZ9dddքZ:dd؄ Z;ddd܄Z<d:S (  DefaultImpla  Provide the entrypoint for major migration operations,
    including database-specific behavioral variances.

    While individual SQL/DDL constructs already provide
    for database-specific implementations, variances here
    allow for entirely different sequences of operations
    to take place for a particular migration, such as
    SQL Server's special 'IDENTITY INSERT' step for
    bulk inserts.

    defaultF;NUMERICDECIMALzTuple[Set[str], ...]type_synonymsrB   zSequence[str]type_arg_extract)orderon_nullzTuple[str, ...]identity_attrs_ignoredialectr%   
connectionOptional[Connection]as_sqlbooltransactional_ddlOptional[bool]output_bufferOptional[TextIO]context_optsr;   returnNonec                 C  s^   || _ || _|| _|dd| _|| _i | _|| _|d ur || _| jr+| js-t	
dd S d S )Nliteral_bindsFz3Can't use literal_binds setting without as_sql mode)rQ   rR   rT   getr]   rX   memorZ   rV   r    CommandError)selfrQ   rR   rT   rV   rX   rZ   rB   rB   rC   r>   j   s    	zDefaultImpl.__init__Type[DefaultImpl]c                 C  s
   t |j S N)r?   name)r@   rQ   rB   rB   rC   get_by_dialect   s   
zDefaultImpl.get_by_dialectr   r7   c                 C  s,   | j d usJ | j |d  | j   d S )Nz

)rX   writeflush)ra   r   rB   rB   rC   static_output   s   zDefaultImpl.static_outputversion_tableversion_table_schemaOptional[str]version_table_pkkwr   r   c                K  s@   t |t tdtddd|d}|r|td| dd |S )a  Generate a :class:`.Table` object which will be used as the
        structure for the Alembic version table.

        Third party dialects may override this hook to provide an alternate
        structure for this :class:`.Table`; requirements are only that it
        be named based on the ``version_table`` parameter and contains
        at least a single string-holding column named ``version_num``.

        .. versionadded:: 1.14

        version_num    F)nullabler   _pkcrd   )r   r   r   r   append_constraintr   )ra   ri   rj   rl   rm   vtrB   rB   rC   version_table_impl   s   
zDefaultImpl.version_table_implbatch_opr4   c                 C     dS )zReturn True if the given :class:`.BatchOperationsImpl`
        would need the table to be recreated and copied in order to
        proceed.

        Normally, only returns True on SQLite when operations other
        than add_column are present.

        FrB   )ra   rv   rB   rB   rC   requires_recreate_in_batch   s   z&DefaultImpl.requires_recreate_in_batch
batch_implr3   tablec                 C  rw   )zperform any operations needed on a table before a new
        one is created to replace it in batch mode.

        the PG dialect uses this to drop constraints on the table
        before the new one uses those same names.

        NrB   )ra   ry   rz   rB   rB   rC   prep_table_for_batch       z DefaultImpl.prep_table_for_batchc                 C     | j S rc   )rR   ra   rB   rB   rC   bind   s   zDefaultImpl.bindN	constructUnion[Executable, str]execution_optionsOptional[Mapping[str, Any]]multiparams%Optional[Sequence[Mapping[str, Any]]]paramsMapping[str, Any]Optional[CursorResult]c                 C  s   t |tr	t|}| jrO|d us|rtd| jr't |tjs'tddid}ni }t	r2t |t
s2J |jd	d| ji|}| t|dd | j  d S | j}|d usXJ |rb|jd	i |}|rl|d urltd|rt|||S |||S )
Nz&SQL parameters not allowed with as_sqlr]   T)compile_kwargsrQ   	z    z2Can't send params and multiparams at the same timerB   )
isinstancer7   r   rT   	TypeErrorr]   r   
DDLElementdictr   r(   compilerQ   rh   replacestripcommand_terminatorrR   r   execute)ra   r   r   r   r   
compile_kwcompiledconnrB   rB   rC   _exec   s<   
zDefaultImpl._execsqlOptional[dict[str, Any]]c                 C  s   |  || d S rc   )r   )ra   r   r   rB   rB   rC   r      s   zDefaultImpl.execute
table_namecolumn_namerp   server_default%Union[_ServerDefault, Literal[False]]rd   type_Optional[TypeEngine]r   autoincrementcomment$Optional[Union[str, Literal[False]]]existing_commentexisting_typeexisting_server_defaultOptional[_ServerDefault]existing_nullableexisting_autoincrementc                 K  s,  |d us|d urt jddd |d ur#| tj||||||||
d |durWi }t||r3tj}nt||rAtj	}| |d< ntj
}| ||||f|||||
d| |d urk| tj||||||||
d |	dur| tj|||	|||||
d |d ur| tj|||||||d d S d S )NzBautoincrement and existing_autoincrement only make sense for MySQL   )
stacklevel)r   r   r   r   r   Fimpl)r   r   r   r   )r    warnr   r   ColumnNullabler!   _server_default_is_computedComputedColumnDefault_server_default_is_identityIdentityColumnDefaultColumnDefault
ColumnTypeColumnComment
ColumnName)ra   r   r   rp   r   rd   r   r   r   r   r   r   r   r   r   rm   cls_rB   rB   rC   alter_column   s   
	zDefaultImpl.alter_columncolumnColumn[Any]!Optional[Union[str, quoted_name]]c                 C     |  tj|||d d S Nr   )r   r   	AddColumn)ra   r   r   r   rB   rB   rC   
add_columno  s   zDefaultImpl.add_columnc                 K  r   r   )r   r   
DropColumn)ra   r   r   r   rm   rB   rB   rC   drop_columnw  s   zDefaultImpl.drop_columnconstc                 C  s,   |j d u s
| | r| t| d S d S rc   )_create_ruler   r   AddConstraintra   r   rB   rB   rC   add_constraint  s   zDefaultImpl.add_constraintr+   c                 C     |  t| d S rc   )r   r   DropConstraintr   rB   rB   rC   drop_constraint     zDefaultImpl.drop_constraintold_table_namenew_table_nameUnion[str, quoted_name]c                 C  r   r   )r   r   RenameTable)ra   r   r   r   rB   rB   rC   rename_table  s   zDefaultImpl.rename_tablec                 K  s   |j j|| jd| d | tj|fi | |j j|| jd| d |jD ]
}| t| q%| j	j
o8| j	j }|j}|rE|rE| | |jD ]}|j}|rV|rV| | qHd S NF)
checkfirst_ddl_runner)dispatchbefore_createrR   r   r   CreateTableafter_createindexesCreateIndexrQ   supports_commentsinline_commentsr   create_table_commentcolumnscreate_column_comment)ra   rz   rm   indexwith_commentr   r   rB   rB   rC   create_table  s(   





zDefaultImpl.create_tablec                 K  sH   |j j|| jd| d | tj|fi | |j j|| jd| d d S r   )r   before_droprR   r   r   	DropTable
after_drop)ra   rz   rm   rB   rB   rC   
drop_table  s   


zDefaultImpl.drop_tabler   r-   c                 K     |  tj|fi | d S rc   )r   r   r   ra   r   rm   rB   rB   rC   create_index     zDefaultImpl.create_indexc                 C  r   rc   )r   r   SetTableCommentra   rz   rB   rB   rC   r     r   z DefaultImpl.create_table_commentc                 C  r   rc   )r   r   DropTableCommentr   rB   rB   rC   drop_table_comment  r   zDefaultImpl.drop_table_commentc                 C  r   rc   )r   r   SetColumnComment)ra   r   rB   rB   rC   r     r   z!DefaultImpl.create_column_commentc                 K  r   rc   )r   r   	DropIndexr   rB   rB   rC   
drop_index  r   zDefaultImpl.drop_indexTUnion[TableClause, Table]rows
List[dict]multiinsertc              	     s   t |ts	td|rt |d tstd| jr8|D ]}|    jdi  fdd|	 D  qd S |r]|rI| j   |d d S |D ]}|    jdi | qKd S d S )NzList expectedr   zList of dictionaries expectedc                   s:   i | ]\}}|t |tjstj|| j| jd n|qS ))r   )r   r!   _literal_bindparamcr=   ).0kvrz   rB   rC   
<dictcomp>  s    
z+DefaultImpl.bulk_insert.<locals>.<dictcomp>)r   rB   )
r   listr   r   rT   r   insertinlinevaluesitems)ra   rz   r   r   rowrB   r   rC   bulk_insert  s0   


 zDefaultImpl.bulk_insertr   Paramsc                 C  s   | j j|j }td|}g }d }|D ]}td|r!|}q|| qt	|d |dd  g i }|r\td|D ]}d|v rS|
d\}	}
|
 |j|	 < q<|j|  q<|S )Nz[\w\-_]+|\(.+?\)z^\(.*\)$r   r   z[^(),]+=)rQ   type_compilerprocessr=   lowerrefindallmatchappendr  splitr   kwargsargs)ra   r   
definitiontokensterm_tokens
paren_termtokenr   termkeyvalrB   rB   rC   _tokenize_column_type  s    z!DefaultImpl._tokenize_column_typeinspector_paramsmetadata_paramsc                 C  s|   |j |j krdS dd | jD }d|j g|j }d|j g|j }|D ]}||h|s8|j |j h|r; dS q&dS )NTc                 S  s   g | ]	}d d |D qS )c                 S  s   h | ]}|  qS rB   )r  )r   trB   rB   rC   	<setcomp>      z=DefaultImpl._column_types_match.<locals>.<listcomp>.<setcomp>rB   )r   batchrB   rB   rC   
<listcomp>  s    z3DefaultImpl._column_types_match.<locals>.<listcomp> F)token0rL   joinr  issubset)ra   r  r  synonymsinspector_all_termsmetadata_all_termsr  rB   rB   rC   _column_types_match  s&   zDefaultImpl._column_types_matchinspected_paramsmeta_paramsc                 C  s   t |jt |jkr|j|jkrdS t |jt |jkr$|j|jkr$dS d|j }d|j }| jD ]}t||}t||}|rV|rV|d|dkrV dS q7dS )a  We want to compare column parameters. However, we only want
        to compare parameters that are set. If they both have `collation`,
        we want to make sure they are the same. However, if only one
        specifies it, dont flag it for being less specific
        Fr  r   T)	lenr  r  r  r  rM   r  searchgroup)ra   r%  r&  inspmetaregmimmrB   rB   rC   _column_args_match$  s   

zDefaultImpl._column_args_matchinspector_columnmetadata_columnc                 C  s8   |  |}|  |}| ||sdS | ||sdS dS )zReturns True if there ARE differences between the types of the two
        columns. Takes impl.type_synonyms into account between retrospected
        and metadata types
        TF)r  r$  r/  )ra   r0  r1  r  r  rB   rB   rC   compare_typeE  s   

zDefaultImpl.compare_typec                 C  s   ||kS rc   rB   )ra   r0  r1  rendered_metadata_defaultrendered_inspector_defaultrB   rB   rC   compare_server_defaultU  s   z"DefaultImpl.compare_server_defaultconn_uniquesSet[UniqueConstraint]conn_indexes
Set[Index]metadata_unique_constraintsmetadata_indexesc                 C     d S rc   rB   )ra   r6  r8  r:  r;  rB   rB   rC   correct_for_autogen_constraints^  s   z+DefaultImpl.correct_for_autogen_constraintsc                 C  s(   |j j|jurt|d ||d< d S d S )Nexpr)r=   _type_affinityr   )ra   existingexisting_transfernew_typerB   rB   rC   cast_for_batch_migrateg  s
   z"DefaultImpl.cast_for_batch_migrater>  r(   is_server_defaultc                 K  s   ddd}t |j| j|dS )zdRender a SQL expression that is typically a server default,
        index expression, etc.

        TF)r]   include_table)rQ   r   )r7   r   rQ   )ra   r>  rD  rm   r   rB   rB   rC   render_ddl_sql_exprm  s   
zDefaultImpl.render_ddl_sql_expr	inspectorr'   r   c                 C  r}   rc   )autogen_column_reflect)ra   rG  rB   rB   rC   _compat_autogen_column_reflect{  s   z*DefaultImpl._compat_autogen_column_reflectconn_fksSet[ForeignKeyConstraint]metadata_fksc                 C  r<  rc   rB   )ra   rJ  rL  rB   rB   rC   correct_for_autogen_foreignkeys~  s   z+DefaultImpl.correct_for_autogen_foreignkeysc                 C  rw   )zA hook that is attached to the 'column_reflect' event for when
        a Table is reflected from the database during the autogenerate
        process.

        Dialects can elect to modify the information gathered here.

        NrB   )ra   rG  rz   column_inforB   rB   rC   rH    r|   z"DefaultImpl.autogen_column_reflectc                 C  rw   )zA hook called when :meth:`.EnvironmentContext.run_migrations`
        is called.

        Implementations can set up per-migration-run state here.

        NrB   r~   rB   rB   rC   start_migrations  r|   zDefaultImpl.start_migrationsc                 C     |  d| j  dS )zEmit the string ``BEGIN``, or the backend-specific
        equivalent, on the current connection context.

        This is used in offline mode and typically
        via :meth:`.EnvironmentContext.begin_transaction`.

        BEGINNrh   r   r~   rB   rB   rC   
emit_begin     zDefaultImpl.emit_beginc                 C  rP  )zEmit the string ``COMMIT``, or the backend-specific
        equivalent, on the current connection context.

        This is used in offline mode and typically
        via :meth:`.EnvironmentContext.begin_transaction`.

        COMMITNrR  r~   rB   rB   rC   emit_commit  rT  zDefaultImpl.emit_committype_objr0   autogen_contextr2   Union[str, Literal[False]]c                 C  rw   NFrB   )ra   rW  rX  rB   rB   rC   render_type  s   zDefaultImpl.render_typec                 C  sv   t ||t dhd\}}t|dd }t|dd }t|t|kr&|d || j ||t|p9t|t|kfS )Nalways)skip)_compare_identity_optionsr   IdentitygetattrrU   adddifference_updaterP   )ra   metadata_identityinspector_identitydiffignoredmeta_alwaysinspector_alwaysrB   rB   rC   _compare_identity_default  s   

z%DefaultImpl._compare_identity_defaultmetadata_indexreflected_indexc                 C  s0   t |j}t |j}||krd| d| S d S )Nzunique=z to unique=)rU   unique)ra   rj  rk  conn_uniquemeta_uniquerB   rB   rC   _compare_index_unique  s
   

z!DefaultImpl._compare_index_unique
constraint_autogen._Copts_constraint_sig[_autogen._C]c                 K     t jd| |fi |S )NTr   from_constraintra   rp  rr  rB   rB   rC   _create_metadata_constraint_sig     z+DefaultImpl._create_metadata_constraint_sigc                 K  rt  rZ  ru  rw  rB   rB   rC    _create_reflected_constraint_sig  ry  z,DefaultImpl._create_reflected_constraint_sigr   c                 C  s   g }|  ||}|r|| | |}| |}t|s J t|s'J ||fD ]}|jr7td|j	 q+|j
|j
krK|d|j
 d|j
  |rRt|S t S )zCompare two indexes by comparing the signature generated by
        ``create_index_sig``.

        This method returns a ``ComparisonResult``.
        zGenerating approximate signature for index %s. The dialect implementation should either skip expression indexes or provide a custom implementation.expression  to )ro  r	  rx  rz  r   is_index_sighas_expressionslogwarningr   column_namesr   	DifferentEqual)ra   rj  rk  msg
unique_msgm_sigr_sigsigrB   rB   rC   compare_indexes  s0   




zDefaultImpl.compare_indexesmetadata_constraintr.   reflected_constraintc                 C  sF   |  |}| |}|j}|j}||krtd| d| S t S )a  Compare two unique constraints by comparing the two signatures.

        The arguments are two tuples that contain the unique constraint and
        the signatures generated by ``create_unique_constraint_sig``.

        This method returns a ``ComparisonResult``.
        r{  r|  )rx  rz  unnamedr   r  r  )ra   r  r  metadata_tupreflected_tupmeta_sigconn_sigrB   rB   rC   compare_unique_constraint
  s   z%DefaultImpl.compare_unique_constraintc              
   C  sh   dd |D }t |D ]&}|j|v rqt|}|r1td|jd| jdtj d || qd S )Nc                 S  s   i | ]}|j |qS rB   rr   )r   r   rB   rB   rC   r   '  r  z8DefaultImpl._skip_functional_indexes.<locals>.<dictcomp>z@autogenerate skipping metadata-specified expression-based index z
; dialect z under SQLAlchemy z6 can't reflect these indexes so they can't be compared)	r   rd   r!   is_expression_indexr    r   r<   sqlalchemy_versiondiscard)ra   r;  r8  conn_indexes_by_nameidxiexrB   rB   rC   _skip_functional_indexes&  s$   


z$DefaultImpl._skip_functional_indexesreflected_objectkindc                 C  s   | di S )Ndialect_options)r^   )ra   r  r  rB   rB   rC    adjust_reflected_dialect_options7  s   z,DefaultImpl.adjust_reflected_dialect_options)rQ   r%   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r;   r[   r\   )rQ   r%   r[   rb   )r   r7   r[   r\   )
ri   r7   rj   rk   rl   rU   rm   r   r[   r   )rv   r4   r[   rU   )ry   r3   rz   r   r[   r\   )r[   rS   )
r   r   r   r   r   r   r   r   r[   r   rc   )r   r   r   r   r[   r\   )NFNNNNFNNNNN) r   r7   r   r7   rp   rW   r   r   rd   rk   r   r   r   rk   r   rW   r   r   r   rk   r   r   r   r   r   rW   r   rW   rm   r   r[   r\   )r   r7   r   r   r   r   r[   r\   )r   r7   r   r   r   rk   r[   r\   )r   r   r[   r\   )r   r+   r[   r\   )r   r7   r   r   r   r   r[   r\   )rz   r   rm   r   r[   r\   )r   r-   rm   r   r[   r\   )rz   r   r[   r\   )r   r   r[   r\   )T)rz   r   r   r   r   rU   r[   r\   )r   r   r[   r  )r  r  r  r  r[   rU   )r%  r  r&  r  r[   rU   )r0  r   r1  r   r[   rU   )
r6  r7  r8  r9  r:  r7  r;  r9  r[   r\   )F)r>  r(   rD  rU   rm   r   r[   r7   )rG  r'   r[   r   )rJ  rK  rL  rK  r[   r\   )r[   r\   )rW  r0   rX  r2   r[   rY  )rj  r-   rk  r-   r[   rk   )rp  rq  rr  r   r[   rs  )rj  r-   rk  r-   r[   r   )r  r.   r  r.   r[   r   )r  r;   r  r7   r[   r;   )=rD   rE   rF   __doc__r<   rV   r   rL   __annotations__rM   rP   r>   classmethodre   rh   ru   rx   r{   propertyr   r    immutabledictr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r$  r/  r2  r5  r=  rC  rF  rI  rM  rH  rO  rS  rV  r[  ri  ro  rx  rz  r  r  r  r  rB   rB   rB   rC   rG   Q   s   
 



"
0t
	




	




&
(

!
		


	






+rG   )	metaclassc                   @  s.   e Zd ZU ded< ded< ded< ded< dS )	r  r7   r  z	List[str]r  r  zDict[str, str]r  N)rD   rE   rF   r  rB   rB   rB   rC   r  =  s
   
 r  metadata_io-Union[schema.Identity, schema.Sequence, None]inspector_io
default_io'Union[schema.Identity, schema.Sequence]r]  Set[str]c                   s   t | }t |}t  t d
 fdd}|||t |t|| t jrFt|d	s2J |t| d	i t|d	i |jt|d	i   fS )N	meta_dictr   	insp_dictdefault_dictattrsIterable[str]c                   s\   t |D ]$}| |}||}||kr+||}||kr&| q | qd S rc   )set
differencer^   ra  )r  r  r  r  attr
meta_value
insp_valuedefault_valuere  ignored_attrr]  rB   rC   check_dictsS  s   



z._compare_identity_options.<locals>.check_dictsdialect_kwargs)r  r   r  r   r  r   r  r  )r!   _get_identity_options_dictr  unionidentity_has_dialect_kwargshasattrr`  r  )r  r  r  r]  meta_dinsp_dr  rB   r  rC   r^  D  s(   
	



r^  )r  r  r  r  r  r  r]  r  )J
__future__r   loggingr  typingr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   
sqlalchemyr   r   r   r   r   r   r   r    r   r   r   r   r    r!   r"   r#   sqlalchemy.enginer$   r%   sqlalchemy.engine.cursorr&   sqlalchemy.engine.reflectionr'   sqlalchemy.sqlr(   r)   sqlalchemy.sql.elementsr*   sqlalchemy.sql.schemar+   r,   r-   r.   sqlalchemy.sql.selectabler/   sqlalchemy.sql.type_apir0   r1   autogenerate.apir2   operations.batchr3   r4   	getLoggerrD   r  r=   r5   r?   r  rG   r  r^  rB   rB   rB   rC   <module>   s|   
     q