
W4                 @   s~   d  d l  Z  d  d l 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
 Gd d   d e  Z d S)    N)Decimal)Apps)BaseDatabaseSchemaEditor)sixc                   s   e  Z d  Z d Z d Z d Z d Z   f d d   Z   f d d   Z d	 d
   Z	 g  g  g  d d d d  Z
 d   f d d  Z d d   Z d d   Z d d d  Z d d   Z d d   Z d d   Z   S)DatabaseSchemaEditorzDROP TABLE %(table)sz'REFERENCES %(to_table)s (%(to_column)s)z7CREATE UNIQUE INDEX %(name)s ON %(table)s (%(columns)s)zDROP INDEX %(name)sc          	      sX   |  j  j   4 } | j d  | j   d |  _ | j d  Wd  QXt t |   j   S)NzPRAGMA foreign_keysr   zPRAGMA foreign_keys = 0)
connectioncursorexecuteZfetchone_initial_pragma_fksuperr   	__enter__)selfc)	__class__ C/tmp/pip-build-5ouued3s/Django/django/db/backends/sqlite3/schema.pyr      s
    zDatabaseSchemaEditor.__enter__c                sR   t  t |   j | | |  |  j j   ! } | j d t |  j   Wd  QXd  S)NzPRAGMA foreign_keys = %s)r   r   __exit__r   r   r	   intr
   )r   exc_type	exc_value	tracebackr   )r   r   r   r      s    zDatabaseSchemaEditor.__exit__c             C   sO  d d  l  } y | j |  } Wn | j k
 r6 Yn Xt | t d   r\ t t |   St | t t f  r{ t |  St | t	 j
  r t |  St | t	 j  r d t	 j |  j d d  S| d  k r d St | t t t	 j f  r/t |  } t j d  } | |  \ } } d | j d	  St d
 | t |  f   d  S)Nr   Tz'%s''z''ZNULL	hex_codeczX'%s'asciiz*Cannot quote parameter value %r of type %s)sqlite3ZadaptZProgrammingError
isinstancetypestrr   r   floatr   integer_typesstring_types	text_typereplacebytes	bytearray
memoryviewcodecs
getencoderdecode
ValueError)r   valuer   Zhex_encoderZ	value_hex_lengthr   r   r   quote_value#   s*    

z DatabaseSchemaEditor.quote_valueNc                s  d d   | j  j D }  f d d   | j  j D } i    d }	 t d d   | D  sv t d d   | D  r x[ t | j    D]D \ }
 } | j r d | _ | }	 | j r | |
 =| | j =q q q Wn  xP | D]H } | | | j <| j	 r | j
 r  j  j |   | | j <q q Wx | D] \ } } | j | j d  | j | j d  | | | j <| j r| j rd	 i  j | j  d
 6 j  j |   d 6} | | | j <n  j | j  | | j <| j   | j <q.WxP | D]H } | | j =| | j =| j	 r| j j j  j r j | j j  SqWt   } t j |  } | d k r  f d d   | j  j D } n  | d k r  f d d   | j  j D } n  i | j  j d 6| j  j d 6| d 6| d 6| d 6} t d t   |  } | | d <| j | d <t | j  j | j |   t j  d d    } | | | j  j d    j! |  j  j | j  j   f d d    j" D  _"  j#   t | j    }  j$ d  j  j  j  d j%  f d d   | D  d j% d d   | D   j | j  j  f   j | d d Wd QXx  j" D] }  j$ |  qRWg   _" |	 rd |	 _ n  d S) a  
        Shortcut to transform a model from old_model into new_model

        The essential steps are:
          1. rename the model's existing table, e.g. "app_model" to "app_model__old"
          2. create a table with the updated definition called "app_model"
          3. copy the data from the old renamed table to the new table
          4. delete the "app_model__old" table
        c             S   s   i  |  ] } | | j   q Sr   )name).0fr   r   r   
