
v^                 @   s   d  d l  Z  d  d l m Z d  d l m Z m Z m Z m Z m Z d  d l m	 Z	 m
 Z
 d  d l m Z d  d l m Z m Z d  d l m Z e  j d  Z d	 d
   Z d d   Z d d   Z Gd d   d  Z d S)    N)datetime)ColumnsForeignKeyName	IndexName	StatementTable)names_digestsplit_identifier)Index)TransactionManagementErroratomic)timezonezdjango.db.backends.schemac             C   sE   |  j  } | j r d S| j r5 | j d g k r5 d S| j | j k S)zz
    When altering the given field, must constraints on its model from the given
    relation be temporarily dropped?
    FNT)fieldmany_to_manyprimary_keyZ	to_fieldsname)ZrelationZaltered_fieldr    r   @/tmp/pip-build-8lau8j11/django/django/db/backends/base/schema.py_is_relevant_relation   s    		r   c             C   s   |  j  j d d d d d d  S)NZforwardFreverseTZinclude_hidden)_metaZ_get_fields)modelr   r   r   _all_related_fields   s    r   c                sE   t   f d d   t  j  D   f d d   t   j  D  S)Nc             3   s$   |  ] } t  |    r | Vq d  S)N)r   ).0obj)	old_fieldr   r   	<genexpr>'   s    z+_related_non_m2m_objects.<locals>.<genexpr>c             3   s$   |  ] } t  |    r | Vq d  S)N)r   )r   r   )	new_fieldr   r   r   (   s    )zipr   r   )r   r   r   )r   r   r   _related_non_m2m_objects#   s    r   c               @   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 d Z d Z d Z d Z e Z d Z e Z d Z d Z d Z e Z d Z d Z d Z  d Z! e Z" d Z# d d d d   Z$ d! d"   Z% d# d$   Z& f  d% d&  Z' d' d(   Z( d) d*   Z) d d+ d,  Z* d- d.   Z+ d/ d0   Z, d1 d2   Z- e. d3 d4    Z/ d5 d6   Z0 d7 d8   Z1 d9 d:   Z2 d; d<   Z3 d= d>   Z4 d? d@   Z5 dA dB   Z6 dC dD   Z7 dE dF   Z8 dG dH   Z9 dI dJ   Z: dK dL   Z; dM dN   Z< dO dP   Z= dQ dR   Z> d dS dT  Z? d dU dV  Z@ dW dX   ZA d dY dZ  ZB d[ d\   ZC d] d^   ZD d_ d` da  ZE d db dc  ZF dd d de d_ df d_ dg d dh f  di d dj f  dk d dl dm  ZG d dn do  ZH dp dq   ZI dr ds   ZJ dt du   ZK dv dw   ZL dx dy   ZM dz d{   ZN d| d}   ZO d~ d   ZP d d   ZQ d d   ZR d d d  ZS d d d d  ZT d d d  ZU d d   ZV d d   ZW d d   ZX d d   ZY d d d d d d d d d d  ZZ d d d  Z[ d d   Z\ d d   Z] f  d d  Z^ d S)BaseDatabaseSchemaEditorz
    This class and its subclasses are responsible for emitting schema-changing
    statements to the databases - model creation/removal/alteration, field
    renaming, index fiddling, and so on.
    z'CREATE TABLE %(table)s (%(definition)s)z1ALTER TABLE %(old_table)s RENAME TO %(new_table)sz7ALTER TABLE %(table)s SET TABLESPACE %(new_tablespace)szDROP TABLE %(table)s CASCADEz:ALTER TABLE %(table)s ADD COLUMN %(column)s %(definition)sz!ALTER TABLE %(table)s %(changes)sz%ALTER COLUMN %(column)s TYPE %(type)sz%ALTER COLUMN %(column)s DROP NOT NULLz$ALTER COLUMN %(column)s SET NOT NULLz/ALTER COLUMN %(column)s SET DEFAULT %(default)sz$ALTER COLUMN %(column)s DROP DEFAULTz4ALTER TABLE %(table)s DROP COLUMN %(column)s CASCADEzDALTER TABLE %(table)s RENAME COLUMN %(old_column)s TO %(new_column)szFUPDATE %(table)s SET %(column)s = %(default)s WHERE %(column)s IS NULLzUNIQUE (%(columns)s)zCHECK (%(check)s)z.ALTER TABLE %(table)s DROP CONSTRAINT %(name)sz"CONSTRAINT %(name)s %(constraint)sz?ALTER TABLE %(table)s ADD CONSTRAINT %(name)s CHECK (%(check)s)zBALTER TABLE %(table)s ADD CONSTRAINT %(name)s UNIQUE (%(columns)s)z|ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) REFERENCES %(to_table)s (%(to_column)s)%(deferrable)sNzFCREATE INDEX %(name)s ON %(table)s (%(columns)s)%(extra)s%(condition)szDCREATE UNIQUE INDEX %(name)s ON %(table)s (%(columns)s)%(condition)szDROP INDEX %(name)szGALTER TABLE %(table)s ADD CONSTRAINT %(name)s PRIMARY KEY (%(columns)s)zDROP PROCEDURE %(procedure)sFTc             C   s@   | |  _  | |  _ |  j r$ g  |  _ |  j  j j o6 | |  _ d  S)N)
