
     h4                        d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZmZmZ d dlmZ  G d d          ZdS )    N)defaultdict)chain)settings)models)
operations)	Migration)AlterModelOptions)MigrationOptimizer)MigrationQuestioner)COMPILED_REGEX_TYPERegexObjectresolve_relation)stable_topological_sortc                   t   e Zd ZdZd5dZd6dZd Zd Zd7dZd Z	d	 Z
ed
             Zd5dZd Zd Zd Zd8dZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d  Z!d! Z"d" Z#d# Z$d$ Z%ed%             Z&d& Z'd' Z(d( Z)d) Z*d* Z+d+ Z,d, Z-d- Z.d. Z/d/ Z0d0 Z1d1 Z2d5d2Z3d3 Z4e5d4             Z6dS )9MigrationAutodetectora  
    Take a pair of ProjectStates and compare them to see what the first would
    need doing to make it match the second (the second usually being the
    project's current state).

    Note that this naturally operates on entire projects at a time,
    as it's likely that changes interact (for example, you can't
    add a ForeignKey without having a migration to add the table it
    depends on first). A user interface may offer single-app usage
    if it wishes, with the caveat that it may not always be possible.
    Nc                 x    || _         || _        |pt                      | _        d |j        D             | _        d S )Nc                     h | ]\  }}|S  r   ).0appmodels      `/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/django/db/migrations/autodetector.py	<setcomp>z1MigrationAutodetector.__init__.<locals>.<setcomp>&   s    FFFjc5cFFF    )
from_stateto_stater   
questionerr   existing_apps)selfr   r   r   s       r   __init__zMigrationAutodetector.__init__"   sB    $ $=(;(=(=FFJ4EFFFr   c                     |                      ||          }|                     |||          }|r|                     ||          }|S )z
        Main entry point to produce a list of applicable changes.
        Take a graph to base names on and an optional set of apps
        to try and restrict to (restriction is not guaranteed)
        )_detect_changesarrange_for_graph_trim_to_apps)r   graphtrim_to_appsconvert_appsmigration_namechangess         r   r)   zMigrationAutodetector.changes(   sS     &&|U;;((%HH 	@((,??Gr   c                 0    t          |t                    r fd|D             S t          |t                    rt           fd|D                       S t          |t                    r  fd|                                D             S t          |t
          j                  r:|j                             |j	                                       |j
                  fS t          |t                    rt          |          S t          |t                    r|S t          |d          rm|                                }t          |t           j                  r
|dd         }|\  }}}| fd|D              fd|                                D             fS |S )	z
        Recursive deconstruction for a field and its arguments.
        Used for full comparison for rename/alter; sometimes a single-level
        deconstruction will not compare correctly.
        c                 :    g | ]}                     |          S r   deep_deconstructr   valuer   s     r   
<listcomp>z:MigrationAutodetector.deep_deconstruct.<locals>.<listcomp>;   s'    BBBUD))%00BBBr   c              3   B   K   | ]}                     |          V  d S Nr,   r.   s     r   	<genexpr>z9MigrationAutodetector.deep_deconstruct.<locals>.<genexpr>=   s1      GG%..u55GGGGGGr   c                 B    i | ]\  }}|                     |          S r   r,   r   keyr/   r   s      r   
<dictcomp>z:MigrationAutodetector.deep_deconstruct.<locals>.<dictcomp>?   s-    TTT*#uC..u55TTTr   deconstruct   Nc                 :    g | ]}                     |          S r   r,   r.   s     r   r0   z:MigrationAutodetector.deep_deconstruct.<locals>.<listcomp>T   s'    @@@%&&u--@@@r   c                 B    i | ]\  }}|                     |          S r   r,   r5   s      r   r7   z:MigrationAutodetector.deep_deconstruct.<locals>.<dictcomp>U   s-    TTTzsEd++E22TTTr   )
isinstancelisttupledictitems	functoolspartialfuncr-   argskeywordsr   r   typehasattrr8   r   Field)r   objdeconstructedpathrD   kwargss   `     r   r-   z&MigrationAutodetector.deep_deconstruct4   s    c4   	BBBBcBBBBU## 	GGGG3GGGGGGT"" 	TTTT		TTTTY.// 	%%ch//%%cl33 
 011 	s###T"" 	 JS-(( 	OO--M#v|,, 2 -abb 1!.D$@@@@4@@@TTTTV\\^^TTT  Jr   c                 
   g }t          |                                          D ]^\  }}|                     |          }|j        r(|j        j        r|d                             dd           |                    |           _|S )z
        Return a definition of the fields that ignores field names and
        what related fields actually relate to. Used for detecting renames (as
        the related fields change during renames).
           toN)sortedr@   r-   remote_fieldr   popappend)r   fields
fields_defnamefielddeconstructions         r   only_relation_agnostic_fieldsz3MigrationAutodetector.only_relation_agnostic_fieldsZ   s     
!&,,..11 	. 	.KD%!22599N! 2e&8&> 2q!%%dD111n----r   c                 v	   i | _         i | _        i | _        i | _        t	                      | _        t	                      | _        t	                      | _        t	                      | _        t	                      | _	        t	                      | _
        | j        j                                        D ]\  \  }}}|j                            dd          s| j                            ||f           @|| j        j        vrS|j                            d          r| j                            ||f           | j                            ||f           | j        j                                        D ]\  \  }}}|j                            dd          s| j
                            ||f           @|| j        j        vs|rW||v rS|j                            d          r| j	                            ||f           | j                            ||f           | j                                         | j                                         |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                   | !                                 | "                                 | #                                 | $                                 | %                                 | &                                 | '                                 | (                                 | )                                 | *                                 | +                                 | ,                                 | -                                 | .                                 | /                                 | 0                                 | 1                    |           | 2                                 | j3        S )aX  
        Return a dict of migration plans which will achieve the
        change from from_state to to_state. The dict has app labels
        as keys and a list of migrations as values.

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

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

        graph is an optional argument that, if provided, can help improve
        dependency generation and avoid potential circular dependencies.
        managedTproxy)4generated_operationsaltered_indexesaltered_constraintsrenamed_fieldssetold_model_keysold_proxy_keysold_unmanaged_keysnew_model_keysnew_proxy_keysnew_unmanaged_keysr   r   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_altered_index_togethergenerate_removed_fieldsgenerate_added_fieldsgenerate_altered_fields&generate_altered_order_with_respect_to generate_altered_unique_togethergenerate_altered_index_togethergenerate_added_indexesgenerate_added_constraintsgenerate_altered_db_table_sort_migrations_build_migration_list_optimize_migrations
