3
g                 @   s   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	 d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ G dd dZdS )    N)chain)settings)models)
operations)	Migration)AlterModelOptions)MigrationOptimizer)MigrationQuestioner)COMPILED_REGEX_TYPERegexObject)stable_topological_sortc               @   st  e Zd ZdZdXddZdYddZdd Zd	d
 ZdZddZdd Z	dd Z
edd Zd[ddZdd Zdd Zdd Zd\ddZdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+d]dRdSZ,dTdU Z-e.dVdW Z/dS )^MigrationAutodetectora  
    Take a pair of ProjectStates and compare them to see what the first would
    need doing to make it match the second (the second usually being the
    project's current state).

    Note that this naturally operates on entire projects at a time,
    as it's likely that changes interact (for example, you can't
    add a ForeignKey without having a migration to add the table it
    depends on first). A user interface may offer single-app usage
    if it wishes, with the caveat that it may not always be possible.
    Nc             C   s.   || _ || _|pt | _dd |jD | _d S )Nc             S   s   h | ]\}}|qS  r   ).0Zappmodelr   r   _/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/db/migrations/autodetector.py	<setcomp>!   s    z1MigrationAutodetector.__init__.<locals>.<setcomp>)
from_stateto_stater	   
questionerr   existing_apps)selfr   r   r   r   r   r   __init__   s    zMigrationAutodetector.__init__c             C   s.   | j ||}| j|||}|r*| j||}|S )z
        Main entry point to produce a list of applicable changes.
        Take a graph to base names on and an optional set of apps
        to try and restrict to (restriction is not guaranteed)
        )_detect_changesarrange_for_graph_trim_to_apps)r   graphZtrim_to_appsconvert_appsmigration_namechangesr   r   r   r   #   s
    zMigrationAutodetector.changesc                s  t |tr fdd|D S t |tr<t fdd|D S t |tr\ fdd|j D S t |tjr|j j|j	 j|j
fS t |trt|S t |tr|S t|dr|j }t |tjr|dd	 }|\}}}| fd
d|D  fdd|j D fS |S d	S )z
        Recursive deconstruction for a field and its arguments.
        Used for full comparison for rename/alter; sometimes a single-level
        deconstruction will not compare correctly.
        c                s   g | ]} j |qS r   )deep_deconstruct)r   value)r   r   r   
<listcomp>6   s    z:MigrationAutodetector.deep_deconstruct.<locals>.<listcomp>c             3   s   | ]} j |V  qd S )N)r    )r   r!   )r   r   r   	<genexpr>8   s    z9MigrationAutodetector.deep_deconstruct.<locals>.<genexpr>c                s   i | ]\}} j ||qS r   )r    )r   keyr!   )r   r   r   
<dictcomp>:   s   z:MigrationAutodetector.deep_deconstruct.<locals>.<dictcomp>deconstruct   Nc                s   g | ]} j |qS r   )r    )r   r!   )r   r   r   r"   N   s    c                s   i | ]\}} j ||qS r   )r    )r   r$   r!   )r   r   r   r%   O   s   )
isinstancelisttupledictitems	functoolspartialfuncr    argskeywordsr
   r   typehasattrr&   r   ZField)r   objZdeconstructedpathr0   kwargsr   )r   r   r    /   s.    







z&MigrationAutodetector.deep_deconstructc             C   sN   g }xDt |j D ]4\}}| j|}|jr<|jjr<|d d= |j| qW |S )z
        Return a definition of the fields that ignores field names and
        what related fields actually relate to. Used for detecting renames (as
        the related fields change during renames).
           to)sortedr,   r    remote_fieldr   append)r   fieldsZ
fields_defnamefieldZdeconstructionr   r   r   only_relation_agnostic_fieldsW   s    

