
    (ph;                        S SK r S SK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5      rg)    N)defaultdict)TopologicalSorter)chain)settings)models)
operations)	Migration)AlterModelOptions)MigrationOptimizer)MigrationQuestioner)COMPILED_REGEX_TYPERegexObjectresolve_relationc                      \ rS rSrSrS8S jrS9S jrS rS rS:S jr	S	 r
S
 r\S 5       rS8S jrS rS rS rS;S 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 r3S2 r4S3 r5S8S4 jr6S5 r7\8S6 5       r9S7r:g)<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 N)
from_stateto_stater   
questionerr   existing_apps)selfr   r   r   appmodels         T/var/www/html/venv/lib/python3.13/site-packages/django/db/migrations/autodetector.py__init__MigrationAutodetector.__init__"   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)r   graphtrim_to_appsconvert_appsmigration_namechangess         r   r'   MigrationAutodetector.changes(   s>     &&|;((H((?G    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valuer   s     r   	<genexpr>9MigrationAutodetector.deep_deconstruct.<locals>.<genexpr>=   s     G3%..u553s   !deconstruct   N)
isinstancelistr,   tupledictitems	functoolspartialfuncargskeywordsr   r   typehasattrr1   r   Field)r   objr.   keydeconstructedpathr;   kwargss   `       r   r,   &MigrationAutodetector.deep_deconstruct4   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).
   toN)sortedr7   r,   remote_fieldr   popappend)r   fields
fields_defnamefielddeconstructions         r   only_relation_agnostic_fields3MigrationAutodetector.only_relation_agnostic_fieldsZ   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_                  5         U Ra                  5         U Rc                  U5        U Re                  5         U Rf                  $ )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)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   r7   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    %MigrationAutodetector._detect_changesh   sp   & %'!!#%   "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446$$&""$$$&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^   ra   kept_unmanaged_keysthrough_usersr   r   renamed_modelsrc   rM   old_field_keysr   new_field_keysr   r   r   
field_names       r   rh   *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.rJ   throughN)rI   r\   r   rc   r   r   rM   r7   r>   getattrrJ   r   r   r   )r   r   r   old_model_nameold_model_stater   rP   through_keys           r   ri   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 S   S:w  a  U S4$ [        [        U S   5      R                  S5      u  pXR                  5       4U SS -   S4$ )	zX
Return the resolved dependency and a boolean denoting whether or not
it was swappable.
r   __setting__Fr2   .rG   NT)r   r   splitlower)
dependencyresolved_app_labelresolved_object_names      r   _resolve_dependency)MigrationAutodetector._resolve_dependency   sb     a=M)u$$3:jm4

%* 	1 #$>$>$@AJqrNRTXXXr)   c           
         0 U l         [        S U R                  R                  5        5       5      nSnU(       Ga.  [	        U R                  5       GH  n/ n[        5       n[        U R                  U   5       GH  nSn[        5       n	UR                   GHB  n
U
nU R                  U
5      u  pU
S   U:w  d  M$  U R                  R                  U
S   / 5       H  nU R                  X5      (       d  M  Sn  O   U(       d    OU(       a  U	R                  US   US   45        M  U
S   U R                   ;   a5  U	R                  U
S   U R                   U
S      S   R                  45        M  U(       ab  U(       aB  UR                  U
S   5      (       a)  U	R                  UR                  U
S   5      S   5        GM'  U	R                  U
S   S45        GM@  SnGME     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                   R/                  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[1        SU R                  -  5      eUnU(       a  GM-  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r-   xs     r   r/   >MigrationAutodetector._build_migration_list.<locals>.<genexpr>  s     I&Hc!ff&H   FTr   r2   	__first__r	   )r   dependencieszauto_%ic              3   8   #    U  H  n[        U5      v   M     g 7fr   r   r   s     r   r/   r   r  s     Q.Pc!ff.Pr   z)Cannot resolve operation dependencies: %rN)r   sumrW   valuesrI   r[   r4   
_auto_depsr   rc   check_dependencyrd   rO   
leaf_nodesrL   updater=   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   +MigrationAutodetector._build_migration_list  s5    Id&?&?&F&F&HII	 $D$=$=>	"u!%d&?&?	&J!KI%)N-0U*(33
 (+040H0H0M-q6Y. 483L3L3P3P #A4 $(#8#8#N#N5:N$)4 $2 %#3$:$>$>)5a,q/(J%& &)Vt%>$:$>$>),QQ1H1L1Q1Q(R%& (1 ,1U5E5Ec!f5M5M,B,F,F050@0@Q0H0K-. -C,F,F14Q0E-. :?a  4b &y1$++,BC 55i@Cs "Lv  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)<_ ?d Qd.G.G.N.N.PQQKg%  $I$C334  "GE gr)   c                   ^ ^ [        T R                  R                  5       5       H  u  p[        5       nU Hd  nUR	                  U5        UR
                   H@  mT R                  T5      S   mTS   U:w  a  M"  UR                  " U/UU 4S jU 5       Q76   MB     Mf     [        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   )r-   r   r   r   s     r   r/   9MigrationAutodetector._sort_migrations.<locals>.<genexpr>  s"      QCq43H3HC3PCs   +	+N)	rI   rW   r7   r   rd   r   r   r4   static_order)r   r   opstsopr   s   `    @r   r   &MigrationAutodetector._sort_migrations}  s    
 %T%>%>%D%D%FGNI"$Br