migrations)r   r'   r%   	app_label
model_namemodel_states         r   r"   z%MigrationAutodetector._detect_changesh   s   & %'!!#%   "ee!ee"%%%!ee!ee"%%%48O4J4P4P4R4R 	E 	E0#Y
[&**9d;; E'++Y
,CDDDD$/";;;&**733 E'++Y
,CDDDD'++Y
,CDDD48M4H4N4N4P4P 		E 		E0#Y
[&**9d;; E'++Y
,CDDDD$/";;; <!*l!:!:&**733 E'++Y
,CDDDD'++Y
,CDDD4466622444 	$$&&& 	!!###((*** 	$$&&&$$&&&%%'''%%'''%%'''&&(((..000 	""$$$ 	##%%%'')))))+++%%'''$$&&&%%'''5577744666$$&&&""$$$$$&&&33555--///,,...##%%%'')))&&(((""5)))!!###r   c                       j          j        z   _         j         j        z   _         j         j        z   _        i  _	         fd j        D              _
         fd j        D              _        dS )z
        Prepare field lists and a list of the fields that used through models
        in the old state so dependencies can be made from the through model
        deletion to the field that uses it.
        c                     h | ]A\  }}j         j        |j                            ||f|          f         j        D ]}|||fBS r   )r   r   renamed_modelsri   rT   r   r   r   
field_namer   s       r   r   z=MigrationAutodetector._prepare_field_lists.<locals>.<setcomp>   sx     
 
 
%	:"o44.22Iz3JJWWW
 
  
J/
 
 
 
r   c                 X    h | ]&\  }}j         j        ||f         j        D ]}|||f'S r   )r   r   rT   r   s       r   r   z=MigrationAutodetector._prepare_field_lists.<locals>.<setcomp>   s]     
 
 
%	:"m29j3HIP
 
  
J/
 
 
 
r   N)rb   re   kept_model_keysrc   rf   kept_proxy_keysrd   rg   kept_unmanaged_keysthrough_usersold_field_keysnew_field_keysr   s   `r   rn   z*MigrationAutodetector._prepare_field_lists   s      $2T5HH#2T5HH#'#:T=T#T 
 
 
 
)-)=
 
 

 
 
 
)-)=
 
 
r   c                 v   t          | j                  D ]\  }}| j                            ||f|          }| j        j        ||f         }|j                                        D ]S\  }}t          |d          r>t          |j
        dd          r(t          |j
        j        ||          }|||f| j        |<   TdS )zThrough model map generation.rQ   throughN)rP   rb   r   ri   r   r   rT   r@   rG   getattrrQ   r   r   r   )r   r   r   old_model_nameold_model_stater   rW   through_keys           r   ro   z1MigrationAutodetector._generate_through_model_map   s    %+D,?%@%@ 	 	!Iz!044J' N #o4Y5NOO%4%;%A%A%C%C  !
E5.11 
g&	47 7 
 #3*2Iz# #K "&"7D&{3	 	r   c                     | d         dk    r| dfS t          t          | d                                       d          \  }}||                                f| dd         z   dfS )	zp
        Return the resolved dependency and a boolean denoting whether or not
        it was swappable.
        r   __setting__Fr9   .rN   NT)r   r   splitlower)
dependencyresolved_app_labelresolved_object_names      r   _resolve_dependencyz)MigrationAutodetector._resolve_dependency   sw     a=M))u$$3:jm4
 4

%** 	10 #$8$>$>$@$@AJqrrNRTXXXr   c           
         i | _         t          d | j                                        D                       }d}|rt	          | j                  D ]}g }t                      }t          | j        |                   D ]}d}t                      }	|j        D ]Q}
|
}|                     |
          \  }
}|
d         |k    r'| j        	                    |
d         g           D ]}| 
                    ||
          rd} n|s n|r$|	                    |d         |d         f           |
d         | j         v r:|	                    |
d         | j         |
d                  d         j        f           |rr|rQ|                    |
d                   r6|	                    |                    |
d                   d                    0|	                    |
d         df           Od}S|r:|                    |           |                    |	           | j        |         d=  |s|r| j        |         r|rt!          dt"          fg g d	          } |d
t%          | j         	                    |g                     dz   z  |          }t          |          |_        ||_        || j        v|_        | j                             |g                               |           d}|| j        |         z   | j        |<   t          d | j                                        D                       }||k    r|sd}nt1          d| j        z            |}|dS dS )ag  
        Chop the lists of operations up into migrations with dependencies on
        each other. Do this by going through an app's list of operations until
        one is found that has an outgoing dependency that isn't in another
        app's migration yet (hasn't been chopped off its list). Then chop off
        the operations before it into a migration and move onto the next app.
        If the loops completes without doing anything, there's a circular
        dependency (which _should_ be impossible as the operations are
        all split at this point so they can't depend and be depended on).
        c              3   4   K   | ]}t          |          V  d S r2   lenr   xs     r   r3   z>MigrationAutodetector._build_migration_list.<locals>.<genexpr>  s(      IIc!ffIIIIIIr   FTr   r9   	__first__r   )r   dependencieszauto_%ic              3   4   K   | ]}t          |          V  d S r2   r   r   s     r   r3   z>MigrationAutodetector._build_migration_list.<locals>.<genexpr>r  s(      QQc!ffQQQQQQr   z)Cannot resolve operation dependencies: %rN)r   sumr]   valuesrP   ra   r=   
_auto_depsr   ri   check_dependencyrj   rV   
leaf_nodesrS   updaterF   r   r   r   r   r   initial
setdefault
ValueError)r   r%   num_ops	chop_moder   choppedr   	operationdeps_satisfiedoperation_dependenciesdeporiginal_depis_swappable_depother_operationsubclassinstancenew_num_opss                    r   r   z+MigrationAutodetector._build_migration_list  s4    IId&?&F&F&H&HIIIII	 b	" $D$=>> Q Q	"uu!%d&?	&J!K!K 9 9I%)N-0UU*(3 0? 0?
 (+040H0H0M0M--q6Y.. 483L3P3P #A4 4 * * $(#8#8##N#N !*5:N$)E!* $2  ? %#3 !?$:$>$>)5a,q/(J%& %& %& %& &)Vt%>%>$:$>$>),QQ1H1L1Q(R%& %& %& %& (1 %? ,1 ).U5E5Ec!f5M5M ).,B,F,F050@0@Q0H0H0K-. -. -. -. -C,F,F14Q0E-. -. -. -. :?% y111$++,BCCC 5i@CC 7 4Y? 9 #''&L+-rBB$ $
 $,8%T_-@-@B-O-O)P)PST)TU%$ $ 15\0B0B-.5++4D<N+N(229bAAHHRRR$)		 $d&?	&JJ 1)< QQd.G.N.N.P.PQQQQQKg%%   $II$C34   "GE  b	" b	" b	" b	" b	"r   c                 ~   t          | j                                                  D ]\  }}d |D             }|D ]i}|j        D ]_}|                     |          d         }|d         |k    r6|D ]3}|                     ||          r||                             |           4`jt          ||          | j        |<   dS )z{
        Reorder to make things possible. Reordering may be needed so FKs work
        nicely inside the same app.
        c                 ,    i | ]}|t                      S r   )ra   )r   ops     r   r7   z:MigrationAutodetector._sort_migrations.<locals>.<dictcomp>  s    888bCEE888r   r   N)rP   r]   r@   r   r   r   rj   r   )r   r   opsdependency_graphr   r   op2s          r   r   z&MigrationAutodetector._sort_migrations}  s    
 %T%>%D%D%F%FGG 	 	NIs88C888 > >= > >C 22377:C1v**#& > >C#44S#>> > 0 4 8 8 = = => 4K%4 4D%i00	 	r   c                    | j                                         D ]D\  }}t          ||dd                    D ]&\  }}|j                            ||j        f           'E| j                                         D ]-}|D ](}t          t          |j                            |_        ).| j                                         D ]6\  }}|D ].}t                      
                    |j        |          |_        /7d S )Nr9   )r   r@   zipr   rS   rV   r   r=   ra   r
   optimizer   )r   r   r   m1m2	migrations         r   r   z*MigrationAutodetector._optimize_migrations  s/   %)_%:%:%<%< 	= 	=!Izj*QRR.99 = =B&&	27';<<<<= /0022 	K 	KJ' K K	)-c)2H.I.I)J)J	&&K &*_%:%:%<%< 	 	!Iz'  	'9';';'D'D()( (	$$	 	r   c                    d         Gd         du r=t          |t          j                  o"|j        d                                         k    S d         ǉd         du rt          |t          j                  oB|j        d                                         k    ot          fd|j        D                       p_t          |t          j                  oE|j        d                                         k    o"|j        d                                         k    S d         jd         du r`t          |t          j	                  oE|j        d                                         k    o"|j        d                                         k    S d         Gd         du r=t          |t          j
                  o"|j        d                                         k    S d         ld         dk    r`t          |t          j                  oE|j        d                                         k    o"|j        d                                         k    S d         d         d	k    rtt          |t          j                  oY|j        d                                         k    o6|j        pd
                                d                                         k    S d         Ud         dk    rIt          |t          j        t          j        f          o"|j        d                                         k    S t!          d          )zn
        Return True if the given operation depends on the given dependency,
        False otherwise.
        rN   N   Tr9   c              3   6   K   | ]\  }}d          |k    V  dS )rN   Nr   )r   r   yr   s      r   r3   z9MigrationAutodetector.check_dependency.<locals>.<genexpr>  s0      HHtq!