z3MigrationAutodetector.only_relation_agnostic_fieldsc             C   s(  i | _ i | _i | _| jj| _| jj| _t	 | _
t	 | _t	 | _t	 | _t	 | _t	 | _xr| jjD ]f\}}| jj||}|jjs| jj||f q`|| jjkr`|jjr| jj||f q`| j
j||f q`W x| jjD ]x\}}| jj||}|jjs| jj||f q|| jjks |r||kr|jjr<| jj||f q| jj||f qW | 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/| | j0  | j1S )aX  
        Return a dict of migration plans which will achieve the
        change from from_state to to_state. The dict has app labels
        as keys and a list of migrations as values.

        The resulting migrations aren't specially named, but the names
        do matter for dependencies inside the set.

        convert_apps is the list of apps to convert to use migrations
        (i.e. to make initial migrations for, in the usual case)

        graph is an optional argument that, if provided, can help improve
        dependency generation and avoid potential circular dependencies.
        )2generated_operationsaltered_indexesaltered_constraintsr   Zconcrete_appsold_appsr   Zappsnew_appssetold_model_keysold_proxy_keysold_unmanaged_keysnew_model_keysnew_proxy_keysnew_unmanaged_keysr   	get_model_metamanagedaddZ	real_appsproxygenerate_renamed_models_prepare_field_lists_generate_through_model_mapgenerate_deleted_modelsgenerate_created_modelsgenerate_deleted_proxiesgenerate_created_proxiesgenerate_altered_optionsgenerate_altered_managerscreate_altered_indexescreate_altered_constraintsgenerate_removed_constraintsgenerate_removed_indexesgenerate_renamed_fieldsgenerate_removed_fieldsgenerate_added_fieldsgenerate_altered_fields&generate_altered_order_with_respect_to generate_altered_unique_togethergenerate_altered_index_togethergenerate_added_indexesgenerate_added_constraintsgenerate_altered_db_table_sort_migrations_build_migration_list_optimize_migrations
migrations)r   r   r   almnr   r   r   r   r   e   sn    




z%MigrationAutodetector._detect_changesc                s`    j  j@  _ j j@  _ j j@  _i  _	 fdd jD  _
 fdd jD  _dS )z
        Prepare field lists and a list of the fields that used through models
        in the old state so dependencies can be made from the through model
        deletion to the field that uses it.
        c                s@   h | ]8\}} j j| jj||f|f jD ]}|||fq,qS r   )r   r   renamed_modelsgetr<   )r   	app_label
model_name
field_name)r   r   r   r      s   z=MigrationAutodetector._prepare_field_lists.<locals>.<setcomp>c                s2   h | ]*\}} j j||f jD ]}|||fqqS r   )r   r   r<   )r   rp   rq   rr   )r   r   r   r      s   N)rF   rI   kept_model_keysrG   rJ   kept_proxy_keysrH   rK   kept_unmanaged_keysthrough_usersold_field_keysnew_field_keys)r   r   )r   r   rR      s    

z*MigrationAutodetector._prepare_field_listsc             C   s   xt | jD ]\}}| jj||f|}| jj||f }xr|jD ]h}| jj||j	j
|}t|dr>t|jddr>|jjj	j r>|jjj	j|jjj	jf}|||f| j|< q>W qW dS )zThrough model map generation.r:   throughN)r9   rF   rn   ro   r   r   r<   rC   rL   rM   	get_fieldr3   getattrr:   ry   auto_createdrp   rq   rv   )r   rp   rq   old_model_nameold_model_staterr   	old_fieldZthrough_keyr   r   r   rS      s    
z1MigrationAutodetector._generate_through_model_mapc             C   sH   | d dkr| dfS t t| d jd\}}||j f| dd  dfS )	zp
        Return the resolved dependency and a boolean denoting whether or not
        it was swappable.
        r   __setting__Fr'   .r7   NT)r{   r   splitlower)
dependencyZresolved_app_labelZresolved_object_namer   r   r   _resolve_dependency   s    z)MigrationAutodetector._resolve_dependencyc             C   s  i | _ tdd | jj D }d}x\|rxt| jD ]}g }t }xPt| j| D ]<}d}t }	x |jD ]}
|
}| j|
\}
}|
d |krvx,| jj	|
d g D ]}| j
||
rd}P qW |sP qv|r|	j|d |d f qv|
d | j kr|	j|
d | j |
d  d jf qv|rh|rT|j|
d rT|	j|j|
d d  n|	j|
d df qvd}qvW |r|j| |j|	 | j| d= q\P q\W |s|r:| j|  s|r&tdtfg g d	}|d
t| j j	|g d  |}t||_||_|| jk|_| j j|g j| d}q:|| j|  | j|< q:W tdd | jj D }||krz|sld}ntd| j |}q&W dS )ag  
        Chop the lists of operations up into migrations with dependencies on
        each other. Do this by going through an app's list of operations until
        one is found that has an outgoing dependency that isn't in another
        app's migration yet (hasn't been chopped off its list). Then chop off
        the operations before it into a migration and move onto the next app.
        If the loops completes without doing anything, there's a circular
        dependency (which _should_ be impossible as the operations are
        all split at this point so they can't depend and be depended on).
        c             s   s   | ]}t |V  qd S )N)len)r   xr   r   r   r#     s    z>MigrationAutodetector._build_migration_list.<locals>.<genexpr>FTr   r'   	__first__r   )r   dependencieszauto_%ic             s   s   | ]}t |V  qd S )N)r   )r   r   r   r   r   r#   G  s    z)Cannot resolve operation dependencies: %rN)rk   sumr@   valuesr9   rE   r)   
_auto_depsr   ro   check_dependencyrO   r=   
leaf_nodesr;   updater2   r   r   r   r   r   initial
setdefault
ValueError)r   r   Znum_opsZ	chop_moderp   Zchoppedr   	operationZdeps_satisfiedZoperation_dependenciesdepZoriginal_depZis_swappable_depZother_operationsubclassinstanceZnew_num_opsr   r   r   ri      sd    
$


 

z+MigrationAutodetector._build_migration_listc             C   s   xt | jj D ]\}}dd |D }x^|D ]V}xP|jD ]F}| j|d }|d |kr8x&|D ]}| j||r\|| j| q\W q8W q,W t||| j|< qW dS )z{
        Reorder to make things possible. Reordering may be needed so FKs work
        nicely inside the same app.
        c             S   s   i | ]}t  |qS r   )rE   )r   opr   r   r   r%   V  s    z:MigrationAutodetector._sort_migrations.<locals>.<dictcomp>r   N)r9   r@   r,   r   r   r   rO   r   )r   rp   opsZdependency_graphr   r   Zop2r   r   r   rh   O  s    

z&MigrationAutodetector._sort_migrationsc             C   s   xH| j j D ]:\}}x0t||dd  D ]\}}|jj||jf q(W qW x0| j j D ]"}x|D ]}tt|j|_q`W qVW x6| j j D ](\}}x|D ]}t	 j
|j||_qW qW d S )Nr'   )rk   r,   zipr   r;   r=   r   r)   rE   r   optimizer   )r   rp   rk   m1m2	migrationr   r   r   rj   d  s    

