
    Hwg4                        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 d dlmZmZmZ d dlmZ  G d d      Zy)    N)defaultdict)chain)settings)models)
operations)	Migration)AlterModelOptions)MigrationOptimizer)MigrationQuestioner)COMPILED_REGEX_TYPERegexObjectresolve_relation)stable_topological_sortc                   `   e Zd ZdZd4dZd5dZd Zd Zd6dZd Z	d	 Z
ed
        Zd4dZd Zd Zd Zd7d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d$        Z&d% Z'd& Z(d' Z)d( Z*d) Z+d* Z,d+ Z-d, Z.d- Z/d. Z0d/ Z1d0 Z2d4d1Z3d2 Z4e5d3        Z6y)8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                     || _         || _        |xs
 t               | _        |j                  D ch c]  \  }}|	 c}}| _        y c c}}w N)
from_stateto_stater   
questionerr   existing_apps)selfr   r   r   appmodels         X/var/www/horilla/myenv/lib/python3.12/site-packages/django/db/migrations/autodetector.py__init__zMigrationAutodetector.__init__"   s@    $ $=(;(=4>4E4EFjc5cFFs   Ac                 x    | 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   graphtrim_to_appsconvert_appsmigration_namechangess         r   r%   zMigrationAutodetector.changes(   sD     &&|U;((%H((,?G    c           
          t        |t              r|D cg c]  } j                  |       c}S t        |t              rt         fd|D              S t        |t              r3|j                         D ci c]  \  }}| j                  |       c}}S t        |t        j                        rA|j                   j                  |j                         j                  |j                        fS t        |t              rt        |      S t        |t              r|S t        |d      r|j                         }t        |t         j"                        r|dd }|\  }}}||D cg c]  } j                  |       c}|j                         D ci c]  \  }}| j                  |       c}}fS |S c c}w c c}}w c c}w c c}}w )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              3   @   K   | ]  }j                  |        y wr   )deep_deconstruct).0valuer   s     r   	<genexpr>z9MigrationAutodetector.deep_deconstruct.<locals>.<genexpr>=   s     G%..u5Gs   deconstruct   N)
isinstancelistr)   tupledictitems	functoolspartialfuncargskeywordsr   r   typehasattrr-   r   Field)r   objr+   keydeconstructedpathr7   kwargss   `       r   r)   z&MigrationAutodetector.deep_deconstruct4   s    c4 >ABUD))%0BBU#G3GGGT"HK		T*#uC..u55TTY../%%chh/%%cll3 
 01s##T" JS-(OO-M#v||, -ab 1!.D$;?@%&&u-@EK\\^TzsEd++E22T  J; C U* ATs   F68F;&GG
