
    +h                        d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZ d dlmZ  G d d          ZdS )    N)chain)settings)models)
operations)	Migration)AlterModelOptions)MigrationOptimizer)MigrationQuestioner)COMPILED_REGEX_TYPERegexObjectresolve_relation)stable_topological_sortc                   b   e Zd ZdZd2dZd3dZd Zd Zd4dZd Z	d	 Z
ed
             Zd2dZd Zd Zd Zd5d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- Z.d. Z/d2d/Z0d0 Z1e2d1             Z3dS )6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      e/var/www/html/e360mart/e360mart_env/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>8   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=       C T**511  r   deconstruct   Nc                 :    g | ]}                     |          S r   r+   r-   s     r   r/   z:MigrationAutodetector.deep_deconstruct.<locals>.<listcomp>P   s'    @@@%&&u--@@@r   c                 B    i | ]\  }}|                     |          S r   r+   r4   s      r   r6   z:MigrationAutodetector.deep_deconstruct.<locals>.<dictcomp>Q   s=       "U ..u55  r   )
isinstancelisttupledictitems	functoolspartialfuncr,   argskeywordsr   r   typehasattrr7   r   Field)r   objdeconstructedpathrC   kwargss   `     r   r,   z&MigrationAutodetector.deep_deconstruct1   s    c4    	BBBBcBBBBU## 	GGGG3GGGGGGT"" 	   "%))++    Y.// 	Hd33CH==t?T?TUXUa?b?bcc011 	s###T"" 	 JS-(( 	OO--M#v|,, 2 -abb 1!.D$@@@@4@@@   &,llnn    Jr   c                     g }t          |                                          D ]K\  }}|                     |          }|j        r|j        j        r	|d         d= |                    |           L|S )z
        Return a definition of the fields that ignores field names and
        what related fields actually relate to. Used for detecting renames (as
        the related fields change during renames).
           to)sortedr?   r,   remote_fieldr   append)r   fields
fields_defnamefielddeconstructions         r   only_relation_agnostic_fieldsz3MigrationAutodetector.only_relation_agnostic_fieldsY   s     
!&,,..11 	. 	.KD%!22599N! ,e&8&> ,"1%d+n----r   c                    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                                         |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                  |                                   | !                                 | "                                 | #                                 | $                                 | %                                 | &                                 | '                                 | (                                 | )                                 | *                                 | +                                 | ,                                 | -                    |           | .                                 | j/        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)0generated_operationsaltered_indexesaltered_constraints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create_altered_indexescreate_altered_constraintsgenerate_removed_constraintsgenerate_removed_indexesgenerate_renamed_fields(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_changesg   sE   & %'!!#%  "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!::: ;"+|";";&**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 	$$&&& 	!!###((*** 	$$&&&$$&&&%%'''%%'''%%'''&&(((
 	##%%%'')))))+++%%'''$$&&&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_modelsrf   rR   r   r   r   
field_namer   s       r   r   z=MigrationAutodetector._prepare_field_lists.<locals>.<setcomp>   s~     
 
 
%	:"o4#''J(?LLM 
 
  
J/
 
 
 
r   c                 X    h | ]&\  }}j         j        ||f         j        D ]}|||f'S r   )r   r   rR   r   s       r   r   z=MigrationAutodetector._prepare_field_lists.<locals>.<setcomp>   s]     
 
 
%	:"m29j3HIP
 
  
J/
 
 
 
r   N)r_   rb   kept_model_keysr`   rc   kept_proxy_keysra   rd   kept_unmanaged_keysthrough_usersold_field_keysnew_field_keysr   s   `r   rk   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.rP   throughN)rO   r_   r   rf   r   r   rR   r?   rF   getattrrP   r   r   r   )r   r   r   old_model_nameold_model_stater   rU   through_keys           r   rl   z1MigrationAutodetector._generate_through_model_map   s    %+D,?%@%@ 	^ 	^!Iz!044i5LjYYN"o4Y5NOO%4%;%A%A%C%C ^ ^!
E5.11 ^ge>PR[]a6b6b ^"253E3MyZd"e"eK7@.R\6]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__Fr8   .rM   NT)r   r   splitlower)
dependencyresolved_app_labelresolved_object_names      r   _resolve_dependencyz)MigrationAutodetector._resolve_dependency   ss     a=M))u$$3:8ZPQ]3S3S3Y3YZ]3^3^0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 r1   lenr   xs     r   r2   z>MigrationAutodetector._build_migration_list.<locals>.<genexpr>  s(      IIc!ffIIIIIIr   FTr   r8   	__first__r   )r   dependencieszauto_%ic              3   4   K   | ]}t          |          V  d S r1   r   r   s     r   r2   z>MigrationAutodetector._build_migration_list.<locals>.<genexpr>G  s(      QQc!ffQQQQQQr   z)Cannot resolve operation dependencies: %rN)r   sumr[   valuesrO   r^   r<   
_auto_depsr   rf   check_dependencyrg   rT   
leaf_nodesrQ   updaterE   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   s    IId&?&F&F&H&HIIIII	 G	" $D$=>> 9n 9n	"uu!%d&?	&J!K!K * *I%)N-0UU*(3 !? !?
 (+040H0H0M0M--q6Y.. 483L3P3PQTUVQWY[3\3\ * *#'#8#8##N#N !*5:N$)E!* $2 ? %#3 !?$:$>$>QQ]^_Q`?a$b$b$b$b%(Vt%>%>$:$>$>AX[\]X^H_`bHcHh?i$j$j$j$j (1 	%? ,1 )^U5E5Ec!f5M5M )^,B,F,FuGWGWX[\]X^G_G_`aGb,c,c,c,c,B,F,FAP[G\,],],],]9>% y111$++,BCCC 5i@CC 