z*MigrationAutodetector._optimize_migrationsc                s<   d dkr6 d dkr6t |tjo4|j d j kS  d dk	r d dkrt |tjr|j d j krt fdd|jD pt |tjo|j d j ko|j d j kS  d dk	oʈ d dkrt |tj	o |j d j ko |j d j kS  d dkr> d dkr>t |tj
o<|j d j kS  d dk	r d d	krt |tjo|j d j ko|j d j kS  d dk	r d d
krt |tjo|j d j ko|jpdj  d j kS  d dk	r* d dkr*t |tjtjfo(|j d j kS td f dS )zn
        Return True if the given operation depends on the given dependency,
        False otherwise.
        r7   N   Tr'   c             3   s   | ]\}} d  |kV  qdS )r7   Nr   )r   r   y)r   r   r   r#     s    z9MigrationAutodetector.check_dependency.<locals>.<genexpr>Falterorder_wrt_unset foo_together_changezCan't handle dependency %r)r(   r   CreateModel
name_lowerr   anyr<   AddFieldZmodel_name_lowerRemoveFieldDeleteModel
AlterFieldAlterOrderWithRespectToorder_with_respect_toAlterUniqueTogetherAlterIndexTogetherr   )r   r   r   r   )r   r   r   t  s<    z&MigrationAutodetector.check_dependencyFc             C   s>   |pg |_ |r&| jj|g jd| n| jj|g j| d S )Nr   )r   r@   r   insertr;   )r   rp   r   r   	beginningr   r   r   add_operation  s    
z#MigrationAutodetector.add_operationc             C   s   y| j j|d |d }dd |jD }d|d |d f }|jjsfd|ksfd|ksftjj |j kr~d|d  d|d  fS W n tk
r   Y nX |S )	z|
        Place potential swappable models first in lists of created models (only
        real way to solve #22783).
        r   r'   c             S   s   g | ]
}|j qS r   )__name__)r   baser   r   r   r"     s    z=MigrationAutodetector.swappable_first_key.<locals>.<listcomp>z%s.%sZAbstractUserZAbstractBaseUserZ___)	rD   rL   	__bases__rM   Z	swappabler   ZAUTH_USER_MODELr   LookupError)r   itemr   Z
base_namesZstring_versionr   r   r   swappable_first_key  s    z)MigrationAutodetector.swappable_first_keyc             C   s\  i | _ i | _| j| j }x<t|D ].\}}| jj||f }| j|j}| j| j } x|D ]\}}||kr^| j	j||f }	| j|	j}
||
kr^| j
j|	|r^| jj||j}g }x&|j D ]}|jr|j| j| qW | j|tj|	j|jd|d || j ||f< d|	j|	jf }d|j|jf | j|< | jj||f | jj||f P q^W q$W dS )z
        Find any renamed models, generate the operations for them, and remove
        the old entry from the model lists. Must be run before other
        model-level generation.
        )old_namenew_name)r   z%s.%sN)rn   renamed_models_relrI   rF   r9   r   r   r?   r<   r   r   Zask_rename_modelrD   rL   rM   Z