c                     g }t        |j                               D ]^  \  }}| j                  |      }|j                  r+|j                  j                  r|d   j                  dd       |j                  |       ` |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).
           toN)sortedr3   r)   remote_fieldr   popappend)r   fields
fields_defnamefielddeconstructions         r   only_relation_agnostic_fieldsz3MigrationAutodetector.only_relation_agnostic_fieldsZ   sy     
!&,,.1 	.KD%!2259N!!e&8&8&>&>q!%%dD1n-		.
 r&   c                    i | _         i | _        i | _        i | _        t	               | _        t	               | _        t	               | _        t	               | _        t	               | _	        t	               | _
        | j                  j                  j                         D ]  \  \  }}}|j                  j                  dd      s| j                  j!                  ||f       C|| j                  j"                  vs\|j                  j                  d      r| j                  j!                  ||f       | j
                  j!                  ||f        | j$                  j                  j                         D ]  \  \  }}}|j                  j                  dd      s| j                  j!                  ||f       C|| j                  j"                  vs|s^||v sc|j                  j                  d      r| j                  j!                  ||f       | j                  j!                  ||f        | j                  j'                          | j$                  j'                          | j)                          | j+                          | j-                          | j/                          | j1                          | j3                          | j5                          | j7                          | j9                          | j;                          | j=                          | j?                          | jA                          | jC                          | jE                          | jG                          | jI                          | jK                          | jM                          | jO                          | jQ                          | jS                          | jU                          | jW                          | jY                          | j[                          | j]                          | j_                          | ja                          | jc                  |       | je                          | jf                  S )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.
        managedTproxy)4generated_operationsaltered_indexesaltered_constraintsrenamed_fieldssetold_model_keysold_proxy_keysold_unmanaged_keysnew_model_keysnew_proxy_keysnew_unmanaged_keysr   r   r3   optionsgetadd	real_appsr   resolve_fields_and_relations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!generate_altered_db_table_commentcreate_renamed_fieldscreate_altered_indexescreate_altered_constraintsgenerate_removed_constraintsgenerate_removed_indexesgenerate_renamed_fieldsgenerate_renamed_indexes(generate_removed_altered_unique_together'generate_removed_altered_index_together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!   	app_label
model_namemodel_states         r   r   z%MigrationAutodetector._detect_changesh   s   & %'!!#%   "e!e"%%!e!e"%%48OO4J4J4P4P4R 	E0#Y
[&&**9d;''++Y
,CD$//";";;&&**73''++Y
,CD''++Y
,CD	E 59MM4H4H4N4N4P 		E0#Y
[&&**9d;''++Y
,CD$//";";;l!:&&**73''++Y
,CD''++Y
,CD		E 	446224 	$$& 	!!#((* 	$$&$$&%%'%%'%%'&&(..0 	""$ 	##%'')))+%%'$$&%%'557446$$&""$$$&335--/,,.##%'')&&(""5)!!#r&   c                 f   | j                   | j                  z  | _        | j                  | j                  z  | _        | j                  | j                  z  | _        i | _	        | j                  D ch c]P  \  }}| j                  j                  || j                  j                  ||f|      f   j                  D ]  }|||f	 R c}}}| _        | j                  D ch c]4  \  }}| j                   j                  ||f   j                  D ]  }|||f	 6 c}}}| _        yc c}}}w c c}}}w )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.
        N)rV   rY   kept_model_keysrW   rZ   kept_proxy_keysrX   r[   kept_unmanaged_keysthrough_usersr   r   renamed_modelsr]   rH   old_field_keysr   new_field_keysr   r   r   
field_names       r   rb   z*MigrationAutodetector._prepare_field_lists   s?     $22T5H5HH#22T5H5HH#'#:#:T=T=T#T  *.)=)=
 
%	:"oo444..22Iz3JJWWf
  
J/
/
 *.)=)=
 
%	:"mm229j3HIPP
  
J/
/


s   2AD%!9D,c                    t        | j                        D ]  \  }}| j                  j                  ||f|      }| j                  j
                  ||f   }|j                  j                         D ]]  \  }}t        |d      st        |j                  dd      s+t        |j                  j                  ||      }|||f| j                  |<   _  y)zThrough model map generation.rE   throughN)rD   rV   r   r]   r   r   rH   r3   r:   getattrrE   r   r   r   )r   r   r   old_model_nameold_model_stater   rK   through_keys           r   rc   z1MigrationAutodetector._generate_through_model_map   s    %+D,?,?%@ 	!Iz!0044J'N #oo44Y5NOO%4%;%;%A%A%C !
E5.1g&&	47 #3**22Iz#K "&"7D&&{3	r&   c                     | d   dk7  r| dfS t        t        | d         j                  d      \  }}||j                         f| dd z   dfS )	zp
        Return the resolved dependency and a boolean denoting whether or not
        it was swappable.
        r   __setting__Fr.   .rB   NT)r   r   splitlower)
dependencyresolved_app_labelresolved_object_names      r   _resolve_dependencyz)MigrationAutodetector._resolve_dependency   sh     a=M)u$$3:jm4

%* 	10 #$8$>$>$@AJqrNRTXXXr&   c           
      *   i | _         t        d | j                  j                         D              }d}|rt	        | j                        D ]q  }g }t               }t        | j                  |         D ]p  }d}t               }	|j                  D ]  }
|
}| j                  |
      \  }
}|
d   |k7  s#| j                  j                  |
d   g       D ]  }| j                  ||
      sd} n |s n|r|	j                  |d   |d   f       }|
d   | j                   v r4|	j                  |
d   | j                   |
d      d   j                  f       |rV|r<|j                  |
d         r(|	j                  |j                  |
d         d          |	j                  |
d   df       d} |r4|j                  |       |j                  |	       | j                  |   d= q n |s|s| j                  |   r|rt!        dt"        fg g d	      } |d
t%        | j                   j                  |g             dz   z  |      }t        |      |_        ||_        || j*                  v|_        | j                   j/                  |g       j                  |       d}S|| j                  |   z   | j                  |<   t t        d | j                  j                         D              }||k(  r|sd}nt1        d| j                  z        |}|ryy)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              3   2   K   | ]  }t        |        y wr   lenr*   xs     r   r,   z>MigrationAutodetector._build_migration_list.<locals>.<genexpr>  s     Ic!fI   FTr   r.   	__first__r   )r   dependencieszauto_%ic              3   2   K   | ]  }t        |        y wr   r   r   s     r   r,   z>MigrationAutodetector._build_migration_list.<locals>.<genexpr>r  s     Qc!fQr   z)Cannot resolve operation dependencies: %rN)r   sumrQ   valuesrD   rU   r0   
_auto_depsr   r]   check_dependencyr^   rJ   
leaf_nodesrG   updater9   r   r   r   r   r   initial
setdefault
ValueError)r   r!   num_ops	chop_moder   choppedr   	operationdeps_satisfiedoperation_dependenciesdeporiginal_depis_swappable_depother_operationsubclassinstancenew_num_opss                    r   r~   z+MigrationAutodetector._build_migration_list  sK    Id&?&?&F&F&HII	 $D$=$=> Q	"u!%d&?&?	&J!K 9I%)N-0U*(33 0?
 (+040H0H0M--q6Y. 483L3L3P3P #A4 * $(#8#8##N5:N$)* $2 %#3$:$>$>)5a,q/(J%& &)Vt%>$:$>$>),QQ1H1L1Q1Q(R%& (1 ,1U5E5Ec!f5M,B,F,F050@0@Q0H0K-. -C,F,F14Q0E-. :?a0?b &y1$++,BC 55i@Cs9v  744Y?9#''&L+-rB$
 $,%T__-@-@B-O)PST)TU%$ 15\0B-.5++4D<N<N+N(229bAHHR$)	 $d&?&?	&JJ 11)<_Qd Qd.G.G.N.N.PQQKg%  $I$C334  "GE r&   c                    t        | j                  j                               D ]  \  }}|D ci c]  }|t                }}|D ]^  }|j                  D ]M  }| j                  |      d   }|d   |k(  s |D ])  }| j                  ||      s||   j                  |       + O ` t        ||      | j                  |<    yc c}w )z{
        Reorder to make things possible. Reordering may be needed so FKs work
        nicely inside the same app.
        r   N)	rD   rQ   r3   rU   r   r   r   r^   r   )r   r   opsopdependency_graphr   op2s          r   r}   z&MigrationAutodetector._sort_migrations}  s    
 %T%>%>%D%D%FG 	NIs478bCE	88 >== >C 2237:C1v*#& >C#44S#> 0 4 8 8 =>>> 4K%4D%%i0	8s   Cc                    | j                   j                         D ]C  \  }}t        ||dd        D ],  \  }}|j                  j	                  ||j
                  f       . E | j                   j                         D ],  }|D ]%  }t        t        |j                              |_        ' . | j                   j                         D ]5  \  }}|D ]+  }t               j                  |j                  |      |_        - 7 y )Nr.   )r   r3   zipr   rG   rJ   r   r0   rU   r
   optimizer   )r   r   r   m1m2	migrations         r   r   z*MigrationAutodetector._optimize_migrations  s    %)__%:%:%< 	=!Izj*QR.9 =B&&	277';<=	=
 //002 	KJ' K	)-c)2H2H.I)J	&K	K
 &*__%:%:%< 	!Iz' 	'9';'D'D(()(	$	r&   c                 &   d   Cd   du r<t        |t        j                        xr  |j                  d   j	                         k(  S d   Éd   du rt        |t        j                        xr@ |j                  d   j	                         k(  xr t        fd|j                  D              xs^ t        |t        j                        xrB |j                  d   j	                         k(  xr  |j                  d   j	                         k(  S d   ed   du r^t        |t        j                        xrB |j                  d   j	                         k(  xr  |j                  d   j	                         k(  S d   Cd   du r<t        |t        j                        xr  |j                  d   j	                         k(  S d   fd   dk(  r^t        |t        j                        xrB |j                  d   j	                         k(  xr  |j                  d   j	                         k(  S d   xd   dk(  rpt        |t        j                        xrT |j                  d   j	                         k(  xr2 |j                  xs d	j	                         d   j	                         k7  S d   Td   d