1*HHHHHHr   Falterorder_wrt_unset foo_together_changezCan't handle dependency )r<   r   CreateModel
name_lowerr   anyrT   AddFieldmodel_name_lowerRemoveFieldDeleteModel
AlterFieldAlterOrderWithRespectToorder_with_respect_toAlterUniqueTogetherAlterIndexTogetherr   )r   r   r   s     `r   r   z&MigrationAutodetector.check_dependency  sS    a= Z]d%:%:9j&<== B(JqM,?,?,A,AA
 ]&:a=D+@+@9j&<== I(JqM,?,?,A,AAIHHHHy7GHHHHH
 9j&9:: B.*Q-2E2E2G2GGB(JqM,?,?,A,AA ]&:a=E+A+A9j&<== B.*Q-2E2E2G2GGB(JqM,?,?,A,AA ]"z!}'='=9j&<== B(JqM,?,?,A,AA
 ]&:a=G+C+C9j&;<< B.*Q-2E2E2G2GGB(JqM,?,?,A,AA ]&:a=<M+M+M9j&HII )(JqM,?,?,A,AA)4:AACCa=&&(() ]&:a=<Q+Q+Q3Z5RS  B (JqM,?,?,A,AA *ZZIJJJr   Fc                     |pg |_         |r1| j                            |g                               d|           d S | j                            |g                               |           d S )Nr   )r   r]   r   insertrS   )r   r   r   r   	beginnings        r   add_operationz#MigrationAutodetector.add_operation  sv      ,1r	 	R%00B??FFq)TTTTT%00B??FFyQQQQQr   c                 n   	 | j         j        |         }d |j        D             }|d         d|d         }|j                            d          s<d|v s8d|v s4t
          j                                        |                                k    rd|d         z   d|d         z   fS n# t          $ r Y nw xY w|S )	z|
        Place potential swappable models first in lists of created models (only
        real way to solve #22783).
        c                 J    h | ] }t          |t                    r|n|j        !S r   )r<   str__name__)r   bases     r   r   z<MigrationAutodetector.swappable_first_key.<locals>.<setcomp>  s=        #4--@4=  r   r   r   r9   	swappableAbstractUserAbstractBaseUser___)	r   r   basesrh   ri   r   AUTH_USER_MODELr   LookupError)r   itemr   
base_namesstring_versions        r   swappable_first_keyz)MigrationAutodetector.swappable_first_key  s    
	-.t4K '-  J )-Qa9N#''44:!Z//%33+1133~7K7K7M7MMMQa99 N  	 	 	D	s   B!B% %
B21B2c                 "   i | _         i | _        | j        | j        z
  }t	          |          D ]\  }}| j        j        ||f         }|                     |j                  }| j        | j        z
  }|D ]\  }}||k    r| j	        j        ||f         }	|                     |	j                  }
||
k    rW| j
                            |	|          r;g }t          |j                                                  d | j        j        ||f                                         D             z   }|D ]9}|j        r0|                    |                     |||| j                             :|                     |t'          j        |	j        |j                  |           || j         ||f<   |	j        d|	j        }|j        d|j        | j        |<   | j                            ||f           | j                            ||f            ndS )z
        Find any renamed models, generate the operations for them, and remove
        the old entry from the model lists. Must be run before other
        model-level generation.
        c                 J    g | ] }|                                 D ]	}|j        
!S r   )r   rQ   )r   	relationsrW   s      r   r0   zAMigrationAutodetector.generate_renamed_models.<locals>.<listcomp>   sY     J J J$- .7-=-=-?-?J J
 %*	 !& 2J J J Jr   )old_namenew_namer   r   N)r   renamed_models_relre   rb   rP   r   r   rY   rT   r   r   ask_rename_modelr=   r   r  is_relationextend!_get_dependencies_for_foreign_keyr   r   RenameModelrV   r   r   removerj   )r   added_modelsr   r   r   model_fields_defremoved_modelsrem_app_labelrem_model_namerem_model_staterem_model_fields_defr   rT   rW   renamed_models_rel_keys                  r   rm   z-MigrationAutodetector.generate_renamed_models  s    !"$*T-@@%+L%9%9 8	" 8	"!Iz-.y*/DEK#AA+BTUU!043FFN1? 3" 3"-~ I--&*o&<%~5'O ,0+M+M'., ,( (+????;;+[  *" ,.L%)+*<*C*C*E*E%F%F J J151H$-z$92""(&((	J J J &F *0 	& 	&#(#4 !&$0$7$7(,(N(N,5,6,1,0M	)* )*%& %& %& !.. ) * 6-<-A-8-=!" !" !" .: /    JXD/	:0EF / 9 9 9 / : :62 !, 5 5 5 + 6 6! !3 6 !/66~7VWWW /33Y
4KLLL!Eq8	" 8	"r   c                 :   ! | j         | j        z  }| j        |z
  }| j        |z
  }t	          t          || j        d          t          || j        d                    }|D ]6\   | j        j         f         }i !d}|j	        
                                D ]X\  }}|j        rL|j        j        r%|j        r|j        j        }n|j        j        s|!|<   t          |j        dd          r|!|<   Y|j                            d          }	|j                            d          }
|j                            dd          }|j                            dd          }|j                            d	d          } dd
fg}|j        D ]}t'          |t(                    rd|v r|                    dd          \  }}|                    ||ddf           | j        j                            ||f          }| j        j                            ||f          }|rd|rbt3          |j	                                      |j	                                      |j	                  }|D ]}|                    |||d
f           |r'|                    t9          |           dz              |                     t=          j        |j         !fd|j	        
                                D             |j        |j        |j!                  |d           |j                            dd          st          !
                                          D ]\  }}| "                     || j                  }|                     ddf           |                     t=          j#         ||          tI          t3          |                               |r6|                     t=          j%         |           |df ddfg            fdt          !          D             }|                     ddf           |	D ].}|                     t=          j&         |          |           /|
D ].}|                     t=          j'         |          |           /|r,|                     t=          j(         |          |           |r,|                     t=          j)         |          |           | j        j*        } f| j+        v r| f         
                                D ]m\  }!| j        j        |         }!
                                D ]A\  }}|                     |j,        t=          j-        |j         ||           ddfg           Bn8dS )a  
        Find all new models (both managed and unmanaged) and make create
        operations for them as well as separate operations to create any
        foreign key or M2M relationships (these are optimized later, if
        possible).

        Defer any model options that refer to collections of fields that might
        be deferred (e.g. unique_together, index_together).
        T)r6   reverseNr   indexesconstraintsunique_togetherindex_togetherr   Fr   r9   )NTc                 (    g | ]}|d          v|S )r   r   )r   drelated_fieldss     r   r0   zAMigrationAutodetector.generate_created_models.<locals>.<listcomp>  s2       Q4~55 555r   rV   rT   rh   r   managers)r   r   r[   r   rV   rW   r  rV   r   c                     g | ]}|d f	S )Tr   )r   rV   r   r   s     r   r0   zAMigrationAutodetector.generate_created_models.<locals>.<listcomp>  s/     $ $ $8<Jd3$ $ $r   r   indexr   
constraintrV   r  rV   r  ).rb   rd   re   rg   r   rP   r   r   r   rT   r@   rQ   r   primary_keyparent_linkr   rh   rR   r   r<   r   r   rS   r   ri   ra   
differenceintersectionr   r   r   r   rV   r  r
  r   r=   r   AddIndexAddConstraintr   r   r  rc   r   r   )"r   old_keysr  added_unmanaged_modelsall_added_modelsr   primary_key_relr   rW   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_fieldrV   related_dependenciesr$  r&  r  related_model_keyrelated_model_staterelated_field_namerelated_fieldr   r   r  s"                                  @@@r   rq   z-MigrationAutodetector.generate_created_modelsH  s    &)@@*X5!%!88!C <T%=tLLL)t/GQUVVV
 
 &6 p	 p	!Iz-.y*/DEKN"O%0%7%=%=%?%? ; ;!
E% ;)/ ? , ?.3.@.FOO!&!3!? ?9>N:6u19dCC ;5:z2 ")--i88G%-11-@@K)1556GNNO(0445EtLLN$/$7$;$;'% %!
 Je4L $)  dC(( SD[[04

30B0B-NI ''D$(OPPP ,0?+A+E+E'3, ,( ,0=+?+C+C'3, ,( , 0D  4 ;<<'Z 4 ;  *\+*<== , 3F  .(//!/<NPU V     ##$'!" 
 ##   &$)   !,!3!9!9!;!;  
 (/%+(1
 
 
 *    $ &**9d;;   &n&:&:&<&<==  e#EEM	    ##Y
D$$GHHH""'#-!#  
 "&c,&7&7!8!8 #     % ""6'.C  
 #J0EtL"Jd;" # 
 
 
$ $ $ $ $@F~@V@V$ $ $  !''Jd(KLLL   ""'#-#   "6 #     *  
"",#-#-   "6 #      ""2'(7   "6 #     ""1''5   "6 #    /I:&$*===9Bz):%'' 5%~ +/-*>?P*Q'=K=Q=Q=S=S 	 	9*M**/9&1+>+C%7&3  
 ,5j$*M)N +    	Op	 p	r   c                    | j         | j        z
  }t          |          D ]\  }}| j        j        ||f         }|j                            d          sJ ||ddfg}|j        D ]M}t          |t                    r6d|v r2|
                    dd          \  }}|                    ||ddf           N|                     |t          j        |j        g |j        |j        |j                  |           dS )	z
        Make CreateModel statements for proxy models. Use the same statements
        as that way there's less code duplication, but for proxy models it's
        safe to skip all the pointless field stuff and chuck out an operation.
        r\   NFr   r9   Tr  r  )rf   rc   rP   r   r   rh   ri   r   r<   r   r   rS   r   r   r   rV   r  )	r   addedr   r   r   r   r   r3  r4  s	            r   rs   z.MigrationAutodetector.generate_created_proxies  s?    #d&99%+E]] 	 	!Iz-.y*/DEK&**733333 Je4L $) Q QdC(( QSD[[04

30B0B-NI ''D$(OPPP&$)'/%+(1   *     	 	r   c           
         | j         | j        z  }| j        |z
  }| j        |z
  }t	          t          |          t          |                    }|D ]\  }}| j        j        ||f         }i }|j        	                                D ]8\  }	}
|
j
        r,|
j
        j        r|
||	<   t          |
j
        dd          r|
||	<   9|j                            dd          }|j                            dd          }|r*|                     |t!          j        |d                     |r*|                     |t!          j        |d                     t          |          D ],}|                     |t!          j        ||                     -g }| j        j        }|||f         	                                D ][\  \  }}}|	                                D ]>\  }	}
|                    |||	df           |
j        s|                    |||	d	f           ?\t          |          D ]}|                    |||df           | j                            ||j        f          }|r+|                    |d
         |d         |d         df           |                     |t!          j        |j                  t9          t;          |                               dS )a  
        Find all deleted models (managed and unmanaged) and make delete
        operations for them as well as separate operations to delete any
        foreign key or M2M relationships (these are optimized later, if
        possible).

        Also bring forward removal of any model options that refer to
        collections of fields - the inverse of generate_created_models().
        r   Nr  r  r'  r(  r   rV   Fr   r   r9   rN   rV   r  )re   rg   rb   rd   r   rP   r   r   rT   r@   rQ   r   r   rh   rR   r   r   r   r   r   r  rS   many_to_manyr   ri   r   r   rV   r=   ra   )r   new_keysdeleted_modelsdeleted_unmanaged_modelsall_deleted_modelsr   r   r   r  r   rW   r  r  rV   r   r  related_object_app_labelobject_namerelation_related_fieldsthrough_users                       r   rp   z-MigrationAutodetector.generate_deleted_models,  s    &)@@,x7#'#:X#E ">""F+C$D$D
 
 &8 O	 O	!Iz/0J1FGKN%0%7%=%=%?%? ; ;!
E% ;)/ ;5:z2u19dCC ;5:z2)1556GNNO(0445EtLLN ""2'(,      ""1''+     ~..  ""*#-!      L1I +4Iz4I*J*P*P*R*R  ' (&)@)F)F)H)H  %J ''1;
ER   !- $++ 8 + * '	   ~.. J J##Y
D%$HIIII-119k>T2UVVL ##!!_l1o|AN   &$)   "#l"3"344     SO	 O	r   c                 
   | j         | j        z
  }t          |          D ]c\  }}| j        j        ||f         }|j                            d          sJ |                     |t          j	        |j
                             ddS )z*Make DeleteModel options for proxy models.r\   rB  N)rc   rf   rP   r   r   rh   ri   r   r   r   rV   )r   deletedr   r   r   s        r   rr   z.MigrationAutodetector.generate_deleted_proxies  s    %(;;%+G__ 	 	!Iz/0J1FGK&**733333&$)     	 	r   c                    g | _         | j                                        }t          | j        |z
            D ]\  }}}| j                            ||f|          }| j        j        ||f         }| j	        j        ||f         }|
                    |          }|                     |          }	t          || j        z
            D ]x\  }
}}|
|k    rj||k    rc|
                    |          }|                     |          }|j        rC|j        j        r7d|d         v r-|d         d         }|| j        v r| j        |         |d         d<   |                    |           |                                d         }||	k    s7|dd         |	dd         k    rt#          |d         |          |	d         k    r| j                            ||||          rf| j                             |
||j        |||||f           |                    |
||f           |                    |||f           || j        |||f<    nz	dS )zWork out renamed fields.rO   rN   r9   r   )	db_columnN)renamed_operationsr   copyrP   r   r   ri   r   r   r   	get_fieldr-   rQ   r   r  set_attributes_from_nameget_attname_columnr?   r   
ask_renamerS   rO  r  rj   r`   )r   r   r   r   r   r   r   new_model_staterW   	field_decr  r  rem_field_name	old_fieldold_field_dec
old_rel_toold_db_columns                    r   rw   z+MigrationAutodetector.create_renamed_fields  s   "$,113317.02
 2
 8	" 8	"-Iz: "044J' N #o4Y5NOO"m29j3HIO#--j99E--e44IAG!44B B -" -"=~~ !I--.J2N2N / 9 9. I II$($9$9)$D$DM*Y!.4Y !M!$444%21%5d%;