get_fieldsZis_relationextend!_get_dependencies_for_foreign_keyr   r   ZRenameModelr=   rp   r   removerO   )r   added_modelsrp   rq   model_stateZmodel_fields_defZremoved_modelsrem_app_labelrem_model_nameZrem_model_stateZrem_model_fields_def
model_optsr   r>   Zrenamed_models_rel_keyr   r   r   rQ     sD    
z-MigrationAutodetector.generate_renamed_modelsc          
      sH  | j | jB }| j| }| j| }tt|| jddt|| jdd}x|D ]\ | jj f }| j	j
 j}i d}xd|jD ]Z}|jr|jjr|jr|jj}n|jjs||j< t|jddr|jjjj r||j< qW xH|jD ]>}|jjr||j< t|jddr|jjjj r||j< qW |jjd}	|jjd}
|jjdd}|jjdd}|jjd	d} dd
fg}x|jD ]}t|trd|kr|jdd\}}|j||ddf | jjj||f}| jjj||f}|r|rt|j j!|j j"|j }x |D ]}|j|||d
f qW qW |rR|j|jj#|jj$ddf | j% t&j'|jfdd|j j( D |j|j|j)d|dd |j*sqJxZtj( D ]J\}}| j+|}|j ddf | j% t&j,||dt-t|d qW |r*| j% t&j.|d |df ddfgd  fddtD }|j ddf x(|	D ] }| j% t&j/|d|d qZW x(|
D ] }| j% t&j0|d|d qW |r| j% t&j1|d|d |r| j% t&j2|d|d  f| j3krJxH|j4D ]>}| j%|j5jj#t&j6|j5jj$|j7j|j7d ddfgd qW qJW dS )a  
        Find all new models (both managed and unmanaged) and make create
        operations for them as well as separate operations to create any
        foreign key or M2M relationships (these are optimized later, if
        possible).

        Defer any model options that refer to collections of fields that might
        be deferred (e.g. unique_together, index_together).
        T)r$   reverseNry   indexesconstraintsunique_togetherindex_togetherr   Fr   r'   c                s   g | ]}|d   kr|qS )r   r   )r   d)related_fieldsr   r   r"   K  s    zAMigrationAutodetector.generate_created_models.<locals>.<listcomp>)r=   r<   optionsbasesmanagers)r   r   )rq   r=   r>   )r   )r=   r   c                s   g | ]} |d fqS )Tr   )r   r=   )rp   rq   r   r   r"   u  s   )rq   index)rq   
constraint)r=   r   )r=   r   )8rF   rH   rI   rK   r   r9   r   r   r   rD   rL   rM   local_fieldsr:   r   Zprimary_keyZparent_linkr=   r{   ry   r|   local_many_to_manyr   popr   r(   strr   r;   r   ro   rE   r<   
differenceintersectionrp   object_namer   r   r   r,   r   rN   r   r   r)   r   AddIndexAddConstraintr   r   rG   related_objectsrelated_modelr   r>   )r   Zold_keysr   Zadded_unmanaged_modelsZall_added_modelsr   r   Zprimary_key_relr>   r   r   r   r   r   r   r   base_app_label	base_nameZold_base_model_stateZnew_base_model_stateZremoved_base_fieldsZremoved_base_fieldr=   Zrelated_dependenciesr   r   related_objectr   )rp   rq   r   r   rU     s    












z-MigrationAutodetector.generate_created_modelsc       	   
   C   s   | j | j }xt|D ]\}}| jj||f }|jjds>t||ddfg}xB|jD ]8}t	|t