n7 
n4Y? 	n9 	n#'i\RTfhCiCi#j#j#+8IT_=P=PQZ\^=_=_9`9`cd9d,egp#q#q04\0B0B-.5++4D<N+N(229bAAHHRRR$)		?FIbclIm?m1)<QQd.G.N.N.P.PQQQQQKg%%  n $II$%PSWSl%lmmm!GO  G	" G	" G	" G	" G	"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   )r^   )r   ops     r   r6   z:MigrationAutodetector._sort_migrations.<locals>.<dictcomp>V  s    888bCEE888r   r   N)rO   r[   r?   r   r   r   rg   r   )r   r   opsdependency_graphr   r   op2s          r   r   z&MigrationAutodetector._sort_migrationsO  s    
 %T%>%D%D%F%FGG 	b 	bNIs88C888 > >= > >C 22377:C1v**#& > >C#44S#>> > 0 4 8 8 = = => 4K3P`3a3aD%i00	b 	b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 )Nr8   )r   r?   zipr   rQ   rT   r   r<   r^   r	   optimizer   )r   r   r   m1m2	migrations         r   r   z*MigrationAutodetector._optimize_migrationsd  s2   %)_%:%:%<%< 	= 	=!Izj*QRR.99 = =B&&	27';<<<<= /0022 	K 	KJ' K K	)-c)2H.I.I)J)J	&&K &*_%:%:%<%< 	f 	f!Iz' f f	'9';';'D'DYEY[d'e'e	$$f	f 	f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.
        rM   N   Tr8   c              3   6   K   | ]\  }}d          |k    V  dS )rM   Nr   )r   r   yr   s      r   r2   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   anyrR   AddFieldmodel_name_lowerRemoveFieldDeleteModel
