o
    ÕèFh$*  ã                   @   s@   d dl mZ d dlmZmZmZ d dlmZ G dd„ deƒZdS )é    )ÚBaseDatabaseSchemaEditor)ÚNOT_PROVIDEDÚFÚUniqueConstraint)Ú
LOOKUP_SEPc                       s:  e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZedd„ ƒZe‡ fdd„ƒZdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZ‡ fdd „Z‡ fd!d"„Z‡ fd#d$„Z‡ fd%d&„Z‡ fd'd(„Zdd)œd*d+„Z ‡ fd,d-„Z!d.d/„ Z"‡ fd0d1„Z#‡ fd2d3„Z$‡ fd4d5„Z%d6d7„ Z&‡ fd8d9„Z'‡ fd:d;„Z(‡  Z)S )<ÚDatabaseSchemaEditorz+RENAME TABLE %(old_table)s TO %(new_table)szMODIFY %(column)s %(type)s NULLz#MODIFY %(column)s %(type)s NOT NULLz2MODIFY %(column)s %(type)s%(collation)s%(comment)sz(ALTER COLUMN %(column)s SET DEFAULT NULLz,ALTER TABLE %(table)s DROP COLUMN %(column)sz)ALTER TABLE %(table)s DROP INDEX %(name)szY, ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) REFERENCES %(to_table)s(%(to_column)s)z/ALTER TABLE %(table)s DROP FOREIGN KEY %(name)sz DROP INDEX %(name)s ON %(table)sz?ALTER TABLE %(table)s RENAME INDEX %(old_name)s TO %(new_name)szGALTER TABLE %(table)s ADD CONSTRAINT %(name)s PRIMARY KEY (%(columns)s)z&ALTER TABLE %(table)s DROP PRIMARY KEYz9CREATE INDEX %(name)s ON %(table)s (%(columns)s)%(extra)sz+ALTER TABLE %(table)s COMMENT = %(comment)sNc                 C   s   | j jrdS dS )Nz8ALTER TABLE %(table)s DROP CONSTRAINT IF EXISTS %(name)sz)ALTER TABLE %(table)s DROP CHECK %(name)s)Ú
connectionÚmysql_is_mariadb©Úself© r   úX/var/www/html/loop/nvenv/lib/python3.10/site-packages/django/db/backends/mysql/schema.pyÚsql_delete_check%   s   z%DatabaseSchemaEditor.sql_delete_checkc                    s$   | j j}|r| j jdk rdS tƒ jS )N©é
   é   é   zCALTER TABLE %(table)s CHANGE %(old_column)s %(new_column)s %(type)s)r   r	   Úmysql_versionÚsuperÚsql_rename_column)r   Ú
is_mariadb©Ú	__class__r   r   r   .   s   z&DatabaseSchemaEditor.sql_rename_columnc                 C   s@   | j  ¡  | j j  || j j j¡}t|tƒrt|tƒr| ¡ }|S ©N)r   Úensure_connectionÚescapeÚencodersÚ
isinstanceÚstrÚbytesÚdecode)r   ÚvalueÚquotedr   r   r   Úquote_value7   s   

