
    EvgPH                       d dl Z d dlZd dlmZ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 d dlmZ d dlmZmZmZ d dlmZ  G d d edd                    Z G d d          Z dS )    N)defaultdict
namedtuple)Enum)TopologicalSorter)chain)settings)models)
operations)	Migration)AlterModelOptions)MigrationOptimizer)MigrationQuestioner)COMPILED_REGEX_TYPERegexObjectresolve_relation)cached_propertyc                   V    e Zd Z G d de          Zed             Zed             ZdS )OperationDependencyc                   "    e Zd ZdZdZdZdZdZdS )OperationDependency.Typer               N)__name__
__module____qualname__CREATEREMOVEALTERREMOVE_ORDER_WRTALTER_FOO_TOGETHER     ^/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/django/db/migrations/autodetector.pyTyper      s+        r$   r&   c                 4    | j                                         S N)
model_namelowerselfs    r%   model_name_lowerz$OperationDependency.model_name_lower!       $$&&&r$   c                 4    | j                                         S r(   )
field_namer*   r+   s    r%   field_name_lowerz$OperationDependency.field_name_lower%   r.   r$   N)r   r   r   r   r&   r   r-   r1   r#   r$   r%   r   r      st            t    ' ' _' ' ' _' ' 'r$   r   z$app_label model_name field_name typec                   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#   r#   ).0appmodels      r%   	<setcomp>z1MigrationAutodetector.__init__.<locals>.<setcomp>;   s    FFFjc5cFFFr$   )
from_stateto_stater   
questionerr	   existing_apps)r,   r:   r;   r<   s       r%   __init__zMigrationAutodetector.__init__7   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%   rG   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r6   valuer,   s     r%   
<listcomp>z:MigrationAutodetector.deep_deconstruct.<locals>.<listcomp>P   s'    BBBUD))%00BBBr$   c              3   B   K   | ]}                     |          V  d S r(   rJ   rL   s     r%   	<genexpr>z9MigrationAutodetector.deep_deconstruct.<locals>.<genexpr>R   s1      GG%..u55GGGGGGr$   c                 B    i | ]\  }}|                     |          S r#   rJ   r6   keyrM   r,   s      r%   
<dictcomp>z:MigrationAutodetector.deep_deconstruct.<locals>.<dictcomp>T   s-    TTT*#uC..u55TTTr$   deconstructr   Nc                 :    g | ]}                     |          S r#   rJ   rL   s     r%   rN   z:MigrationAutodetector.deep_deconstruct.<locals>.<listcomp>i   s'    @@@%&&u--@@@r$   c                 B    i | ]\  }}|                     |          S r#   rJ   rR   s      r%   rT   z:MigrationAutodetector.deep_deconstruct.<locals>.<dictcomp>j   s-    TTTzsEd++E22TTTr$   )
isinstancelisttupledictitems	functoolspartialfuncrK   argskeywordsr   r   typehasattrrU   r	   Field)r,   objdeconstructedpathr`   kwargss   `     r%   rK   z&MigrationAutodetector.deep_deconstructI   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).
        r   toN)sortedr\   rK   remote_fieldr8   popappend)r,   fields
fields_defnamefielddeconstructions         r%   only_relation_agnostic_fieldsz3MigrationAutodetector.only_relation_agnostic_fieldso   s     
!&,,..11 	. 	.KD%!22599N! 2e&8&> 2q!%%dD111n----r$   c                 &	   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                                 | j1        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)2generated_operationsaltered_indexesaltered_constraintsrenamed_fieldssetold_model_keysold_proxy_keysold_unmanaged_keysnew_model_keysnew_proxy_keysnew_unmanaged_keysr:   r	   r\   optionsgetadd	real_appsr;   resolve_fields_and_relationsgenerate_renamed_models_prepare_field_lists_generate_through_model_mapgenerate_deleted_modelsgenerate_created_modelsgenerate_deleted_proxiesgenerate_created_proxiesgenerate_altered_optionsgenerate_altered_managers!generate_altered_db_table_commentcreate_renamed_fieldscreate_altered_indexescreate_altered_constraintsgenerate_removed_constraintsgenerate_removed_indexesgenerate_renamed_fieldsgenerate_renamed_indexes(generate_removed_altered_unique_togethergenerate_removed_fieldsgenerate_added_fieldsgenerate_altered_fields&generate_altered_order_with_respect_to generate_altered_unique_togethergenerate_added_indexesgenerate_added_constraintsgenerate_altered_db_table_sort_migrations_build_migration_list_optimize_migrations
migrations)r,   rE   rC   	app_labelr)   model_states         r%   r@   z%MigrationAutodetector._detect_changes}   sa   & %'!!#%   "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$$&&&""$$$$$&&&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_modelsr   ro   r6   r   r)   r0   r,   s       r%   r9   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	   ro   r   s       r%   r9   z=MigrationAutodetector._prepare_field_lists.<locals>.<setcomp>   s]     
 
 
%	:"m29j3HIP
 
  
J/
 
 
 