k(  rLt        |t        j                  t        j                  f      xr  |j                  d   j	                         k(  S t!        d      )zn
        Return True if the given operation depends on the given dependency,
        False otherwise.
        rB      Tr.   c              3   4   K   | ]  \  }}d    |k(    yw)rB   N )r*   r   yr   s      r   r,   z9MigrationAutodetector.check_dependency.<locals>.<genexpr>  s     Htq!
1*Hs   Falterorder_wrt_unset foo_together_changezCan't handle dependency )r/   r   CreateModel
name_lowerr   anyrH   AddFieldmodel_name_lowerRemoveFieldDeleteModel
AlterFieldAlterOrderWithRespectToorder_with_respect_toAlterUniqueTogetherAlterIndexTogetherr   )r   r   r   s     `r   r   z&MigrationAutodetector.check_dependency  s&    a= Z]d%:9j&<&<= B((JqM,?,?,AA
 ]&:a=D+@9j&<&<= I((JqM,?,?,AAIHy7G7GHH
 9j&9&9: B..*Q-2E2E2GGB((JqM,?,?,AA ]&:a=E+A9j&<&<= B..*Q-2E2E2GGB((JqM,?,?,AA ]"z!}'=9j&<&<= B((JqM,?,?,AA
 ]&:a=G+C9j&;&;< B..*Q-2E2E2GGB((JqM,?,?,AA ]&:a=<M+M9j&H&HI )((JqM,?,?,AA)44:AACa=&&() ]&:a=<Q+Q33Z5R5RS B ((JqM,?,?,AA ZIJJr&   c                     |xs g |_         |r-| j                  j                  |g       j                  d|       y | j                  j                  |g       j	                  |       y )Nr   )r   rQ   r   insertrG   )r   r   r   r   	beginnings        r   add_operationz#MigrationAutodetector.add_operation  sX      ,1r	%%00B?FFq)T%%00B?FFyQr&   c                    	 | j                   j                  |   }|j                  D ch c]   }t        |t              r|n|j
                  " }}|d   d|d   }|j                  j                  d      s7d|v s3d|v s/t        j                  j                         |j                         k(  rd|d   z   d|d   z   fS 	 |S c c}w # t        $ r Y |S w xY w)z|
        Place potential swappable models first in lists of created models (only
        real way to solve #22783).
        r   r   r.   	swappableAbstractUserAbstractBaseUser___)r   r   basesr/   str__name__r\   r]   r   AUTH_USER_MODELr   LookupError)r   itemr   base
base_namesstring_versions         r   swappable_first_keyz)MigrationAutodetector.swappable_first_key  s    
	--..t4K (-- #4-4==@J  )-Qa9N##''4!Z/%3++113~7K7K7MMQa99 N
   		s#   'C %CA0C C 	CCc                    i | _         i | _        | j                  | j                  z
  }t	        |      D ]K  \  }}| j
                  j                  ||f   }| j                  |j                        }| j                  | j                  z
  }|D ]  \  }}||k(  s| j                  j                  ||f   }	| j                  |	j                        }
||
k(  sI| j                  j                  |	|      sfg }t        |j                  j                               | j
                  j                  ||f   j                         D cg c]#  }|j                         D ]  }|j                   % c}}z   }|D ]<  }|j                   s|j#                  | j%                  |||| j
                               > | j'                  |t)        j*                  |	j,                  |j,                        |       || j                   ||f<   |	j.                  d|	j0                  }|j.                  d|j0                  | j                  |<   | j                  j3                  ||f       | j                  j5                  ||f        K N yc c}}w )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   r   N)r   renamed_models_relrY   rV   rD   r   r   rM   rH   r   r   ask_rename_modelr0   r   	relationsrE   is_relationextend!_get_dependencies_for_foreign_keyr   r   RenameModelrJ   r   r   remover^   )r   added_modelsr   r   r   model_fields_defremoved_modelsrem_app_labelrem_model_namerem_model_staterem_model_fields_defr   r   rK   rH   renamed_models_rel_keys                   r   ra   z-MigrationAutodetector.generate_renamed_models  s    !"$**T-@-@@%+L%9 8	"!Iz--..y*/DEK#AA+BTBTU!0043F3FFN1? 3"-~ I-&*oo&<&<%~5'O ,0+M+M'..,( (+????;;+[ ,.L%)+*<*<*C*C*E%F151H1H$-z$92""(&(	J$- .7-=-=-?J
 %*	 !& 2 2J 2J &F *0 	&#(#4#4$0$7$7(,(N(N,5,6,1,0MM	)*%&	& !.. ) * 6 6-<-A-A-8-=-=!" .: /  JXD//	:0EF / 9 9 / : :62 !, 5 5 + 6 6! !33 6 !//66~7VW //33Y
4KL!g3"8	"$Js   (Jc                 
   | j                   | j                  z  }| j                  |z
  }| j                  |z
  }t	        t        || j                  d      t        || j                  d            }|D ]  \  }}| j                  j                  ||f   }i }d}	|j                  j                         D ]  \  }
}|j                  s|j                  j                  r>|j                  r|j                  j                  }	n|j                  j                  s|||
<   t        |j                  dd      s|||
<    |j                   j#                  d      }|j                   j#                  d      }|j                   j#                  dd      }|j                   j#                  dd      }|j                   j#                  d	d      }||dd
fg}|j$                  D ]  }t'        |t(              sd|v s|j+                  dd      \  }}|j-                  ||ddf       | j.                  j                  j1                  ||f      }| j                  j                  j1                  ||f      }|s|st3        |j                        j5                  |j                        j7                  |j                        }|D ]  }|j-                  |||d
f         |	r|j-                  t9        |	||      dz          | j;                  |t=        j>                  |j@                  |j                  j                         D cg c]  }|d   |vr| c}|j                   |j$                  |jB                        |d       |j                   j1                  dd      s t        |j                               D ]t  \  }}| jE                  |||| j                        }|j-                  ||ddf       | j;                  |t=        jF                  |||      tI        t3        |                   v |r3| j;                  |t=        jJ                  ||      |||df||ddfg       t        |      D cg c]  }|||df
 }}|j-                  ||ddf       |D ]+  }| j;                  |t=        jL                  ||      |       - |D ]+  }| j;                  |t=        jN                  ||      |       - |r)| j;                  |t=        jP                  ||      |       |r)| j;                  |t=        jR                  ||      |       | j                  jT                  }||f| jV                  v s|||f   j                         D ]y  \  }}| j                  j                  |   } |j                         D ]H  \  }!}"| j;                  | jX                  t=        jZ                  | j@                  |!|"      ||ddfg       J {  yc c}w c c}w )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=   reverseNr   indexesconstraintsunique_togetherindex_togetherr   Fr   r.   )NTr   rJ   rH   r\   r   managers)r   r   rO   r   rJ   rK   r   rJ   r   r   indexr   
constraintrJ   r	  rJ   r
  ).rV   rX   rY   r[   r   rD   r   r   r   rH   r3   rE   r   primary_keyparent_linkr   r\   rF   r   r/   r   r   rG   r   r]   rU   
differenceintersectionr   r   r   r   rJ   r  r   r   r0   r   AddIndexAddConstraintr   r   r   rW   r   r   )#r   old_keysr   added_unmanaged_modelsall_added_modelsr   r   r   related_fieldsprimary_key_relr   rK   r  r  r	  r
  r   r   r   base_app_label	base_nameold_base_model_statenew_base_model_stateremoved_base_fieldsremoved_base_fielddrJ   related_dependenciesr  r  r   related_model_keyrelated_model_staterelated_field_namerelated_fields#                                      r   re   z-MigrationAutodetector.generate_created_modelsH  s    &&)@)@@**X5!%!8!88!C <T%=%=tL)t/G/GQUV
 &6 p	!Iz--..y*/DEKN"O%0%7%7%=%=%? ;!
E%%))// ,,.3.@.@.F.FO!&!3!3!?!?9>N:6u119dC5:z2; "))--i8G%--11-@K)11556GNO(00445EtLN$/$7$7$;$;'%!
 Je4L $)) dC(SD[04

30B-NI ''D$(OP ,0??+A+A+E+E'3,( ,0==+?+?+C+C'3,( ,0D 4 ; ;<'Z 4 ; ; *\+*<*<= , 3F .(//!/<NPU V)2 ##$'!"
 ## &&$)) "-!3!3!9!9!;Q4~5 
 (//%++(11
 *  $ &&**9d;  &n&:&:&<= e#EEMM	  ##Y
D$$GH""''#-!#
 "&c,&7!8 # ( %""66'.C
 #J0EtL"Jd;" # 
 AG~@V$8<Jd3$  $ !''Jd(KL  ""''#-# "6 #  * 
"",,#-#- "6 #  ""22'(7 "6 #  ""11''5 "6 #  //I:&$*=*==9Bz):%'5%~ +/--*>*>?P*Q'=K=Q=Q=S 	9*M**/99&11+>+C+C%7&3
 ,5j$*M)N + 		Gp	Hd$s   4W;$X c                 $   | j                   | j                  z
  }t        |      D ]  \  }}| j                  j                  ||f   }|j
                  j                  d      sJ ||ddfg}|j                  D ]B  }t        |t              sd|v s|j                  dd      \  }}|j                  ||ddf       D | j                  |t        j                  |j                  g |j
                  |j                  |j                         |        y)	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.   Tr  r   )rZ   rW   rD   r   r   r\   r]   r   r/   r   r   rG   r   r   r   rJ   r  )	r   addedr   r   r   r   r   r   r!  s	            r   rg   z.MigrationAutodetector.generate_created_proxies  s!    ##d&9&99%+E] 	!Iz--..y*/DEK&&**7333 Je4L $)) QdC(SD[04

30B-NI ''D$(OPQ
 &&$))'//%++(11 *  	r&   c           
         | j                   | j                  z  }| j                  |z
  }| j                  |z
  }t	        t        |      t        |            }|D ]  \  }}| j                  j                  ||f   }i }|j                  j                         D ]J  \  }	}
|
j                  s|
j                  j                  r|
||	<   t        |
j                  dd      sF|
||	<   L |j                  j                  dd      }|j                  j                  dd      }|r'| j                  |t!        j"                  |d             |r'| j                  |t!        j$                  |d             t        |      D ])  }| j                  |t!        j&                  ||             + g }| j                  j(                  }|||f   j                         D ]W  \  \  }}}|j                         D ]<  \  }	}
|j+                  |||	df       |
j,                  r(|j+                  |||	d	f       > Y t        |      D ]  }|j+                  |||df        | j.                  j1                  ||j2                  f      }|r|j+                  |d
   |d   |d   df       | j                  |t!        j4                  |j6                        t9        t;        |                    y)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().
        r   Nr	  r
  r  r  r   rJ   Fr   r   r.   rB   rJ   r   )rY   r[   rV   rX   r   rD   r   r   rH   r3   rE   r   r   r\   rF   r   r   r   r   r   r   rG   many_to_manyr   r]   r   r   rJ   r0   rU   )r   new_keysdeleted_modelsdeleted_unmanaged_modelsall_deleted_modelsr   r   r   r  r   rK   r	  r
  rJ   r   r   related_object_app_labelobject_namerelation_related_fieldsthrough_users                       r   rd   z-MigrationAutodetector.generate_deleted_models,  s    &&)@)@@,,x7#'#:#:X#E ">"F+C$D
 &8 O	!Iz//00J1FGKN%0%7%7%=%=%? ;!
E%%))//5:z2u119dC5:z2; *11556GNO(00445EtLN""22'(, ""11''+ ~. ""**#-! L11I +4Iz4I*J*P*P*R ' (&)@)F)F)H %J ''1;
ER !--$++ 8 + * '		$ ~. J##Y
D%$HIJ  --119k>T>T2UVL##!!_l1o|AN &&$)) "#l"34  SO	r&   c                 ,   | j                   | j                  z
  }t        |      D ]m  \  }}| j                  j                  ||f   }|j
                  j                  d      sJ | j                  |t        j                  |j                               o y)z*Make DeleteModel options for proxy models.rP   r0  N)rW   rZ   rD   r   r   r\   r]   r   r   r   rJ   )r   deletedr   r   r   s        r   rf   z.MigrationAutodetector.generate_deleted_proxies  s    %%(;(;;%+G_ 	!Iz//00J1FGK&&**7333&&$))	r&   c                 h   g | _         | j                  j                         }t        | j                  |z
        D ]  \  }}}| j
                  j                  ||f|      }| j                  j                  ||f   }| j                  j                  ||f   }|j                  |      }| j                  |      }	t        || j                  z
        D ]\  \  }
}}|
|k(  s||k(  s|j                  |      }| j                  |      }|j                  rH|j                  j                  r2d|d   v r+|d   d   }|| j                  v r| j                  |   |d   d<   |j                  |       |j!                         d   }||	k(  s#|dd |	dd k(  st#        |d   |      |	d   k(  s| j$                  j'                  ||||      s| j                   j)                  |
||j*                  |||||f       |j-                  |
||f       |j/                  |||f       || j0                  |||f<      y)zWork out renamed fields.rC   rB   r.   r   )	db_columnN)renamed_operationsr   copyrD   r   r   r]   r   r   r   	get_fieldr)   rE   r   r   set_attributes_from_nameget_attname_columnr2   r   
ask_renamerG   r=  r   r^   rT   )r   r   r   r   r   r   r   new_model_staterK   	field_decr   r  rem_field_name	old_fieldold_field_dec
old_rel_toold_db_columns                    r   rk   z+MigrationAutodetector.create_renamed_fields  s{   "$,,11317.02
 8	"-Iz: "0044J'N #oo44Y5NOO"mm229j3HIO#--j9E--e4IAG!4!44B -"=~~ !I-.J2N / 9 9. II$($9$9)$DM**!..44 M!$44%21%5d%;
%)@)@@595L5LZ5XM!,T266~F$-$@$@$B1$EM$	1 &a*i!n< q!1]K$Q<(  ??55&
E !33::$1$2$-$7$7$2$-$.$)$.	!" +11!. O +..	:z/RS !/ !// ):z A "[-"8	"r&   c           	         | j                   D ]  \  }}}}}}}}||j                  k7  r?|j                         }	||	_        | j	                  |t        j                  |||	             | j	                  |t        j                  |||             | j                  j                  |||f       | j                  j                  |||f        y)z Generate RenameField operations.r  )r   r   r   N)r>  r=  clonerJ   r   r   r   RenameFieldr   r   r^   )
r   r   r  rem_db_columnrF  r   r   rK   r   altered_fields
             r   rp   z-MigrationAutodetector.generate_renamed_fields  s     $$!	I 	

 / %%3"""))#-++ &&)+' &&~~'VW##Y
J$GHC!	Ir&   c                     t        | j                  | j                  z
        D ]  \  }}}| j                  |||        y)zMake AddField operations.N)rD   r   r   _generate_added_fieldr   s       r   ru   z+MigrationAutodetector.generate_added_fields  sG    17$"5"552
 	J-Iz: &&y*jI	Jr&   c           	         | j                   j                  ||f   j                  |      }|||dfg}|j                  rC|j                  j                  r-|j                  | j                  |||| j                                t        j                  t        j                  t        j                  f}|j                  xsV |j                         xsD |j                  xs6 |j                  xr |j                  xs t        ||      xr |j                   }|sk|j#                         }t        ||      r.|j$                  r"| j&                  j)                  ||      |_        n!| j&                  j-                  ||      |_        |j.                  rM|j*                  t        j0                  ur1t3        |j*                        r| j&                  j5                  ||       | j7                  |t9        j:                  ||||      |       y )NFr   rJ   rK   preserve_defaultr   )r   r   r@  rE   r   r   r   	DateFieldDateTimeField	TimeFieldnullhas_defaultr1  blankempty_strings_allowedr/   auto_nowrL  auto_now_addr   ask_auto_now_add_additiondefaultask_not_null_additionuniqueNOT_PROVIDEDcallable$ask_unique_callable_default_additionr   r   r   )r   r   r   r   rK   r   time_fieldsrT  s           r   rQ  z+MigrationAutodetector._generate_added_field  s   $$Y
%:;EEjQ"J
EBC%"4"4":":66MM	 '')=)=v?O?OPJJ C  "C!!C ; ; ;C 5+.A5>> 	  KKME%-%2D2D $ I I
! !% E E
! LLV%8%88'OO@@ZX%!1	 & 	 		
r&   c                     t        | j                  | j                  z
        D ]  \  }}}| j                  |||        y)zMake RemoveField operations.N)rD   r   r   _generate_removed_fieldr   s       r   rt   z-MigrationAutodetector.generate_removed_fields7  sG    17$"5"552
 	L-Iz: ((J
K	Lr&   c           	      j    | j                  |t        j                  ||      |||df|||dfg       y )Nr/  r   r   r   )r   r   r   r   s       r   rg  z-MigrationAutodetector._generate_removed_field>  sM    ""% J
4EFJ
4IJ 	 	
r&   c           
      d   t        | j                  | j                  z        D ]   \  }}}| j                  j	                  ||f|      }| j
                  j	                  |||f|      }| j                  j                  ||f   j                  |      }| j                  j                  ||f   j                  |      }g }t        |d      rt        |j                  dd      rt        |j                  j                  ||      }	|	| j                  v r%|j                  j                  |j                  _        t        |j                  dd      }
|
r^|	|
fz   }|| j
                  v rJ|j                  j                  |j                  _        |j                  j                  |j                  _        t        |dd      }|r||f}t!        |D cg c]"  }| j
                  j	                  ||fz   |      $ c}      |_        t!        |j$                  D cg c]"  }| j
                  j	                  |	|fz   |      $ c}      |_        |j'                  | j)                  |||| j                               t        |d      rkt        |j                  dd      rTt        |j                  j*                  ||      }	|	| j                  v r%|j                  j*                  |j                  _        | j-                  |      }| j-                  |      }||k7  s||k(  s|j.                  xr |j.                  }|j.                   xr |j.                   }|s|rd}|j0                  rp|j0                  sd|j3                         sT|j.                  sH|j5                         }| j6                  j9                  ||      }|t        j:                  ur||_        d}n|}| j?                  |tA        jB                  ||||	      |
       | jE                  |||       | jG                  |||        yc c}w c c}w )zp
        Make AlterField operations, or possibly RemovedField/AddField if alter
        isn't possible.
        rE   r   Nr   from_fieldsr   TFrS  r   )$rD   r   r   r   r]   rT   r   r   r@  r   r:   r   rE   r   r   r   r1   rj  	to_fieldsr   r   r   r)   r1  rX  rY  rL  r   ask_not_null_alterationrb  r_  r   r   r   rg  rQ  )r   r   r   r   r   old_field_namerG  	new_fieldr   
rename_keyremote_field_nameto_field_rename_keyrj  from_rename_key
from_fieldto_fieldrH  new_field_decboth_m2mneither_m2mrT  rK   new_defaults                          r   rv   z-MigrationAutodetector.generate_altered_fieldsN  s-   
 28$"5"552
 p	R-Iz: "0044J'N "0044J
3ZN ..y./HISSI ,,Y
-BCMMI L y.1g&&7 .**00)Z
 !4!443<3I3I3O3OI**0$+I,B,BLRV$W!$*48I7K*K'*d.A.AA 8A7M7M7S7S	..4%22== "..9 &iE'0*&=O,1
 /:	 !+ !//33 /:- ?-I) +0 -6,?,? ( !//33J(4LhW+I' ##::!"!	 y.1g&&	47 .**22Iz
 !4!445>5K5K5S5SI**2 11)<M 11)<M -.J2N$11Li6L6L"+"8"88WAWAW=W{'+$! ) ) 5 5 7 ) 6 6 ) 1&*oo&M&M&
' 'f.A.AA,7EM/4, )&&!"--'1!+"'-=	 &2 ' 	 00J
S..y*jQap	RXs   
'P(
'P-
c                    t         j                  j                  }t        t              | _        t        | j                        D ]  \  }}| j                  j                  ||f|      }| j                  j                  ||f   }| j                  j                  ||f   }|j                  |   }|j                  |   }|D 	cg c]	  }	|	|vs|	 }
}	|D 	cg c]	  }	|	|vs|	 }}	g }g }g }|
D ]  }|j                         }|d   j                  d      }|D ]h  }|j                         }|d   j                  d      }||k(  s-||k7  s3|j!                  ||d f       |j!                  |       |j!                  |       j  | j#                  t         j$                  j                        D ]  \  }}}}}||k7  s||k7  r|j'                  |      }|D ]  }g }|
D ]T  }|j                         \  }}}|r|j(                  t	        |      k(  s3t+        |      ddhk(  sD|j!                  |       V t-        |      dk(  sm|d   } |j!                  |        |j!                  d | j.                  |f       | j
                  ||f   j!                  |         |
D 	cg c]	  }	|	|vs|	 }
}	|D 	cg c]	  }	|	|vs|	 }}	| j0                  j3                  ||f|
||di        y c c}	w c c}	w c c}	w c c}	w )NrB   rJ   rH   r.   r   )added_indexesremoved_indexesrenamed_indexes)r   r  option_namer   r0   renamed_index_together_valuesrD   r   r   r]   r   r   r   r\   r-   rF   rG   $_get_altered_foo_together_operationsr   r  rH   rU   r   rJ   rR   r   )!r   r}  r   r   r   r   rD  old_indexesnew_indexesidxrz  r{  r|  remove_from_addedremove_from_removed	new_indexnew_index_decnew_index_name	old_indexold_index_decold_index_name	old_value	new_valueindex_together_app_labelindex_together_model_namer   removed_valuesremoved_index_togetherrenamed_index_together_indexes_r7   r@   renamed_indexs!                                    r   rl   z,MigrationAutodetector.create_altered_indexes  ss    ))55-8->*%+D,@,@%A S	!Iz!0044J'N #oo44Y5NOO"mm229j3HIO)11+>K)11+>K,7RS3k;QSRMR.9TsS=SsTOT O ""$* >	 ) 5 5 7!.q!1!5!5f!=!0 
>I$-$9$9$;M%21%5%9%9&%AN &6*n<'..PT/UV)00;+229=
>>* ::--99#9 ()
 !99!%>>!*!5!5i!@.< 9*572%2 M	*3*?*?*A4 !% ) 0 0D9O4P P #F/A A:AA)LM 9:a?(Fq(I)00?'..!=#5#57MN ::46OO &!78)9#9N  -;L0LM   /#=P2PO    ''
+)6+:+:.WS	 STts0   	K'K'	K,K,	K1&K10	K6:K6c           	          | j                   j                         D ]9  \  \  }}}|d   D ])  }| j                  |t        j                  ||             + ; y )Nrz  r  )rR   r3   r   r   r  r   r   r   alt_indexesr  s        r   rz   z,MigrationAutodetector.generate_added_indexes  sc    484H4H4N4N4P 	0#Y
[$_5 ""''#-#	r&   c           	          | j                   j                         D ]C  \  \  }}}|d   D ]3  }| j                  |t        j                  ||j
                               5 E y )Nr{  r/  )rR   r3   r   r   RemoveIndexrJ   r  s        r   ro   z.MigrationAutodetector.generate_removed_indexes)  sh    484H4H4N4N4P 	0#Y
[$%67 ""**#-"ZZ	r&   c                     | j                   j                         D ]?  \  \  }}}|d   D ]/  \  }}}| j                  |t        j                  ||||             1 A y )Nr|  )r   r   r   
old_fields)rR   r3   r   r   RenameIndex)r   r   r   r  r  r  r  s          r   rq   z.MigrationAutodetector.generate_renamed_indexes4  su    484H4H4N4N4P 	0#Y
[>I!? :
 ""**#-!/!/#-		r&   c                    t         j                  j                  }t        | j                        D ]  \  }}| j
                  j                  ||f|      }| j                  j                  ||f   }| j                  j                  ||f   }|j                  |   }|j                  |   }|D 	cg c]	  }	|	|vs|	 }
}	|D 	cg c]	  }	|	|vs|	 }}	| j                  j                  ||f|
|di        y c c}	w c c}	w )N)added_constraintsremoved_constraints)r   r  r}  rD   r   r   r]   r   r   r   r\   rS   r   )r   r}  r   r   r   r   rD  old_constraintsnew_constraintscadd_constraintsrem_constraintss               r   rm   z0MigrationAutodetector.create_altered_constraintsC  s    ..::%+D,@,@%A 	!Iz!0044J'N #oo44Y5NOO"mm229j3HIO-55kBO-55kBO*9VQQo=UqVOV*9VQQo=UqVOV$$++
+-</>.	 WVs   ,	C56C5 	C:
C:c           	          | j                   j                         D ]9  \  \  }}}|d   D ])  }| j                  |t        j                  ||             + ; y )Nr  r  )rS   r3   r   r   r  r   r   r   alt_constraintsr  s        r   r{   z0MigrationAutodetector.generate_added_constraintsZ  sm     #66<<>	  
-.AB 
"",,#-#-		r&   c           	          | j                   j                         D ]C  \  \  }}}|d   D ]3  }| j                  |t        j                  ||j
                               5 E y )Nr  r/  )rS   r3   r   r   RemoveConstraintrJ   r  s        r   rn   z2MigrationAutodetector.generate_removed_constraintsh  sq     #66<<>	  
-.CD 
""//#-'__		r&   c                 
   d }t        j                  d      rj                  j                  }nW|j                  | |f   }|j	                         D ]3  \  \  }}}t        fd|j                         D              s,| d| } n t        dd       }	|	d}
|	}nt        || |      \  }
}|
|d dfg}t        j                  dd       r9t        j                  j                  | |      \  }}|j                  ||d df       |S )Nr   c              3   <   K   | ]  }|j                   k(    y wr   )rE   )r*   r+  rK   s     r   r,   zJMigrationAutodetector._get_dependencies_for_foreign_key.<locals>.<genexpr>~  s$      % ]777s   r   swappable_settingr   Tr   )r:   rE   r   r   r3   r   r   r   r   r   rG   )r   r   rK   project_stateremote_field_modelr   remote_app_labelremote_model_namerH   r  dep_app_labeldep_object_namer   through_app_labelthrough_object_names     `            r   r   z7MigrationAutodetector._get_dependencies_for_foreign_keyv  s<   !5%%w/!&!3!3!9!9%//	:0EFIAJAR =5!#4v )/  -=+=Q?P>Q)R& $E+>E()M/O-=".*M?
 'tDE5%%y$75E""**622
 !24Gt TUr&   c              #      K   t         j                        D ]X  \   j                  j                  f      } j                  j
                  |f   } j                  j
                  f   }|j                  j                  |      }|r$|D ch c]  }t         fd|D               c}n	t               }|j                  j                  |      }|rt        |      n	t               }||k7  sg }|D ]k  }	|	D ]d  }