connectioncollect_sqlcollected_sqlfeaturescan_rollback_ddlatomic_migration)selfr!   r"   r   r   r   r   __init__`   s
    				z!BaseDatabaseSchemaEditor.__init__c             C   s8   g  |  _  |  j r4 t |  j j  |  _ |  j j   |  S)N)deferred_sqlr&   r   r!   alias	__enter__)r'   r   r   r   r+   i   s
    		z"BaseDatabaseSchemaEditor.__enter__c             C   sP   | d  k r- x |  j  D] } |  j |  q W|  j rL |  j j | | |  d  S)N)r)   executer&   r   __exit__)r'   exc_type	exc_value	tracebacksqlr   r   r   r-   p   s
    	z!BaseDatabaseSchemaEditor.__exit__c          
   C   s  |  j  r2 |  j j r2 |  j j j r2 t d   t |  } t j d | | d d | d | i |  j  r | j	 d  r d n d } | d k	 r |  j
 j | t t |  j |   |  q |  j
 j | |  n) |  j j    } | j | |  Wd QRXd S)	z:Execute the given SQL statement, with optional parameters.ziExecuting DDL statements while in a transaction on databases that can't perform a rollback is prohibited.z%s; (params %r)extraparamsr1   ; N)r"   r!   Zin_atomic_blockr$   r%   r   strloggerdebugendswithr#   appendtuplemapquote_valuecursorr,   )r'   r1   r3   Zendingr>   r   r   r   r,   y   s    &	%	-z BaseDatabaseSchemaEditor.executec             C   s   |  j  j j |  S)N)r!   ops
quote_name)r'   r   r   r   r   r@      s    z#BaseDatabaseSchemaEditor.quote_namec          	      s  xI   j  j D]; }   f d d   | D }  j j  j   |   q Wg  } g  } x  j  j D]}  j   |  \ } } | d k r qe | j d  j  }	 |	 d r | d  j	 |	 7} | j
 d  j  }
 |
 r | d |
 7} | j |  | j r| j r| j j j  j } | j j j  j | j j  j }  j r| d  j d  j |  d	  j |  i 7} n.  j j j r j j  j   | d
   | j d  j | j  | f  | j   d k re  j j j   j  j | j  } | re  j j |  qe W   f d d     j  j D }  j d  j   j  j  d d j d d   | |  D  i }   j  j r j j j   j  j  } | r| d | 7} | | f S)z-Take a model and return its table definition.c                s%   g  |  ] }   j  j |  j  q Sr   )r   	get_fieldcolumn)r   r   )r   r   r   