r$   N)r}   r   kept_model_keysr~   r   kept_proxy_keysr   r   kept_unmanaged_keysthrough_usersold_field_keysnew_field_keysr+   s   `r%   r   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.rl   throughN)rk   r}   r   r   r:   r	   ro   r\   rc   getattrrl   r   r   r   )r,   r   r)   old_model_nameold_model_stater0   rr   through_keys           r%   r   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                     | j         dk    r| dfS t          t          | j                                      d          \  }}t          ||                                | j        | j                  dfS )zp
        Return the resolved dependency and a boolean denoting whether or not
        it was swappable.
        __setting__F.T)	r   r   r   r)   splitr   r*   r0   rb   )
dependencyresolved_app_labelresolved_object_names      r%   _resolve_dependencyz)MigrationAutodetector._resolve_dependency  s     =00u$$3:j+4
 4

%** 	10  "$**,,%	  
 	
r$   c           
         i | _         t          d | j                                        D                       }d}|rt	          | j                  D ]}g }t                      }t          | j        |                   D ]}d}t                      }	|j        D ]G}
|
}|                     |
          \  }
}|
j	        |k    r| j        
                    |
j	        g           D ]}|                     ||
          rd} n|s n|r"|	                    |j	        |j        f           |
j	        | j         v r8|	                    |
j	        | j         |
j	                 d         j        f           |ro|rO|                    |
j	                  r5|	                    |                    |
j	                  d                    '|	                    |
j	        df           Ed}I|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t5          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 r(   lenr6   xs     r%   rP   z>MigrationAutodetector._build_migration_list.<locals>.<genexpr>2  s(      IIc!ffIIIIIIr$   FTr   	__first__r   )r
   dependencieszauto_%ir   c              3   4   K   | ]}t          |          V  d S r(   r   r   s     r%   rP   z>MigrationAutodetector._build_migration_list.<locals>.<genexpr>  s(      QQc!ffQQQQQQr$   z)Cannot resolve operation dependencies: %rN)r   sumrx   valuesrk   r|   rY   
_auto_depsr   r   r   check_dependencyr   r)   rq   
leaf_nodesrn   updaterb   r   r   r   r
   r=   initial
setdefault
ValueError)r,   rC   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	 h	" $D$=>> W W	"uu!%d&?	&J!K!K ? ?I%)N-0UU*(3 6? 6?
 (+040H0H0M0M--=I55 483L3P3P #r4 4 * * $(#8#8##N#N !*5:N$)E!* $2 &? %#3 #!?$:$>$>,8,B,8,C)*%& %& %& %& &)]do%E%E$:$>$>,/M,0OCM,J2,N,S)*%& %& %& %& (1 %? ,1 ).U5E5Ecm5T5T ).,B,F,F050@0@0O0OPQ0R-. -. -. -. -C,F,F140L-. -. -. -. :?% 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   "GQ  h	" h	" h	" h	" h	"r$   c                     t           j                                                  D ]\  }}t                      }|D ]c}|                    |           |j        D ]D                               d         j        |k    r) |j        |g fd|D             R   Edt          |	                                           j        |<   dS )z{
        Reorder to make things possible. Reordering may be needed so FKs work
        nicely inside the same app.
        r   c              3   H   K   | ]}                     |          |V  d S r(   )r   )r6   r   r   r,   s     r%   rP   z9MigrationAutodetector._sort_migrations.<locals>.<genexpr>  s8       Q Qq43H3HC3P3P Q Q Q Q Q Q Qr$   N)
rk   rx   r\   r   r   r   r   r   rY   static_order)r,   r   opstsopr   s   `    @r%   r   z&MigrationAutodetector._sort_migrations  s   
 %T%>%D%D%F%FGG 	K 	KNIs"$$B S Sr