%)@@@595LZ5XM!,T266~FFF$-$@$@$B$B1$EM$	11 &ac*i!n<< q!1]KKK$Q<( (  ?55&
E  " !3::$1$2$-$7$2$-$.$)$.	!"   +11!. O   +..	:z/RSSS !/ !/ ):z A "Eq8	" 8	"r   c           	         | j         D ]\  }}}}}}}}||j        k    rF|                                }	||	_        |                     |t          j        |||	                     |                     |t          j        |||                     | j        	                    |||f           | j        
                    |||f           dS )z Generate RenameField operations.r   )r   r  r  N)rP  rO  clonerV   r   r   r   RenameFieldr   r  rj   )
r   r  r  rem_db_columnrX  r   r   rW   r   altered_fields
             r   r|   z-MigrationAutodetector.generate_renamed_fields  s    $!	I !	I 	

 // %%3""")#-++     &)+'     &&~~'VWWW##Y
J$GHHHHC!	I !	Ir   c                 z    t          | j        | j        z
            D ]\  }}}|                     |||           dS )zMake AddField operations.N)rP   r   r   _generate_added_fieldr   r   r   r   s       r   r   z+MigrationAutodetector.generate_added_fields  s[    17$"552
 2
 	J 	J-Iz: &&y*jIIII	J 	Jr   c           	         | j         j        ||f                             |          }|||dfg}|j        r<|j        j        r0|                    |                     |||| j                              t          j        t          j        t          j	        f}|j
        p?|                                p+|j        p$|j        o|j        pt          ||          o|j        }|sl|                                }t          ||          r(|j        r!| j                            ||          |_        n | j                            ||          |_        |j        rB|j        t          j        ur/t3          |j                  r| j                            ||           |                     |t9          j        ||||          |           d S )NFr   rV   rW   preserve_defaultr  )r   r   rR  rQ   r   r	  r
  	DateFieldDateTimeField	TimeFieldnullhas_defaultrC  blankempty_strings_allowedr<   auto_nowr^  auto_now_addr   ask_auto_now_add_additiondefaultask_not_null_additionuniqueNOT_PROVIDEDcallable$ask_unique_callable_default_additionr   r   r   )r   r   r   r   rW   r   time_fieldsrg  s           r   rc  z+MigrationAutodetector._generate_added_field  s   $Y