|j                  |
      }|j                  s!|j                  j                  s8|j                   j                  | j                               f m |||f [ y c c}w w)Nc              3   \   K   | ]#  }j                   j                  |f|       % y wr   )rT   r]   )r*   nr   r   r   s     r   r,   zMMigrationAutodetector._get_altered_foo_together_operations.<locals>.<genexpr>  s3       ++//J0JANs   ),)rD   r   r   r]   r   r   r   r\   r1   rU   r@  rE   r   r   r   )r   r}  r   r   rD  r  ra  r  r   foo_togethersr   rK   r   r   s   `           @@r   r  z:MigrationAutodetector._get_altered_foo_together_operations  s    %+D,@,@%A ,	!Iz!0044J'N #oo44Y5NOO"mm229j3HIO (//33K@I  #,
 	  !'  U  (//33K@I*3III%!%. M&3 

 / 9 9* E --%2D2D2J2J(// $ F F$-$.$)$(MM	!"
   M,	s&   BE=E84AE=9)E=#E=:AE=c           
      @   | j                  |j                        D ]{  \  }}}}}|t        j                  k(  r"|D ch c]  }|| j                  ||f   vr| }}|j                  |      }|s|sT| j                  | |dd|i|j                  |i|       } y c c}w NrJ   r   r   )r  r}  r   r   r~  r  r   )	r   r   r  r  r   r   r   r+   removal_values	            r   &_generate_removed_altered_foo_togetherz<MigrationAutodetector._generate_removed_altered_foo_together  s     66y7L7LM	 
J999 "+==i>STU 	  &229=M	"" ',5,A,A=+Q ". # !	s   Bc                 B    | j                  t        j                         y r   )r  r   r   r   s    r   rr   z>MigrationAutodetector.generate_removed_altered_unique_together  s    33J4R4RSr&   c                 B    | j                  t        j                         y r   )r  r   r   r  s    r   rs   z=MigrationAutodetector.generate_removed_altered_index_together  s    33J4Q4QRr&   c           
          | j                  |j                        D ]G  \  }}}}}|j                  |      }||k7  s | j                  | |dd|i|j                  |i|       I y r  )r  r}  r  r   )r   r   r  r  r   r   r   r  s           r   _generate_altered_foo_togetherz4MigrationAutodetector._generate_altered_foo_together  s     66y7L7LM	 
