o
    DfA                     @  s@  d dl m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 erd 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' G dd  d eZ(G d!d" d"e(Z)G d#d$ d$eZ*G d%d& d&eZ+G d'd( d(e+Z,eed)d*eed)d*eed)d*eed)d*d+d, Z-ee*d)d*dFd2d3Z.ee,d)d*dGd4d5Z/ee+d)d*dHd6d7Z0dIdAdBZ1ee	j"d)d*dJdDdEZ2dS )K    )annotationsN)Any)Optional)TYPE_CHECKING)Unionschema)types   )alter_table)AlterColumn)ColumnDefault)
ColumnName)ColumnNullable)
ColumnType)format_column_name)format_server_default)DefaultImpl   )util)sqla_compat)_is_mariadb)_is_type_bound)compiles)Literal)MySQLDDLCompiler)DropConstraint)
Constraint)
TypeEngine)_ServerDefaultc                      s   e Zd ZdZdZejddhddhf ZddgZ																						d2d3 fd#d$Zd4 fd'd(Z	d5d*d+Z
d,d- Zd.d/ Zd0d1 Z  ZS )6	MySQLImplmysqlFBOOLTINYINTJSONLONGTEXTzcharacter set ([\w\-_]+)zcollate ([\w\-_]+)N
table_namestrcolumn_namenullableOptional[bool]server_default%Union[_ServerDefault, Literal[False]]nameOptional[str]type_Optional[TypeEngine]r   existing_typeexisting_server_defaultOptional[_ServerDefault]existing_nullableautoincrementexisting_autoincrementcomment$Optional[Union[str, Literal[False]]]existing_commentkwr   returnNonec                   s  t ||	st ||	rt j||f|||||
||	d| |d us/| |d ur+|n||rk| t||||d ur;|n||d urB|n|
d urH|
nd|d urO|n||durV|n|	|d ur]|n||durd|n|d	 d S |d us{|d us{|d us{|dur| t||||d ur|n||d ur|n|
d ur|
nd|d ur|n||dur|n|	|d ur|n||dur|n|d	 d S |dur| t	||||d d S d S )N)r)   r/   r   r1   r4   r+   r2   TF)r   newnamer)   r/   defaultr5   r7   r   )
r   _server_default_is_identity_server_default_is_computedsuperalter_column$_is_mysql_allowed_functional_default_execMySQLChangeColumnMySQLModifyColumnMySQLAlterDefault)selfr&   r(   r)   r+   r-   r/   r   r1   r2   r4   r5   r6   r7   r9   r:   	__class__ J/home/ubuntu/webapp/venv/lib/python3.10/site-packages/alembic/ddl/mysql.pyrB   3   s   
zMySQLImpl.alter_columnconstr   c                   s(   t |tjrt|rd S t | d S N)
isinstancer   CheckConstraintr   rA   drop_constraint)rH   rM   rI   rK   rL   rQ      s   zMySQLImpl.drop_constraintboolc                 C  s   |d uo|j tju o|d uS rN   )_type_affinitysqltypesDateTime)rH   r/   r+   rK   rK   rL   rC      s
   
z.MySQLImpl._is_mysql_allowed_functional_defaultc                 C  s  |j jtju r|jr|js|s|dkrdS |r/|j jtju r/|d ur)tdd|nd }||kS |rG|j jtju rGtdd|}|d| dkS |r|rt	d|
 }t	d|
 }|rw|sadS |d|dkrmdS |d	}|d	}td
d|
 td
d|
 kS ||kS )Nz'0'Fz^'|'$ 'z(.*) (on update.*?)(?:\(\))?$Tr   r
   z(.*?)(?:\(\))?$z\1)typerS   rT   Integerprimary_keyr5   resubStringmatchlowergroup)rH   inspector_columnmetadata_columnrendered_metadata_defaultrendered_inspector_defaultmetadata_defaultonupdate_insonupdate_metrK   rK   rL   compare_server_default   s\   



z MySQLImpl.compare_server_defaultc           	      C  s   t  }t|D ]?}|jrq|jD ]5}|j|jkr%|| ||j  n!|jD ]}|j|jkr=|| ||j  nq(|j|v rE nqqt|D ]}|j|v rW|| qKd S rN   )setlistuniquecolumnsr-   removeaddforeign_keys)	rH   conn_unique_constraintsconn_indexesmetadata_unique_constraintsmetadata_indexesremovedidxcolfkrK   rK   rL   correct_for_autogen_constraints   s0   	






z)MySQLImpl.correct_for_autogen_constraintsc                   s    fdd|D } fdd|D }t ||D ]2}|| }|| }|jd ur7|j dkr7|jd u r7d|_|jd urK|j dkrK|jd u rKd|_qd S )Nc                      i | ]	}  |j|qS rK   ) _create_reflected_constraint_sigunnamed_no_options.0rw   rH   rK   rL   
<dictcomp>      z=MySQLImpl.correct_for_autogen_foreignkeys.<locals>.<dictcomp>c                   ry   rK   )_create_metadata_constraint_sigr{   r|   r~   rK   rL   r     r   restrictRESTRICT)ri   intersectionondeleter_   onupdate)rH   conn_fksmetadata_fksconn_fk_by_sigmetadata_fk_by_sigsigmdfkcnfkrK   r~   rL   correct_for_autogen_foreignkeys  s&   