<listcomp>   s   	 z6BaseDatabaseSchemaEditor.table_sql.<locals>.<listcomp>Nr!   check z %sto_table	to_columnz_fk_%(to_table)s_%(to_column)sz%s %s	AutoFieldBigAutoFieldSmallAutoFieldc                s"   g  |  ] } | j       q Sr   )Zconstraint_sql)r   
constraint)r   r'   r   r   rC      s   	 table
definitionz, c             s   s   |  ] } | r | Vq d  S)Nr   )r   rK   r   r   r   r      s    z5BaseDatabaseSchemaEditor.table_sql.<locals>.<genexpr>)rH   rI   rJ   ) r   Zunique_togetherr)   r:   _create_unique_sqllocal_fields
column_sqldb_parametersr!   sql_check_constraintZdb_type_suffixextendremote_fielddb_constraintr   db_tablerA   
field_namerB   sql_create_inline_fkr@   r$   supports_foreign_keys_create_fk_sqlget_internal_typer?   autoinc_sqlconstraintssql_create_tablejoindb_tablespacetablespace_sql)r'   r   fieldscolumnsZcolumn_sqlsr3   r   rM   Zextra_params	db_paramsZcol_type_suffixrF   rG   r\   r]   r1   ra   r   )r   r'   r   	table_sql   sR     
!		!")z"BaseDatabaseSchemaEditor.table_sqlc             C   s  | j  d |  j  } | d } g  } | d k r5 d S| j } | oQ |  j |  } | r |  j |  } d |  j |  }	 | d k	 r |  j j j r | |	 |  j |  7} n | |	 7} | | g 7} | j	 r | j
 r |  j j j r d } | r|  j j j r| d 7} n | s#| d 7} | j
 r9| d 7} n | j rL| d	 7} | j p^| j j }
 |
 r|  j j j r| j r| d
 |  j j j |
 d d 7} | | f S)z
        Take a field and return its column definition.
        The field must already have had set_attributes_from_name() called.
        r!   typeNz	 DEFAULT Tz NULLz	 NOT NULLz PRIMARY KEYz UNIQUEz %sinline)NN)rQ   r!   nullskip_defaulteffective_default_column_default_sqlr$   requires_literal_defaultsprepare_defaultempty_strings_allowedr   !interprets_empty_strings_as_nullsZimplied_column_nulluniquer`   r   Zsupports_tablespacesr?   ra   )r'   r   r   include_defaultrd   r1   r3   rh   default_valueZcolumn_defaultZ
tablespacer   r   r   rP      s<    
	

		
#z#BaseDatabaseSchemaEditor.column_sqlc             C   s   d S)z
        Some backends don't accept default values for certain columns types
        (i.e. MySQL longtext and longblob).
        Fr   )r'   r   r   r   r   ri      s    z%BaseDatabaseSchemaEditor.skip_defaultc             C   s   t  d   d S)zU
        Only used for backends which have requires_literal_defaults feature
        zsubclasses of BaseDatabaseSchemaEditor for backends which have requires_literal_defaults must provide a prepare_default() methodN)NotImplementedError)r'   valuer   r   r   rm     s    z(BaseDatabaseSchemaEditor.prepare_defaultc             C   s   d S)z
        Return the SQL to use in a DEFAULT clause. The resulting string should
        contain a '%s' placeholder for a default value.
        z%sr   )r'   r   r   r   r   rk     s    z,BaseDatabaseSchemaEditor._column_default_sqlc             C   s   |  j    r |  j   } n |  j ra |  j ra |  j ra |  j   d k rU t   } q t   } n t |  d d  s t |  d d  r t	 j
   } |  j   } | d k r | j   } q | d k r | j   } q | d k r t j
   } n d  } | S)NZBinaryFieldZauto_nowFZauto_now_addZ	DateFieldZ	TimeFieldZDateTimeField)has_defaultZget_defaultrh   Zblankrn   r[   bytesr6   getattrr   nowdatetimer   )r   defaultZinternal_typer   r   r   _effective_default  s"    $z+BaseDatabaseSchemaEditor._effective_defaultc             C   s   | j  |  j |  |  j  S)z2Return a field's effective database default value.)Zget_db_prep_saver|   r!   )r'   r   r   r   r   rj   -  s    z*BaseDatabaseSchemaEditor.effective_defaultc             C   s   t     d S)aX  
        Return a quoted version of the value so it's safe to use in an SQL
        string. This is not safe against injection from user code; it is
        intended only for use in making SQL scripts or preparing default values
        for particularly tricky backends (defaults are not user-defined, though,
        so this is safe).
        N)rs   )r'   rt   r   r   r   r=   1  s    z$BaseDatabaseSchemaEditor.quote_valuec             C   s   |  j  |  \ } } |  j | | p' d  |  j j |  j |   x9 | j j D]+ } | j j j j	 rQ |  j
 | j j  qQ Wd S)zr
        Create a table and any accompanying indexes or unique constraints for
        the given `model`.
        N)re   r,   r)   rS   _model_indexes_sqlr   local_many_to_manyrT   throughauto_createdcreate_model)r'   r   r1   r3   r   r   r   r   r   =  s    z%BaseDatabaseSchemaEditor.create_modelc             C   s   x9 | j  j D]+ } | j j j  j r |  j | j j  q W|  j |  j d |  j | j  j	  i  xK t
 |  j  D]: } t | t  ru | j | j  j	  ru |  j j |  qu Wd S)z!Delete a model from the database.rL   N)r   r~   rT   r   r   delete_modelr,   sql_delete_tabler@   rV   listr)   
isinstancer   Zreferences_tableremove)r'   r   r   r1   r   r   r   r   N  s    $z%BaseDatabaseSchemaEditor.delete_modelc             C   s#   |  j  | j | |   d d d S)zAdd an index on a model.r3   N)r,   
create_sql)r'   r   indexr   r   r   	add_index^  s    z"BaseDatabaseSchemaEditor.add_indexc             C   s   |  j  | j | |    d S)zRemove an index from a model.N)r,   
remove_sql)r'   r   r   r   r   r   remove_indexb  s    z%BaseDatabaseSchemaEditor.remove_indexc             C   s)   | j  | |   } | r% |  j |  d S)zAdd a constraint to a model.N)r   r,   )r'   r   rK   r1   r   r   r   add_constraintf  s    z'BaseDatabaseSchemaEditor.add_constraintc             C   s)   | j  | |   } | r% |  j |  d S)z!Remove a constraint from a model.N)r   r,   )r'   r   rK   r1   r   r   r   remove_constraintl  s    z*BaseDatabaseSchemaEditor.remove_constraintc                s   d d   | D } d d   | D } x6 | j  |  D]% } |  j   | d d i |  j  q6 WxI | j  |  D]8 }   f d d   | D } |  j |  j   |   qo Wd S)	z
        Deal with a model changing its unique_together. The input
        unique_togethers must be doubly-nested, not the single-nested
        ["foo", "bar"] format.
        c             S   s   h  |  ] } t  |   q Sr   )r;   )r   rb   r   r   r   	<setcomp>x  s   	 zABaseDatabaseSchemaEditor.alter_unique_together.<locals>.<setcomp>c             S   s   h  |  ] } t  |   q Sr   )r;   )r   rb   r   r   r   r   y  s   	 rp   Tc                s%   g  |  ] }   j  j |  j  q Sr   )r   rA   rB   )r   r   )r   r   r   rC     s   	 zBBaseDatabaseSchemaEditor.alter_unique_together.<locals>.<listcomp>N)
difference_delete_composed_indexsql_delete_uniquer,   rN   )r'   r   Zold_unique_togetherZnew_unique_togetheroldsnewsrb   rc   r   )r   r   alter_unique_togetherr  s    #z.BaseDatabaseSchemaEditor.alter_unique_togetherc                s   d d   | D } d d   | D } x6 | j  |  D]% } |  j   | d d i |  j  q6 WxO | j  |  D]> }   f d d   | D } |  j |  j   | d d	  qo Wd
 S)z
        Deal with a model changing its index_together. The input
        index_togethers must be doubly-nested, not the single-nested
        ["foo", "bar"] format.
        c             S   s   h  |  ] } t  |   q Sr   )r;   )r   rb   r   r   r   r     s   	 z@BaseDatabaseSchemaEditor.alter_index_together.<locals>.<setcomp>c             S   s   h  |  ] } t  |   q Sr   )r;   )r   rb   r   r   r   r     s   	 r   Tc                s"   g  |  ] }   j  j |   q Sr   )r   rA   )r   r   )r   r   r   rC     s   	 zABaseDatabaseSchemaEditor.alter_index_together.<locals>.<listcomp>suffix_idxN)r   r   sql_delete_indexr,   _create_index_sql)r'   r   Zold_index_togetherZnew_index_togetherr   r   rb   field_namesr   )r   r   alter_index_together  s    #z-BaseDatabaseSchemaEditor.alter_index_togetherc       	         s   d d     j  j D } d d     j  j D }   f d d   | D } |  j   | d | | B| } t |  d k r t d t |    j  j d	 j |  f   |  j |  j	 |   | d
   d  S)Nc             S   s   h  |  ] } | j   q Sr   )r   )r   rK   r   r   r   r     s   	 zBBaseDatabaseSchemaEditor._delete_composed_index.<locals>.<setcomp>c             S   s   h  |  ] } | j   q Sr   )r   )r   rK   r   r   r   r     s   	 c                s%   g  |  ] }   j  j |  j  q Sr   )r   rA   rB   )r   r   )r   r   r   rC     s   	 zCBaseDatabaseSchemaEditor._delete_composed_index.<locals>.<listcomp>exclude   z1Found wrong number (%s) of constraints for %s(%s)z, r   )
r   r]   indexes_constraint_nameslen
ValueErrorrV   r_   r,   _delete_constraint_sql)	r'   r   rb   Zconstraint_kwargsr1   meta_constraint_namesmeta_index_namesrc   constraint_namesr   )r   r   r     s    			z/BaseDatabaseSchemaEditor._delete_composed_indexc             C   s   | | k s3 |  j  j j r7 | j   | j   k r7 d S|  j |  j d |  j |  d |  j |  i  x0 |  j D]% } t | t	  rs | j
 | |  qs Wd S)z#Rename the table a model points to.NZ	old_tableZ	new_table)r!   r$   Zignores_table_name_caselowerr,   sql_rename_tabler@   r)   r   r   Zrename_table_references)r'   r   Zold_db_tableZnew_db_tabler1   r   r   r   alter_db_table  s    z'BaseDatabaseSchemaEditor.alter_db_tablec          	   C   sK   |  j  |  j d |  j | j j  d |  j |  d |  j |  i  d S)z)Move a model's table between tablespaces.rL   Zold_tablespaceZnew_tablespaceN)r,   sql_retablespace_tabler@   r   rV   )r'   r   Zold_db_tablespaceZnew_db_tablespacer   r   r   alter_db_tablespace  s    z,BaseDatabaseSchemaEditor.alter_db_tablespacec             C   s  | j  r. | j j j j r. |  j | j j  S|  j | | d d \ } } | d k r\ d S| j d |  j  } | d r | d |  j	 | 7} | j r|  j j
 j r| j rd } |  j rb| j j j j } | j j j j | j j  j } | d |  j d |  j | | |  d	 |  j | j  d
 |  j |  d |  j |  d |  j j j   i 7} n |  j j |  j | | |   |  j d |  j | j j  d	 |  j | j  d | i }	 |  j |	 |  |  j |  rI|  j |  d k	 rI|  j | d | d d \ }
 } |  j d |  j | j j  d |
 i }	 |  j |	 |  |  j j  |  j! | |   |  j j
 j" r|  j j#   d S)z
        Create a field on a model. Usually involves adding a column, but may
        involve adding a table instead (for M2M fields).
        rq   TNr!   rD   rE   z_fk_%(to_table)s_%(to_column)sr   rB   rF   rG   
deferrablerL   rM   dropchanges)$r   rT   r   r   r   r   rP   rQ   r!   rR   r$   rY   rU   sql_create_column_inline_fkr   rV   rA   rW   rB   _fk_constraint_namer@   r?   deferrable_sqlr)   r:   rZ   sql_create_columnr,   ri   rj   _alter_column_default_sqlsql_alter_columnrS   _field_indexes_sqlconnection_persists_old_columnsclose)r'   r   r   rM   r3   rd   Zconstraint_suffixrF   rG   r1   changes_sqlr   r   r   	add_field  sD    
!	!%!z"BaseDatabaseSchemaEditor.add_fieldc             C   sW  | j  r. | j j j j r. |  j | j j  S| j d |  j  d d k rQ d S| j r |  j | | j	 g d d } x' | D] } |  j
 |  j | |   q W|  j d |  j | j j  d |  j | j	  i } |  j
 |  |  j j j r |  j j   xQ t |  j  D]@ } t | t  r| j | j j | j	  r|  j j |  qWd S)z
        Remove a field from a model. Usually involves deleting a column,
        but for M2Ms may involve deleting a table.
        r!   rf   Nforeign_keyTrL   rB   )r   rT   r   r   r   r   rQ   r!   r   rB   r,   _delete_fk_sqlsql_delete_columnr@   rV   r$   r   r   r   r)   r   r   Zreferences_columnr   )r'   r   r   fk_namesfk_namer1   r   r   r   remove_field  s"    	*z%BaseDatabaseSchemaEditor.remove_fieldc       	   	   C   s  | j  d |  j  } | d } | j  d |  j  } | d } | d k rY | j d k st | d k r | j d k r t d | | f   n | d k r | d k r | j j r | j j r | j j j j r | j j j j r |  j | | | |  S| d k rQ| d k rQ| j j rQ| j j rQ| j j j j rQ| j j j j rQd S| d k si| d k rt d | | f   |  j | | | | | | | |  d S)a'  
        Allow a field's type, uniqueness, nullability, default, column,
        constraints, etc. to be modified.
        `old_field` is required to compute the necessary changes.
        If `strict` is True, raise errors if the old column does not match
        `old_field` precisely.
        r!   rf   NzqCannot alter field %s into %s - they do not properly define db_type (are you using a badly-written custom field?)zCannot alter field %s into %s - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields))	rQ   r!   rT   r   r   r   r   _alter_many_to_many_alter_field)	r'   r   r   r   strictold_db_paramsold_typenew_db_paramsnew_typer   r   r   alter_field  s2    	

z$BaseDatabaseSchemaEditor.alter_fieldc	       '   	   C   s	  t    }	 | j r | j r |  j | | j g d d }
 | ry t |
  d k ry t d t |
  | j j | j f   x: |
 D]2 } |	 j	 | j f  |  j
 |  j | |   q W| j r| j s |  j | |  rd d   | j j D } |  j | | j g d d d d	 d
 | } | r^t |  d k r^t d t |  | j j | j f   x' | D] } |  j
 |  j | |   qeW| j r| j s| j o| j o| | k } | r/xn t | |  D]] \ } } |  j | j | j j g d d } x* | D]" } |  j
 |  j | j |   qWqW| j r| j r| j sU| j rd d   | j j D } |  j | | j g d d d t j d
 | } x' | D] } |  j
 |  j | |   qW| d | d k r| d rd d   | j j D } |  j | | j g d d d
 | } | r`t |  d k r`t d t |  | j j | j f   x' | D] } |  j
 |  j | |   qgW| j | j k r|  j
 |  j | j j | | |   x? |  j D]4 } t | t  r| j | j j | j | j  qWg  } g  } g  } | | k rY|  j | | | |  \ } } | j  |  | j! |  |  j" |  } |  j" |  } | j# o| j# o| | k o| d k	 o|  j$ |  } | r| j  |  j% | | |   | j# | j# k r|  j& | | |  } | r| j  |  | j'   o*| j# o*| j# } | s9| rv| sI| | } |  j( j) j* r| rt+ t, |    \ } }  d j- |  t. |  g   f g } xF | D]> \ } }  |  j
 |  j/ d |  j0 | j j  d | i |   qW| rv|  j
 |  j1 d |  j0 | j j  d |  j0 | j  d d i | g  xF | D]> \ } }  |  j
 |  j/ d |  j0 | j j  d | i |   q4W| rx$ | D] \ } }  |  j
 | |   qW| j r| j r|  j2 | |  |  j3 | |  r|  j
 |  j4 | | j g   | j s
| j r9| j r9| j r9|  j
 |  j5 | | g   g  }! | r[|! j! t | |   |  j | |  r|  j
 |  j6 | |   |! j! t | |   x |! D] \ }" } | j j7 d |  j(  }# |# d }$ |  j | j |" j | j |$  \ } } |  j
 |  j/ d |  j0 | j j j  d | d i | d  x$ | D] \ } }  |  j
 | |   q<WqW| j r|	 s| j s| j r| j r|  j
 |  j8 | | d   | r	xT | j9 j j: D]C }% t; |% |  r|% j j r|  j
 |  j8 |% j |% j d   qW| d | d k rg	| d rg	|  j< | j j | j g d d } |  j
 |  j= | | | d   | r	|  j% | | | d  d \ }& }  |  j/ d |  j0 | j j  d |& i } |  j
 | |   |  j( j) j> r	|  j( j?   d S)!z3Perform a "physical" (non-ManyToMany) field update.r   Tr   z<Found wrong number (%s) of foreign key constraints for %s.%sc             S   s   h  |  ] } | j   q Sr   )r   )r   rK   r   r   r   r   J  s   	 z8BaseDatabaseSchemaEditor._alter_field.<locals>.<setcomp>rp   r   Fr   z7Found wrong number (%s) of unique constraints for %s.%sc             S   s   h  |  ] } | j   q Sr   )r   )r   r   r   r   r   r   t  s   	 r   type_rD   c             S   s   h  |  ] } | j   q Sr   )r   )r   rK   r   r   r   r     s   	 z6Found wrong number (%s) of check constraints for %s.%sNz, rL   r   rB   r{   z%sr!   rf   r   z_fk_%(to_table)s_%(to_column)sZ_fkr   _checkr   )@setrT   rU   r   rB   r   r   r   rV   addr,   r   rp   _field_became_primary_keyr]   _delete_unique_sqlr   r   Zrelated_modelr   db_indexr   r
   r   _delete_index_sql_delete_check_sql_rename_field_sqlr)   r   r   Zrename_column_references_alter_column_type_sqlr:   rS   rj   rh   ri   r   _alter_column_null_sqlru   r!   r$   Zsupports_combined_altersr;   r   r_   sumr   r@   sql_update_with_default_delete_primary_key_unique_should_be_addedrN   r   _create_primary_key_sqlrQ   rZ   r   Zrelated_objectsr   _create_index_name_create_check_sqlr   r   )'r'   r   r   r   r   r   r   r   r   Zfks_droppedr   r   r   r   constraint_nameZdrop_foreign_keysZ_old_relZnew_relZrel_fk_namesr   Zindex_names
index_namer1   ZactionsZnull_actionsZpost_actionsfragmentZother_actionsZold_defaultnew_defaultZneeds_database_defaultZfour_way_default_alterationr3   Zrels_to_updateZold_relZrel_db_paramsZrel_typerelr   r   r   r   r   7  s*   			%			$&				%#	

!




&
!		&$ !z%BaseDatabaseSchemaEditor._alter_fieldc             C   s   |  j  j j r% | j   d k r% d S| j d |  j   } | j rL |  j n |  j } | d |  j | j	  d | d i g  f Sd S)z
        Hook to specialize column null alteration.

        Return a (sql, params) fragment to set a column to null or non-null
        as required by new_field, or None if no changes are required.
        	CharField	TextFieldNr!   rB   rf   )r   r   )
r!   r$   ro   r[   rQ   rh   sql_alter_column_nullsql_alter_column_not_nullr@   rB   )r'   r   r   r   r   r1   r   r   r   r   +  s    z/BaseDatabaseSchemaEditor._alter_column_null_sqlc       
      C   s   |  j  |  } |  j |  } | g } | r6 g  } n$ |  j j j rZ |  j |  } g  } | j d |  j  } | r~ |  j n |  j }	 |	 d |  j	 | j
  d | d d | i | f S)z
        Hook to specialize column default alteration.

        Return a (sql, params) fragment to add or drop (depending on the drop
        argument) a default to new_field's column.
        r!   rB   rf   r{   )rj   rk   r!   r$   rl   rm   rQ   sql_alter_column_no_defaultsql_alter_column_defaultr@   rB   )
r'   r   r   r   r   r   r{   r3   r   r1   r   r   r   r   A  s    		

z2BaseDatabaseSchemaEditor._alter_column_default_sqlc             C   s/   |  j  d |  j | j  d | i g  f g  f S)a  
        Hook to specialize column type alteration for different backends,
        for cases when a creation type is different to an alteration type
        (e.g. SERIAL in PostgreSQL, PostGIS fields).

        Return a two-tuple of: an SQL fragment of (sql, params) to insert into
        an ALTER TABLE statement and a list of extra (sql, params) tuples to
        run once the field is altered.
        rB   rf   )sql_alter_column_typer@   rB   )r'   r   r   r   r   r   r   r   r   `  s
    