%229=MM)""T:T)2G2G1ST!- # 	r&   c                 B    | j                  t        j                         y r   )r  r   r   r  s    r   rx   z6MigrationAutodetector.generate_altered_unique_together  s    ++J,J,JKr&   c                 B    | j                  t        j                         y r   )r  r   r   r  s    r   ry   z5MigrationAutodetector.generate_altered_index_together  s    ++J,I,IJr&   c                    | j                   j                  | j                  | j                        }t	        |      D ]  \  }}| j
                  j                  ||f|      }| j                  j                  ||f   }| j                  j                  ||f   }|j                  j                  d      }|j                  j                  d      }||k7  s| j                  |t        j                  ||              y )Ndb_table)rJ   table)r   unionr   r   rD   r   r]   r   r   r   r\   r   r   AlterModelTable)	r   models_to_checkr   r   r   r   rD  old_db_table_namenew_db_table_names	            r   r|   z/MigrationAutodetector.generate_altered_db_table  s    ..44  $":":
 &,O%< 	!Iz!0044J'N #oo44Y5NOO"mm229j3HIO / 7 7 ; ;J G / 7 7 ; ;J G $55""..'/	r&   c                    | j                   j                  | j                  | j                        }t	        |      D ]  \  }}| j
                  j                  ||f|      }| j                  j                  ||f   }| j                  j                  ||f   }|j                  j                  d      }|j                  j                  d      }||k7  s| j                  |t        j                  ||              y )Ndb_table_comment)rJ   table_comment)r   r  r   r   rD   r   r]   r   r   r   r\   r   r   AlterModelTableComment)	r   r  r   r   r   r   rD  old_db_table_commentnew_db_table_comments	            r   rj   z7MigrationAutodetector.generate_altered_db_table_comment  s    ..44  $":":
 &,O%< 	!Iz!0044J'N #oo44Y5NOO"mm229j3HIO#2#:#:#>#>?Q#R #2#:#:#>#>?Q#R #';;""55'&:	r&   c                    | j                   j                  | j                  | j                  | j                  | j
                  z  | j                  | j                  z        }t        |      D ]  \  }}| j                  j                  ||f|      }| j                  j                  ||f   }| j                  j                  ||f   }|j                  j                         D ci c]  \  }}|t         j"                  v r|| }	}}|j                  j                         D ci c]  \  }}|t         j"                  v r|| }
}}|	|
k7  s| j%                  |t'        j                   ||
              yc c}}w c c}}w )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).
        )rJ   r\   N)r   r  r   r   rX   rY   rV   r[   rD   r   r]   r   r   r   r\   r3   r	   ALTER_OPTION_KEYSr   r   )r   r  r   r   r   r   rD  r=   r+   old_optionsnew_optionss              r   rh   z.MigrationAutodetector.generate_altered_options*  s    ..44  $$##d&9&99$"9"99
 &,O%< 	!Iz!0044J'N #oo44Y5NOO"mm229j3HIO #2"9"9"?"?"AC+=== U