%:;EEjQQ"J
EBC 	%"4": 	66M	    ')=v?OPJ C  ""C!C ; ;C 5+..A5> 	   		KKMME%-- %2D  $ I I
! ! !% E E
! ! L	YV%888'' 9 O@@ZXXX%!1	   & 	 		
 		
 		
 		
 		
r   c                 z    t          | j        | j        z
            D ]\  }}}|                     |||           dS )zMake RemoveField operations.N)rP   r   r   _generate_removed_fieldrd  s       r   r   z-MigrationAutodetector.generate_removed_fields7  s[    17$"552
 2
 	L 	L-Iz: ((J
KKKK	L 	Lr   c           	      r    |                      |t          j        ||          |||df|||dfg           d S )NrA  r   r   r  )r   r   r   rd  s       r   rz  z-MigrationAutodetector._generate_removed_field>  sh    "%   J
4EFJ
4IJ 	 	
 	
 	
 	
 	
r   c           
          t           j         j        z            D ]_\  }}} j                            ||f|          } j                            |||f|          } j        j        ||f                             |          } j	        j        ||f                             |          }g }t          |d          r2t          |j        dd          rt          |j        j        ||           j        v r|j        j        |j        _        t          |j        dd          }	|	r;|	fz   }
|
 j        v r,|j        j        |j        _        |j        j        |j        _        t          |dd          }|rK||ft!           fd|D                       |_        t!           fd|j        D                       |_        |                                         ||| j	                             t          |d          rPt          |j        dd          r:t          |j        j        ||           j        v r|j        j        |j        _                             |          }                     |          }||k    r||k    r|j        o|j        }|j         o|j         }|s|rd	}|j        ri|j        sb|                                sN|j        sG|                                } j                            ||          }|t          j        ur	||_        d
}n|}                     |tA          j!        ||||          |           1 "                    |||            #                    |||           adS )zp
        Make AlterField operations, or possibly RemovedField/AddField if alter
        isn't possible.
        rQ   r   Nr   from_fieldsc                 N    g | ]!}j                             |fz   |          "S r   r`   ri   )r   