z)MySQLImpl.correct_for_autogen_foreignkeys)NFNNNNNNNNFN) r&   r'   r(   r'   r)   r*   r+   r,   r-   r.   r/   r0   r   r.   r1   r0   r2   r3   r4   r*   r5   r*   r6   r*   r7   r8   r9   r.   r:   r   r;   r<   )rM   r   r;   r<   )r/   r0   r+   r,   r;   rR   )__name__
__module____qualname____dialect__transactional_ddlr   type_synonymstype_arg_extractrB   rQ   rC   rh   rx   r   __classcell__rK   rK   rI   rL   r    )   s4    c
	C%r    c                   @  s   e Zd ZdZdS )MariaDBImplmariadbN)r   r   r   r   rK   rK   rK   rL   r   0  s    r   c                      s"   e Zd Z	dd fddZ  ZS )rG   Nr-   r'   r(   r>   r   r   r.   r;   r<   c                   s$   t t| j||d || _|| _d S )Nr   )rA   r   __init__r(   r>   )rH   r-   r(   r>   r   rI   rK   rL   r   5  s   
zMySQLAlterDefault.__init__rN   )
r-   r'   r(   r'   r>   r   r   r.   r;   r<   r   r   r   r   r   rK   rK   rI   rL   rG   4  s    rG   c                      s.   e Zd Z							dd fddZ  ZS )rE   NFr-   r'   r(   r   r.   r=   r/   r0   r)   r*   r>   /Optional[Union[_ServerDefault, Literal[False]]]r5   r7   r8   r;   r<   c
           
        sZ   t t| j||d || _|| _|| _|| _|| _|	| _|d u r%t	
dt|| _d S )Nr   zDAll MySQL CHANGE/MODIFY COLUMN operations require the existing type.)rA   r   r   r(   r)   r=   r>   r5   r7   r   CommandErrorrT   to_instancer/   )
rH   r-   r(   r   r=   r/   r)   r>   r5   r7   rI   rK   rL   r   B  s   zMySQLChangeColumn.__init__)NNNNFNF)r-   r'   r(   r'   r   r.   r=   r.   r/   r0   r)   r*   r>   r   r5   r*   r7   r8   r;   r<   r   rK   rK   rI   rL   rE   A  s    rE   c                   @  s   e Zd ZdS )rF   N)r   r   r   rK   rK   rK   rL   rF   ^  s    rF   r!   r   c                 K  s   t d)Nz9Individual alter column constructs not supported by MySQL)NotImplementedErrorelementcompilerr:   rK   rK   rL    _mysql_doesnt_support_individualb  s   r   r   r   r   r;   r'   c                 K  s@   dt || j| jt|| j| jd urdt|| j f S df S )Nz%s ALTER COLUMN %s %szSET DEFAULT %szDROP DEFAULT)r   r&   r   r   r(   r>   r   r   rK   rK   rL   _mysql_alter_defaultl  s   

r   c                 K  s<   dt || j| jt|| jt|| j| j| j| j	| j
df S )Nz%s MODIFY %s %sr)   r+   r/   r5   r7   )r   r&   r   r   r(   _mysql_colspecr)   r>   r/   r5   r7   r   rK   rK   rL   _mysql_modify_columny  s   
r   c                 K  sF   dt || j| jt|| jt|| jt|| j| j| j	| j
| jdf S )Nz%s CHANGE %s %s %sr   )r   r&   r   r   r(   r=   r   r)   r>   r/   r5   r7   r   rK   rK   rL   _mysql_change_column  s   

r   r)   r*   r+   r   r/   r   r5   r7   r8   c                 C  sn   d| j j||rdndf }|r|d7 }|dur&|d ur&|dt| | 7 }|r5|d| j|t  7 }|S )Nz%s %sNULLzNOT NULLz AUTO_INCREMENTFz DEFAULT %sz COMMENT %s)dialecttype_compilerprocessr   sql_compilerrender_literal_valuerT   r]   )r   r)   r+   r/   r5   r7   specrK   rK   rL   r     s   

r   r   c                 K  s   | j }t|tjtjtjfr|rJ || S t|tjr@t|j	r1d|j
|j|j
|f S d|j
|j|j
|f S td)zVRedefine SQLAlchemy's drop constraint to
    raise errors for invalid constraint type.z!ALTER TABLE %s DROP CONSTRAINT %szALTER TABLE %s DROP CHECK %szFNo generic 'DROP CONSTRAINT' in MySQL - please specify constraint type)r   rO   r   ForeignKeyConstraintPrimaryKeyConstraintUniqueConstraintvisit_drop_constraintrP   r   r   preparerformat_tabletableformat_constraintr   )r   r   r:   
constraintrK   rK   rL   _mysql_drop_constraint  s.   



r   )r   rG   r   r   r;   r'   )r   rF   r   r   r;   r'   )r   rE   r   r   r;   r'   )r   r   r)   r*   r+   r   r/   r   r5   r*   r7   r8   r;   r'   )r   r   r   r   r;   r'   )3
__future__r   r[   typingr   r   r   r   
sqlalchemyr   r	   rT   baser   r   r   r   r   r   r   r   implr   rV   r   r   util.sqla_compatr   r   r   r   sqlalchemy.dialects.mysql.baser   sqlalchemy.sql.ddlr   sqlalchemy.sql.schemar   sqlalchemy.sql.type_apir   r   r    r   rG   rE   rF   r   r   r   r   r   r   rK   rK   rK   rL   <module>   sd     	