AlterFieldAlterOrderWithRespectToorder_with_respect_toAlterUniqueTogetherAlterIndexTogetherr   )r   r   r   s     `r   r   z&MigrationAutodetector.check_dependencyt  sO    a= Z]d%:%:9j&<== >$
1(;(;(=(==
 ]&:a=D+@+@ y**@AA I(JqM,?,?,A,AAIHHHHy7GHHHHH	 y**=>> B.*Q-2E2E2G2GGB(JqM,?,?,A,AA ]&:a=E+A+A9j&<== >*jm.A.A.C.CC>$
1(;(;(=(== ]"z!}'='=9j&<== >$
1(;(;(=(==
 ]&:a=G+C+C9j&;<< >*jm.A.A.C.CC>$
1(;(;(=(== ]&:a=<M+M+M9j&HII Y$
1(;(;(=(==Y06B==??:a=CVCVCXCXX ]&:a=<Q+Q+Q9z'E'1'D'F G G >$
1(;(;(=(== *ZZIJJJr   Fc                     |pg |_         |r1| j                            |g                               d|           d S | j                            |g                               |           d S )Nr   )r   r[   r   insertrQ   )r   r   r   r   	beginnings        r   add_operationz#MigrationAutodetector.add_operation  st    +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   r8   	swappableAbstractUserAbstractBaseUser___)	r   r   basesre   rf   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:*,,"j00(..00N4H4H4J4JJJQa99 K  	 	 	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   rP   )r   	relationsrU   s      r   r/   zAMigrationAutodetector.generate_renamed_models.<locals>.<listcomp>  sW     J J J$--6-=-=-?-?J J %* !& 2J J J Jr   )old_namenew_namer   r   N)r   renamed_models_relrb   r_   rO   r   r   rW   rR   r   r   ask_rename_modelr<   r   r   is_relationextend!_get_dependencies_for_foreign_keyr   r   RenameModelrT   r   r   removerg   )r   added_modelsr   r   r   model_fields_defremoved_modelsrem_app_labelrem_model_namerem_model_staterem_model_fields_defr   rR   rU   renamed_models_rel_keys                  r   rj   z-MigrationAutodetector.generate_renamed_models  s    !"$*T-@@%+L%9%9 +	" +	"!Iz-.y*/DEK#AA+BTUU!043FFN1? &" &"-~ I--&*o&<]N=Z&[O+/+M+MoNd+e+e('+????;;O[YY !"+-L%)+*<*C*C*E*E%F%F J J151HT^I^1_1f1f1h1hJ J J &F
 *0 & &#(#4 !&$0$7$7(,(N(N,5z5$-)* )*%& %& %&
 !.. ) * 6-<-A-8-=!" !" !" .: /    JXD/	:0EF / 9 9 9 / : :62
 !, 5 5 5 + 6 6OD34JK !/66~7VWWW /33Y
4KLLL!EW+	" +	"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)r5   reverseNr   indexesconstraintsunique_togetherindex_togetherr   Fr   r8   )NTc                 (    g | ]}|d          v|S )r   r   )r   drelated_fieldss     r   r/   zAMigrationAutodetector.generate_created_models.<locals>.<listcomp>M  s(    ```!QqTQ_E_E_AE_E_E_r   rT   rR   re   r   managers)r   r   rY   )r   rT   rU   r   rT   r   c                     g | ]}|d f	S )Tr   )r   rT   r   r   s     r   r/   zAMigrationAutodetector.generate_created_models.<locals>.<listcomp>x  s2     $ $ $ Jd3$ $ $r   r   indexr   
constraintrT   r  rT   r  ).r_   ra   rb   rd   r   rO   r   r   r   rR   r?   rP   r   primary_keyparent_linkr   re   popr   r;   r   r   rQ   r   rf   r^   
differenceintersectionr   r   r   r   rT   r  r  r   r<   r   AddIndexAddConstraintr   r   r   r`   r   r   )"r   old_keysr  added_unmanaged_modelsall_added_modelsr   primary_key_relr   rU   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_fieldrT   related_dependenciesr  r  r   related_model_keyrelated_model_staterelated_field_namerelated_fieldr   r   r  s"                                  @@@r   rn   z-MigrationAutodetector.generate_created_models  sf    &)@@*X5!%!88!C <T%=tLLL)t/GQUVVV
 
 &6 W	 W	!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$;$;<SUY$Z$Z! Je4L $) h hdC(( hSD[[04

30B0B-NI ''D$(OPPP ,0?+A+E+E~W`Fa+b+b(+/=+?+C+C^U^D_+`+`(+ h0D h.12F2M.N.N.Y.Y07/ /&,{'9:: , 3F h h.(//L^`e0fgggg ##$'J $%   &$)````{'9'?'?'A'A```'/%+(1   *     &**9d;;   &n&:&:&<&<==  e#EEz5$-    ##Y
D$$GHHH""'#-!#  
 "&c,&7&7!8!8 #     % ""6'.C  
 #J0EtL"Jd;" # 
 
 
$ $ $ $ $">22$ $ $  !''Jd(KLLL   ""'#-#   "6 #     *  
"",#-#-   "6 #      ""2'(7   "6 #     ""1''5   "6 #    /I:&$*===9B9jCX9Y9_9_9a9a  5%~*.-*>?P*Q'=K=Q=Q=S=S 	 	9*M**/9&1+>+C%7&3  
 ,5j$*M)N +    	]W	 W	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.
        rZ   NFr   r8   Tr  r   )rc   r`   rO   r   r   re   rf   r   r;   r   r   rQ   r   r   r   rT   r  )	r   addedr   r   r   r   r   r-  r.  s	            r   rp   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   rT   Fr   r   r8   rM   rT   r   )rb   rd   r_   ra   r   rO   r   r   rR   r?   rP   r   r   re   r$  r   r   r   r   r   r   rQ   many_to_manyr   rf   r   r   rT   r<   r^   )r   new_keysdeleted_modelsdeleted_unmanaged_modelsall_deleted_modelsr   r   r   r  r   rU   r  r  rT   r   r   related_object_app_labelobject_namerelation_related_fieldsthrough_users                       r   rm   z-MigrationAutodetector.generate_deleted_models  s    &)@@,x7#'#:X#E "6.#9#96BZ;[;[\\%7 E	 E	!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)Z/06688
 
P7);9P *A)F)F)H)H  %J ''1;
ER   !- $++5{JPWX   ~.. J J##Y
D%$HIIII-119k>T2UVVL `##\!_l1o|TUX]$^___&$)   "#l"3"344     E	 E	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.rZ   r<  N)r`   rc   rO   r   r   re   rf   r   r   r   rT   )r   deletedr   r   r   s        r   ro   z.MigrationAutodetector.generate_deleted_proxies(  s    %(;;%+G__ 	 	!Iz/0J1FGK&**733333&$)     	 	r   c           
         i | _         t          | j        | j        z
            D ]\  }}}| j                            ||f|          }| j        j        ||f         }| j        j        ||f         }|	                    |          }| 
                    |          }t          | j        | j        z
            D ]\  }	}
}|	|k    rx|
|k    rq|	                    |          }| 
                    |          }|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                            ||||          rt|                     |t)          j        |||                     | j                            |	|