from_fieldfrom_rename_keyr   s     r   r0   zAMigrationAutodetector.generate_altered_fields.<locals>.<listcomp>  sJ        !+ !/33 /:- ?   r   c                 N    g | ]!}j                             |fz   |          "S r   r  )r   to_field
rename_keyr   s     r   r0   zAMigrationAutodetector.generate_altered_fields.<locals>.<listcomp>  sC        ( !/33J(4LhWW  r   r   TFrf  r  )$rP   r   r   r   ri   r`   r   r   rR  r   rG   r   rQ   r   r   r   r>   r}  	to_fieldsr	  r
  r   r-   rC  rk  rl  r^  r   ask_not_null_alterationru  rr  r   r   r   rz  rc  )r   r   r   r   r   old_field_namerY  	new_fieldr   remote_field_nameto_field_rename_keyr}  rZ  new_field_decboth_m2mneither_m2mrg  rW   new_defaultr  r  s   `                  @@r   r   z-MigrationAutodetector.generate_altered_fieldsN  s   
 28$"552
 2
 p	R p	R-Iz: "044J' N "044J
3Z N .y./HISS I ,Y
-BCMM I L y.11 -g&7 7 - .*0)Z 
 !4443<3I3OI*0$+I,BLRV$W$W!$ 	*48I7K*K'*d.AAA 8A7M7S	.4%2= ".9 &iEE '0*&=O,1     /:	  - -I) +0    ,5,?  + +I' ##::!"!	    y.11 Tg&	47 7 T .*2Iz 
 !4445>5K5SI*2 11)<<M 11)<<M --.J2N2N$1Li6L"+"88WAW=W R{ R'+$!* )* !* 5 5 7 7* !* 6	* !* 1 1&*o&M&M&
' ' 'f.AAA,7EM/4, )&&!"-'1!+"'-=	   &2 ' 	 	 	 	 00J
SSS..y*jQQQap	R p	Rr   c                    t           j        j        }t          t                    | _        t          | j                  D ]\  }}| j        	                    ||f|          }| j
        j        ||f         }| j        j        ||f         }|j        |         |j        |         fdD             }fdD             }g }	g g |D ]}
|
                                }|d                             d          }|D ]}|                                }|d                             d          }||k    rH||k    rB|	                    ||d f                               |
                               |           |                     t           j        j                  D ]\  }}}}}||k    s||k    r|                    |          }|D ]}g }|D ]^}
|
                                \  }}}|sB|
j        t	          |          k    r*t+          |          ddhk    r|                    |
           _t-          |          dk    r\|d         }                    |           |	                    d |j        |f           | j        ||f                             |           Րfd|D             }fd	|D             }| j                            ||f|||	d
i           d S )Nc                     g | ]}|v|	S r   r   )r   idxold_indexess     r   r0   z@MigrationAutodetector.create_altered_indexes.<locals>.<listcomp>  s#    RRRS3k;Q;QS;Q;Q;Qr   c                     g | ]}|v|	S r   r   )r   r  new_indexess     r   r0   z@MigrationAutodetector.create_altered_indexes.<locals>.<listcomp>  s#    TTTsS=S=Ss=S=S=Sr   rN   rV   rT   r9   r   c                     g | ]}|v|	S r   r   )r   r  remove_from_addeds     r   r0   z@MigrationAutodetector.create_altered_indexes.<locals>.<listcomp>  s+       ;L0L0L0L0L0Lr   c                     g | ]}|v|	S r   r   )r   r  remove_from_removeds     r   r0   z@MigrationAutodetector.create_altered_indexes.<locals>.<listcomp>  s+       #=P2P2P2P2P2Pr   )added_indexesremoved_indexesrenamed_indexes)r   r-  option_namer   r=   renamed_index_together_valuesrP   r   r   ri   r   r   r   rh   r8   rR   rS   $_get_altered_foo_together_operationsr   r+  rT   ra   r   rV   r^   r   ) r   r  r   r   r   r   rV  r  r  r  	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_rD   rL   renamed_indexr  r  r  r  s                                @@@@r   rx   z,MigrationAutodetector.create_altered_indexes  s    )5-8->->*%+D,@%A%A S	 S	!Iz!044J' N #o4Y5NOO"m29j3HIO)1+>K)1+>KRRRRKRRRMTTTTkTTTO O ""$* > >	 ) 5 5 7 7!.q!1!5!5f!=!=!0 
> 
>I$-$9$9$;$;M%21%5%9%9&%A%AN &66*n<<'..PT/UVVV)00;;;+229===
>$ ::-9 #9 #9 ()
 !999!%>>>!*!5!5i!@!@.< 9 9*572%2 M M	*3*?*?*A*A4 !%M ) 0D9O4P4P P P #F/A A A:AA)LLL9::a??(Fq(I)00???'..!=#57MN   :46OO &!7888)9.   ,  M   .  O  ''
+)6+:+:. .   WS	 S	r   c           	          | j                                         D ]=\  \  }}}|d         D ],}|                     |t          j        ||                     ->d S )Nr  r#  )r^   r@   r   r   r-  r   r   r   alt_indexesr$  s        r   r   z,MigrationAutodetector.generate_added_indexes  s    484H4N4N4P4P 	 	0#Y
[$_5  ""'#-#     	 	r   c           	          | j                                         D ]B\  \  }}}|d         D ]1}|                     |t          j        ||j                             2Cd S )Nr  rA  )r^   r@   r   r   RemoveIndexrV   r  s        r   r{   z.MigrationAutodetector.generate_removed_indexes)  s    484H4N4N4P4P 	 	0#Y
[$%67  ""*#-"Z     	 	r   c                     | j                                         D ]C\  \  }}}|d         D ]2\  }}}|                     |t          j        ||||                     3Dd S )Nr  )r   r  r  
old_fields)r^   r@   r   r   RenameIndex)r   r   r   r  r  r  r  s          r   r}   z.MigrationAutodetector.generate_renamed_indexes4  s    484H4N4N4P4P 	 	0#Y
[>I!?  :
 ""*#-!/!/#-	     	 	r   c                   	
 t           j        j        }t          | j                  D ]\  }}| j                            ||f|          }| j        j        ||f         }| j	        j        ||f         }|j
        |         
|j
        |         	