K  #2"9"9"?"?"AC+=== U
K 
 k)""00' +#	
s   $E6#E<c           
      \   t        | j                        D ]  \  }}| j                  j                  ||f|      }| j                  j
                  ||f   }| j                  j
                  ||f   }|j                  j                  d      |j                  j                  d      k7  sg }|j                  j                  d      r"|j                  |||j                  d   df       | j                  |t        j                  ||j                  j                  d            |        y )Nr   Tr  r   )rD   r   r   r]   r   r   r   r\   rG   r   r   r   )r   r   r   r   r   rD  r   s          r   rw   z<MigrationAutodetector.generate_altered_order_with_respect_toR  s3   %+D,@,@%A 	!Iz!0044J'N #oo44Y5NOO"mm229j3HIO&&**' ((,,-DEF
  ""**../FG ''%&+334KL 	 ""66'.=.E.E.I.I3/ ". # 	-	r&   c                 |   t        | j                        D ]  \  }}| j                  j                  ||f|      }| j                  j
                  ||f   }| j                  j
                  ||f   }|j                  |j                  k7  st| j                  |t        j                  ||j                                y )N)rJ   r  )rD   r   r   r]   r   r   r   r  r   r   AlterModelManagers)r   r   r   r   r   rD  s         r   ri   z/MigrationAutodetector.generate_altered_managerst  s    %+D,@,@%A 	!Iz!0044J'N #oo44Y5NOO"mm229j3HIO''?+C+CC""11'!0!9!9	r&   c           	      j   |j                         }i }t        |j                               D ]3  \  }}|s