|f           | j                            |||f           || j         |||f<    ndS )zWork out renamed fields.rN   rM   r8   r   )	db_column)r   r   r   N)renamed_fieldsrO   r   r   r   rf   r   r   r   	get_fieldr,   rP   r   r   set_attributes_from_nameget_attname_columnr>   r   
ask_renamer   r   RenameFieldr  rg   )r   r   r   r   r   r   new_model_staterU   	field_decr
  r  rem_field_name	old_fieldold_field_dec
old_rel_toold_db_columns                   r   rw   z-MigrationAutodetector.generate_renamed_fields5  s    178KdNa8a1b1b "	" "	"-Iz:!044i5LjYYN"o4Y5NOO"m29j3HIO#--j99E--e44IAGH[^b^qHqArAr " "=~~ I--.J2N2N / 9 9. I II$($9$9)$D$DM) Ye.@.F Y4S`abScKcKc%21%5d%;
%)@@@595LZ5XM!,T266~FFF$-$@$@$B$B1$EM%22 *!A#.)AaC.@@ q!1]KKKyYZ|[[?55j.R\^cdd " .. ) * 6/9-;-7!" !" !"   !/66~We7fggg /33Y
J4WXXXUcD/	:z0QR!EE"	" "	"r   c                 z    t          | j        | j        z
            D ]\  }}}|                     |||           dS )zMake AddField operations.N)rO   r   r   _generate_added_fieldr   r   r   r   s       r   r{   z+MigrationAutodetector.generate_added_fields\  sT    178KdNa8a1b1b 	J 	J-Iz:&&y*jIIII	J 	Jr   c           	         | j         j        ||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                            ||          |_        |                     |t1          j        ||||          |           d S )Nr   rT   rU   preserve_defaultr   )r   r   rK  rP   r   r  r  	DateFieldDateTimeField	TimeFieldnullhas_defaultr=  blankempty_strings_allowedr;   auto_nowcloneauto_now_addr   ask_auto_now_add_additiondefaultask_not_null_additionr   r   r   )r   r   r   r   rU   r   time_fieldsr\  s           r   rX  z+MigrationAutodetector._generate_added_fielda  s   $Y