rTd|krT|jdd\}}|j||ddf qTW | j|tj|jg |j|j|jd|d qW dS )	z
        Make CreateModel statements for proxy models. Use the same statements
        as that way there's less code duplication, but for proxy models it's
        safe to skip all the pointless field stuff and chuck out an operation.
        rP   NFr   r'   T)r=   r<   r   r   r   )r   )rJ   rG   r9   r   r   r   ro   AssertionErrorr   r(   r   r   r;   r   r   r   r=   r   )	r   addedrp   rq   r   r   r   r   r   r   r   r   rW     s$    z.MigrationAutodetector.generate_created_proxiesc             C   sd  | j | jB }| j| }| j| }tt|t|}x*|D ] \}}| jj||f }| jj	||}i }	xN|j
jD ]B}
|
jrp|
jjr|
|	|
j< t|
jddrp|
jjj
j rp|
|	|
j< qpW xH|j
jD ]<}
|
jjr|
|	|
j< t|
jddr|
jjj
j r|
|	|
j< qW |jjdd}|jjdd}|r8| j|tj|dd |rT| j|tj|dd x(t|	D ]}| j|tj||d q^W g }xZ|j
jD ]N}|jj
j}|jj
j}|jj}|j|||df |j s|j|||d	f qW x$t|	D ]}|j|||df qW | j!j"||j#f}|r:|j|d
 |d |d df | j|tj$|jdt%t&|d q:W dS )a  
        Find all deleted models (managed and unmanaged) and make delete
        operations for them as well as separate operations to delete any
        foreign key or M2M relationships (these are optimized later, if
        possible).

        Also bring forward removal of any model options that refer to
        collections of fields - the inverse of generate_created_models().
        ry   Nr   r   )r=   r   )r=   r   )rq   r=   Fr   r   r'   r7   )r=   )r   )'rI   rK   rF   rH   r   r9   r   r   rC   rL   rM   r   r:   r   r=   r{   ry   r|   r   r   r   r   r   r   r   r   r   r   rp   r   r>   r;   many_to_manyrv   ro   r   r   r)   rE   )r   Znew_keysZdeleted_modelsZdeleted_unmanaged_modelsZall_deleted_modelsrp   rq   r   r   r   r>   r   r   r=   r   r   Zrelated_object_app_labelr   rr   Zthrough_userr   r   r   rT     st    








z-MigrationAutodetector.generate_deleted_modelsc             C   s\   | j | j }xJt|D ]>\}}| jj||f }|jjds>t| j|t	j
|jd qW dS )z*Make DeleteModel options for proxy models.rP   )r=   N)rG   rJ   r9   r   r   r   ro   r   r   r   r   r=   )r   Zdeletedrp   rq   r   r   r   r   rV   %  s    z.MigrationAutodetector.generate_deleted_proxiesc          	   C   s  i | _ xt| j| j D ]\}}}| jj||f|}| jj||f }| jj	||j
j|}| j|}x6t| j| j D ] \}}	}
||ko|	|kr||j|
 }| j|}|jr|jjrd|d kr|d d }|| jkr| j| |d d< |j|
 |j d }||ks>|dd |dd kr|t|d |d|d kr|| jj||
||r|| j|tj||
|d | jj||	|
f | jj|||f |
| j |||f< P q|W qW dS )zWork out renamed fields.r8   r7   r'   r   )Z	db_column)rq   r   r   N)renamed_fieldsr9   rx   rw   rn   ro   r   r   rD   rL   rM   rz   r    r<   r:   r   r   Zset_attributes_from_nameZget_attname_columnr+   r   Z
ask_renamer   r   ZRenameFieldr   rO   )r   rp   rq   rr   r}   r~   r>   Z	field_decr   r   Zrem_field_namer   old_field_decZ
old_rel_toZold_db_columnr   r   r   r^   2  s<     
 