= S SC 22377:C}	11 BF2R Q Q Q Q QC Q Q QRRRRRS 488I8I3J3JD%i00	K 	K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 )Nr   )r   r\   zipr   rn   rq   r   rY   r|   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                    j         Dj        t          j        j        k    r*t          |t          j                  o|j        j	        k    S j         j        t          j        j        k    rt          |t          j                  o/|j        j	        k    ot          fd|j        D                       p9t          |t          j                  o|j	        j	        k    o|j        j        k    S j         Tj        t          j        j        k    r:t          |t          j                  o|j	        j	        k    o|j        j        k    S j         Dj        t          j        j        k    r*t          |t          j                  o|j        j	        k    S j         Tj        t          j        j        k    r:t          |t          j                  o|j	        j	        k    o|j        j        k    S j         hj        t          j        j        k    rNt          |t          j                  o3|j        j	        k    o#|j        pd                                j        k    S j         Pj        t          j        j        k    r6t          |t          j        t          j        f          o|j        j	        k    S t5          d          )zn
        Return True if the given operation depends on the given dependency,
        False otherwise.
        Nc              3   4   K   | ]\  }}j         |k    V  d S r(   )r0   )r6   r   yr   s      r%   rP   z9MigrationAutodetector.check_dependency.<locals>.<genexpr>  s/      PPtq!
-2PPPPPPr$    zCan't handle dependency )r0   rb   r   r&   r   rX   r
   CreateModel
name_lowerr-   anyro   AddFieldr1   r   RemoveFieldDeleteModelr    
AlterFieldr!   AlterOrderWithRespectToorder_with_respect_tor*   r"   AlterUniqueTogetherAlterIndexTogetherr   )r,   r   r   s     `r%   r   z&MigrationAutodetector.check_dependency  s    !)#6#;#BBB 9j&<== H(J,GG !-#6#;#BBB 9j&<== Q(J,GGQPPPPy?OPPPPP
 9j&9:: H.*2MMH(J,GG !-#6#;#BBB 9j&<== H.*2MMH(J,GG !)#6#;#BBB 9j&<== H(J,GG !-#6#;#AAA 9j&;<< H.*2MMH(J,GG !-#6#;#LLL 9j&HII /(J,GG/4:AACC./ !-#6#;#NNN 3Z5RS  H (J,GG *ZZIJJJr$   Fc                     |pg |_         |r1| j                            |g                               d|           d S | j                            |g                               |           d S )Nr   )r   rx   r   insertrn   )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#   )rX   strr   )r6   bases     r%   r9   z<MigrationAutodetector.swappable_first_key.<locals>.<setcomp>&  s=        #4--@4=  r$   r   r   r   	swappableAbstractUserAbstractBaseUser___)	r;   r	   basesr   r   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   rl   )r6   	relationsrr   s      r%   rN   zAMigrationAutodetector.generate_renamed_models.<locals>.<listcomp>Q  sY     J J J$- .7-=-=-?-?J J
 %*	 !& 2J J J Jr$   )old_namenew_namer   r   N)r   renamed_models_relr   r}   rk   r;   r	   rt   ro   r:   r<   ask_rename_modelrY   r   r  is_relationextend!_get_dependencies_for_foreign_keyr   r
   RenameModelrq   r   r   remover   )r,   added_modelsr   r)   r   model_fields_defremoved_modelsrem_app_labelrem_model_namerem_model_staterem_model_fields_defr   ro   rr   renamed_models_rel_keys                  r%   r   z-MigrationAutodetector.generate_renamed_models6  s    !"$*T-@@%+L%9%9 9	" 9	"!Iz-.y*/DEK#AA+BTUU!043FFN1? 4" 4"-~ 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 %0$9$9$9$/$:$:!" !34JK !/66~7VWWW /33Y
4KLLL!Es9	" 9	"r$   c                     | j         | j        z  }| j        |z
  }| j        |z
  }t	          t          || j        d          t          || j        d                    }|D ]\  | 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          }t%          dt$          j        j                  g}|j        D ].}t-          |t.                    rd	|v r|                    d	d
          \  }}|                    t%          ||dt$          j        j                             | j        j                            ||f          }| j        j                            ||f          }|r|r}t;          |j	                                      |j	                                      |j	                  }|D ]6}|                    t%          |||t$          j        j                             70|rA|                    t%          g tA          |          dt$          j        j        R             | !                    tE          j#        |j$         fd|j	        
                                D             |j        |j        |j%                  |d           |j                            dd          st           
                                          D ]\  }}| &                    || j                  }|                    t%          dt$          j        j                             | !                    tE          j'        ||          tQ          t;          |                               |rl| !                    tE          j)        |          t%          |t$          j        j                  t%          dt$          j        j                  g           fdt                     D             }|                    t%          dt$          j        j                             |	D ].}| !                    tE          j*        |          |           /|