ÿz DatabaseSchemaEditor.quote_valuec                 C   s$   |  | j¡}|d uo| ¡ | jjv S r   )Údb_typer   ÚlowerÚ_limited_data_types)r   Úfieldr$   r   r   r   Ú_is_limited_data_typeA   s   þz*DatabaseSchemaEditor._is_limited_data_typec                 C   s   | j s|  |¡S dS )NF)Ú$_supports_limited_data_type_defaultsr(   ©r   r'   r   r   r   Úskip_defaultH   s   
z!DatabaseSchemaEditor.skip_defaultc                 C   s   |   |¡r| jjsdS dS )NTF)r(   r   r	   r*   r   r   r   Úskip_default_on_alterM   s   z*DatabaseSchemaEditor.skip_default_on_alterc                 C   s   | j jrdS | j jdkS )NT)é   r   é   )r   r	   r   r
   r   r   r   r)   T   s   z9DatabaseSchemaEditor._supports_limited_data_type_defaultsc                    s(   | j js| jr|  |¡rdS tƒ  |¡S )Nz(%s))r   r	   r)   r(   r   Ú_column_default_sqlr*   r   r   r   r/   [   s   ÿþýz(DatabaseSchemaEditor._column_default_sqlc                    sf   t ƒ  ||¡ |  |¡r/|jd tfvr1|  |¡}|  d|  |jj	¡|  |j
¡dœ |g¡ d S d S d S )Nz%UPDATE %(table)s SET %(column)s = %%s)ÚtableÚcolumn)r   Ú	add_fieldr+   Údefaultr   Úeffective_defaultÚexecuteÚ
quote_nameÚ_metaÚdb_tabler1   )r   Úmodelr'   r4   r   r   r   r2   f   s   

þÿúþzDatabaseSchemaEditor.add_fieldc                    s@   t |tƒr| || ¡d ur| j||j|jd� tƒ  ||¡ d S )N©ÚfieldsÚexpressions)r   r   Ú
create_sqlÚ_create_missing_fk_indexr;   r<   r   Úremove_constraint)r   r9   Ú
constraintr   r   r   r?   v   s   ÿýz&DatabaseSchemaEditor.remove_constraintc                    s0   | j |dd„ |jD ƒ|jd� tƒ  ||¡ d S )Nc                 S   s   g | ]\}}|‘qS r   r   )Ú.0Ú
field_nameÚ_r   r   r   Ú
<listcomp>…   s    z5DatabaseSchemaEditor.remove_index.<locals>.<listcomp>r:   )r>   Úfields_ordersr<   r   Úremove_index)r   r9   Úindexr   r   r   rF   ‚   s   ýz!DatabaseSchemaEditor.remove_indexc                    sV   t ƒ  ||¡s	dS | jj | j ¡ |jj¡}|dkr%| ¡ dkr%|j	r%dS |  
|¡ S )NFÚInnoDBÚ
ForeignKey)r   Ú_field_should_be_indexedr   ÚintrospectionÚget_storage_engineÚcursorr7   r8   Úget_internal_typeÚdb_constraintr(   )r   r9   r'   Ústorager   r   r   rJ   Š   s   ÿÿz-DatabaseSchemaEditor._field_should_be_indexed)r<   c                   sö   d}|r	|d }n|r#| j jjr#t|d tƒr#t|d jvr#|d j}|s'dS |j |¡}| 	¡ dkrw| j j
 |j¡‰ | j  ¡ �}‡ fdd„| j j
 ||jj¡ ¡ D ƒ}W d  ƒ n1 s^w   Y  t|ƒdkry|  | j||gdd�¡ dS dS dS )	aŠ  
        MySQL can remove an implicit FK index on a field when that field is
        covered by another index like a unique_together. "covered" here means
        that the more complex index has the FK field as its first field (see
        https://bugs.mysql.com/bug.php?id=37910).

        Manually create an implicit FK index to make it possible to remove the
        composed index.
        Nr   rI   c                    s,   g | ]\}}|d  r|d d ˆ kr|‘qS )rG   Úcolumnsr   r   )rA   ÚnameÚinfodict©r1   r   r   rD   À   s    ûüzADatabaseSchemaEditor._create_missing_fk_index.<locals>.<listcomp>é   Ú )r;   Úsuffix)r   ÚfeaturesÚsupports_expression_indexesr   r   r   rR   r7   Ú	get_fieldrN   rK   Úidentifier_converterr1   rM   Úget_constraintsr8   ÚitemsÚlenr5   Ú_create_index_sql)r   r9   r;   r<   Úfirst_field_nameÚfirst_fieldrM   Úconstraint_namesr   rT   r   r>   œ   s@   