d}|D ]  }	|	d   |k(  s|	} n |9| j                  j	                  |      s|D ]  }
|df|||
j
                  f<    ||= Z|d}n| j                  |d         xs ddz   }t        |      D ]  \  }}
|dk(  r|r|
j                  j                  |       d|z  g}|r|j                  |       n;|dk(  r|s|j                  d       n"|j                  |
j                         dd        dj                  |      }||f|||
j
                  f<   |dz  }||
_         6 |j                         D ]8  }|D ]1  }
|
j                  D cg c]  }|j                  ||       c}|
_        3 : |S c c}w )	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   r  )r   r0   r3   r   ask_initialrJ   parse_number	enumerater   rG   suggest_namejoinr   r]   )r   r%   r!   r$   leavesname_mapr   r   app_leafleafr   next_numberinew_name_partsr   r&  s                   r   r   z'MigrationAutodetector.arrange_for_graph  s    !!#%)'--/%: #	*!IzH 7i'#H
 (C(CI(N!+ UI=F<THi89UI&#00!=BaG )* 5 *96h**11(;"(;"6!7!")).9!VH")))4")))*@*@*B4C*HI88N39BH8M)Y^^45q !)	*-#	*J "..* 	J' 	090F0F*+,HLLA&*	&	
 *s   	F0c           	         i }|j                         D ]I  \  }}|D ]?  }|j                  D ].  \  }}|j                  |t                     j	                  |       0 A K t        |      }	d}