D ].}| !                    tE          j+        |          |           /|r,| !                    tE          j,        |          |           | j        j-        }f| j.        v r|f         
                                D ]\  } | j        j        |         } 
                                D ]\\  }}| !                    |j/        tE          j0        |j$        ||          t%          dt$          j        j                  g           ]dS )ar  
        Find all new models (both managed and unmanaged) and make create
        operations for them as well as separate operations to create any
        foreign key or M2M relationships (these are optimized later, if
        possible).

        Defer any model options that refer to collections of fields that might
        be deferred (e.g. unique_together).
        T)rS   reverseNr   indexesconstraintsunique_togetherr   r   r   c                 (    g | ]}|d          v|S )r   r#   )r6   drelated_fieldss     r%   rN   zAMigrationAutodetector.generate_created_models.<locals>.<listcomp>  s2       Q4~55 555r$   rq   ro   r   r  managers)r   r   rv   r)   rq   rr   r  rq   r   c                 R    g | ]#}t          |t           j        j                  $S r#   )r   r&   r   )r6   rq   r   r)   s     r%   rN   zAMigrationAutodetector.generate_created_models.<locals>.<listcomp>  sE     $ $ $  $z41D1I1P $ $ $r$   r)   indexr)   
constraintrq   r(  )1r}   r   r   r   r   rk   r  r;   r	   ro   r\   rl   r8   primary_keyparent_linkr   r   rm   r   r&   r   r  rX   r  r   rn   r   r:   r   r|   
differenceintersectionr   r   r
   r   rq   r-  r  r   rY   r   AddIndexAddConstraintr   r  r~   r   r   )!r,   old_keysr  added_unmanaged_modelsall_added_modelsr   primary_key_relr0   rr   r&  r'  r(  r   r   r  base_app_label	base_nameold_base_model_statenew_base_model_stateremoved_base_fieldsremoved_base_fieldrq   related_dependenciesr2  r4  r  related_model_keyrelated_model_staterelated_field_namerelated_fieldr   r)   r+  s!                                 @@@r%   r   z-MigrationAutodetector.generate_created_modelsz  s    &)@@*X5!%!88!C <T%=tLLL)t/GQUVVV
 
 &6 J	 J	!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$/$7$;$;'% %!
 $z41D1I1P L $) # #dC(( "SD[[04

30B0B-NI ''+*% /4;	    ,0?+A+E+E'3, ,( ,0=+?+C+C'3, ,( , 0D  4 ;<<'Z 4 ;  *\+*<== , 3F  .(// 3$2$-$6$7$<$C	!" !"     ##' )/9jQQ ,07     &$)   !,!3!9!9!;!;  
 (/%+(1
 
 
 *    $ &**9d;;   &n&:&:&<&<==  e#EEM	    ##'!:t5H5M5T    ""'#-!#  
 "&c,&7&7!8!8 #     % ""6'.C  
 ,%&1/4;	  ,%z49L9Q9X 
" #   $$ $ $ $ $ #>22	$ $ $  !''#z41D1I1P   
 !  ""'#-#   "6 #     *  
"",#-#-   "6 #      ""2'(7   "6 #    /I:&$*===9Bz):%'' 5%~ +/-*>?P*Q'=K=Q=Q=S=S  9*M**/9&1+>+C%7&3   !4$-$.$($7$<$C	!" !"* +    uJ	 J	r$   c                 \   | j         | j        z
  }t          |          D ]\  }}| j        j        ||f         }|j                            d          sJ t          ||dt          j        j	                  g}|j
        D ]h}t          |t                    rQd|v rM|                    dd          \  }}|                    t          ||dt          j        j                             i|                     |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.
        rw   Nr   r   r,  r  )r   r~   rk   r;   r	   r   r   r   r&   r   r  rX   r  r   rn   r   r   r
   r   rq   r-  )	r,   addedr   r)   r   r   r  r@  rA  s	            r%   r   z.MigrationAutodetector.generate_created_proxiesW  si    #d&99%+E]] !	 !	!Iz-.y*/DEK&**733333 $z41D1I1P L $) 
 