fd	D             }	fd
D             }| j                            ||f||di           d S )Nc                     g | ]}|v|	S r   r   )r   cold_constraintss     r   r0   zDMigrationAutodetector.create_altered_constraints.<locals>.<listcomp>N  #    VVVQQo=U=Uq=U=U=Ur   c                     g | ]}|v|	S r   r   )r   r  new_constraintss     r   r0   zDMigrationAutodetector.create_altered_constraints.<locals>.<listcomp>O  r  r   )added_constraintsremoved_constraints)r   r.  r  rP   r   r   ri   r   r   r   rh   r_   r   )r   r  r   r   r   r   rV  add_constraintsrem_constraintsr  r  s            @@r   ry   z0MigrationAutodetector.create_altered_constraintsC  s    .:%+D,@%A%A 	 	!Iz!044J' N #o4Y5NOO"m29j3HIO-5kBO-5kBOVVVV/VVVOVVVV/VVVO$++
+-</>. .   	 	r   c           	          | j                                         D ]=\  \  }}}|d         D ],}|                     |t          j        ||                     ->d S )Nr  r%  )r_   r@   r   r   r.  r   r   r   alt_constraintsr&  s        r   r   z0MigrationAutodetector.generate_added_constraintsZ  s     #6<<>>	 	  
-.AB  
"",#-#-     		 	r   c           	          | j                                         D ]B\  \  }}}|d         D ]1}|                     |t          j        ||j                             2Cd S )Nr  rA  )r_   r@   r   r   RemoveConstraintrV   r  s        r   rz   z2MigrationAutodetector.generate_removed_constraintsh  s     #6<<>>	 	  
-.CD  
""/#-'_     		 	r   c                    d }t          j        d          rj        j        }nb|j        | |f         }|                                D ]>\  \  }}}t          fd|                                D                       r	| d| } n?t          dd           }	|	d}
|	}nt          || |          \  }
}|
|d dfg}t          j        dd           r7t          j        j	        | |          \  }}|
                    ||d df           |S )Nr   c              3   .   K   | ]}|j         k    V  d S r2   )rQ   )r   r=  rW   s     r   r3   zJMigrationAutodetector._get_dependencies_for_foreign_key.<locals>.<genexpr>~  s?        % ]77     r   r   swappable_settingr   Tr   )rG   rQ   r   r  r@   r   r   r   r   r   rS   )r   r   rW   project_stateremote_field_modelr  remote_app_labelremote_model_namerT   r  dep_app_labeldep_object_namer   through_app_labelthrough_object_names     `            r   r
  z7MigrationAutodetector._get_dependencies_for_foreign_keyv  s   !5%w// 
	!&!3!9%/	:0EFIAJARAR  =5!#4v    )/      -=)R)R?P)R)R&E $E+>EE()M/OO-=". .*M?
 'tDE5%y$77 	V5E"*6 622
 !24Gt TUUUr   c              #      K   t           j                  D ]1\   j                            f          } j        j        |f         } j        j        f         }|j                            |          }|r fd|D             nt                      }|j                            |          }|rt          |          nt                      }||k    rmg }|D ]_}|D ]Z}	|	                    |	          }
|
j
        r<|
j
        j        r0|                                         |
 j                             [`|||fV  3d S )Nc                 J    h | ]}t          fd |D                        S )c              3   T   K   | ]"}j                             |f|          V  #d S r2   r  )r   nr   r   r   s     r   r3   zWMigrationAutodetector._get_altered_foo_together_operations.<locals>.<setcomp>.<genexpr>  sQ         +//J0JANN     r   )r>   )r   rt  r   r   r   s     r   r   zMMigrationAutodetector._get_altered_foo_together_operations.<locals>.<setcomp>  se       
 	       !'      r   )rP   r   r   ri   r   r   r   rh   ra   rR  rQ   r   r	  r
  )r   r  r   r   rV  r  r  r   foo_togethersr   rW   r   r   s   `          @@r   r  z:MigrationAutodetector._get_altered_foo_together_operations  s     %+D,@%A%A ,	 ,	!Iz!044J' N #o4Y5NOO"m29j3HIO (/33K@@I       
 #,    UU  (/33K@@I*3>III%%!%.  M&3 
 

 / 9 9* E E - %2D2J (// $ F F$-$.$)$(M	!" !"  
     M,	 ,	r   c           
                                |j                  D ]h\  }}}|t          j        k    r fd|D             }|                    |          }|s|r'                      |ddi|j        |i|           id S )Nc                 6    h | ]}|j         f         v|S r   )r  )r   r/   r   r   r   s     r   r   zOMigrationAutodetector._generate_removed_altered_foo_together.<locals>.<setcomp>  sK       =i>STU U U U Ur   rV   r  r   )r  r  r   r   r,  r   )r   r   r  r  r   removal_valuer   r   s   `     @@r   &_generate_removed_altered_foo_togetherz<MigrationAutodetector._generate_removed_altered_foo_together  s     66y7LMM	 	 
J999     !*  	 &229==M 	 ""I  ',5,A=+Q  ". #   !	 	r   c                 D    |                      t          j                   d S r2   )r  r   r   r   s    r   r~   z>MigrationAutodetector.generate_removed_altered_unique_together  s    33J4RSSSSSr   c                 D    |                      t          j                   d S r2   )r  r   r   r   s    r   r   z=MigrationAutodetector.generate_removed_altered_index_together  s    33J4QRRRRRr   c           
          |                      |j                  D ]J\  }}}}}|                    |          }||k    r'|                     | |dd|i|j        |i|           Kd S )NrV   r  r   )r  r  r,  r   )r   r   r  r  r   r   r   r  s           r   _generate_altered_foo_togetherz4MigrationAutodetector._generate_altered_foo_together  s     66y7LMM	 	 