%:;EEjQQ 	%"4": 	 F F:udm! !   
 ')=v?OPJ @%++-- @1C @[8U8@{++> 	
   	^KKMME%-- ^%2D ^ $ I I*V` a a $ E EjR\ ] ]%!1	   & 	 		
 		
 		
 		
 		
r   c                 z    t          | j        | j        z
            D ]\  }}}|                     |||           dS )zMake RemoveField operations.N)rO   r   r   _generate_removed_fieldrY  s       r   rz   z-MigrationAutodetector.generate_removed_fields  sT    178KdNa8a1b1b 	L 	L-Iz:((J
KKKK	L 	Lr   c           	      r    |                      |t          j        ||          |||df|||dfg           d S )Nr;  r   r   r   )r   r   r   rY  s       r   rl  z-MigrationAutodetector._generate_removed_field  sh    "%   J
4EFJ
4IJ 	 	
 	
 	
 	
 	
r   c           
          t           j         j        z            D ]Y\  }}} 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|j        o|j        }|j         o|j         }|s|rd	}|j        ri|j        sb|                                sN|j        sG|                                } j                            ||          }|t          j        ur	||_        d
}n|}                     |tA          j!        ||||          |           + "                    |||            #                    |||           [dS )zp
        Make AlterField operations, or possibly RemovedField/AddField if alter
        isn't possible.
        rP   r   Nr   from_fieldsc                 N    g | ]!}j                             |fz   |          "S r   rJ  rf   )r   
from_fieldfrom_rename_keyr   s     r   r/   zAMigrationAutodetector.generate_altered_fields.<locals>.<listcomp>  sD     3 3 3& +//:-0OQ[\\3 3 3r   c                 N    g | ]!}j                             |fz   |          "S r   rq  )r   to_field
rename_keyr   s     r   r/   zAMigrationAutodetector.generate_altered_fields.<locals>.<listcomp>  sC     1 1 1$ +//
h[0H(SS1 1 1r   r   TFr[  r   )$rO   r   r   r   rf   rJ  r   r   rK  r   rF   r   rP   r   r   r   r=   ro  	to_fieldsr  r  r   r,   r=  r`  ra  re  r   ask_not_null_alterationNOT_PROVIDEDrh  r   r   r   rl  rX  )r   r   r   r   r   old_field_namerS  	new_fieldr   remote_field_nameto_field_rename_keyro  rT  new_field_decboth_m2mneither_m2mr\  rU   new_defaultrs  rv  s   `                  @@r   r|   z-MigrationAutodetector.generate_altered_fields  s2   
 288KdNa8a1b1b K	R K	R-Iz:!044i5LjYYN!044iZ5XZdeeN.y./HISSTbccI,Y
-BCMMjYYIL y.11 gi>TV]_c6d6d -i.D.JIWabb
!4443<3I3OI*0$+I,BLRV$W$W!$ ^*48I7K*K'*d.AAA 8A7M7S	.4<E<R<]	.9%iEE 	'0*&=O,1 3 3 3 3 3*53 3 3 - -I) +0 1 1 1 1 1(1(;1 1 1 + +I' ##D$J$Jz9dm% %    	>22T	.	4@@T .i.D.LiYcdd
!4445>5K5SI*2 11)<<M 11)<<M--$1Li6L"+"88WAW=W R{ R'+$! *y~ *iF[F[F]F] * ) 6* ) 1 1&*o&M&MjZd&e&e&f.AAA,7EM/4, )&&!"-'1!+"'-=	   &2 ' 	 	 	 	 00J
SSS..y*jQQQWK	R K	R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   idxold_indexess     r   r/   z@MigrationAutodetector.create_altered_indexes.<locals>.<listcomp>  #    LLLsS5K5Ks5K5K5Kr   c                     g | ]}|v|	S r   r   )r   r  new_indexess     r   r/   z@MigrationAutodetector.create_altered_indexes.<locals>.<listcomp>  r  r   )added_indexesremoved_indexes)r   r'  option_namerO   r   r   rf   r   r   r   re   r\   r   )r   r  r   r   r   r   rP  add_idxrem_idxr  r  s            @@r   rs   z,MigrationAutodetector.create_altered_indexes  s    )5%+D,@%A%A 	 	!Iz!044i5LjYYN"o4Y5NOO"m29j3HIO)1+>K)1+>KLLLLkLLLGLLLLkLLLG ''J'%,* *)    	 	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  r;  )r\   r?   r   r   RemoveIndexrT   r  s        r   rv   z.MigrationAutodetector.generate_removed_indexes  s    484H4N4N4P4P 	 	0#Y
[$%67  ""*#-"Z     	 	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   r/   zDMigrationAutodetector.create_altered_constraints.<locals>.<listcomp>  #    VVVQQo=U=Uq=U=U=Ur   c                     g | ]}|v|	S r   r   )r   r  new_constraintss     r   r/   zDMigrationAutodetector.create_altered_constraints.<locals>.<listcomp>  r  r   )added_constraintsremoved_constraints)r   r(  r  rO   r   r   rf   r   r   r   re   r]   r   )r   r  r   r   r   r   rP  add_constraintsrem_constraintsr  r  s            @@r   rt   z0MigrationAutodetector.create_altered_constraints  s    .:%+D,@%A%A 	 	!Iz!044i5LjYYN"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$++J')8Q`* *-    	 	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_constraints#  s    8<8P8V8V8X8X 	 	4#Y
_-.AB  
"",#-#-     	 	r   c           	          | j                                         D ]B\  \  }}}|d         D ]1}|                     |t          j        ||j                             2Cd S )Nr  r;  )r]   r?   r   r   RemoveConstraintrT   r  s        r   ru   z2MigrationAutodetector.generate_removed_constraints.  s    8<8P8V8V8X8X 	 	4#Y
_-.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           r-t          || |          \  }}|	                    ||d df           |S )Nr   c              3   .   K   | ]}|j         k    V  d S r1   )rP   )r   r7  rU   s     r   r2   zJMigrationAutodetector._get_dependencies_for_foreign_key.<locals>.<genexpr>A  s?        % ]77     r   r   swappable_settingr   Tr   )
rF   rP   r   r   r?   r   r   r   r   rQ   )r   r   rU   project_stateremote_field_modelr   remote_app_labelremote_model_namerR   r  dep_app_labeldep_object_namer   through_app_labelthrough_object_names     `            r   r  z7MigrationAutodetector._get_dependencies_for_foreign_key9  sv   !5%w// 
	!&!3!9%/	:0EFIAJARAR  =5!#4v    )/      -=)R)R?P)R)R&E $E+>EE()M/OO-="Iz. .*M? 'tDE5%y$77 	V5E"Iz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 r1   rq  )r   nr   r   r   s     r   r2   zWMigrationAutodetector._get_altered_foo_together_operations.<locals>.<setcomp>.<genexpr>a  sQ         '++Y
A,FJJ     r   )r=   )r   uniquer   r   r   s     r   r   zMMigrationAutodetector._get_altered_foo_together_operations.<locals>.<setcomp>`  se       
 	       #      r   )rO   r   r   rf   r   r   r   re   r^   rK  rP   r   r  r  )r   r  r   r   rP  	old_value	new_valuer   foo_togethersr   rU   r   r   s   `          @@r   $_get_altered_foo_together_operationsz:MigrationAutodetector._get_altered_foo_together_operationsX  s     %+D,@%A%A !	 !	!Iz!044i5LjYYN"o4Y5NOO"m29j3HIO (/33K@@I &      
 (    !$  (/33K@@I*3>III%%!%.  M&3  
 / 9 9* E E - %2D2J (//0V0V ):udm1 1        7!	 !	r   c           
          |                      |j                  D ]H\  }}}}}|                    |          }|s|r'|                     | |dd|i|j        |i|           Id S NrT   r   r   r  r  r&  r   r   r   r  r  r   r   r   removal_values           r   &_generate_removed_altered_foo_togetherz<MigrationAutodetector._generate_removed_altered_foo_together|  s     66y7LMM	 	 
