
    hPH                        S SK r S SKrS SKJrJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJr  S SKJr  S SKJrJrJr  S SKJr   " S S\" SS5      5      r " S S5      r g)    N)defaultdict
namedtuple)Enum)TopologicalSorter)chain)settings)models)
operations)	Migration)AlterModelOptions)MigrationOptimizer)MigrationQuestioner)COMPILED_REGEX_TYPERegexObjectresolve_relation)cached_propertyc                   J    \ rS rSr " S S\5      r\S 5       r\S 5       rSr	g)OperationDependency   c                   (    \ rS rSrSrSrSrSrSrSr	g)	OperationDependency.Type   r                N)
__name__
__module____qualname____firstlineno__CREATEREMOVEALTERREMOVE_ORDER_WRTALTER_FOO_TOGETHER__static_attributes__r       S/var/www/html/env/lib/python3.13/site-packages/django/db/migrations/autodetector.pyTyper      s    r(   r*   c                 6    U R                   R                  5       $ N)
model_namelowerselfs    r)   model_name_lower$OperationDependency.model_name_lower!       $$&&r(   c                 6    U R                   R                  5       $ r,   )
field_namer.   r/   s    r)   field_name_lower$OperationDependency.field_name_lower%   r3   r(   r   N)
r   r   r    r!   r   r*   r   r1   r6   r'   r   r(   r)   r   r      s6    t  ' ' ' 'r(   r   z$app_label model_name field_name typec                   t   \ rS rSrSrS6S jrS7S jrS rS rS8S jr	S	 r
S
 r\S 5       rS6S jrS rS rS rS9S jrS rS rS rS rS rS rS rS rS rS rS rS rS rS r S r!S  r"S! r#S" r$S# r%S$ r&\S% 5       r'S& r(S' r)S( r*S) r+S* r,S+ r-S, r.S- r/S. r0S/ r1S0 r2S1 r3S6S2 jr4S3 r5\6S4 5       r7S5r8g):MigrationAutodetector*   a  
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l         X l        U=(       d
    [        5       U l        UR                   VVs1 s H  u  pEUiM	     snnU l        g s  snnf r,   )
from_stateto_stater   
questionerr	   existing_apps)r0   r<   r=   r>   appmodels         r)   __init__MigrationAutodetector.__init__7   s@    $ $=(;(=4>4E4EF4Ejcc4EFFs   Ac                 |    U R                  X15      nU R                  XQU5      nU(       a  U R                  XR5      nU$ )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)r0   graphtrim_to_appsconvert_appsmigration_namechangess         r)   rL   MigrationAutodetector.changes=   s>     &&|;((H((?Gr(   c           
        ^  [        U[        5      (       a"  U Vs/ s H  nT R                  U5      PM     sn$ [        U[        5      (       a  [        U 4S jU 5       5      $ [        U[        5      (       a5  UR                  5        VVs0 s H  u  p2UT R                  U5      _M     snn$ [        U[        R                  5      (       aA  UR                  T R                  UR                  5      T R                  UR                  5      4$ [        U[        5      (       a  [        U5      $ [        U[        5      (       a  U$ [        US5      (       a  UR                  5       n[        U[         R"                  5      (       a  USS nUu  pVnUU Vs/ s H  nT R                  U5      PM     snUR                  5        VVs0 s H  u  p2UT R                  U5      _M     snn4$ U$ s  snf s  snnf s  snf s  snnf )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   F   >#    U  H  nTR                  U5      v   M     g 7fr,   )deep_deconstruct).0valuer0   s     r)   	<genexpr>9MigrationAutodetector.deep_deconstruct.<locals>.<genexpr>R   s     G3%..u553   !deconstructr   N)
isinstancelistrP   tupledictitems	functoolspartialfuncargskeywordsr   r   typehasattrrV   r	   Field)r0   objrR   keydeconstructedpathr_   kwargss   `       r)   rP   &MigrationAutodetector.deep_deconstructI   s    c4  >ABcUD))%0cBBU##G3GGGT""HK		T*#C..u55TTY..//%%chh/%%cll3 
 011s##T"" JS-((OO-M#v||,, -ab 1!.D;?@4%&&u-4@EK\\^T^zsd++E22^T  J; C U* ATs   G(G-G3G8
c                    / n[        UR                  5       5       Hh  u  p4U R                  U5      nUR                  (       a0  UR                  R                  (       a  US   R                  SS5        UR                  U5        Mj     U$ )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).
r   toN)sortedr[   rP   remote_fieldrA   popappend)r0   fields
fields_defnamefielddeconstructions         r)   only_relation_agnostic_fields3MigrationAutodetector.only_relation_agnostic_fieldso   sr     
!&,,.1KD!2259N!!e&8&8&>&>q!%%dD1n-	 2
 r(   c                    0 U l         0 U l        0 U l        0 U l        [	        5       U l        [	        5       U l        [	        5       U l        [	        5       U l        [	        5       U l	        [	        5       U l
        U R                  R                  R                  5        H  u  u  p4nUR                  R                  SS5      (       d  U R                  R!                  X445        MG  X0R                  R"                  ;  d  Mb  UR                  R                  S5      (       a  U R                  R!                  X445        M  U R
                  R!                  X445        M     U R$                  R                  R                  5        H  u  u  p4nUR                  R                  SS5      (       d  U R                  R!                  X445        MG  X0R                  R"                  ;  d  U(       d  Mi  X1;   d  Mp  UR                  R                  S5      (       a  U R                  R!                  X445        M  U R                  R!                  X445        M     U R                  R'                  5         U R$                  R'                  5         U R)                  5         U R+                  5         U R-                  5         U R/                  5         U R1                  5         U R3                  5         U R5                  5         U R7                  5         U R9                  5         U R;                  5         U R=                  5         U R?                  5         U RA                  5         U RC                  5         U RE                  5         U RG                  5         U RI                  5         U RK                  5         U RM                  5         U RO                  5         U RQ                  5         U RS                  5         U RU                  5         U RW                  5         U RY                  5         U R[                  5         U R]                  5         U R_                  U5        U Ra                  5         U Rb                  $ )a  
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)2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	   r[   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_fieldsgenerate_added_fieldsgenerate_altered_fields&generate_altered_order_with_respect_to generate_altered_unique_togethergenerate_added_indexesgenerate_added_constraintsgenerate_altered_db_table_sort_migrations_build_migration_list_optimize_migrations
migrations)r0   rJ   rH   	app_labelr-   model_states         r)   rE   %MigrationAutodetector._detect_changes}   sX   & %'!!#%   "e!e"%%!e!e"%%48OO4J4J4P4P4R0#Y[&&**9d;;''++Y,CD//";";;&&**733''++Y,CD''++Y,CD 5S 59MM4H4H4N4N4P0#Y[&&**9d;;''++Y,CD//";";;!:&&**733''++Y,CD''++Y,CD 5Q 	446224 	$$& 	!!#((* 	$$&$$&%%'%%'%%'&&(..0 	""$ 	##%'')))+%%'$$&%%'557$$&""$$$&335--/##%'')&&(""5)!!#r(   c                 p   U R                   U R                  -  U l        U R                  U R                  -  U l        U R                  U R                  -  U l        0 U l	        U R                   VVVs1 s HP  u  pU R                  R                  XR                  R                  X4U5      4   R                    H  nXU4iM	     MR     snnnU l        U R                   VVVs1 s H5  u  pU R                   R                  X4   R                    H  nXU4iM	     M7     snnnU l        gs  snnnf s  snnnf )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)r   r   kept_model_keysr   r   kept_proxy_keysr   r   kept_unmanaged_keysthrough_usersr<   r	   renamed_modelsr   rp   old_field_keysr=   new_field_keysr0   r   r-   r5   s       r)   r   *MigrationAutodetector._prepare_field_lists   s*     $22T5H5HH#22T5H5HH#'#:#:T=T=T#T  *.)=)=
)=%	"oo44..22I3JJWWf
 J/ 0)=
 *.)=)=
)=%	"mm2293HIPP
 J/P 0)=


s   2AD*#<D1c                    [        U R                  5       H  u  pU R                  R                  X4U5      nU R                  R
                  X4   nUR                  R                  5        Hh  u  pV[        US5      (       d  M  [        UR                  SS5      (       d  M6  [        UR                  R                  X5      nUUU4U R                  U'   Mj     M     g)zThrough model map generation.rm   throughN)rl   r   r   r   r<   r	   rp   r[   rb   getattrrm   r   r   r   )r0   r   r-   old_model_nameold_model_stater5   rs   through_keys           r)   r   1MigrationAutodetector._generate_through_model_map   s    %+D,?,?%@!I!0044'N #oo44Y5NOO%4%;%;%A%A%C!
5.11g&&	47 7 #3**22I#K "&"7D&&{3 &D &Ar(   c                     U R                   S:w  a  U S4$ [        [        U R                  5      R	                  S5      u  p[        UUR                  5       U R                  U R                  5      S4$ )zX
Return the resolved dependency and a boolean denoting whether or not
it was swappable.
__setting__F.T)	r   r   r   r-   splitr   r.   r5   ra   )
dependencyresolved_app_labelresolved_object_names      r)   _resolve_dependency)MigrationAutodetector._resolve_dependency  sy     =0u$$3:j++4

%* 	1  "$**,%%	 
 	
r(   c           
      f   0 U l         [        S U R                  R                  5        5       5      nSnU(       Gav  [	        U R                  5       GH  n/ n[        5       n[        U R                  U   5       GH  nSn[        5       n	UR                   GH  n
U
nU R                  U
5      u  pU
R                  U:w  d  M+  U R                  R                  U
R                  / 5       H  nU R                  X5      (       d  M  Sn  O   U(       d    GOU(       a)  U	R                  UR                  UR                  45        M  U
R                  U R                   ;   aD  U	R                  U
R                  U R                   U
R                     S   R                  45        GM
  U(       aw  U(       aP  UR                  U
R                  5      (       a0  U	R                  UR                  U
R                  5      S   5        GMh  U	R                  U
R                  S45        GM  SnGM     U(       a5  UR!                  U5        UR#                  U	5        U R                  U   S	 GM    O   U(       d
  U(       d  GM$  U R                  U   (       a  U(       a  [%        S[&        4/ / S.5      nU" S	[)        U R                   R                  U/ 5      5      S
-   -  U5      n[        U5      Ul        X_l        X@R.                  ;  Ul        U R                   R3                  U/ 5      R!                  U5        SnGM  XPR                  U   -   U R                  U'   GM      [        S U R                  R                  5        5       5      nUU:X  a"  U(       d  SnO[5        SU R                  -  5      eUnU(       a  GMu  gg)a  
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   8   #    U  H  n[        U5      v   M     g 7fr,   lenrQ   xs     r)   rS   >MigrationAutodetector._build_migration_list.<locals>.<genexpr>2  s     I&Hc!ff&H   FTr   	__first__r   )r
   dependencieszauto_%ir   c              3   8   #    U  H  n[        U5      v   M     g 7fr,   r   r   s     r)   rS   r     s     Q.Pc!ff.Pr   z)Cannot resolve operation dependencies: %rN)r   sumrz   valuesrl   r~   rX   
_auto_depsr   r   r   check_dependencyr   r-   rr   
leaf_nodesro   updatera   r   r   r   r
   r?   initial
setdefault
ValueError)r0   rH   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   +MigrationAutodetector._build_migration_list&  s@    Id&?&?&F&F&HII	 $D$=$=>	"u!%d&?&?	&J!KI%)N-0U*(33
 (+040H0H0M-==I5 483L3L3P3P #r4 $(#8#8#N#N5:N$)4 $2 %#3$:$>$>,8,B,B,8,C,C)*%& &)]]doo%E$:$>$>,/MM,0OOCMM,J2,N,S,S)*%& (1 ,1U5E5Ecmm5T5T,B,F,F050@0@0OPQ0R-. -C,F,F140L-. :?m  4n &y1$++,BC 55i@C "LB  7744Y?9#''&L+-rB$
 $,%T__-@-@B-O)PST)TU%$ 15\0B-.5++4<N<N+N(229bAHHR$)	 $&?&?	&JJ 11)<k ?p Qd.G.G.N.N.PQQKg%  $I$C334  "GQ gr(   c                   ^ ^ [        T R                  R                  5       5       H  u  p[        5       nU Hk  nUR	                  U5        UR
                   HG  mT R                  T5      S   mTR                  U:w  a  M)  UR                  " U/UU 4S jU 5       Q76   MI     Mm     [        UR                  5       5      T R                  U'   M     g)zc
Reorder to make things possible. Reordering may be needed so FKs work
nicely inside the same app.
r   c              3   Z   >#    U  H   nTR                  UT5      (       d  M  Uv   M"     g 7fr,   )r   )rQ   r   r   r0   s     r)   rS   9MigrationAutodetector._sort_migrations.<locals>.<genexpr>  s"      QCq43H3HC3PCs   +	+N)
rl   rz   r[   r   r   r   r   r   rX   static_order)r0   r   opstsopr   s   `    @r)   r   &MigrationAutodetector._sort_migrations  s    
 %T%>%>%D%D%FGNI"$Br
==C 2237:C}}	1 FF2R QC QR )  488I3JD%%i0 Hr(   c                    U R                   R                  5        HB  u  p[        X"SS  5       H+  u  p4UR                  R	                  XR
                  45        M-     MD     U R                   R                  5        H/  nU H&  n[        [        UR                  5      5      Ul        M(     M1     U R                   R                  5        H7  u  pU H,  n[        5       R                  UR                  U5      Ul        M.     M9     g )Nr   )r   r[   zipr   ro   rr   r   rX   r~   r   optimizer
   )r0   r   r   m1m2	migrations         r)   r   *MigrationAutodetector._optimize_migrations  s    %)__%:%:%<!IjQR.9&&	77';< : &=
 //002J'	)-c)2H2H.I)J	& ( 3
 &*__%:%:%<!I'	'9';'D'D(()(	$ ( &=r(   c                 P  ^ TR                   cb  TR                  [        R                  R                  :X  a:  [        U[        R                  5      =(       a    UR                  TR                  :H  $ TR                   b  TR                  [        R                  R                  :X  a  [        U[        R                  5      =(       a?    UR                  TR                  :H  =(       a    [        U4S jUR                   5       5      =(       dZ    [        U[        R                  5      =(       a9    UR                  TR                  :H  =(       a    UR                  TR                  :H  $ TR                   b  TR                  [        R                  R                  :X  aZ  [        U[        R                  5      =(       a9    UR                  TR                  :H  =(       a    UR                  TR                  :H  $ TR                   cb  TR                  [        R                  R                  :X  a:  [        U[        R                   5      =(       a    UR                  TR                  :H  $ TR                   b  TR                  [        R                  R"                  :X  aZ  [        U[        R$                  5      =(       a9    UR                  TR                  :H  =(       a    UR                  TR                  :H  $ TR                   b  TR                  [        R                  R&                  :X  aq  [        U[        R(                  5      =(       aP    UR                  TR                  :H  =(       a0    UR*                  =(       d    SR-                  5       TR                  :g  $ TR                   br  TR                  [        R                  R.                  :X  aJ  [        U[        R0                  [        R2                  45      =(       a    UR                  TR                  :H  $ [5        ST< 35      e)zV
Return True if the given operation depends on the given dependency,
False otherwise.
c              3   F   >#    U  H  u  pTR                   U:H  v   M     g 7fr,   )r5   )rQ   r   yr   s      r)   rS   9MigrationAutodetector.check_dependency.<locals>.<genexpr>  s      P?Otq
--2?OrU    zCan't handle dependency )r5   ra   r   r*   r"   rW   r
   CreateModel
name_lowerr1   anyrp   AddFieldr6   r#   RemoveFieldDeleteModelr$   
AlterFieldr%   AlterOrderWithRespectToorder_with_respect_tor.   r&   AlterUniqueTogetherAlterIndexTogetherr   )r0   r   r   s     `r)   r   &MigrationAutodetector.check_dependency  sN    !!)#6#;#;#B#BB 9j&<&<= H((J,G,GG !!-#6#;#;#B#BB 9j&<&<= Q((J,G,GGQPy?O?OPP
 9j&9&9: H..*2M2MMH((J,G,GG !!-#6#;#;#B#BB 9j&<&<= H..*2M2MMH((J,G,GG !!)#6#;#;#B#BB 9j&<&<= H((J,G,GG !!-#6#;#;#A#AA 9j&;&;< H..*2M2MMH((J,G,GG !!-#6#;#;#L#LL 9j&H&HI /((J,G,GG/44:AAC../ !!-#6#;#;#N#NN 33Z5R5RS H ((J,G,GG ZIJJr(   c                     U=(       d    / Ul         U(       a-  U R                  R                  U/ 5      R                  SU5        g U R                  R                  U/ 5      R	                  U5        g )Nr   )r   rz   r   insertro   )r0   r   r   r   	beginnings        r)   add_operation#MigrationAutodetector.add_operation  sX      ,1r	%%00B?FFq)T%%00B?FFyQr(   c                     U R                   R                  U   nUR                   Vs1 s H&  n[        U[        5      (       a  UOUR
                  iM(     nnUS   < SUS   < 3nUR                  R                  S5      (       d<  SU;   d6  SU;   d0  [        R                  R                  5       UR                  5       :X  a  SUS   -   SUS   -   4$  U$ s  snf ! [         a     U$ f = f)zd
Place potential swappable models first in lists of created models (only
real way to solve #22783).
r   r   r   	swappableAbstractUserAbstractBaseUser___)r=   r	   basesrW   strr   r   r   r   AUTH_USER_MODELr.   LookupError)r0   itemr   base
base_namesstring_versions         r)   swappable_first_key)MigrationAutodetector.swappable_first_key  s    
	--..t4K (---D #4--4==@-   )-Qa9N##''44!Z/%3++113~7K7K7MMQa99 N
   		s#   'C -CA<C C 
C)(C)c                 0   0 U l         0 U l        U R                  U R                  -
  n[	        U5       GHY  u  p#U R
                  R                  X#4   nU R                  UR                  5      nU R                  U R                  -
  nU GH  u  pxXr:X  d  M  U R                  R                  Xx4   n	U R                  U	R                  5      n
XZ:X  d  MI  U R                  R                  X5      (       d  Mk  / n[        UR                  R                  5       5      U R
                  R                  X#4   R                  5        VVs/ s H'  nUR                  5         H  nUR                  PM     M)     snn-   nU HC  nUR                   (       d  M  UR#                  U R%                  UUUU R
                  5      5        ME     U R'                  U[(        R*                  " U	R,                  UR,                  S9US9  XR                   X#4'   U	R.                  < SU	R0                  < 3nUR.                  < SUR0                  < 3U R                  U'   U R                  R3                  Xx45        U R                  R5                  X#45          GMW     GM\     gs  snnf )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_relr   r   rl   r=   r	   ru   rp   r<   r>   ask_rename_modelrX   r   	relationsrm   is_relationextend!_get_dependencies_for_foreign_keyr  r
   RenameModelrr   r   r   remover   )r0   added_modelsr   r-   r   model_fields_defremoved_modelsrem_app_labelrem_model_namerem_model_staterem_model_fields_defr   r"  rs   rp   renamed_models_rel_keys                   r)   r   -MigrationAutodetector.generate_renamed_models6  s    !"$**T-@-@@%+L%9!I--..y/DEK#AA+BTBTU!0043F3FFN1?- -&*oo&<&<%5'O ,0+M+M'..,( (???;;+  ,.L%)+*<*<*C*C*E%F151H1H$-$92""(&(2+J2+I .7-=-=-?E	 !& 2 2 .@	 !32+J &F *0#(#4#4#4$0$7$7(,(N(N,5,6,1,0MM	)*%& *0 !.. ) * 6 6-<-A-A-8-=-=!" .: /  JX//	0EF / 9 9 / : :62 %0$9$9$/$:$:!" !334JK !//667VW //33Y4KL!i 2@ &:$Js   .Jc                 \   U R                   U R                  -  nU R                  U-
  nU R                  U-
  n[	        [        X R                  SS9[        X0R                  SS95      nU GH  u  pVU R                  R                  XV4   n0 nSn	UR                  R                  5        H  u  pUR                  (       d  M  UR                  R                  (       aG  UR                  (       a  UR                  R                  n	OUR                  R                  (       d  XU
'   [        UR                  SS5      (       d  M  XU
'   M     UR                   R#                  S5      nUR                   R#                  S5      nUR                   R#                  SS5      nUR                   R#                  SS5      n[%        XVS[$        R&                  R(                  5      /nUR*                   GHR  n[-        U[.        5      (       d  M  S	U;   d  M#  UR1                  S	S
5      u  nnUR3                  [%        UUS[$        R&                  R4                  5      5        U R6                  R                  R9                  UU45      nU R                  R                  R9                  UU45      nU(       d  M  U(       d  M  [;        UR                  5      R=                  UR                  5      R?                  UR                  5      nU H8  nUR3                  [%        UUU[$        R&                  R(                  5      5        M:     GMU     U	(       a@  UR3                  [%        / [A        XU5      QSP[$        R&                  R4                  P76 5        U RC                  U[D        RF                  " URH                  UR                  R                  5        Vs/ s H  nUS   U;  d  M  UPM     snUR                   UR*                  URJ                  S9USS9  UR                   R9                  SS5      (       d  GM  [        UR                  5       5       H  u  nnU RM                  UUUU R                  5      nUR3                  [%        XVS[$        R&                  R4                  5      5        U RC                  U[D        RN                  " UUUS9[Q        [;        U5      5      S9  M     U(       am  U RC                  U[D        RR                  " UUS9[%        UUU[$        R&                  R4                  5      [%        XVS[$        R&                  R4                  5      /S9  [        U5       Vs/ s H(  n[%        XVU[$        R&                  R4                  5      PM*     nnUR3                  [%        XVS[$        R&                  R4                  5      5        U H'  nU RC                  U[D        RT                  " UUS9US9  M)     U H'  nU RC                  U[D        RV                  " UUS9US9  M)     U(       a$  U RC                  U[D        RX                  " UUS9US9  U R                  RZ                  nXV4U R\                  ;   d  GM  UXV4   R                  5        H  u  nnU R                  R                  U   nUR                  5        Hd  u  n n!U RC                  UR^                  [D        R`                  " URH                  U U!S9[%        UUS[$        R&                  R4                  5      /S9  Mf     M     GM     gs  snf s  snf )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).
T)re   reverseNr   indexesconstraintsunique_togetherr  r   r   r   rr   rp   r   r  managers)r   r
  rx   r-   rr   rs   r  rr   r  r-   indexr-   
constraintrr   r5  )1r   r   r   r   r   rl   r  r=   r	   rp   r[   rm   rA   primary_keyparent_linkr   r   rn   r   r*   r#   r  rW   r  r   ro   r"   r<   r   r~   
differenceintersectionr   r  r
   r   rr   r7  r%  r   rX   r  AddIndexAddConstraintr  r"  r   r   r  )"r0   old_keysr(  added_unmanaged_modelsall_added_modelsr   r-   r   related_fieldsprimary_key_relr5   rs   r3  r4  r5  r  r   r  base_app_label	base_nameold_base_model_statenew_base_model_stateremoved_base_fieldsremoved_base_fielddrr   related_dependenciesr;  r=  r"  related_model_keyrelated_model_staterelated_field_namerelated_fields"                                     r)   r   -MigrationAutodetector.generate_created_modelsz  s    &&)@)@@**X5!%!8!88!C <%=%=tL)/G/GQUV
 &6!I--..y/DEKN"O%0%7%7%=%=%?!
