o
    uvXjö  ã                   @   s,   d dl mZ d dlmZ G dd„ deƒZdS )é    )ÚBaseDatabaseSchemaEditor)ÚNOT_PROVIDEDc                       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d„ Zdd„ Zdd„ Z‡ fdd„Z‡ fdd„Z‡ fdd„Zdd„ Z‡ fdd„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 NULLzMODIFY %(column)s %(type)sz,ALTER TABLE %(table)s DROP COLUMN %(column)szCALTER TABLE %(table)s CHANGE %(old_column)s %(new_column)s %(type)sz)ALTER TABLE %(table)s DROP INDEX %(name)sz/ALTER TABLE %(table)s DROP FOREIGN KEY %(name)sz DROP INDEX %(name)s ON %(table)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)sc                 C   s6   | j  ¡  | j j  || j j j¡}t|tƒr| ¡ }|S ©N)Ú
connectionÚensure_connectionÚescapeÚencodersÚ
isinstanceÚstrÚdecode)ÚselfÚvalueÚquoted© r   ú\/var/www/html/myproject/venv/lib/python3.10/site-packages/django/db/backends/mysql/schema.pyÚquote_value   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_type$   s   z*DatabaseSchemaEditor._is_limited_data_typec                 C   s
   |   |¡S r   )r   )r   r   r   r   r   Úskip_default(   s   
z!DatabaseSchemaEditor.skip_defaultc                    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)ÚsuperÚ	add_fieldr   Údefaultr   Úeffective_defaultÚexecuteÚ
quote_nameÚ_metaÚdb_tabler   )r   Úmodelr   r   ©Ú	__class__r   r   r   +   s   

þýþzDatabaseSchemaEditor.add_fieldc                    sZ   t ƒ  ||¡}| jj | j ¡ |jj¡}|dkr%|r%| ¡ dkr%|j	r%dS |  
|¡ o,|S )NÚInnoDBÚ
ForeignKeyF)r   Ú_field_should_be_indexedr   ÚintrospectionÚget_storage_engineÚcursorr!   r"   Úget_internal_typeÚdb_constraintr   )r   r#   r   Úcreate_indexÚstorager$   r   r   r(   7   s   ÿÿÿz-DatabaseSchemaEditor._field_should_be_indexedc                    sb   |j  |d ¡}| ¡ dkr&| j||jgdd�}|s&|  | j||gdd�¡ tƒ j||g|¢R Ž 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 starts like the simpler one.
        http://bugs.mysql.com/bug.php?id=37910 / Django ticket #24757
        We check here before removing the [unique|index]_together if we have to
        recreate a FK index.
        r   r'   T)ÚindexÚ )Úsuffix)	r!   Ú	get_fieldr,   Ú_constraint_namesr   r   Ú_create_index_sqlr   Ú_delete_composed_index)r   r#   ÚfieldsÚargsÚfirst_fieldÚconstraint_namesr$   r   r   r6   F   s   	z+DatabaseSchemaEditor._delete_composed_indexc                 C   s   |j r	|d7 }|S |d7 }|S )zt
        Keep the null property of the old field. If it has changed, it will be
        handled separately.
        z NULLz	 NOT NULL)Únull)r   r   Únew_typer   r   r   Ú_set_field_new_type_null_statusV   s
   ÿz4DatabaseSchemaEditor._set_field_new_type_null_statusc                    ó   |   ||¡}tƒ  ||||¡S r   )r=   r   Ú_alter_column_type_sql)r   r#   Ú	old_fieldÚ	new_fieldr<   r$   r   r   r?   a   ó   z+DatabaseSchemaEditor._alter_column_type_sqlc                    r>   r   )r=   r   Ú_rename_field_sql)r   r   r@   rA   r<   r$   r   r   rC   e   rB   z&DatabaseSchemaEditor._rename_field_sql)Ú__name__Ú
__module__Ú__qualname__Úsql_rename_tableÚsql_alter_column_nullÚsql_alter_column_not_nullÚsql_alter_column_typeÚsql_delete_columnÚsql_rename_columnÚsql_delete_uniqueÚsql_delete_fkÚsql_delete_indexÚsql_create_pkÚsql_delete_pkÚsql_create_indexr   r   r   r   r(   r6   r=   r?   rC   Ú__classcell__r   r   r$   r   r      s,    r   N)Údjango.db.backends.base.schemar   Údjango.db.modelsr   r   r   r   r   r   Ú<module>   s    