|
|	k7  r>t        |	      }
 |	j
                  |	D cg c]  }|j                  |d       c}  |
|	k7  r>t        |      D ]
  }||	vs||=  |S c c}w )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.
        Nr   )r3   r   r   rU   r^   r   r]   r0   )r   r%   
app_labelsapp_dependenciesr   r   r   r  rJ   required_appsold_required_appss              r   r    z#MigrationAutodetector._trim_to_apps  s     %,]]_ 	U!Iz' U	+4+A+A U'M4$//	35AEEmTUU	U J =0 #M 2 M  GTU)"&&y"5U  =0 g 	'I-I&	'  Vs   C	c                     t        j                  d|      x}rt        |d         S t        j                  d|      }|rt        |d         S y)u   
        Given a migration name, try to extract a number from the beginning of
        it. For a squashed migration such as '0001_squashed_0004…', return the
        second number. If no number is found, return None.
        z.*_squashed_(\d+)r.   z^\d+r   N)researchintmatch)clsrJ   squashed_matchr  s       r   r  z"MigrationAutodetector.parse_number  sO      YY';TBB>B~a())$'uQx= r&   r   )NNN)NN)NF)7r   
__module____qualname____doc__r   r%   r)   rM   r   rb   rc   staticmethodr   r~   r}   r   r   r   r   ra   re   rg   rd   rf   rk   rp   ru   rQ  rt   rg  rv   rl   rz   ro   rq   rm   r{   rn   r   r  r  rr   rs   r  rx   ry   r|   rj   rh   rw   ri   r   r    classmethodr  r   r&   r   r   r      sO   
G
$LiV
.( 
Y 
Yp"d.$=K~R.A"FAFB_B<"|#IJJ1
fL
 uRnWr		.    D-^2TS LK*,&P D 2h6  r&   r   )r4   r  collectionsr   	itertoolsr   django.confr   	django.dbr   django.db.migrationsr   django.db.migrations.migrationr   &django.db.migrations.operations.modelsr	   django.db.migrations.optimizerr
   django.db.migrations.questionerr   django.db.migrations.utilsr   r   r   django.utils.topological_sortr   r   r   r&   r   <module>r     sB     	 #     + 4 D = ? 
 BJ Jr&   