z/BaseDatabaseSchemaEditor._alter_column_type_sqlc             C   s   | j  j j j | j  j j j k rU |  j | j  j | j  j j j | j  j j j  |  j | j  j | j  j j j | j    | j  j j j | j     |  j | j  j | j  j j j | j    | j  j j j | j     d S)z*Alter M2Ms to repoint their to= endpoints.N)	rT   r   r   rV   r   r   rA   Zm2m_reverse_field_nameZm2m_field_name)r'   r   r   r   r   r   r   r   r   u  s    $		z,BaseDatabaseSchemaEditor._alter_many_to_manyr5   c       	      C   s%  t  |  \ } } d t | | d d | f } |  j j j   pF d } d | d j |  | f } t |  | k r{ | St |  | d k r | d | d  } | t |  d	 d
 } d | d |  d j |  d |  | f } | d d k s| d j   r!d | d d  } | S)z
        Generate a unique name for an index/unique constraint.

        The name is divided into 3 parts: the table name, the column names,
        and a unique digest and suffix.
        z%s%slength      z%s_%s_%s_   N   r   r   zD%s)r	   r   r!   r?   Zmax_name_lengthr_   r   isdigit)	r'   Z
table_namecolumn_namesr   r   Zhash_suffix_part
max_lengthr   Zother_lengthr   r   r   r     s     
 z+BaseDatabaseSchemaEditor._create_index_namec             C   sz   | d  k rS t  |  d k r; | d j r; | d j } n | j j rS | j j } | d  k	 rv d |  j j j |  Sd S)Nr   r   rE   r5   )r   r`   r   r!   r?   ra   )r'   r   rb   r`   r   r   r   _get_index_tablespace_sql  s    z2BaseDatabaseSchemaEditor._get_index_tablespace_sqlr   r   usingr`   col_suffixesr1   	opclasses	conditionc               s    j  | | d | } d d   | D } | p7  j } | j j }    f d d   } t | d t |  j  d t | | | |  d | d	  j | | | |	  d
 | d |
 r d |
 n d S)z
        Return the SQL statement to create the index for one or several fields.
        `sql` can be specified if the syntax differs from the standard (GIS
        indexes, ...).
        r`   c             S   s   g  |  ] } | j   q Sr   )rB   )r   r   r   r   r   rC     s   	 z>BaseDatabaseSchemaEditor._create_index_sql.<locals>.<listcomp>c                 s+     d  k r  j  |  |      j    S)N)r   r@   )argskwargs)r   r'   r   r   create_index_name  s    zEBaseDatabaseSchemaEditor._create_index_sql.<locals>.create_index_namerL   r   r   rc   r2   r   z WHERE r5   )	r   sql_create_indexr   rV   r   r   r@   r   _index_columns)r'   r   rb   r   r   r   r`   r   r1   r   r   ra   rc   r   rL   r   r   )r   r'   r   r     s    z*BaseDatabaseSchemaEditor._create_index_sqlc             C   s:   t  | p |  j d t | j j |  j  d |  j |  S)NrL   r   )r   r   r   r   rV   r@   )r'   r   r   r1   r   r   r   r     s    z*BaseDatabaseSchemaEditor._delete_index_sqlc             C   s   t  | | |  j d | S)Nr   )r   r@   )r'   rL   rc   r   r   r   r   r   r     s    z'BaseDatabaseSchemaEditor._index_columnsc                s     j  j s%   j  j s%   j  j r) g  Sg  } x-   j  j D] } | j |  j   |   q< WxL   j  j D]> }   f d d   | D } | j |  j	   | d d  ql Wx-   j  j
 D] } | j | j   |    q W| S)z
        Return a list of all index SQL statements (field indexes,
        index_together, Meta.indexes) for the specified model.
        c                s"   g  |  ] }   j  j |   q Sr   )r   rA   )r   r   )r   r   r   rC     s   	 z?BaseDatabaseSchemaEditor._model_indexes_sql.<locals>.<listcomp>r   r   )r   ZmanagedproxyZswappedrO   rS   r   Zindex_togetherr:   r   r   r   )r'   r   outputr   r   rb   r   r   )r   r   r}     s    %#z+BaseDatabaseSchemaEditor._model_indexes_sqlc             C   s8   g  } |  j  | |  r4 | j |  j | | g   | S)zT
        Return a list of all index SQL statements for the specified field.
        )_field_should_be_indexedr:   r   )r'   r   r   r   r   r   r   r     s    z+BaseDatabaseSchemaEditor._field_indexes_sqlc             C   s   | j  o | j S)N)r   rp   )r'   r   r   r   r   r   r     s    z1BaseDatabaseSchemaEditor._field_should_be_indexedc             C   s   | j  o | j  S)N)r   )r'   r   r   r   r   r   r     s    z2BaseDatabaseSchemaEditor._field_became_primary_keyc             C   s-   | j  r | j  p, | j o, | j o, | j  S)N)rp   r   )r'   r   r   r   r   r   r     s    z0BaseDatabaseSchemaEditor._unique_should_be_addedc          	   C   sD   |  j  d |  j |  d |  j | j  d |  j | j  d | i S)NrL   Z
old_columnZ
new_columnrf   )sql_rename_columnr@   rB   )r'   rL   r   r   r   r   r   r   r     s
    z*BaseDatabaseSchemaEditor._rename_field_sqlc       
      C   s   t  | j j |  j  } |  j | | |  } t | j j | j g |  j  } t  | j j j j |  j  } t | j j j j | j j g |  j  } |  j	 j
 j   }	 t |  j d | d | d | d | d | d |	 S)NrL   r   rB   rF   rG   r   )r   r   rV   r@   r   r   rB   target_fieldr   r!   r?   r   r   sql_create_fk)
r'   r   r   r   rL   r   rB   rF   rG   r   r   r   r   rZ     s    !*	z'BaseDatabaseSchemaEditor._create_fk_sqlc                sV     f d d   } t  | j j | j g t | j j j j  d | j j g | |  S)Nc                 s     j    j |  |    S)N)r@   r   )r   r   )r'   r   r   create_fk_name  s    zDBaseDatabaseSchemaEditor._fk_constraint_name.<locals>.create_fk_namer   )r   r   rV   rB   r	   r   r   )r'   r   r   r   r   r   )r'   r   r     s    		z,BaseDatabaseSchemaEditor._fk_constraint_namec             C   s   |  j  |  j | |  S)N)r   sql_delete_fk)r'   r   r   r   r   r   r   %  s    z'BaseDatabaseSchemaEditor._delete_fk_sqlc             C   s   | r> |  j  | | d | d | } | r: |  j j |  d  S|  j d d j t |  j |   i } |  j d |  j |  d | i S)Nr   r   rc   z, rK   )rN   r)   r:   sql_unique_constraintr_   r<   r@   sql_constraint)r'   r   rb   r   r   r1   rK   r   r   r   _unique_sql(  s    "z$BaseDatabaseSchemaEditor._unique_sqlc                s     f d d   } t  | j j   j  } | d  k rT t | j j | d |  } n   j |  } t | |   j  } | r   j j j r t	   j
 d | d | d | d d | Sd  St	   j d | d | d | Sd  S)	Nc                 s     j    j |  |    S)N)r@   r   )r   r   )r'   r   r   create_unique_name9  s    zGBaseDatabaseSchemaEditor._create_unique_sql.<locals>.create_unique_nameZ_uniqrL   r   rc   r   z WHERE )r   r   rV   r@   r   r   r!   r$   supports_partial_indexesr   sql_create_unique_indexsql_create_unique)r'   r   rc   r   r   r  rL   r   )r'   r   rN   8  s    <	z+BaseDatabaseSchemaEditor._create_unique_sqlc             C   sE   | r/ |  j  j j r+ |  j |  j | |  Sd  S|  j |  j | |  S)N)r!   r$   r  r   r   r   )r'   r   r   r   r   r   r   r   R  s    )z+BaseDatabaseSchemaEditor._delete_unique_sqlc             C   s-   |  j  d |  j |  d |  j d | i i S)Nr   rK   rD   )r  r@   rR   )r'   r   rD   r   r   r   
_check_sqlZ  s    z#BaseDatabaseSchemaEditor._check_sqlc             C   s:   t  |  j d t | j j |  j  d |  j |  d | S)NrL   r   rD   )r   sql_create_checkr   r   rV   r@   )r'   r   r   rD   r   r   r   r   `  s
    	z*BaseDatabaseSchemaEditor._create_check_sqlc             C   s   |  j  |  j | |  S)N)r   sql_delete_check)r'   r   r   r   r   r   r   h  s    z*BaseDatabaseSchemaEditor._delete_check_sqlc             C   s1   t  | d t | j j |  j  d |  j |  S)NrL   r   )r   r   r   rV   r@   )r'   templater   r   r   r   r   r   k  s    z/BaseDatabaseSchemaEditor._delete_constraint_sqlc
                st  | d k	 r%   f d d   | D }   j  j   % }
   j  j j |
 | j j  } Wd QRXg  } x| j   D] \ } } | d k s | | d k ro | d k	 r | d | k r qo | d k	 r | d | k r qo | d k	 r | d | k r qo | d k	 r| d | k rqo | d k	 r-| d	 r-qo | d k	 rL| d
 | k rLqo |	 s_| |	 k ro | j |  qo W| S)z@Return all constraint names matching the columns and conditions.Nc                s%   g  |  ] }   j  j j |   q Sr   )r!   introspectionZidentifier_converter)r   r   )r'   r   r   rC   x  s   	z>BaseDatabaseSchemaEditor._constraint_names.<locals>.<listcomp>rc   rp   r   r   rD   r   rf   )r!   r>   r  Zget_constraintsr   rV   itemsr:   )r'   r   r   rp   r   r   r   rD   r   r   r>   r]   resultr   Zinfodictr   )r'   r   r   r  s.    