<dictcomp>P   s   	 z6DatabaseSchemaEditor._remake_table.<locals>.<dictcomp>c                s(   i  |  ] }   j  | j  | j  q Sr   )
quote_namecolumn)r.   r/   )r   r   r   r0   S   s   	 Nc             s   s   |  ] } | j  Vq d  S)N)primary_key)r.   r/   r   r   r   	<genexpr>Y   s    z5DatabaseSchemaEditor._remake_table.<locals>.<genexpr>c             s   s   |  ] \ } } | j  Vq d  S)N)r3   )r.   onr   r   r   r4   Y   s    Fzcoalesce(%(col)s, %(default)s)coldefaultc                s)   g  |  ] }   f d  d   | D  q S)c                s"   g  |  ] }   j  | |   q Sr   )get)r.   r6   )rename_mappingr   r   
<listcomp>   s   	 zADatabaseSchemaEditor._remake_table.<locals>.<listcomp>.<listcomp>r   )r.   unique)r:   r   r   r;      s   	z6DatabaseSchemaEditor._remake_table.<locals>.<listcomp>c                s)   g  |  ] }   f d  d   | D  q S)c                s"   g  |  ] }   j  | |   q Sr   )r9   )r.   r6   )r:   r   r   r;      s   	 zADatabaseSchemaEditor._remake_table.<locals>.<listcomp>.<listcomp>r   )r.   index)r:   r   r   r;      s   		app_labeldb_tableunique_togetherindex_togetherappsZMeta
__module__c             s   s-   |  j  j } | |  j  _ d  V| |  j  _ d  S)N)_metar?   )modelZtemporary_table_nameZoriginal_table_namer   r   r   altered_table_name   s    z>DatabaseSchemaEditor._remake_table.<locals>.altered_table_nameZ__oldc                s(   g  |  ] }   j  j | k r |  q Sr   )rD   r?   )r.   x)
temp_modelr   r   r;      s   	 z%INSERT INTO %s (%s) SELECT %s FROM %sz, c             3   s$   |  ] \ } }   j  |  Vq d  S)N)r1   )r.   rG   y)r   r   r   r4      s    c             s   s   |  ] \ } } | Vq d  S)Nr   )r.   rG   rI   r   r   r   r4      s    handle_autom2mT)&rD   Zlocal_concrete_fieldsanylistitemsr3   auto_createdr2   r-   many_to_manyZconcreter,   Zeffective_defaultpopnullr1   remote_fieldthroughdelete_modelr   copydeepcopyr@   rA   r>   r?   r   tuplerC   Zobject_name	__bases__
contextlibcontextmanagerZalter_db_tableZdeferred_sqlcreate_modelr	   join)r   rE   create_fieldsdelete_fieldsalter_fieldsoverride_uniquesoverride_indexesbodymappingZrestore_pk_fieldr-   field	old_field	new_fieldZcase_sqlrB   Zmeta_contentsmetarF   Z
field_mapsZsqlr   )r:   r   rH   r   _remake_tableD   s    2			 

	

		z"DatabaseSchemaEditor._remake_tableTc                sM   | r t  t |   j |  n* |  j |  j i |  j | j j  d 6 d  S)Ntable)r   r   rT   r	   sql_delete_tabler1   rD   r?   )r   rE   rJ   )r   r   r   rT      s    z!DatabaseSchemaEditor.delete_modelc             C   sH   | j  r. | j j j j r. |  j | j j  S|  j | d | g d S)z
        Creates a field on a model.
        Usually involves adding a column, but may involve adding a
        table instead (for M2M fields)
        r]   N)rO   rR   rS   rD   rN   r[   rh   )r   rE   rd   r   r   r   	add_field   s    zDatabaseSchemaEditor.add_fieldc             C   sq   | j  r4 | j j j j rm |  j | j j  qm n9 | j d |  j  d d k rW d S|  j | d | g d S)z
        Removes a field from a model. Usually involves deleting a column,
        but for M2Ms may involve deleting a table.
        r   r   Nr^   )	rO   rR   rS   rD   rN   rT   Zdb_parametersr   rh   )r   rE   rd   r   r   r   remove_field   s    	z!DatabaseSchemaEditor.remove_fieldFc	       	      C   s    |  j  | d | | f g d S)z<Actually perform a "physical" (non-ManyToMany) field update.r_   N)rh   )	r   rE   re   rf   Zold_typeZnew_typeZold_db_paramsZnew_db_paramsstrictr   r   r   _alter_field   s    z!DatabaseSchemaEditor._alter_fieldc             C   s   |  j  | d | d S)z
        Deals with a model changing its index_together.
        Note: The input index_togethers must be doubly-nested, not the single-
        nested ["foo", "bar"] format.
        ra   N)rh   )r   rE   Zold_index_togetherZnew_index_togetherr   r   r   alter_index_together   s    z)DatabaseSchemaEditor.alter_index_togetherc             C   s   |  j  | d | d S)z
        Deals with a model changing its unique_together.
        Note: The input unique_togethers must be doubly-nested, not the single-
        nested ["foo", "bar"] format.
        r`   N)rh   )r   rE   Zold_unique_togetherZnew_unique_togetherr   r   r   alter_unique_together   s    z*DatabaseSchemaEditor.alter_unique_togetherc             C   s?  | j  j j j | j  j j j k r |  j | j  j d | j  j j j | j    | j  j j j | j    f g d | j   | j   f d S|  j | j  j  |  j	 d |  j
 | j  j j j  d j d | j   | j   g  d j d | j   | j   g  |  j
 | j  j j j  f  |  j | j  j  d S)z=
        Alters M2Ms to repoint their to= endpoints.
        r_   r`   Nz%INSERT INTO %s (%s) SELECT %s FROM %sz, id)rR   rS   rD   r?   rh   	get_fieldZm2m_reverse_field_nameZm2m_field_namer[   r	   r1   r\   Zm2m_column_nameZm2m_reverse_namerT   )r   rE   re   rf   rm   r   r   r   _alter_many_to_many  s(    $$			 z(DatabaseSchemaEditor._alter_many_to_many)__name__rC   __qualname__rj   Zsql_create_inline_fkZsql_create_uniqueZsql_delete_uniquer   r   r,   rh   rT   rk   rl   rn   ro   rp   rs   r   r   )r   r   r      s    !	r   )r&   rY   rU   decimalr   Zdjango.apps.registryr   Zdjango.db.backends.base.schemar   Zdjango.utilsr   r   r   r   r   r   <module>   s   