3
7^                 @   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 j j}t|tr2|j }|S )N)
connectionZensure_connectionescapeencoders
isinstancestrdecode)selfvaluequoted r   A/tmp/pip-build-8app2_gc/Django/django/db/backends/mysql/schema.pyquote_value   s
    

z DatabaseSchemaEditor.quote_valuec             C   s$   |j | j}|d k	o"|j | 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 )N)r   )r   r   r   r   r   skip_default(   s    z!DatabaseSchemaEditor.skip_defaultc                s^   t  j|| | j|rZ|jd tfkrZ| j|}| jd| j|jj	| j|j
d |g d S )Nz%UPDATE %(table)s SET %(column)s = %%s)tablecolumn)super	add_fieldr   defaultr   effective_defaultexecuteZ
quote_name_metadb_tabler   )r   modelr   r   )	__class__r   r   r   +   s    
zDatabaseSchemaEditor.add_fieldc                sZ   t  j||}| jjj| jj |jj}|dkrJ|rJ|j dkrJ|j	rJdS | j
| oX|S )NZInnoDB
ForeignKeyF)r   _field_should_be_indexedr   ZintrospectionZget_storage_enginecursorr   r   get_internal_typeZdb_constraintr   )r   r   r   Zcreate_indexZstorage)r    r   r   r"   7   s    z-DatabaseSchemaEditor._field_should_be_indexedc                s`   |j j|d }|j dkrL| j||jgdd}|sL| j| 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   r!   T)index )suffix)	r   	get_fieldr$   Z_constraint_namesr   r   Z_create_index_sqlr   _delete_composed_index)r   r   fieldsargsZfirst_fieldZconstraint_names)r    r   r   r)   F   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_statusV   s    
z4DatabaseSchemaEditor._set_field_new_type_null_statusc                s   | j ||}t j||||S )N)r.   r   _alter_column_type_sql)r   r   	old_field	new_fieldr-   )r    r   r   r/   a   s    z+DatabaseSchemaEditor._alter_column_type_sqlc                s   | j ||}t j||||S )N)r.   r   _rename_field_sql)r   r   r0   r1   r-   )r    r   r   r2   e   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_delete_columnZsql_rename_columnZsql_delete_uniqueZsql_delete_fkZsql_delete_indexZsql_create_pkZsql_delete_pkZsql_create_indexr   r   r   r   r"   r)   r.   r/   r2   __classcell__r   r   )r    r   r      s*   r   N)Zdjango.db.backends.base.schemar   Zdjango.db.modelsr   r   r   r   r   r   <module>   s   