%z*BaseDatabaseSchemaEditor._constraint_namesc             C   s}   |  j  | d d } | rO t |  d k rO t d t |  | j j f   x' | D] } |  j |  j | |   qV Wd  S)Nr   Tr   z0Found wrong number (%s) of PK constraints for %s)r   r   r   r   rV   r,   _delete_primary_key_sql)r'   r   r   r   r   r   r   r   r     s    	z,BaseDatabaseSchemaEditor._delete_primary_keyc             C   ss   t  |  j d t | j j |  j  d |  j |  j | j j | j g d d  d t | j j | j g |  j  S)NrL   r   r   Z_pkrc   )	r   sql_create_pkr   r   rV   r@   r   rB   r   )r'   r   r   r   r   r   r     s    	'z0BaseDatabaseSchemaEditor._create_primary_key_sqlc             C   s   |  j  |  j | |  S)N)r   sql_delete_pk)r'   r   r   r   r   r   r    s    z0BaseDatabaseSchemaEditor._delete_primary_key_sqlc             C   s<   |  j  d |  j |  d d j |  i } |  j |  d  S)NZ	procedureparam_types,)sql_delete_procedurer@   r_   r,   )r'   Zprocedure_namer  r1   r   r   r   remove_procedure  s    z)BaseDatabaseSchemaEditor.remove_procedure)___name__
__module____qualname____doc__r^   r   r   r   r   r   r   r   r   r   r   r   r   r   r  rR   Zsql_delete_constraintr  r	  r
  r  r   r   rX   r   r   r   r  r   r  r  r  r(   r+   r-   r,   r@   re   rP   ri   rm   rk   staticmethodr|   rj   r=   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r}   r   r   r   r   r   rZ   r   r   r  rN   r   r  r   r   r   r   r   r   r  r  r   r   r   r   r    ,   s   		;1	7*
		

r    )loggingr   Z!django.db.backends.ddl_referencesr   r   r   r   r   Zdjango.db.backends.utilsr   r	   Zdjango.db.modelsr
   Zdjango.db.transactionr   r   Zdjango.utilsr   	getLoggerr7   r   r   r   r    r   r   r   r   <module>   s   (	