dC(( 	SD[[04

30B0B-NI ''+*% /4;	    &$)'/%+(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          }|r*|                     |t!          j        |d                     t          |          D ],}|                     |t!          j        ||                     -g }| j        j        }|||f         	                                D ]\  \  }}}|	                                D ]t\  }	}
|                    t+          |||	t*          j        j                             |
j        s4|                    t+          |||	t*          j        j                             ut          |          D ]6}|                    t+          |||t*          j        j                             7| j                            ||j        f          }|r0|                    t+          g |t*          j        j        R             |                     |t!          j        |j                  t?          tA          |                               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(  r5  r)   rq   rq   r  )!r   r   r}   r   r   rk   r:   r	   ro   r\   rl   r8   r   r   rm   r   r
   r   r   r  rn   r   r&   r   many_to_manyr    r   r   r   r   rq   rY   r|   )r,   new_keysdeleted_modelsdeleted_unmanaged_modelsall_deleted_modelsr   r)   r   r+  r0   rr   r(  rq   r   r  related_object_app_labelobject_namerelation_related_fieldsthrough_users                      r%   r   z-MigrationAutodetector.generate_deleted_models  s    &)@@,x7#'#:X#E ">""F+C$D$D
 
 &8 M	 M	!Iz/0J1FGKN%0%7%=%=%?%? ; ;!
E% ;)/ ;5:z2u19dCC ;5:z2)1556GNNO ""2'(,     ~..  ""*#-!      L1I +4Iz4I*J*P*P*R*R  ' (&)@)F)F)H)H  %J ''+4'&/4;	    !- $++/ 8 + * 3 8 >	   & ~..  ##'!:t5H5M5T      -119k>T2UVVL ##'WW7J7O7VWWW   &$)   "#l"3"344     OM	 M	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.rw   rO  N)r~   r   rk   r:   r	   r   r   r   r
   r   rq   )r,   deletedr   r)   r   s        r%   r   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.rj   r   r   r   )	db_columnN)renamed_operationsr   copyrk   r   r   r   r:   r	   r;   	get_fieldrK   rl   r8   r  set_attributes_from_nameget_attname_columnr[   r<   
ask_renamern   r\  r  r   r{   )r,   r   r   r)   r0   r   r   new_model_staterr   	field_decr  r   rem_field_name	old_fieldold_field_dec
old_rel_toold_db_columns                    r%   r   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 . !/	:z0QR "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)r]  r\  clonerq   r   r
   r   RenameFieldr   r  r   )
r,   r  r   rem_db_columnre  r   r)   rr   r0   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)rk   r   r   _generate_added_fieldr,   r   r)   r0   s       r%   r   z+MigrationAutodetector.generate_added_fieldsP  s[    17$"552
 2
 	J 	J-Iz: &&y*jIIII	J 	Jr$   c           	      @   | j         j        ||f                             |          }t          |||t          j        j                  g}|j        r<|j        j        r0|                    | 	                    |||| j                              |j
        r(|                    |                     |                     t          j        t          j        t          j        f}|j        pR|                                p>|j        t          j        u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/t?          |j                  r| j                             ||           | !                    |tE          j#        ||||          |           d S )Nr)   rq   rr   preserve_defaultr  )$r;   r	   r_  r   r&   r   rl   r8   r  r  	generated%_get_dependencies_for_generated_field	DateFieldDateTimeField	TimeFieldnullhas_default
db_defaultNOT_PROVIDEDrP  blankempty_strings_allowedrX   auto_nowrk  auto_now_addr<   ask_auto_now_add_additiondefaultask_not_null_additionuniquecallable$ask_unique_callable_default_additionr   r
   r   )r,   r   r)   r0   rr   r   time_fieldsrt  s           r%   rp  z+MigrationAutodetector._generate_added_fieldW  sa   $Y
%:;EEjQQ  :z3F3K3R 
  	%"4": 	66M	    ? 	S J J5 Q QRRR ')=v?OPJ C  ""Cv'::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)rk   r   r   _generate_removed_fieldrq  s       r%   r   z-MigrationAutodetector.generate_removed_fields  s[    17$"552
 2
 	L 	L-Iz: ((J
KKKK	L 	Lr$   c                     |                      |t          j        ||          t          |||t          j        j                  t          |||t          j        j                  g           d S )NrN  r  )r   r
   r   r   r&   r!   r"   rq  s       r%   r  z-MigrationAutodetector._generate_removed_field  s    "%   $',=	  $',?	  	 	
 	
 	
 	
 	
r$   c           
          t           j         j        z            D ]\  }}} j                            ||f|          } j                            |||f|          } j        j        ||f                             |          } j	        j        ||f                             |          }g }t          |d          rrt          |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          }|r||ft!           fd|D                       |_        t!           fd|j        D                       |_        t          |dd          x}r-t!          |          |_        t!          |j                  |_        |                                         ||| 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        r||j        su|                                sa|j        t          j        u rN|j        sG|                                } j                            ||          }|t          j        ur	||_        d
}n|}                      |tC          j"        ||||          |            #                    |||            $                    |||           dS )zp
        Make AlterField operations, or possibly RemovedField/AddField if alter
        isn't possible.
        rl   r8   Nr0   from_fieldsc                 N    g | ]!}j                             |fz   |          "S r#   r{   r   )r6   