%%%))// ,,.3.@.@.F.FO!&!3!3!?!?9>:6u119dCC5:z2 &@ "))--i8G%--11-@K)11556GNO$/$7$7$;$;'%!
 $41D1I1I1P1PL $))dC((SD[04

30B-NI ''+*% /44;;	 ,0??+A+A+E+E'3,( ,0==+?+?+C+C'3,( ,+0D0D 4 ; ;<'Z 4 ; ; *\+*<*<= , 3F.(// 3$2$-$6$7$<$<$C$C	!" 3F7 *J ##' )/jQ ,0077 &&$)) "-!3!3!9!9!;!;AQ4~5 !;
 (//%++(11
 *  $ &&**9d;;  &n&:&:&<=e#EEMM	  ##'!t5H5M5M5T5T ""''#-!#
 "&c,&7!8 #   >0 %""66'.C
 ,%&1/44;;	 ,%49L9Q9Q9X9X
" # , #>2	$ 3D $41D1I1I1P1P 3	 ! $ !''#41D1I1I1P1P
 !""''#-# "6 #  ! *
"",,#-#- "6 #  * ""22'(7 "6 #  //I&$*=*==9B):%':5%~ +/--*>*>?P*Q'=K=Q=Q=S9*M**/99&11+>+C+C%7&3 !4$-$.$($7$<$<$C$C	!"* +  >T	:m &6^z$s   	\$\$/\)c                    U R                   U R                  -
  n[        U5       GH2  u  p#U R                  R                  X#4   nUR
                  R                  S5      (       d   e[        X#S[        R                  R                  5      /nUR                   Hk  n[        U[        5      (       d  M  SU;   d  M"  UR                  SS5      u  pxUR                  [        UUS[        R                  R                  5      5        Mm     U R!                  U["        R$                  " UR&                  / UR
                  UR                  UR(                  S9US9  GM5     g)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.
ry   Nr   r   r6  r  )r   r   rl   r=   r	   r   r   r   r*   r#   r  rW   r  r   ro   r"   r  r
   r   rr   r7  )	r0   addedr   r-   r   r   r  rJ  rK  s	            r)   r   .MigrationAutodetector.generate_created_proxiesW  s<    ##d&9&99%+E]!I--..y/DEK&&**73333 $41D1I1I1P1PL $))dC((SD[04

30B-N ''+*% /44;;	 * &&$))'//%++(11 *  - &3r(   c                 V   U R                   U R                  -  nU R                  U-
  nU R                  U-
  n[	        [        U5      [        U5      5      nU GH  u  pVU R                  R                  XV4   n0 nUR                  R                  5        HY  u  pU
R                  (       d  M  U
R                  R                  (       a  XU	'   [        U
R                  SS5      (       d  MU  XU	'   M[     UR                  R                  SS5      nU(       a%  U R                  U[         R"                  " USS95        [        U5       H(  nU R                  U[         R$                  " UUS95        M*     / nU R                  R&                  nXU4   R                  5        H  u  u  nnnUR                  5        H  u  pUR)                  [+        UUU	[*        R,                  R.                  5      5        U
R0                  (       a  MM  UR)                  [+        UUU	[*        R,                  R2                  5      5        M     M     [        U5       H7  nUR)                  [+        XVU[*        R,                  R.                  5      5        M9     U R4                  R7                  XWR8                  45      nU(       a4  UR)                  [+        / UQ[*        R,                  R.                  P76 5        U R                  U[         R:                  " UR<                  S9[?        [A        U5      5      S9  GM     g)aN  
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   Nr5  r>  r-   rr   rr   r  )!r   r   r   r   r   rl   r<   r	   rp   r[   rm   rA   r   r   rn   r  r
   r  r   r"  ro   r   r*   r#   many_to_manyr$   r   r   r   r  rr   rX   r~   )r0   new_keysdeleted_modelsdeleted_unmanaged_modelsall_deleted_modelsr   r-   r   rH  r5   rs   r5  rr   r   r"  related_object_app_labelobject_namerelation_related_fieldsthrough_users                      r)   r   -MigrationAutodetector.generate_deleted_models  s    &&)@)@@,,x7#'#:#:X#E ">"F+C$D
 &8!I//001FGKN%0%7%7%=%=%?!
%%%))//5:z2u119dCC5:z2 &@ *11556GNO""22'(, ~.""**#-! / L11I +4z4I*J*P*P*R' (&)@)F)F)H%J ''+4'&/44;;	 !---$++/ 8 + * 3 8 8 > >	 *I +S( ~.##'!t5H5M5M5T5T /  --119>T>T2UVL##'WW7J7O7O7V7VW &&$)) "#l"34  O &8r(   c                 2   U R                   U R                  -
  n[        U5       Ho  u  p#U R                  R                  X#4   nUR
                  R                  S5      (       d   eU R                  U[        R                  " UR                  S95        Mq     g)z*Make DeleteModel options for proxy models.ry   r\  N)r   r   rl   r<   r	   r   r   r  r
   r  rr   )r0   deletedr   r-   r   s        r)   r   .MigrationAutodetector.generate_deleted_proxies  s    %%(;(;;%+G_!I//001FGK&&**73333&&$)) &5r(   c                    / U l         U R                  R                  5       n[        U R                  U-
  5       GH  u  p#nU R
                  R                  X#4U5      nU R                  R                  X%4   nU R                  R                  X#4   nUR                  U5      nU R                  U5      n	[        XR                  -
  5       GHn  u  pnX:X  d  M  X:X  d  M  UR                  U5      nU R                  U5      nUR                  (       aP  UR                  R                  (       a5  SUS   ;   a,  US   S   nXR                  ;   a  U R                  U   US   S'   UR                  U5        UR!                  5       S   nX:X  d$  USS U	SS :X  d  M  [#        US   US9U	S   :X  d  M  U R$                  R'                  X<XH5      (       d  GM	  U R                   R)                  U
UUR*                  UUUUU45        UR-                  XU45        UR/                  X#U45        UU R0                  X#U4'     GM     GM     g)zWork out renamed fields.rk   r   r   r   )	db_columnN)renamed_operationsr   copyrl   r   r   r   r<   r	   r=   	get_fieldrP   rm   rA   r   set_attributes_from_nameget_attname_columnrZ   r>   
ask_renamero   rk  r'  r   r}   )r0   r   r   r-   r5   r   r   new_model_staters   	field_decr+  r,  rem_field_name	old_fieldold_field_dec
old_rel_toold_db_columns                    r)   r   +MigrationAutodetector.create_renamed_fields  sT   "$,,11317.02
-I: "0044'N #oo44Y5NOO"mm2293HIO#--j9E--e4IAG!4!44B=~ !-.2N / 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&
  !33::$1$2$-$7$7$2$-$.$)$.	!" +11!. O +..	z/RS . !//	z0QR "[B2
r(   c           
         U R                    H  u  nnnnnnnnX7R                  :w  a<  UR                  5       n	XIl        U R	                  U[
        R                  " UUU	S95        U R	                  U[
        R                  " UUUS95        U R                  R                  XU45        U R                  R                  XVU45        M     g)z Generate RenameField operations.r8  )r-   r  r  N)rl  rk  clonerr   r  r
   r  RenameFieldr   r'  r   )
r0   r+  r,  rem_db_columnrt  r   r-   rs   r5   altered_fields
             r)   r   -MigrationAutodetector.generate_renamed_fields+  s     $$	

 / %%3"""))#-++ &&)+' &&~'VW##YJ$GH1 %r(   c                     [        U R                  U R                  -
  5       H  u  pnU R                  XU5        M     g)zMake AddField operations.N)rl   r   r   _generate_added_fieldr   s       r)   r   +MigrationAutodetector.generate_added_fieldsP  s<    17$"5"552
-I: &&yjI2
r(   c           
      `   U R                   R                  X4   R                  U5      n[        XU[        R                  R
                  5      /nUR                  (       aH  UR                  R                  (       a-  UR                  U R                  UUUU R                   5      5        UR                  (       a   UR                  U R                  U5      5        [        R                  [        R                  [        R                  4nUR                  =(       d    UR!                  5       =(       dz    UR"                  [        R$                  L=(       dW    UR&                  =(       dD    UR(                  =(       a    UR*                  =(       d    [-        XF5      =(       a    UR.                  nU(       dr  UR1                  5       n[-        XF5      (       a2  UR2                  (       a!  U R4                  R7                  X25      Ul        O U R4                  R;                  X25      Ul        UR<                  (       aR  UR8                  [        R$                  La5  [?        UR8                  5      (       a  U R4                  RA                  X25        U RC                  U[D        RF                  " UUUUS9US9  g )Nr-   rr   rs   preserve_defaultr  )$r=   r	   rn  r   r*   r#   rm   rA   r$  r%  	generated%_get_dependencies_for_generated_field	DateFieldDateTimeField	TimeFieldnullhas_default
db_defaultNOT_PROVIDEDr]  blankempty_strings_allowedrW   auto_nowr{  auto_now_addr>   ask_auto_now_add_additiondefaultask_not_null_additionuniquecallable$ask_unique_callable_default_additionr  r
   r   )r0   r   r-   r5   rs   r   time_fieldsr  s           r)   r  +MigrationAutodetector._generate_added_fieldW  s   $$Y%:;EEjQ  z3F3K3K3R3R
 %"4"4":":66MM	 ?? J J5 QR '')=)=v?O?OPJJ C  "Cv':'::C !!C ; ; ;	C
 5.A5>> 	  KKME%--%2D2D $ I I! !% E E! LLV%8%88''OO@@X%!1	 & 	 		
r(   c                     [        U R                  U R                  -
  5       H  u  pnU R                  XU5        M     g)zMake RemoveField operations.N)rl   r   r   _generate_removed_fieldr   s       r)   r   -MigrationAutodetector.generate_removed_fields  s<    17$"5"552
-I: ((
K2
r(   c                     U R                  U[        R                  " UUS9[        UUU[        R                  R
                  5      [        UUU[        R                  R                  5      /S9  g )Nr[  r  )r  r
   r   r   r*   r%   r&   r   s       r)   r  -MigrationAutodetector._generate_removed_field  su    ""% $',,==	 $',,??	 	 	
r(   c                 	   [        U R                  U R                  -  5       GH  u  pnU R                  R	                  X4U5      nU R
                  R	                  XU4U5      nU R                  R                  X4   R                  U5      nU R                  R                  X4   R                  U5      n/ n[        US5      (       Ga  [        UR                  SS5      (       Ga  [        UR                  R                  X5      n	XR                  ;   a%  UR                  R                  UR                  l        [        UR                  SS5      n
U
(       a^  X4-   nXR
                  ;   aJ  UR                  R                  UR                  l        UR                  R                  UR                  l        [        USS5      nU(       a  X4n[!        U Vs/ s H"  nU R
                  R	                  X4-   U5      PM$     sn5      Ul        [!        UR$                   Vs/ s H"  nU R
                  R	                  X4-   U5      PM$     sn5      Ul        [        USS5      =n(       a*  [!        U5      Ul        [!        UR$                  5      Ul        UR'                  U R)                  UUUU R                  5      5        [        US5      (       ap  [        UR                  SS5      (       aT  [        UR                  R*                  X5      n	XR                  ;   a%  UR                  R*                  UR                  l        U R-                  U5      nU R-                  U5      nUU:w  d  GMI  XS:X  d  GMQ  UR.                  =(       a    UR.                  nUR.                  (       + =(       a    UR.                  (       + nU(       d  U(       a  SnUR0                  (       a  UR0                  (       d  UR3                  5       (       dv  UR4                  [        R6                  L aY  UR.                  (       dH  UR9                  5       nU R:                  R=                  X25      nU[        R6                  La	  UUl        SnOUnU RA                  U[B        RD                  " UUUUS	9US
9  GM  U RG                  XU5        U RI                  XU5        GM     gs  snf s  snf )zX
Make AlterField operations, or possibly RemovedField/AddField if alter
isn't possible.
rm   rA   Nr5   from_fieldsr   TFr  r  )%rl   r   r   r   r   r}   r<   r	   rn  r=   rb   r   rm   r   rA   r5   rY   r  	to_fieldsr$  r%  r   rP   r]  r  r  r  r  r{  r>   ask_not_null_alterationr  r  r
   r  r  r  )r0   r   r-   r5   r   old_field_nameru  	new_fieldr   
rename_keyremote_field_nameto_field_rename_keyr  from_rename_key
from_fieldto_fieldold_from_fieldsrv  new_field_decboth_m2mneither_m2mr  rs   new_defaults                           r)   r   -MigrationAutodetector.generate_altered_fields  sg   
 28$"5"552
-I: "0044'N "0044
3ZN ..y/HISSI ,,Y-BCMMI L y.11g&&7 7 .**00)
 !4!443<3I3I3O3OI**0$+I,B,BLRV$W!$*47K*K'*.A.AA 8A7M7M7S7S	..4%22== "..9 &iE'0&=O,1
 /:	 /:
 !//33 /- ? /:	-I) +0 -6,?,?,? !//33J4LhW,?+I' +2)]D*QQQ05o0F	-.3I4G4G.H	+##::!"!	 y.11g&&	47 7 .**22I
 !4!445>5K5K5S5SI**2 11)<M 11)<M -.2N$11Li6L6L"+"8"88WAWAW=W{'+$! ) ) 5 5 7 7%00F4G4GG ) 6 6 ) 1&*oo&M&M&' 'f.A.AA,7EM/4, )&&!"--'1!+"'-=	 &2 ' 	 00
S..yjQi2
Xs   )S
 )S
c                 T   [         R                  R                  n[        [        5      U l        [        U R                  5       GH  u  p#U R                  R                  X#4U5      nU R                  R                  X$4   nU R                  R                  X#4   nUR                  U   nUR                  U   nU V	s/ s H  oU;  d  M
  U	PM     n
n	U V	s/ s H  oU;  d  M
  U	PM     nn	/ n/ n/ nU
 H  nUR                  5       nUS   R                  S5      nU Hm  nUR                  5       nUS   R                  S5      nUU:X  d  M/  UU:w  d  M7  UR!                  UUS 45        UR!                  U5        UR!                  U5        Mo     M     U R#                  [         R$                  R                  5       H  u  nnnnnUU:w  d  UU:w  a  M  UR'                  U5      nU H  n/ nU
 H_  nUR                  5       u  nnnU(       a  M   UR(                  [	        U5      :X  d  M;  [+        U5      SS1:X  d  MN  UR!                  U5        Ma     [-        U5      S:X  d  M{  US   n UR!                  U 5        UR!                  S U R.                  U45        U R
                  UU4   R!                  U5        M     GM      U
 V	s/ s H  oU;  d  M
  U	PM     n
n	U V	s/ s H  oU;  d  M
  U	PM     nn	U R0                  R3                  X#4U
UUS.05        GM     g s  sn	f s  sn	f s  sn	f s  sn	f )Nr   rr   rp   r   r   )added_indexesremoved_indexesrenamed_indexes)r
   rC  option_namer   rX   renamed_index_together_valuesrl   r   r   r   r<   r	   r=   r   rV   rn   ro   $_get_altered_foo_together_operationsr  rA  rp   r~   r   rr   r{   r   )!r0   r  r   r-   r   r   rr  old_indexesnew_indexesidxr  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_r_   rh   renamed_indexs!                                    r)   r   ,MigrationAutodetector.create_altered_indexes-  sH    ))55-8->*%+D,@,@%A!I!0044'N #oo44Y5NOO"mm2293HIO)11+>K)11+>K,7RKSk;QSKMR.9Tks=SskOT O ""$*	 ) 5 5 7!.q!1!5!5f!=!0I$-$9$9$;M%21%5%9%9&%AN &6*n<'..PT/UV)00;+229= "1 +* ::--99()
 !99!%>>!*!5!5i!@.<*572%2	*3*?*?*A4 !% ) 0 0D9O4P P #F/A A:AA)L &3 9:a?(Fq(I)00?'..!=#5#57MN ::46OO &!78) /=B  -,;L0L}    /.=P2P     ''+)6+:+:.W &B STts0   =	L
L	L#L 	L L 	L%&L%c           
          U R                   R                  5        HI  u  u  pnU R                  X5      nUS    H'  nU R                  U[        R
                  " UUS9US9  M)     MK     g )Nr  r:  r  )r{   r[   _get_dependencies_for_modelr  r
   rC  )r0   r   r-   alt_indexesr   r;  s         r)   r   ,MigrationAutodetector.generate_added_indexes  sr    484H4H4N4N4P0#Y[;;IRL$_5""''#-# ". #  6 5Qr(   c           
          U R                   R                  5        HC  u  u  pnUS    H2  nU R                  U[        R                  " UUR
                  S95        M4     ME     g )Nr  r[  )r{   r[   r  r
   RemoveIndexrr   )r0   r   r-   r  r;  s        r)   r   .MigrationAutodetector.generate_removed_indexes  s]    484H4H4N4N4P0#Y[$%67""**#-"ZZ 8 5Qr(   c                     U R                   R                  5        H>  u  u  pnUS    H-  u  pEnU R                  U[        R                  " UUUUS95        M/     M@     g )Nr  )r-   r  r  
old_fields)r{   r[   r  r
   RenameIndex)r0   r   r-   r  r  r  r  s          r)   r   .MigrationAutodetector.generate_renamed_indexes  sh    484H4H4N4N4P0#Y[>I!?:
 ""**#-!/!/#-	? 5Qr(   c                    [         R                  R                  n[        U R                  5       H  u  p#U R
                  R                  X#4U5      nU R                  R                  X$4   nU R                  R                  X#4   nUR                  U   nUR                  U   nU V	s/ s H  oU;  d  M
  U	PM     n
n	U V	s/ s H  oU;  d  M
  U	PM     nn	U R                  R                  X#4U
US.05        M     g s  sn	f s  sn	f )N)added_constraintsremoved_constraints)r
   rD  r  rl   r   r   r   r<   r	   r=   r   r|   r   )r0   r  r   r-   r   r   rr  old_constraintsnew_constraintscadd_constraintsrem_constraintss               r)   r   0MigrationAutodetector.create_altered_constraints  s     ..::%+D,@,@%A!I!0044'N #oo44Y5NOO"mm2293HIO-55kBO-55kBO*9V/Qo=Uq/OV*9V/Qo=Uq/OV$$+++-</>. &B WVs   (	C<5C<	DDc           
          U R                   R                  5        HJ  u  u  nnnU R                  X5      nUS    H'  nU R                  U[        R
                  " UUS9US9  M)     ML     g )Nr  r<  r  )r|   r[   r  r  r
   rD  )r0   r   r-   alt_constraintsr   r=  s         r)   r   0MigrationAutodetector.generate_added_constraints  s~     #66<<> 
;;IRL-.AB
"",,#-#- ". #  C ?r(   c           
          U R                   R                  5        HD  u  u  nnnUS    H2  nU R                  U[        R                  " UUR
                  S95        M4     MF     g )Nr  r[  )r|   r[   r  r
   RemoveConstraintrr   )r0   r   r-   r  r=  s        r)   r   2MigrationAutodetector.generate_removed_constraints  sh     #66<<> 
-.CD
""//#-'__ E ?r(   c                   ^ S n[        TR                  S5      (       a  TR                  R                  nO]UR                  X4   nUR	                  5        H9  u  u  pgn[        U4S jUR                  5        5       5      (       d  M2  U SU 3n  O   [        TSS 5      n	U	b  Sn
U	nO[        UU U5      u  p[        XS [        R                  R                  5      /n[        TR                  SS 5      (       aX  [        TR                  R                  U U5      u  pUR                  [        UUS [        R                  R                  5      5        U$ )NrA   c              3   B   >#    U  H  nTUR                   :H  v   M     g 7fr,   )rm   )rQ   rU  rs   s     r)   rS   JMigrationAutodetector._get_dependencies_for_foreign_key.<locals>.<genexpr>  s#      )8 ]777)8s   r   swappable_settingr   r   )rb   rm   rA   r"  r[   r   r   r   r   r   r*   r"   r   ro   )r   r-   rs   project_stateremote_field_modelr"  remote_app_labelremote_model_namerp   r  dep_app_labeldep_object_namer   through_app_labelthrough_object_names     `            r)   r%  7MigrationAutodetector._get_dependencies_for_foreign_key  se   !5%%w//!&!3!3!9!9%//	0EFIAJAR=5!v )/   -=+=Q?P>Q)R& BS $E+>E()M/O-=".*M  6I6N6N6U6U

 5%%y$775E""**62
 #%'',,33	 r(   c           	         / n[         R                  " UR                  5      R                  n[	        U R
                  U R                  -
  5      nU H  u  pVnU R                  R                   XV4   R                  U5      nUR                  (       a  UR                  R                  (       d  UR                  U;   d  Mm  UR                  [        UUUR                  [        R                  R                  5      5        M     U$ r,   )r	   Q
expressionreferenced_base_fieldsrl   r   r   r=   rn  rm   rA   rr   ro   r   r*   r"   )	r0   rs   r   r  newly_added_fieldsr   r-   added_field_nameadded_fields	            r)   r  ;MigrationAutodetector._get_dependencies_for_generated_field  s    !'%*:*:!;!R!R#D$7$7$:M:M$MN7I3I#3--..y/DEOO K (([-E-E-K-K!!%;;##'!"#((+0077	 8J r(   c           	          / nU R                   R                  X4   nUR                  R                  5        HC  nUR                  (       d  M  UR                  U R                  UUUU R                   5      5        ME     U$ )z3Return foreign key dependencies of the given model.)r=   r	   rp   r   r#  r$  r%  )r0   r   r-   r   r   rs   s         r)   r  1MigrationAutodetector._get_dependencies_for_model%  sw    mm**9+@A ''..0E   ##::!"	 1 r(   c              #   @  ^ ^^#    [        T R                  5       GHx  u  mmT R                  R                  TT4T5      nT R                  R
                  TU4   nT R                  R
                  TT4   nUR                  R                  U5      nU(       a(  U Vs1 s H  n[        UUU 4S jU 5       5      iM     snO	[        5       nUR                  R                  U5      nU(       a  [        U5      O	[        5       nXW:w  d  M  / nU Hz  n	U	 Hq  n
UR                  U
5      nUR                  (       d  M'  UR                  R                  (       d  MD  UR                  T R                  TTUT R                  5      5        Ms     M|     UUTTU4v   GM{     g s  snf 7f)Nc              3   b   >#    U  H$  nTR                   R                  TTU4U5      v   M&     g 7fr,   )r}   r   )rQ   nr   r-   r0   s     r)   rS   MMigrationAutodetector._get_altered_foo_together_operations.<locals>.<genexpr>A  s5      !'A ++//J0JANN!'s   ,/)rl   r   r   r   r<   r	   r=   r   rY   r~   rn  rm   rA   r$  r%  )r0   r  r   r   rr  r  r  r  r   foo_togethersr5   rs   r   r-   s   `           @@r)   r  :MigrationAutodetector._get_altered_foo_together_operations5  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%!%.M&3
 / 9 9* E ---%2D2D2J2J2J(// $ F F$-$.$)$(MM	!" '4 &/   M &Bs&   BF!F=AF,F8FA	Fc           
      b   U R                  UR                  5       H  u  nnnnnU[        R                  :X  a(  U Vs1 s H  nUU R                  XE4   ;  d  M  UiM     nnUR                  U5      nU(       d	  U(       d  Mf  U R                  UU" SSU0UR                  U0D6US9  M     g s  snf Nrr   r  r   )r  r  r
   r  r  rB  r  )	r0   r   r  r  r   r-   r   rR   removal_values	            r)   &_generate_removed_altered_foo_together<MigrationAutodetector._generate_removed_altered_foo_togetherd  s     66y7L7LM
J999 "+!*==i>STU !*   &229=M		"" ',5,A,A=+Q ". #  Ns   B,B,c                 B    U R                  [        R                  5        g r,   )r  r
   r  r/   s    r)   r   >MigrationAutodetector.generate_removed_altered_unique_together}  s    33J4R4RSr(   c           
          U R                  UR                  5       HF  u  nnnnnUR                  U5      nX7:w  d  M!  U R                  UU" SSU0UR                  U0D6US9  MH     g r  )r  r  rB  r  )r0   r   r  r  r   r-   r   r  s           r)   _generate_altered_foo_together4MigrationAutodetector._generate_altered_foo_together  s|     66y7L7LM
%229=M)""T:T)2G2G1ST!- #  Nr(   c                 B    U R                  [        R                  5        g r,   )r  r
   r  r/   s    r)   r   6MigrationAutodetector.generate_altered_unique_together  s    ++J,J,JKr(   c           	         U R                   R                  U R                  U R                  5      n[	        U5       H  u  p#U R
                  R                  X#4U5      nU R                  R                  X$4   nU R                  R                  X#4   nUR                  R                  S5      nUR                  R                  S5      nXx:w  d  M  U R                  U[        R                  " UUS95        M     g )Ndb_table)rr   table)r   unionr   r   rl   r   r   r<   r	   r=   r   r  r
   AlterModelTable)	r0   models_to_checkr   r-   r   r   rr  old_db_table_namenew_db_table_names	            r)   r   /MigrationAutodetector.generate_altered_db_table  s    ..44  $":":
 &,O%<!I!0044'N #oo44Y5NOO"mm2293HIO / 7 7 ; ;J G / 7 7 ; ;J G 5""..'/ &=r(   c           	         U R                   R                  U R                  U R                  5      n[	        U5       H  u  p#U R
                  R                  X#4U5      nU R                  R                  X$4   nU R                  R                  X#4   nUR                  R                  S5      nUR                  R                  S5      nXx:w  d  M  U R                  U[        R                  " UUS95        M     g )Ndb_table_comment)rr   table_comment)r   r  r   r   rl   r   r   r<   r	   r=   r   r  r
   AlterModelTableComment)	r0   r  r   r-   r   r   rr  old_db_table_commentnew_db_table_comments	            r)   r   7MigrationAutodetector.generate_altered_db_table_comment  s    ..44  $":":
 &,O%<!I!0044'N #oo44Y5NOO"mm2293HIO#2#:#:#>#>?Q#R #2#:#:#>#>?Q#R #;""55'&: &=r(   c           	         U R                   R                  U R                  U R                  U R                  U R
                  -  U R                  U R                  -  5      n[        U5       GH  u  p#U R                  R                  X#4U5      nU R                  R                  X$4   nU R                  R                  X#4   nUR                  R                  5        VVs0 s H  u  pxU[         R"                  ;   d  M  Xx_M     n	nnUR                  R                  5        VVs0 s H  u  pxU[         R"                  ;   d  M  Xx_M     n
nnX:w  d  M  U R%                  U[&        R                   " UU
S95        GM     gs  snnf s  snnf )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).
)rr   r   N)r   r  r   r   r   r   r   r   rl   r   r   r<   r	   r=   r   r[   r   ALTER_OPTION_KEYSr  r
   )r0   r  r   r-   r   r   rr  re   rR   old_optionsnew_optionss              r)   r   .MigrationAutodetector.generate_altered_options  sy    ..44  $$##d&9&99$"9"99
 &,O%<!I!0044'N #oo44Y5NOO"mm2293HIO #2"9"9"?"?"A"AJC+=== 
"A   #2"9"9"?"?"A"AJC+=== 
"A  
 )""00' +# &=
s    E=>E=$FFc           
         [        U R                  5       GH2  u  pU R                  R                  X4U5      nU R                  R
                  X4   nU R                  R
                  X4   nUR                  R                  S5      UR                  R                  S5      :w  d  M  / nUR                  R                  S5      (       aB  UR                  [        UUUR                  S   [        R                  R                  5      5        U R                  U[        R                  " UUR                  R                  S5      S9US9  GM5     g )Nr  r9  r  )rl   r   r   r   r<   r	   r=   r   ro   r   r*   r"   r  r
   r  )r0   r   r-   r   r   rr  r   s          r)   r   <MigrationAutodetector.generate_altered_order_with_respect_to  s9   %+D,@,@%A!I!0044'N #oo44Y5NOO"mm2293HIO&&**' ((,,-DEF
  ""**../FGG ''+%&+334KL/44;;	 ""66'.=.E.E.I.I3/ ". # 	- &Br(   c           	      x   [        U R                  5       H  u  pU R                  R                  X4U5      nU R                  R
                  X4   nU R                  R
                  X4   nUR                  UR                  :w  d  Mr  U R                  U[        R                  " UUR                  S95        M     g )N)rr   r7  )rl   r   r   r   r<   r	   r=   r7  r  r
   AlterModelManagers)r0   r   r-   r   r   rr  s         r)   r   /MigrationAutodetector.generate_altered_managers  s    %+D,@,@%A!I!0044'N #oo44Y5NOO"mm2293HIO''?+C+CC""11'!0!9!9 &Br(   c           	         UR                  5       n0 n[        UR                  5       5       GHX  u  pgU(       d  M  SnU H  n	U	S   U:X  d  M  U	n  O   Uc?  U R                  R	                  U5      (       d  U H  n
US4XVU
R
                  4'   M     X	 Mi  Uc  SnO U R                  US   5      =(       d    SS-   n[        U5       H  u  pUS:X  a"  U(       a  U
R                  R                  U5        SU-  /nU(       a  UR                  U5        OAUS:X  a  U(       d  UR                  S5        O"UR                  U
R                  5       SS 5        SR                  U5      nXn4XVU
R
                  4'   US-  nXl        M     GM[     UR                  5        H<  nU H3  n
U
R                   Vs/ s H  oR                  X5      PM     snU
l        M5     M>     U$ s  snf )	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   rX   r[   r>   ask_initialrr   parse_number	enumerater   ro   suggest_namejoinr   r   )r0   rL   rH   rK   leavesname_mapr   r   app_leafleafr   next_numberinew_name_partsr  rP  s                   r)   rF   'MigrationAutodetector.arrange_for_graph  s    !!#%)'--/%:!IH7i'#H 
 (C(CI(N(N!+I=F<TH89 ",&#00!=BaG )* 56h**11(;"(;"6!7!")).9!VH")))4")))*@*@*B4C*HI88N39B8MY^^45q !) !6- &;J "..*J'	090F0F*0F1LL&0F*	& ( +
 *s   /Gc           	         0 nUR                  5        HL  u  pEU HA  nUR                   H.  u  pxUR                  U[        5       5      R	                  U5        M0     MC     MN     [        U5      n	Sn
X:w  aA  [        U	5      n
U	R
                  " U	 Vs/ s H  oCR                  US5      PM     sn6   X:w  a  MA  [        U5       H  nXI;  d  M
  X	 M     U$ s  snf )a	  
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   )r[   r   r   r~   r   r   r   rX   )r0   rL   
app_labelsapp_dependenciesr   r   r   r  rr   required_appsold_required_appss              r)   rG   #MigrationAutodetector._trim_to_appsL  s     %,]]_!I'	+4+A+A'M$//	35AEEmT ,B ( &5 J 0 #M 2  GTU})&&y"5}U  0 gI-& '  Vs   Cc                     [         R                  " SU5      =n(       a  [        US   5      $ [         R                  " SU5      nU(       a  [        US   5      $ g)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)clsrr   squashed_matchrD  s       r)   r.  "MigrationAutodetector.parse_numberg  sO      YY';TBB>B~a())$'uQx= r(   )r|   r{   r?   r<   rz   r   r   r   r   r   r   r   r   r   r   r   r   r>   r}   r  r   r   rl  r   r=   r,   )NNN)NN)NF)9r   r   r    r!   __doc__rB   rL   rP   ru   rE   r   r   staticmethodr   r   r   r   r   r  r  r   r   r   r   r   r   r   r   r  r   r  r   r   r   r   r   r   r   r   r%  r  r  r  r  r   r  r   r   r   r   r   r   rF   rG   classmethodr.  r'   r   r(   r)   r9   r9   *   sM   
G
$LgR
.( 
 
(v"pK$$RKhR.B"H[z(T]~<"|#IJJ8
tL
4yRvWr	.  + +Z* -^2T L*,&P D 2h6  r(   r9   )!r\   rA  collectionsr   r   enumr   graphlibr   	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.functionalr   r   r9   r   r(   r)   <module>rX     s]     	 /  &     + 4 D = ? 
 4'$&LM'&I Ir(   