%229==M 	 ""IXX:X)2G1WXX!- #   	 	r   c                 D    |                      t          j                   d S r1   )r  r   r   r   s    r   rx   z>MigrationAutodetector.generate_removed_altered_unique_together  s    33J4RSSSSSr   c                 D    |                      t          j                   d S r1   )r  r   r   r   s    r   ry   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 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 r1   )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 r1   )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)rT   table)r   unionr   r   rO   r   rf   r   r   r   re   r   r   AlterModelTable)	r   models_to_checkr   r   r   r   rP  old_db_table_namenew_db_table_names	            r   r   z/MigrationAutodetector.generate_altered_db_table  s    .44T5I4Kcdd%+O%<%< 	 	!Iz!044i5LjYYN"o4Y5NOO"m29j3HIO / 7 ; ;J G G / 7 ; ;J G G $55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   r5   r.   s      r   r6   zBMigrationAutodetector.generate_altered_options.<locals>.<dictcomp>  7       )sE+=== U===r   c                 6    i | ]\  }}|t           j        v ||S r   r  r  s      r   r6   zBMigrationAutodetector.generate_altered_options.<locals>.<dictcomp>  r  r   )rT   re   N)r   r  r   r   ra   rb   r_   rd   rO   r   rf   r   r   r   re   r?   r   r   r   )	r   r  r   r   r   r   rP  old_optionsnew_optionss	            r   rq   z.MigrationAutodetector.generate_altered_options  sO    .44 $#d&99$"99
 
 &,O%<%< 	 	!Iz!044i5LjYYN"o4Y5NOO"m29j3HIO -<-D-J-J-L-L  K -<-D-J-J-L-L  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   )rO   r   r   rf   r   r   r   re   rQ   r   r   r   )r   r   r   r   r   rP  r   s          r   r}   z<MigrationAutodetector.generate_altered_order_with_respect_to  sN   %+D,@%A%A 	 	!Iz!044i5LjYYN"o4Y5NOO"m29j3HIO'++,CDD#+//0GHHI I  ""*../FGG  ''!"'/0GH	)    ""6'.=.E.I.IJa.b.b   ". #   #	 	r   c                 B   t          | j                  D ]\  }}| j                            ||f|          }| j        j        ||f         }| j        j        ||f         }|j        |j        k    r/|                     |t          j
        ||j                             d S )N)rT   r  )rO   r   r   rf   r   r   r   r  r   r   AlterModelManagers)r   r   r   r   r   rP  s         r   rr   z/MigrationAutodetector.generate_altered_managers  s    %+D,@%A%A 	 	!Iz!044i5LjYYN"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   r8   z%04ir   d   _c                 <    g | ]}                     ||          S r   rf   )r   r  name_maps     r   r/   z;MigrationAutodetector.arrange_for_graph.<locals>.<listcomp>5  s'    )])])](,,q!*<*<)])])]r   )r   r<   r?   r   ask_initialrT   parse_number	enumerater   rQ   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  sO    !!##%)'--//%:%: #	* #	*!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' ^ ^	)])])])]iF\)])])]	&&^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   r/   z7MigrationAutodetector._trim_to_apps.<locals>.<listcomp>J  s*    "f"f"f9#3#7#7	2#F#F"f"f"fr   )r?   r   r   r^   rg   r   r<   )r   r(   