==C 2237:C1v* 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 )Nr2   )r   r7   zipr   rL   rO   r   r4   r[   r   optimizer   )r   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                   ^ TS   cI  TS   SL aA  [        U[        R                  5      =(       a     UR                  TS   R	                  5       :H  $ TS   b  TS   SL a  [        U[        R                  5      =(       aF    UR                  TS   R	                  5       :H  =(       a    [        U4S jUR                   5       5      =(       dh    [        U[        R                  5      =(       aG    UR                  TS   R	                  5       :H  =(       a     UR                  TS   R	                  5       :H  $ TS   bp  TS   SL ah  [        U[        R                  5      =(       aG    UR                  TS   R	                  5       :H  =(       a     UR                  TS   R	                  5       :H  $ TS   cI  TS   SL aA  [        U[        R                  5      =(       a     UR                  TS   R	                  5       :H  $ TS   bq  TS   S:X  ah  [        U[        R                  5      =(       aG    UR                  TS   R	                  5       :H  =(       a     UR                  TS   R	                  5       :H  $ TS   b  TS   S:X  a  [        U[        R                  5      =(       a^    UR                  TS   R	                  5       :H  =(       a7    UR                  =(       d    S	R	                  5       TS   R	                  5       :g  $ TS   bZ  TS   S
:X  aQ  [        U[        R                  [        R                  45      =(       a     UR                  TS   R	                  5       :H  $ [!        ST< 35      e)zV
Return True if the given operation depends on the given dependency,
False otherwise.
rG      Tr2   c              3   8   >#    U  H  u  pTS    U:H  v   M     g7f)rG   N )r-   r   yr   s      r   r/   9MigrationAutodetector.check_dependency.<locals>.<genexpr>  s     H7Gtq
1*7Gs   Falterorder_wrt_unset foo_together_changezCan't handle dependency )r3   r   CreateModel
name_lowerr   anyrM   AddFieldmodel_name_lowerRemoveFieldDeleteModel
AlterFieldAlterOrderWithRespectToorder_with_respect_toAlterUniqueTogetherAlterIndexTogetherr   )r   r   r   s     `r   r   &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                     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   rW   r   insertrL   )r   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   r2   	swappableAbstractUserAbstractBaseUser___)r   r   basesr3   str__name__rb   rc   r   AUTH_USER_MODELr   LookupError)r   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\   rI   r   r   rR   rM   r   r   ask_rename_modelr4   r   	relationsrJ   is_relationextend!_get_dependencies_for_foreign_keyr   r   RenameModelrO   r   r   removerd   )r   added_modelsr   r   r   model_fields_defremoved_modelsrem_app_labelrem_model_namerem_model_staterem_model_fields_defr   r  rP   rM   renamed_models_rel_keys                   r   rg   -MigrationAutodetector.generate_renamed_models   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                 d   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UR                   R#                  S	S5      nXVSS
4/nUR$                   GH  n['        U[(        5      (       d  M  SU;   d  M#  UR+                  SS5      u  nnUR-                  UUSS45        U R.                  R                  R1                  UU45      nU R                  R                  R1                  UU45      nU(       d  M  U(       d  M  [3        UR                  5      R5                  UR                  5      R7                  UR                  5      nU H  nUR-                  UUUS
45        M     GM     U	(       a  UR-                  [9        U	UU5      S-   5        U R;                  U[<        R>                  " UR@                  UR                  R                  5        Vs/ s H  nUS   U;  d  M  UPM     snUR                   UR$                  URB                  S9USS9  UR                   R1                  SS5      (       d  GM_  [        UR                  5       5       Ho  u  nnU RE                  UUUU R                  5      nUR-                  XVSS45        U R;                  U[<        RF                  " UUUS9[I        [3        U5      5      S9  Mq     U(       a,  U R;                  U[<        RJ                  " UUS9XVUS4XVSS4/S9  [        U5       Vs/ s H  nXVUS4PM
     nnUR-                  XVSS45        U H'  nU R;                  U[<        RL                  " UUS9US9  M)     U H'  nU R;                  U[<        RN                  " UUS9US9  M)     U(       a$  U R;                  U[<        RP                  " UUS9US9  U(       a$  U R;                  U[<        RR                  " UUS9US9  U R                  RT                  nXV4U RV                  ;   d  GM*  UXV4   R                  5        Hv  u  nnU R                  R                  U   n UR                  5        HC  u  n!n"U R;                  U RX                  [<        RZ                  " U R@                  U!U"S9XVSS4/S9  ME     Mx     GM     gs  snf s  snf )aJ  
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)rA   reverseNr   indexesconstraintsunique_togetherindex_togetherr   Fr   r2   NTr   rO   rM   rb   r   managers)r   r   rU   r   rO   rP   r  rO   r   r   indexr   
constraintrO   r  rO   r  ).r\   r^   r_   ra   r   rI   r  r   r   rM   r7   rJ   r   primary_keyparent_linkr   rb   rK   r   r3   r   r   rL   r   rc   r[   
differenceintersectionr   r   r   r   rO   r"  r  r   r4   r   AddIndexAddConstraintr   r   r  r]   r   r   )#r   old_keysr  added_unmanaged_modelsall_added_modelsr   r   r   related_fieldsprimary_key_relr   rP   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drO   related_dependenciesr&  r(  r  related_model_keyrelated_model_staterelated_field_namerelated_fields#                                      r   rk   -MigrationAutodetector.generate_created_modelsD  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(00445EtLN$/$7$7$;$;'%!
 e4L $))dC((SD[04

30B-NI ''D$(OP ,0??+A+A+E+E'3,( ,0==+?+?+C+C'3,( ,+0D0D 4 ; ;<'Z 4 ; ; *\+*<*<= , 3F.(//!/<NPU V 3F) *2 ##$'!"
 ## &&$)) "-!3!3!9!9!;!;AQ4~5 !;
 (//%++(11
 *  $ &&**9d;;  &n&:&:&<=e#EEMM	  ##YD$$GH""''#-!#
 "&c,&7!8 #   >( %""66'.C
 #0EtL"d;" # 
 AG~@V$@Vd3@V ! $ !''d(KL ""''#-# "6 #  ! *
"",,#-#- "6 #  * ""22'(7 "6 #  ""11''5 "6 #  //I&$*=*==9B):%':5%~ +/--*>*>?P*Q'=K=Q=Q=S9*M**/99&11+>+C+C%7&3
 ,5$*M)N +  >T	:G &6Hd$s   $X(5X(X-c                 4   U R                   U R                  -
  n[        U5       H  u  p#U R                  R                  X#4   nUR
                  R                  S5      (       d   eX#SS4/nUR                   HJ  n[        U[        5      (       d  M  SU;   d  M"  UR                  SS5      u  pxUR                  XxSS45        ML     U R                  U[        R                  " UR                  / UR
                  UR                  UR                   S9US9  M     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.
rV   NFr   r2   Tr!  r  )r`   r]   rI   r   r   rb   rc   r   r3   r   r   rL   r   r   r   rO   r"  )	r   addedr   r   r   r   r   r6  r7  s	            r   rm   .MigrationAutodetector.generate_created_proxies  s    ##d&9&99%+E]!I--..y/DEK&&**73333 e4L $))dC((SD[04

30B-N ''D$(OP *
 &&$))'//%++(11 *   &3r)   c           
         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R                  R                  SS5      nU(       a%  U R                  U[         R"                  " USS95        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        HB  u  pUR+                  UUU	S45        U
R,                  (       a  M-  UR+                  UUU	S	45        MD     Ma     [        U5       H  nUR+                  XVUS45        M     U R.                  R1                  XWR2                  45      nU(       a  UR+                  US
   US   US   S45        U R                  U[         R4                  " UR6                  S9[9        [;        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   Nr  r  r)  r*  r   rO   Fr   r   r2   rG   rO   r  )r_   ra   r\   r^   r   rI   r   r   rM   r7   rJ   r   r   rb   rK   r   r   r   r   r   r  rL   many_to_manyr   rc   r   r   rO   r4   r[   )r   new_keysdeleted_modelsdeleted_unmanaged_modelsall_deleted_modelsr   r   r   r4  r   rP   r  r  rO   r   r  related_object_app_labelobject_namerelation_related_fieldsthrough_users                       r   rj   -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(00445EtLN""22'(, ""11''+ ~.""**#-! / L11I +4z4I*J*P*P*R' (&)@)F)F)H%J ''1;
ER !---$++ 8 + * '	 *I +S ~.##YD%$HI /  --119>T>T2UVL##!!_l1o|AN &&$)) "#l"34  S &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.rV   rH  N)r]   r`   rI   r   r   rb   rc   r   r   r   rO   )r   deletedr   r   r   s        r   rl   .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.rH   rG   r2   r   )	db_columnN)renamed_operationsr   copyrI   r   r   rc   r   r   r   	get_fieldr,   rJ   r   r	  set_attributes_from_nameget_attname_columnr6   r   
ask_renamerL   rW  r  rd   rZ   )r   r   r   r   r   r   r   new_model_staterP   	field_decr  r  rem_field_name	old_fieldold_field_dec
old_rel_toold_db_columns                    r   rq   +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.r#  )r   r  r  N)rX  rW  clonerO   r   r   r   RenameFieldr   r  rd   )
r   r  r  rem_db_columnr`  r   r   rP   r   altered_fields
             r   rv   -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)rI   r   r   _generate_added_fieldr   s       r   r{   +MigrationAutodetector.generate_added_fields  s<    17$"5"552
-I: &&yjI2
r)   c           
          U R                   R                  X4   R                  U5      nXUS4/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R+                  5       n['        XF5      (       a2  UR,                  (       a!  U R.                  R1                  X25      Ul        O U R.                  R5                  X25      Ul        UR6                  (       aR  UR2                  [        R                  La5  [9        UR2                  5      (       a  U R.                  R;                  X25        U R=                  U[>        R@                  " UUUUS9US9  g )NFr   rO   rP   preserve_defaultr  )!r   r   rZ  rJ   r   r  r  	generated%_get_dependencies_for_generated_field	DateFieldDateTimeField	TimeFieldnullhas_default
db_defaultNOT_PROVIDEDrI  blankempty_strings_allowedr3   auto_nowrg  auto_now_addr   ask_auto_now_add_additiondefaultask_not_null_additionuniquecallable$ask_unique_callable_default_additionr   r   r   )r   r   r   r   rP   r   time_fieldsrq  s           r   rm  +MigrationAutodetector._generate_added_field   s   $$Y%:;EEjQ"
EBC%"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)rI   r   r   _generate_removed_fieldr   s       r   rz   -MigrationAutodetector.generate_removed_fields6  s<    17$"5"552
-I: ((
K2
r)   c           	      \    U R                  U[        R                  " UUS9XUS4XUS4/S9  g )NrG  r   r   r  )r   r   r   r   s       r   r  -MigrationAutodetector._generate_removed_field=  sI    ""% 
4EF
4IJ 	 	
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.
rJ   r   Nr   from_fieldsr   TFrp  r  )%rI   r   r   r   rc   rZ   r   r   rZ  r   r>   r   rJ   r   r   r   r5   r  	to_fieldsr  r  r   r,   rI  rw  rx  ry  rz  rg  r   ask_not_null_alterationr  r   r   r   r  rm  )r   r   r   r   r   old_field_namera  	new_fieldr   
rename_keyremote_field_nameto_field_rename_keyr  from_rename_key
from_fieldto_fieldold_from_fieldsrb  new_field_decboth_m2mneither_m2mrq  rP   new_defaults                           r   r|   -MigrationAutodetector.generate_altered_fieldsM  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 )NrG   rO   rM   r2   r   )added_indexesremoved_indexesrenamed_indexes)r   r/  option_namer   r4   renamed_index_together_valuesrI   r   r   rc   r   r   r   rb   r1   rK   rL   $_get_altered_foo_together_operationsr   r-  rM   r[   r   rO   rX   r   )!r   r  r   r   r   r   r^  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;   rD   renamed_indexs!                                    r   rr   ,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  )rX   r7   _get_dependencies_for_modelr   r   r/  )r   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  rG  )rX   r7   r   r   RemoveIndexrO   )r   r   r   r  r&  s        r   ru   .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)rX   r7   r   r   RenameIndex)r   r   r   r  r  r  r  s          r   rw   .MigrationAutodetector.generate_renamed_indexes9  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   r0  r  rI   r   r   rc   r   r   r   rb   rY   r   )r   r  r   r   r   r   r^  old_constraintsnew_constraintscadd_constraintsrem_constraintss               r   rs   0MigrationAutodetector.create_altered_constraintsH  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  )rY   r7   r  r   r   r0  )r   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  rG  )rY   r7   r   r   RemoveConstraintrO   )r   r   r   r  r(  s        r   rt   2MigrationAutodetector.generate_removed_constraintso  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  pXS S4/n[        TR                  SS 5      (       a7  [        TR                  R                  U U5      u  pUR                  XS S45        U$ )Nr   c              3   B   >#    U  H  nTUR                   :H  v   M     g 7fr   )rJ   )r-   rA  rP   s     r   r/   JMigrationAutodetector._get_dependencies_for_foreign_key.<locals>.<genexpr>  s#      )8 ]777)8s   r   swappable_settingr   Tr   )r>   rJ   r   r  r7   r   r   r   r   r   rL   )r   r   rP   project_stateremote_field_modelr  remote_app_labelremote_model_namerM   r  dep_app_labeldep_object_namer   through_app_labelthrough_object_names     `            r   r  7MigrationAutodetector._get_dependencies_for_foreign_key}  s5   !5%%w//!&!3!3!9!9%//	0EFIAJAR=5!v )/   -=+=Q?P>Q)R& BS $E+>E()M/O-=".*M
 'tDE5%%y$775E""**62
 !2t TU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                  XVUR                  S45        M     U$ r   )r   Q
expressionreferenced_base_fieldsrI   r   r   r   rZ  rJ   r   rO   rL   )	r   rP   r   r  newly_added_fieldsr   r   added_field_nameadded_fields	            r   rs  ;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!!%;;##YK<L<Ld$ST 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   rM   r   r  r  r  )r   r   r   r   r   rP   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   )rZ   rc   )r-   nr   r   r   s     r   r/   MMigrationAutodetector._get_altered_foo_together_operations.<locals>.<genexpr>  s5      !'A ++//J0JANN!'s   ,/)rI   r   r   rc   r   r   r   rb   r5   r[   rZ  rJ   r   r  r  )r   r  r   r   r^  r  r  r  r   foo_togethersr   rP   r   r   s   `           @@r   r  :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%!%.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 NrO   r  r   )r  r  r   r   r  r.  r   )	r   r   r  r  r   r   r   r.   removal_values	            r   &_generate_removed_altered_foo_together<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 ". #  Ns   B,B,c                 B    U R                  [        R                  5        g r   )r  r   r   r   s    r   rx   >MigrationAutodetector.generate_removed_altered_unique_together  s    33J4R4RSr)   c                 B    U R                  [        R                  5        g r   )r  r   r   r  s    r   ry   =MigrationAutodetector.generate_removed_altered_index_together
  s    33J4Q4QRr)   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  r.  r   )r   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                 B    U R                  [        R                  5        g r   )r  r   r   r  s    r   r   5MigrationAutodetector.generate_altered_index_together!  s    ++J,I,IJr)   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)rO   table)r   unionr   r   rI   r   rc   r   r   r   rb   r   r   AlterModelTable)	r   models_to_checkr   r   r   r   r^  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)rO   table_comment)r   r  r   r   rI   r   rc   r   r   r   rb   r   r   AlterModelTableComment)	r   r  r   r   r   r   r^  old_db_table_commentnew_db_table_comments	            r   rp   7MigrationAutodetector.generate_altered_db_table_comment9  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).
)rO   rb   N)r   r  r   r   r^   r_   r\   ra   rI   r   rc   r   r   r   rb   r7   r
   ALTER_OPTION_KEYSr   r   )r   r  r   r   r   r   r^  rA   r.   old_optionsnew_optionss              r   rn   .MigrationAutodetector.generate_altered_optionsO  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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      UR                  R                  S5      :w  d  M  / nUR                  R                  S5      (       a"  UR                  UUUR                  S   S45        U R                  U[        R                  " UUR                  R                  S5      S9US9  GM     g )Nr   Tr$  r  )rI   r   r   rc   r   r   r   rb   rL   r   r   r   )r   r   r   r   r   r^  r   s          r   r}   <MigrationAutodetector.generate_altered_order_with_respect_tow  s*   %+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 	 ""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)rO   r"  )rI   r   r   rc   r   r   r   r"  r   r   AlterModelManagers)r   r   r   r   r   r^  s         r   ro   /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   r2   z%04ir   d   r  )r   r4   r7   r   ask_initialrO   parse_number	enumerater   rL   suggest_namejoinr   rc   )r   r'   r#   r&   leavesname_mapr   r   app_leafleafr   next_numberinew_name_partsr  r<  s                   r   r!   '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   )r7   r   r   r[   rd   r   rc   r4   )r   r'   
app_labelsapp_dependenciesr   r   r   r  rO   required_appsold_required_appss              r   r"   #MigrationAutodetector._trim_to_apps  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+)r2   z^\d+r   N)researchintmatch)clsrO   squashed_matchr5  s       r   r  "MigrationAutodetector.parse_number  sO      YY';TBB>B~a())$'uQx= r)   )rY   rX   r   r   rW   r   r   r   r   r   r_   r`   ra   r   r\   r]   r^   r   rZ   r  r   r	  rX  r   r   r   )NNN)NN)NF);r   
__module____qualname____firstlineno____doc__r   r'   r,   rR   r    rh   ri   staticmethodr   r   r   r   r   r   r  rg   rk   rm   rj   rl   rq   rv   r{   rm  rz   r  r|   rr   r   ru   rw   rs   r   rt   r  rs  r  r  r  rx   ry   r  r~   r   r   rp   rn   r}   ro   r!   r"   classmethodr  __static_attributes__r   r)   r   r   r      sZ   
G
$LiV
.( 
Y 
Yp"dK$$=K~R.B"HAFB_B<"|#IJJ4
lL
 yRvWr	.     D -^2TS LK*,&P D 2h6  r)   r   )r8   r2  collectionsr   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   r   r   r)   r   <module>rK     s?     	 # &     + 4 D = ? o or)   