B
    CVvgÜ  ã               @   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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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'MODIFY %(column)s %(type)s%(collation)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)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   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   úC/tmp/pip-install-o3oxmrkh/Django/django/db/backends/mysql/schema.pyÚsql_delete_check    s    z%DatabaseSchemaEditor.sql_delete_checkc                s6   | j jr| j jdkr2tƒ jS n| j jdkr2tƒ jS dS )N)é
   é   é   )é   r   é   zCALTER TABLE %(table)s CHANGE %(old_column)s %(new_column)s %(type)s)r   r   Úmysql_versionÚsuperÚsql_rename_column)r   )Ú	__class__r   r	   r   )   s    
z&DatabaseSchemaEditor.sql_rename_columnc             C   sV   | j  ¡  t|tƒr | dd¡}| j j  || j j j¡}t|tƒrRt|tƒrR| ¡ }|S )Nú%z%%)	r   Zensure_connectionÚ
isinstanceÚstrÚreplaceÚescapeÚencodersÚbytesÚdecode)r   ÚvalueÚquotedr   r   r	   Úquote_value4   s    

z DatabaseSchemaEditor.quote_valuec             C   s$   |  | j¡}|d k	o"| ¡ | jjkS )N)Údb_typer   ÚlowerZ_limited_data_types)r   Úfieldr   r   r   r	   Ú_is_limited_data_type>   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_defaultB   s    
z!DatabaseSchemaEditor.skip_defaultc             C   s   |   |¡r| jjsdS dS )NTF)r"   r   r   )r   r!   r   r   r	   Úskip_default_on_alterG   s    z*DatabaseSchemaEditor.skip_default_on_alterc             C   s    | j jr| j jdkS | j jdkS )N)r   r   é   )r   r   é   )r   r   r   )r   r   r   r	   r#   N   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_sql)r   r!   )r   r   r	   r(   V   s
    
z(DatabaseSchemaEditor._column_default_sqlc                s^   t ƒ  ||¡ |  |¡rZ|jd tfkrZ|  |¡}|  d|  |jj	¡|  |j
¡dœ |g¡ d S )Nz%UPDATE %(table)s SET %(column)s = %%s)ÚtableÚcolumn)r   Ú	add_fieldr$   Údefaultr   Úeffective_defaultÚexecuteZ
quote_nameÚ_metaÚdb_tabler*   )r   Úmodelr!   r-   )r   r   r	   r+   a   s    
zDatabaseSchemaEditor.add_fieldc                sZ   t ƒ  ||¡}| jj | j ¡ |jj¡}|dkrJ|rJ| ¡ dkrJ|j	rJdS |  
|¡ oX|S )NZInnoDBÚ
ForeignKeyF)r   Ú_field_should_be_indexedr   ZintrospectionZget_storage_engineÚcursorr/   r0   Úget_internal_typeZdb_constraintr"   )r   r1   r!   Zcreate_indexZstorage)r   r   r	   r3   m   s    z-DatabaseSchemaEditor._field_should_be_indexedc                s`   |j  |d ¡}| ¡ dkrL| j||jgdd�}|sL|  | j||gdd�¡ tƒ j||f|žŽ 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   r2   T)ÚindexÚ )ÚfieldsÚsuffix)	r/   Ú	get_fieldr5   Z_constraint_namesr*   r.   Z_create_index_sqlr   Ú_delete_composed_index)r   r1   r8   ÚargsZfirst_fieldZconstraint_names)r   r   r	   r;   |   s    	z+DatabaseSchemaEditor._delete_composed_indexc             C   s   |j r|d7 }n|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_statusŽ   s    
z4DatabaseSchemaEditor._set_field_new_type_null_statusc                s   |   ||¡}tƒ  ||||¡S )N)r?   r   Ú_alter_column_type_sql)r   r1   Ú	old_fieldÚ	new_fieldr>   )r   r   r	   r@   ™   s    z+DatabaseSchemaEditor._alter_column_type_sqlc                s   |   ||¡}tƒ  ||||¡S )N)r?   r   Ú_rename_field_sql)r   r)   rA   rB   r>   )r   r   r	   rC   �   s    z&DatabaseSchemaEditor._rename_field_sql)!Ú__name__Ú
__module__Ú__qualname__Zsql_rename_tableZsql_alter_column_nullZsql_alter_column_not_nullZsql_alter_column_typeZsql_alter_column_collateZ sql_alter_column_no_default_nullZsql_delete_columnZsql_delete_uniqueZsql_create_column_inline_fkZsql_delete_fkZsql_delete_indexZsql_create_pkZsql_delete_pkZsql_create_indexÚpropertyr
   r   r   r"   r$   r%   r#   r(   r+   r3   r;   r?   r@   rC   Ú__classcell__r   r   )r   r	   r      s8   	
r   N)Zdjango.db.backends.base.schemar   Zdjango.db.modelsr   r   r   r   r   r	   Ú<module>   s   