app_labelsr   r   r   r  rT   required_appsold_required_appsr  s             @r   r#   z#MigrationAutodetector._trim_to_apps8  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 "f"f"f"fXe"f"f"fgg  =00 g 	' 	'I--I&r   c                 ^    t          j        d|          }|rt          |d                   S dS )z
        Given a migration name, try to extract a number from the beginning of
        it. If no number is found, return None.
        z^\d+r   N)rematchint)clsrT   r  s      r   r  z"MigrationAutodetector.parse_numberQ  s2     $'' 	!uQx== tr   r1   )NNN)NN)NF)4r   
__module____qualname____doc__r   r(   r,   rW   r!   rk   rl   staticmethodr   r   r   r   r   r   r   rj   rn   rp   rm   ro   rw   r{   rX  rz   rl  r|   rs   r   rv   rt   r   ru   r  r  r  rx   ry   r  r~   r   r   rq   r}   rr   r"   r#   classmethodr  r   r   r   r   r      s]       
 
G G G G
 
 
 
& & &P  b b b bH
 
 
0^ ^ ^ Y Y \YU" U" U" U"nb b b*f f f =K =K =K~R R R R  .4" 4" 4"lh h hT  BS S Sj  %" %" %"NJ J J

 
 
BL L L

 
 
 PR PR PRd  $	 	 		 	 	  $	 	 		 	 	   \<" " "H   T T TS S S   L L LK K K  "" " "H  6  0 0 0 0d  2   [  r   r   )r@   r  	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H H H H H H H H H Hr   