ÿþý
ÿ
ÿüÿ
ÿñz-DatabaseSchemaEditor._create_missing_fk_indexc                    s$   | j ||d� tƒ j||g|¢R Ž S )N)r;   )r>   r   Ú_delete_composed_index)r   r9   r;   Úargsr   r   r   rc   Î   s   z+DatabaseSchemaEditor._delete_composed_indexc                    s^   |j tur ˆ  |¡\}}|t‡ fdd„|D ƒƒ; }|d|› �7 }|jr)|d7 }|S |d7 }|S )z‚
        Keep the NULL and DEFAULT properties of the old field. If it has
        changed, it will be handled separately.
        c                 3   s   � | ]}ˆ   |¡V  qd S r   )r#   )rA   Úpr
   r   r   Ú	<genexpr>Ù   s   € z;DatabaseSchemaEditor._set_field_new_type.<locals>.<genexpr>z	 DEFAULT z NULLz	 NOT NULL)Ú
db_defaultr   Údb_default_sqlÚtupleÚnull)r   r'   Únew_typeÚdefault_sqlÚparamsr   r
   r   Ú_set_field_new_typeÒ   s   
ÿz(DatabaseSchemaEditor._set_field_new_typec                    s"   |   ||¡}tƒ  ||||||¡S r   )rn   r   Ú_alter_column_type_sql)r   r9   Ú	old_fieldÚ	new_fieldrk   Úold_collationÚnew_collationr   r   r   ro   á   s   ÿz+DatabaseSchemaEditor._alter_column_type_sqlc                    s*   | j jr| j jdkrtƒ  ||¡S |d S )Nr   Úcheck)r   r	   r   r   Ú_field_db_check)r   r'   Úfield_db_paramsr   r   r   ru   é   s   z$DatabaseSchemaEditor._field_db_checkc                    s   |   ||¡}tƒ  ||||¡S r   )rn   r   Ú_rename_field_sql)r   r0   rp   rq   rk   r   r   r   rw   ö   s   z&DatabaseSchemaEditor._rename_field_sqlc                 C   s   dg fS )NrV   r   )r   r9   rq   rk   Únew_db_commentr   r   r   Ú_alter_column_comment_sqlú   s   z.DatabaseSchemaEditor._alter_column_comment_sqlc                    s   t ƒ  |¡}d|› �S )Nz	 COMMENT )r   Ú_comment_sql)r   ÚcommentÚcomment_sqlr   r   r   rz   þ   s   
z!DatabaseSchemaEditor._comment_sqlc                    sR   |j tu rtƒ  |||¡S |j| jd�}|  ||d ¡}d|  |j¡|dœ g fS )N)r   ÚtypezMODIFY %(column)s %(type)s)r1   r}   )	rg   r   r   Ú_alter_column_null_sqlÚdb_parametersr   rn   r6   r1   )r   r9   rp   rq   Únew_db_paramsÚtype_sqlr   r   r   r~     s   

þÿúz+DatabaseSchemaEditor._alter_column_null_sql)*Ú__name__Ú
__module__Ú__qualname__Úsql_rename_tableÚsql_alter_column_nullÚsql_alter_column_not_nullÚsql_alter_column_typeÚ sql_alter_column_no_default_nullÚsql_delete_columnÚsql_delete_uniqueÚsql_create_column_inline_fkÚsql_delete_fkÚsql_delete_indexÚsql_rename_indexÚsql_create_pkÚsql_delete_pkÚsql_create_indexÚsql_alter_table_commentÚsql_alter_column_commentÚpropertyr   r   r#   r(   r+   r,   r)   r/   r2   r?   rF   rJ   r>   rc   rn   ro   ru   rw   ry   rz   r~   Ú__classcell__r   r   r   r   r      sX    ÿÿ


û2r   N)	Údjango.db.backends.base.schemar   Údjango.db.modelsr   r   r   Údjango.db.models.constantsr   r   r   r   r   r   Ú<module>   s    