from_fieldfrom_rename_keyr,   s     r%   rN   zAMigrationAutodetector.generate_altered_fields.<locals>.<listcomp>  sJ        !+ !/33 /:- ?   r$   c                 N    g | ]!}j                             |fz   |          "S r#   r  )r6   to_field
rename_keyr,   s     r%   rN   zAMigrationAutodetector.generate_altered_fields.<locals>.<listcomp>  sC        ( !/33J(4LhWW  r$   r   TFrs  r  )%rk   r   r   r   r   r{   r:   r	   r_  r;   rc   r   rl   r   r8   r0   rZ   r  	to_fieldsr  r  r   rK   rP  rz  r{  r|  r}  rk  r<   ask_not_null_alterationr  r   r
   r   r  rp  )r,   r   r)   r0   r   old_field_namerf  	new_fieldr   remote_field_nameto_field_rename_keyr  old_from_fieldsrg  new_field_decboth_m2mneither_m2mrt  rr   new_defaultr  r  s   `                   @@r%   r   z-MigrationAutodetector.generate_altered_fields  s   
 28$"552
 2
 t	R t	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 0g&7 7 0 .*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 I'0*&=O,1     /:	  - -I) +0    ,5,?  + +I' +2)]D*Q*QQ I05o0F0F	-.3I4G.H.H	+##::!"!	    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* &0F4GGG ) 6 H !* 1 1&*o&M&M&
' ' 'f.AAA,7EM/4, )&&!"-'1!+"'-=	   &2 ' 	 	 	 	 00J
SSS..y*jQQQit	R t	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#   )r6   idxold_indexess     r%   rN   z@MigrationAutodetector.create_altered_indexes.<locals>.<listcomp>:  s#    RRRS3k;Q;QS;Q;Q;Qr$   c                     g | ]}|v|	S r#   r#   )r6   r  new_indexess     r%   rN   z@MigrationAutodetector.create_altered_indexes.<locals>.<listcomp>;  s#    TTTsS=S=Ss=S=S=Sr$   r   rq   ro   r   r   c                     g | ]}|v|	S r#   r#   )r6   r  remove_from_addeds     r%   rN   z@MigrationAutodetector.create_altered_indexes.<locals>.<listcomp>u  s+       ;L0L0L0L0L0Lr$   c                     g | ]}|v|	S r#   r#   )r6   r  remove_from_removeds     r%   rN   z@MigrationAutodetector.create_altered_indexes.<locals>.<listcomp>x  s+       #=P2P2P2P2P2Pr$   )added_indexesremoved_indexesrenamed_indexes)r
   r:  option_namer   rY   renamed_index_together_valuesrk   r   r   r   r:   r	   r;   r   rU   rm   rn   $_get_altered_foo_together_operationsr   r8  ro   r|   r   rq   ry   r   ) r,   r  r   r)   r   r   rc  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_r`   rh   renamed_indexr  r  r  r  s                                @@@@r%   r   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 ]U\  \  }}}|                     ||          }|d         D ].}|                     |t	          j        ||          |           /Vd S )Nr  r1  r  )ry   r\   _get_dependencies_for_modelr   r
   r:  )r,   r   r)   alt_indexesr   r2  s         r%   r   z,MigrationAutodetector.generate_added_indexes  s    484H4N4N4P4P 
	 
	0#Y
[;;IzRRL$_5  ""'#-#   ". #    
	 
	r$   c           	          | j                                         D ]B\  \  }}}|d         D ]1}|                     |t          j        ||j                             2Cd S )Nr  rN  )ry   r\   r   r
   RemoveIndexrq   )r,   r   r)   r  r2  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)ry   r\   r   r
   RenameIndex)r,   r   r)   r  r  r  r  s          r%   r   z.MigrationAutodetector.generate_renamed_indexes  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#   )r6   cold_constraintss     r%   rN   zDMigrationAutodetector.create_altered_constraints.<locals>.<listcomp>  #    VVVQQo=U=Uq=U=U=Ur$   c                     g | ]}|v|	S r#   r#   )r6   r  new_constraintss     r%   rN   zDMigrationAutodetector.create_altered_constraints.<locals>.<listcomp>  r  r$   )added_constraintsremoved_constraints)r
   r;  r  rk   r   r   r   r:   r	   r;   r   rz   r   )r,   r  r   r)   r   r   rc  add_constraintsrem_constraintsr  r  s            @@r%   r   z0MigrationAutodetector.create_altered_constraints  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 ]U\  \  }}}|                     ||          }|d         D ].}|                     |t	          j        ||          |           /Vd S )Nr  r3  r  )rz   r\   r  r   r
   r;  )r,   r   r)   alt_constraintsr   r4  s         r%   r   z0MigrationAutodetector.generate_added_constraints  s     #6<<>>	 	  