z-MigrationAutodetector.generate_renamed_fieldsc             C   s2   x,t | j| j D ]\}}}| j||| qW dS )zMake AddField operations.N)r9   rx   rw   _generate_added_field)r   rp   rq   rr   r   r   r   r`   X  s    z+MigrationAutodetector.generate_added_fieldsc             C   s   | j j||jj|}g }|jr8|jjr8|j| j| tj	tj
tjf}|jpv|j pv|jpv|jrh|jpvt||ov|j}|s|j }t||r|jr| jj|||_n| jj|||_| j|tj||||d|d d S )N)rq   r=   r>   preserve_default)r   )rD   rL   rM   rz   r:   r   r   r   r   Z	DateFieldZDateTimeFieldZ	TimeFieldnullhas_defaultr   ZblankZempty_strings_allowedr(   Zauto_nowcloneZauto_now_addr   Zask_auto_now_add_additiondefaultZask_not_null_additionr   r   r   )r   rp   rq   rr   r>   r   Ztime_fieldsr   r   r   r   r   ]  s*    z+MigrationAutodetector._generate_added_fieldc             C   s2   x,t | j| j D ]\}}}| j||| qW dS )zMake RemoveField operations.N)r9   rw   rx   _generate_removed_field)r   rp   rq   rr   r   r   r   r_   |  s    z-MigrationAutodetector.generate_removed_fieldsc             C   s2   | j |tj||d|||df|||dfgd d S )N)rq   r=   r   r   )r   )r   r   r   )r   rp   rq   rr   r   r   r   r     s    
z-MigrationAutodetector._generate_removed_fieldc          	      s  xt jj@ D ]t\}}}jj||f|}jj|||f|}jj||jj	|}j
j||jj	|}g }t|dot|jddr\|jjjj|jjjjfjkr|jj|j_t|jdd}	|	r|	f }
|
jkr|jj|j_|jj|j_t|dd}|rL||f t fdd|D |_tfdd|jD |_|jj| t|drt|jd	dr|jjjj|jjjjfjkr|jj|j_j|}j|}||kr|jo|j}|j o|j }|s|rnd
}|jrJ|j rJ|j  rJ|j rJ|j }jj||}|tj k	rN||_!d}n|}j"|t#j$||||d|d qj%||| j&||| qW dS )zp
        Make AlterField operations, or possibly RemovedField/AddField if alter
        isn's possible.
        r:   r   Nrr   from_fieldsc                s    g | ]}j j |f |qS r   )r   ro   )r   Z
from_field)from_rename_keyr   r   r   r"     s   zAMigrationAutodetector.generate_altered_fields.<locals>.<listcomp>c                s    g | ]}j j |f |qS r   )r   ro   )r   Zto_field)
rename_keyr   r   r   r"     s   ry   TF)rq   r=   r>   r   )r   )'r9   rw   rx   rn   ro   r   rC   rL   rM   rz   rD   r3   r{   r:   r   rp   rq   rr   r*   r   Z	to_fieldsr   r   ry   r    r   r   r   r   r   Zask_not_null_alterationr   ZNOT_PROVIDEDr   r   r   r   r   r   )r   rp   rq   rr   r}   Zold_field_namer   Z	new_fieldr   Zremote_field_nameZto_field_rename_keyr   r   Znew_field_decZboth_m2mZneither_m2mr   r>   Znew_defaultr   )r   r   r   r   ra     sr     








z-MigrationAutodetector.generate_altered_fieldsc       	         s   t jj}xt| jD ]\}}| jj||f|}| jj||f }| j	j||f }|j
| |j
|  fdd D } fddD }| jj||f||di qW d S )Nc                s   g | ]}| kr|qS r   r   )r   idx)old_indexesr   r   r"     s    z@MigrationAutodetector.create_altered_indexes.<locals>.<listcomp>c                s   g | ]}| kr|qS r   r   )r   r   )new_indexesr   r   r"     s    )added_indexesremoved_indexes)r   r   option_namer9   rs   rn   ro   r   r   r   r   rA   r   )	r   r   rp   rq   r}   r~   new_model_stateZadd_idxZrem_idxr   )r   r   r   rZ     s    

z,MigrationAutodetector.create_altered_indexesc             C   sH   xB| j j D ]4\\}}}x&|d D ]}| j|tj||d q"W qW d S )Nr   )rq   r   )rA   r,   r   r   r   )r   rp   rq   alt_indexesr   r   r   r   re     s    z,MigrationAutodetector.generate_added_indexesc             C   sJ   xD| j j D ]6\\}}}x(|d D ]}| j|tj||jd q"W qW d S )Nr   )rq   r=   )rA   r,   r   r   ZRemoveIndexr=   )r   rp   rq   r   r   r   r   r   r]     s    z.MigrationAutodetector.generate_removed_indexesc       	         s   t jj}xt| jD ]\}}| jj||f|}| jj||f }| j	j||f }|j
| |j
|  fdd D } fddD }| jj||f||di qW d S )Nc                s   g | ]}| kr|qS r   r   )r   c)old_constraintsr   r   r"     s    zDMigrationAutodetector.create_altered_constraints.<locals>.<listcomp>c                s   g | ]}| kr|qS r   r   )r   r   )new_constraintsr   r   r"     s    )added_constraintsremoved_constraints)r   r   r   r9   rs   rn   ro   r   r   r   r   rB   r   )	r   r   rp   rq   r}   r~   r   Zadd_constraintsZrem_constraintsr   )r   r   r   r[     s    

z0MigrationAutodetector.create_altered_constraintsc             C   sH   xB| j j D ]4\\}}}x&|d D ]}| j|tj||d q"W qW d S )Nr   )rq   r   )rB   r,   r   r   r   )r   rp   rq   alt_constraintsr   r   r   r   rf     s    z0MigrationAutodetector.generate_added_constraintsc             C   sJ   xD| j j D ]6\\}}}x(|d D ]}| j|tj||jd q"W qW d S )Nr   )rq   r=   )rB   r,   r   r   ZRemoveConstraintr=   )r   rp   rq   r   r   r   r   r   r\   )  s    z2MigrationAutodetector.generate_removed_constraintsc             C   s   t |dd }|d k	rd}|}n|jjjj}|jjjj}||d dfg}t |jdd r|jjjj r|j|jjjj|jjjjd df |S )Nswappable_settingr   Try   )	r{   r:   r   rM   rp   r   ry   r|   r;   )r   r>   r   dep_app_labelZdep_object_namer   r   r   r   r   4  s    

z7MigrationAutodetector._get_dependencies_for_foreign_keyc                s   |j }xtjD ]\ jj f}jj |f }jj f }|jj|}|rv fdd|D nt	 }|jj|}|rt	|nt	 }||krg }xN|D ]F}	x@|	D ]8}
j
j jj|
}|jr|jjr|jj| qW qW j |f di||i|d qW d S )Nc                s&   h | ]}t  fd d|D qS )c             3   s"   | ]}j j |f|V  qd S )N)r   ro   )r   n)rp   rq   r   r   r   r#   R  s   zQMigrationAutodetector._generate_altered_foo_together.<locals>.<setcomp>.<genexpr>)r*   )r   unique)rp   rq   r   r   r   r   P  s   zGMigrationAutodetector._generate_altered_foo_together.<locals>.<setcomp>r=   )r   )r   r9   rs   rn   ro   r   r   r   r   rE   rD   rL   rM   rz   r:   r   r   r   r   )r   r   r   r}   r~   r   	old_value	new_valuer   Zfoo_togethersrr   r>   r   )rp   rq   r   r   _generate_altered_foo_togetherG  s,     


z4MigrationAutodetector._generate_altered_foo_togetherc             C   s   | j tj d S )N)r  r   r   )r   r   r   r   rc   l  s    z6MigrationAutodetector.generate_altered_unique_togetherc             C   s   | j tj d S )N)r  r   r   )r   r   r   r   rd   o  s    z5MigrationAutodetector.generate_altered_index_togetherc       	      C   s   | j j| j| j}x|t|D ]p\}}| jj||f|}| jj||f }| j	j||f }|j
jd}|j
jd}||kr| j|tj||d qW d S )NZdb_table)r=   table)rs   unionrt   ru   r9   rn   ro   r   r   r   r   r   r   ZAlterModelTable)	r   models_to_checkrp   rq   r}   r~   r   Zold_db_table_nameZnew_db_table_namer   r   r   rg   r  s    z/MigrationAutodetector.generate_altered_db_tablec       	      C   s   | j j| j| j| j| j@ | j| j@ }xt|D ]\}}| j	j
||f|}| jj||f }| jj||f }dd |jj D }dd |jj D }||kr0| j|tj||d q0W dS )z
        Work out if any non-schema-affecting options have changed and make an
        operation to represent them in state changes (in case Python code in
        migrations needs them).
        c             S   s    i | ]\}}|t jkr||qS r   )r   ALTER_OPTION_KEYS)r   r$   r!   r   r   r   r%     s   zBMigrationAutodetector.generate_altered_options.<locals>.<dictcomp>c             S   s    i | ]\}}|t jkr||qS r   )r   r  )r   r$   r!   r   r   r   r%     s   )r=   r   N)rs   r  rt   ru   rH   rI   rF   rK   r9   rn   ro   r   r   r   r   r,   r   r   r   )	r   r  rp   rq   r}   r~   r   Zold_optionsnew_optionsr   r   r   rX     s&    
z.MigrationAutodetector.generate_altered_optionsc             C   s   xt | jD ]\}}| jj||f|}| jj||f }| jj||f }|jjd|jjdkrg }|jjdr|j|||jd df | j	|t
j||jjdd|d qW d S )Nr   T)r=   r   )r   )r9   rs   rn   ro   r   r   r   r   r;   r   r   r   )r   rp   rq   r}   r~   r   r   r   r   r   rb     s&    
z<MigrationAutodetector.generate_altered_order_with_respect_toc             C   sr   xlt | jD ]^\}}| jj||f|}| jj||f }| jj||f }|j|jkr| j|t	j
||jd qW d S )N)r=   r   )r9   rs   rn   ro   r   r   r   r   r   r   ZAlterModelManagers)r   rp   rq   r}   r~   r   r   r   r   rY     s    z/MigrationAutodetector.generate_altered_managersc                s  |j  }i  xLt|j D ]:\}}|s,qd}x|D ]}|d |kr6|}P q6W |dkr| jj| rx|D ]}	|df ||	jf< qlW ||= q|dkrd}
n| j|d pdd }
xt|D ]\}}	|dkr|r|	jj	| d|
 g}|r|j	| n4|dkr| r|j	d n|j	|	j
 dd  dj|}||f ||	jf< |
d7 }
||	_qW qW x8|j D ],}x$|D ]}	 fd	d
|	jD |	_qpW qfW |S )z
        Take a result from changes() and a MigrationGraph, and fix the names
        and dependencies of the changes so they extend the graph from the leaf
        nodes for each app.
        Nr   r   r'   z%04ir   d   _c                s   g | ]} j ||qS r   )ro   )r   r   )name_mapr   r   r"     s    z;MigrationAutodetector.arrange_for_graph.<locals>.<listcomp>)r   r)   r,   r   Zask_initialr=   parse_number	enumerater   r;   Zsuggest_namejoinr   )r   r   r   r   Zleavesrp   rk   Zapp_leafZleafr   Znext_numberiZnew_name_partsr   r   )r	  r   r     sF    




"z'MigrationAutodetector.arrange_for_graphc       
         s   i  xJ|j  D ]>\}}x4|D ],}x&|jD ]\}} j|t j| q(W qW qW t|}d}	x,|	|krt|}	|j fdd|D   q^W xt|D ]}||kr||= qW |S )a1  
        Take changes from arrange_for_graph() and set of app labels, and return
        a modified set of changes which trims out as many migrations that are
        not in app_labels as possible. Note that some other migrations may
        still be present as they may be required dependencies.
        Nc                s   g | ]} j |f qS r   )ro   )r   rp   )app_dependenciesr   r   r"     s    z7MigrationAutodetector._trim_to_apps.<locals>.<listcomp>)r,   r   r   rE   rO   r   r)   )
r   r   Z
app_labelsrp   rk   r   r   r=   Zrequired_appsZold_required_appsr   )r  r   r     s    
 

z#MigrationAutodetector._trim_to_appsc             C   s    t jd|}|rt|d S dS )z
        Given a migration name, try to extract a number from the beginning of
        it. If no number is found, return None.
        z^\d+r   N)rematchint)clsr=   r  r   r   r   r
    s    z"MigrationAutodetector.parse_number)N)NNN)NN)N)NF)N)0r   
__module____qualname____doc__r   r   r    r?   r   rR   rS   staticmethodr   ri   rh   rj   r   r   r   rQ   rU   rW   rT   rV   r^   r`   r   r_   r   ra   rZ   re   r]   r[   rf   r\   r   r  rc   rd   rg   rX   rb   rY   r   r   classmethodr
  r   r   r   r   r      sX   

(
a
W?
. /!Y&S%$
2r   )r-   r  	itertoolsr   Zdjango.confr   Z	django.dbr   Zdjango.db.migrationsr   Zdjango.db.migrations.migrationr   Z&django.db.migrations.operations.modelsr   Zdjango.db.migrations.optimizerr   Zdjango.db.migrations.questionerr	   Zdjango.db.migrations.utilsr
   r   Zdjango.utils.topological_sortr   r   r   r   r   r   <module>   s   