%229==MM))""ITT:T)2G1STT!- #   	 	r   c                 D    |                      t          j                   d S r2   )r  r   r   r   s    r   r   z6MigrationAutodetector.generate_altered_unique_together  s    ++J,JKKKKKr   c                 D    |                      t          j                   d S r2   )r  r   r   r   s    r   r   z5MigrationAutodetector.generate_altered_index_together  s    ++J,IJJJJJr   c                    | j                             | j        | j                  }t	          |          D ]\  }}| j                            ||f|          }| j        j        ||f         }| j	        j        ||f         }|j
                            d          }|j
                            d          }||k    r*|                     |t          j        ||                     d S )Ndb_table)rV   table)r   unionr   r   rP   r   ri   r   r   r   rh   r   r   AlterModelTable)	r   models_to_checkr   r   r   r   rV  old_db_table_namenew_db_table_names	            r   r   z/MigrationAutodetector.generate_altered_db_table  s
   .44 $":
 
 &,O%<%< 	 	!Iz!044J' N #o4Y5NOO"m29j3HIO / 7 ; ;J G G / 7 ; ;J G G $555"".'/    	 	r   c                    | j                             | j        | j                  }t	          |          D ]\  }}| j                            ||f|          }| j        j        ||f         }| j	        j        ||f         }|j
                            d          }|j
                            d          }||k    r*|                     |t          j        ||                     d S )Ndb_table_comment)rV   table_comment)r   r  r   r   rP   r   ri   r   r   r   rh   r   r   AlterModelTableComment)	r   r  r   r   r   r   rV  old_db_table_commentnew_db_table_comments	            r   rv   z7MigrationAutodetector.generate_altered_db_table_comment  s   .44 $":
 
 &,O%<%< 	 	!Iz!044J' N #o4Y5NOO"m29j3HIO#2#:#>#>?Q#R#R #2#:#>#>?Q#R#R #';;;""5'&:    	 	r   c                 (   | j                             | j        | j        | j        | j        z  | j        | j        z            }t          |          D ]\  }}| j	        
                    ||f|          }| j        j        ||f         }| j        j        ||f         }d |j                                        D             }d |j                                        D             }||k    r*|                     |t#          j        ||                     dS )z
        Work out if any non-schema-affecting options have changed and make an
        operation to represent them in state changes (in case Python code in
        migrations needs them).
        c                 6    i | ]\  }}|t           j        v ||S r   r	   ALTER_OPTION_KEYSr   r6   r/   s      r   r7   zBMigrationAutodetector.generate_altered_options.<locals>.<dictcomp>?  7       C+=== U===r   c                 6    i | ]\  }}|t           j        v ||S r   r  r  s      r   r7   zBMigrationAutodetector.generate_altered_options.<locals>.<dictcomp>D  r  r   )rV   rh   N)r   r  r   r   rd   re   rb   rg   rP   r   ri   r   r   r   rh   r@   r   r   r	   )	r   r  r   r   r   r   rV  old_optionsnew_optionss	            r   rt   z.MigrationAutodetector.generate_altered_options*  sV    .44 $#d&99$"99
 
 &,O%<%< 	 	!Iz!044J' N #o4Y5NOO"m29j3HIO "1"9"?"?"A"A  K
 "1"9"?"?"A"A  K
 k))""0' +    #	 	r   c           
      <   t          | j                  D ]\  }}| j                            ||f|          }| j        j        ||f         }| j        j        ||f         }|j                            d          |j                            d          k    rg }|j                            d          r$|                    |||j        d         df           | 	                    |t          j        ||j                            d                    |           d S )Nr   Tr!  r  )rP   r   r   ri   r   r   r   rh   rS   r   r   r   )r   r   r   r   r   rV  r   s          r   r   z<MigrationAutodetector.generate_altered_order_with_respect_toR  s]   %+D,@%A%A 	 	!Iz!044J' N #o4Y5NOO"m29j3HIO&**'  (,,-DEEF F
  ""*../FGG  ''%&+34KL 	   ""6'.=.E.I.I3/ /   ". # 	 	 	-	 	r   c                 B   t          | j                  D ]\  }}| j                            ||f|          }| j        j        ||f         }| j        j        ||f         }|j        |j        k    r/|                     |t          j
        ||j                             d S )N)rV   r  )rP   r   r   ri   r   r   r   r  r   r   AlterModelManagers)r   r   r   r   r   rV  s         r   ru   z/MigrationAutodetector.generate_altered_managerst  s    %+D,@%A%A 	 	!Iz!044J' N #o4Y5NOO"m29j3HIO'?+CCC""1'!0!9    	 	r   c                 t   |                                 }i t          |                                          D ]I\  }}|s	d}|D ]}|d         |k    r|} n|1| j                            |          s|D ]}	|df||	j        f<   ||= S|d}
n |                     |d                   pddz   }
t          |          D ]\  }}	|dk    r|r|	j        	                    |           d|
z  g}|r|	                    |           nM|dk    r|s|	                    d           n/|	                    |	
                                dd                    d                    |          }||f||	j        f<   |
dz  }
||	_        K|                                D ]}|D ]}	fd	|	j        D             |	_         |S )
z
        Take a result from changes() and a MigrationGraph, and fix the names
        and dependencies of the changes so they extend the graph from the leaf
        nodes for each app.
        Nr   r   r9   z%04ir   d   r  c                 <    g | ]}                     ||          S r   ri   )r   r  name_maps     r   r0   z;MigrationAutodetector.arrange_for_graph.<locals>.<listcomp>  s4     * * *+,HLLA&&* * *r   )r   r=   r@   r   ask_initialrV   parse_number	enumerater   rS   suggest_namejoinr   )r   r)   r%   r(   leavesr   r   app_leafleafr   next_numberinew_name_partsr  r  s                 @r   r#   z'MigrationAutodetector.arrange_for_graph  s[    !!##%)'--//%:%: #	* #	*!Iz H  7i''#HE ( (C(CI(N(N!+ U UI=F<THi899I&#00!==BaG )* 5 5 * *966h6*11(;;;"(;"6!7! J")).9999!VVHV")))4444")))*@*@*B*B4C4*HIII88N339BH8M)Y^45q !)	* "..** 	 	J'  	* * * *090F* * *	&& r   c                   
 i 
|                                 D ]L\  }}|D ]D}|j        D ]:\  }}
                    |t                                                    |           ;EMt          |          }d}	|	|k    r+t          |          }	 |j        
fd|D               |	|k    +t          |          D ]	}||vr||= 
|S )a1  
        Take changes from arrange_for_graph() and set of app labels, and return
        a modified set of changes which trims out as many migrations that are
        not in app_labels as possible. Note that some other migrations may
        still be present as they may be required dependencies.
        Nc                 <    g | ]}                     |d           S )r   r  )r   r   app_dependenciess     r   r0   z7MigrationAutodetector._trim_to_apps.<locals>.<listcomp>  s*    UUU)"&&y"55UUUr   )r@   r   r   ra   rj   r   r=   )r   r)   
app_labelsr   r   r   r  rV   required_appsold_required_appsr  s             @r   r$   z#MigrationAutodetector._trim_to_apps  s    %,]]__ 	U 	U!Iz' U U	+4+A U U'M4$//	355AAEEmTTTTUU J =00 #M 2 2 M UUUU}UUU   =00 g 	' 	'I--I&r   c                     t          j        d|          x}rt          |d                   S t          j        d|          }|rt          |d                   S dS )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+)r9   z^\d+r   N)researchintmatch)clsrV   squashed_matchr  s       r   r   z"MigrationAutodetector.parse_number  s_      Y';TBBB> 	*~a()))$'' 	!uQx== tr   r2   )NNN)NN)NF)7r   
__module____qualname____doc__r    r)   r-   rY   r"   rn   ro   staticmethodr   r   r   r   r   r   r   rm   rq   rs   rp   rr   rw   r|   r   rc  r   rz  r   rx   r   r{   r}   ry   r   rz   r
  r  r  r~   r   r  r   r   r   rv   rt   r   ru   r#   r$   classmethodr   r   r   r   r   r      s       
 
G G G G
 
 
 
$ $ $L  i i i iV
 
 
.  ( 
Y 
Y \
Yp" p" p" p"d  .  $=K =K =K~R R R R  .A" A" A"FA A AF  B_ _ _B  <" <" <"|#I #I #IJJ J J1
 1
 1
fL L L
 
 
 uR uR uRnW W Wr	 	 		 	 	    .         \ D- - -^  2T T TS S S   L L LK K K  *  ,& & &P     D   2 2 2 2h  6   [  r   r   )rA   r  collectionsr   	itertoolsr   django.confr   	django.dbr   django.db.migrationsr   django.db.migrations.migrationr   &django.db.migrations.operations.modelsr	   django.db.migrations.optimizerr
   django.db.migrations.questionerr   django.db.migrations.utilsr   r   r   django.utils.topological_sortr   r   r   r   r   <module>r'     s?       				 # # # # # #                         + + + + + + 4 4 4 4 4 4 D D D D D D = = = = = = ? ? ? ? ? ?         
 B A A A A AJ J J J J J J J J Jr   