;;IzRRL-.AB  
"",#-#-   ". #    	 	r$   c           	          | j                                         D ]B\  \  }}}|d         D ]1}|                     |t          j        ||j                             2Cd S )Nr  rN  )rz   r\   r   r
   RemoveConstraintrq   )r,   r   r)   r  r4  s        r%   r   z2MigrationAutodetector.generate_removed_constraints  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          || |          \  }
}t          |
|d t          j
        j                  g}t          j        dd           rRt          j        j        | |          \  }}|                    t          ||d t          j
        j                             |S )Nr8   c              3   .   K   | ]}|j         k    V  d S r(   )rl   )r6   rJ  rr   s     r%   rP   zJMigrationAutodetector._get_dependencies_for_foreign_key.<locals>.<genexpr>  s?        % ]77     r$   r   swappable_settingr   r   )rc   rl   r8   r  r\   r   r   r   r   r   r&   r   r   rn   )r   r)   rr   project_stateremote_field_modelr  remote_app_labelremote_model_namero   r  dep_app_labeldep_object_namer   through_app_labelthrough_object_names     `            r%   r  z7MigrationAutodetector._get_dependencies_for_foreign_key  s   !5%w// 
	!&!3!9%/	:0EFIAJARAR  =5!#4v    )/      -=)R)R?P)R)R&E $E+>EE()M/OO-=". .*M?  6I6N6U 

 5%y$77 	5E"*6 622
 #%'',3	    r$   c           	         g }t          j        |j                  j        }t	          | j        | j        z
            }|D ]\  }}}| j        j         ||f                             |          }|j	        r|j	        j
        s	|j        |v r9|                    t          |||j        t          j        j                             |S r(   )r	   Q
expressionreferenced_base_fieldsrk   r   r   r;   r_  rl   r8   rq   rn   r   r&   r   )	r,   rr   r   r  newly_added_fieldsr   r)   added_field_nameadded_fields	            r%   rv  z;MigrationAutodetector._get_dependencies_for_generated_field  s    !'%*:!;!;!R#D$7$:M$MNN7I 	 	3Iz#3-.y*/DEOO  K (
-8-E-K
!%;;;##'!"#(+07	    r$   c           	          g }| j         j        ||f         }|j                                        D ]9}|j        r0|                    |                     |||| j                              :|S )z3Return foreign key dependencies of the given model.)r;   r	   ro   r   r  r  r  )r,   r   r)   r   r   rr   s         r%   r  z1MigrationAutodetector._get_dependencies_for_model%  s    m*9j+@A '..00 		 		E  ##::!"	    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 r(   r  )r6   nr   r)   r,   s     r%   rP   zWMigrationAutodetector._get_altered_foo_together_operations.<locals>.<setcomp>.<genexpr>A  sQ         +//J0JANN     r$   )rZ   )r6   r  r   r)   r,   s     r%   r9   zMMigrationAutodetector._get_altered_foo_together_operations.<locals>.<setcomp>@  se       
 	       !'      r$   )rk   r   r   r   r:   r	   r;   r   r|   r_  rl   r8   r  r  )r,   r  r   r   rc  r  r  r   foo_togethersr0   rr   r   r)   s   `          @@r%   r  z:MigrationAutodetector._get_altered_foo_together_operations5  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  )r6   rM   r   r)   r,   s     r%   r9   zOMigrationAutodetector._generate_removed_altered_foo_together.<locals>.<setcomp>m  sK       =i>STU U U U Ur$   rq   r  r#   )r  r  r
   r   r9  r   )r,   r   r  r  r   removal_valuer   r)   s   `     @@r%   &_generate_removed_altered_foo_togetherz<MigrationAutodetector._generate_removed_altered_foo_togetherd  s     66y7LMM	 	 
J999     !*  	 &229==M 	 ""I  ',5,A=+Q  ". #   !	 	r$   c                 D    |                      t          j                   d S r(   )r  r
   r   r+   s    r%   r   z>MigrationAutodetector.generate_removed_altered_unique_together}  s    33J4RSSSSSr$   c           
          |                      |j                  D ]J\  }}}}}|                    |          }||k    r'|                     | |dd|i|j        |i|           Kd S )Nrq   r  r#   )r  r  r9  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 r(   )r  r
   r   r+   s    r%   r   z6MigrationAutodetector.generate_altered_unique_together  s    ++J,JKKKKKr$   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)rq   table)r   unionr   r   rk   r   r   r:   r	   r;   r   r   r
   AlterModelTable)	r,   models_to_checkr   r)   r   r   rc  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)rq   table_comment)r   r  r   r   rk   r   r   r:   r	   r;   r   r   r
   AlterModelTableComment)	r,   r  r   r)   r   r   rc  old_db_table_commentnew_db_table_comments	            r%   r   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r6   rS   rM   s      r%   rT   zBMigrationAutodetector.generate_altered_options.<locals>.<dictcomp>  7       C+=== U===r$   c                 6    i | ]\  }}|t           j        v ||S r#   r  r  s      r%   rT   zBMigrationAutodetector.generate_altered_options.<locals>.<dictcomp>  r	  r$   )rq   r   N)r   r  r   r   r   r   r}   r   rk   r   r   r:   r	   r;   r   r\   r   r
   r   )	r,   r  r   r)   r   r   rc  old_optionsnew_optionss	            r%   r   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           
      r   t          | j                  D ] \  }}| j                            ||f|          }| j        j        ||f         }| j        j        ||f         }|j                            d          |j                            d          k    rg }|j                            d          r?|                    t          |||j        d         t          j
        j                             |                     |t          j        ||j                            d                    |           "d S )Nr   r/  r  )rk   r   r   r   r:   r	   r;   r   rn   r   r&   r   r   r
   r   )r,   r   r)   r   r   rc  r   s          r%   r   z<MigrationAutodetector.generate_altered_order_with_respect_to  sk   %+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/4;	    ""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)rq   r-  )rk   r   r   r   r:   r	   r;   r-  r   r
   AlterModelManagers)r,   r   r)   r   r   rc  s         r%   r   z/MigrationAutodetector.generate_altered_managers  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   r   z%04ir   d   r  c                 <    g | ]}                     ||          S r#   r   )r6   r*  name_maps     r%   rN   z;MigrationAutodetector.arrange_for_graph.<locals>.<listcomp>G  s4     * * *+,HLLA&&* * *r$   )r   rY   r\   r<   ask_initialrq   parse_number	enumerater   rn   suggest_namejoinr   )r,   rG   rC   rF   leavesr   r   app_leafleafr   next_numberinew_name_partsr  r  s                 @r%   rA   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  )r6   r   app_dependenciess     r%   rN   z7MigrationAutodetector._trim_to_apps.<locals>.<listcomp>_  s*    UUU)"&&y"55UUUr$   )r\   r   r   r|   r   r   rY   )r,   rG   
app_labelsr   r   r   r  rq   required_appsold_required_appsr"  s             @r%   rB   z#MigrationAutodetector._trim_to_appsL  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+)r   z^\d+r   N)researchintmatch)clsrq   squashed_matchr*  s       r%   r  z"MigrationAutodetector.parse_numberg  s_      Y';TBBB> 	*~a()))$'' 	!uQx== tr$   r(   )NNN)NN)NF)7r   r   r   __doc__r>   rG   rK   rt   r@   r   r   staticmethodr   r   r   r   r   r   r  r   r   r   r   r   r   r   r   rp  r   r  r   r   r   r   r   r   r   r   r  rv  r  r  r  r   r  r   r   r   r   r   r   rA   rB   classmethodr  r#   r$   r%   r3   r3   *   s       
 
G G G G
 
 
 
$ $ $L  g g g gR
 
 
.  ( 
 
 \
(v" v" v" v"pK K K$  $RK RK RKhR R R R  .B" B" B"H[ [ [z( ( (T] ] ]~  <" <" <"|#I #I #IJJ J J8
 8
 8
tL L L
 
 
4yR yR yRvW W Wr  	 	 	    .      + + \+Z  *   - - -^  2T T T   L L L  *  ,& & &P     D   2 2 2 2h  6   [  r$   r3   )!r]   r'  collectionsr   r   enumr   graphlibr   	itertoolsr   django.confr   	django.dbr	   django.db.migrationsr
   django.db.migrations.migrationr   &django.db.migrations.operations.modelsr   django.db.migrations.optimizerr   django.db.migrations.questionerr   django.db.migrations.utilsr   r   r   django.utils.functionalr   r   r3   r#   r$   r%   <module>r=     s       				 / / / / / / / /       & & & & & &                         + + + + + + 4 4 4 4 4 4 D D D D D D = = = = = = ? ? ? ? ? ?         
 4 3 3 3 3 3' ' ' ' 'J$&LMM' ' '&I I I I I I I I I Ir$   