
    h2                     t    S SK Jr  S SKJr  SSKJrJr  \ " S S5      5       r " S S\5      r " S	 S
5      r	g)    )total_ordering)ProjectState   )CircularDependencyErrorNodeNotFoundErrorc                   N    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rSrg)Node   zd
A single node in the migration graph. Contains direct links to adjacent
nodes in either direction.
c                 L    Xl         [        5       U l        [        5       U l        g N)keysetchildrenparents)selfr   s     L/var/www/html/env/lib/python3.13/site-packages/django/db/migrations/graph.py__init__Node.__init__   s    u    c                      U R                   U:H  $ r   r   r   others     r   __eq__Node.__eq__   s    xx5  r   c                      U R                   U:  $ r   r   r   s     r   __lt__Node.__lt__   s    xx%r   c                 ,    [        U R                  5      $ r   )hashr   r   s    r   __hash__Node.__hash__   s    DHH~r   c                      U R                   U   $ r   r   )r   items     r   __getitem__Node.__getitem__   s    xx~r   c                 ,    [        U R                  5      $ r   )strr   r!   s    r   __str__Node.__str__    s    488}r   c                 |    SU R                   R                  < SU R                  S   < SU R                  S   < S3$ )N<z: (r   , r   z)>)	__class____name__r   r!   s    r   __repr__Node.__repr__#   s*    #'>>#:#:DHHQKRSUUr   c                 :    U R                   R                  U5        g r   )r   add)r   childs     r   	add_childNode.add_child&   s    % r   c                 :    U R                   R                  U5        g r   )r   r4   )r   parents     r   
add_parentNode.add_parent)   s     r   )r   r   r   N)r0   
__module____qualname____firstlineno____doc__r   r   r   r"   r&   r*   r1   r6   r:   __static_attributes__ r   r   r	   r	      s5    

! V!!r   r	   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )	DummyNode-   a   
A node that doesn't correspond to a migration file on disk.
(A squashed migration that was removed, for example.)

After the migration graph is processed, all dummy nodes should be removed.
If there are any left, a nonexistent dependency error is raised.
c                 <   > [         TU ]  U5        X l        X0l        g r   )superr   originerror_message)r   r   rG   rH   r/   s       r   r   DummyNode.__init__6   s    *r   c                 T    [        U R                  U R                  U R                  S9e)N)rG   )r   rH   r   rG   r!   s    r   raise_errorDummyNode.raise_error;   s     2 2DHHT[[QQr   )rH   rG   )	r0   r<   r=   r>   r?   r   rK   r@   __classcell__)r/   s   @r   rC   rC   -   s    +
R Rr   rC   c                       \ rS rSrSrS rS rS rSS jrS r	S r
S	 rS
 rS rSS jrSS jrSS jrS rS rS rS rS rSS jrS rSrg)MigrationGraph?   a  
Represent the digraph of all migrations in a project.

Each migration is a node, and each dependency is an edge. There are
no implicit dependencies between numbered migrations - the numbering is
merely a convention to aid file listing. Every new numbered migration
has a declared dependency to the previous number, meaning that VCS
branch merges can be detected and resolved.

Migrations files can be marked as replacing another set of migrations -
this is to support the "squash" feature. The graph handler isn't responsible
for these; instead, the code to load them in here should examine the
migration files and if the replaced migrations are all either unapplied
or not present, it should ignore the replaced ones, load in just the
replacing migration, and repoint any dependencies that pointed to the
replaced migrations to point to the replacing one.

A node should be a tuple: (app_path, migration_name). The tree special-cases
things within an app - namely, root nodes and leaf nodes ignore dependencies
to other apps.
c                      0 U l         0 U l        g r   node_mapnodesr!   s    r   r   MigrationGraph.__init__V   s    
r   c                 t    XR                   ;  d   e[        U5      nX0R                   U'   X R                  U'   g r   )rS   r	   rT   )r   r   	migrationnodes       r   add_nodeMigrationGraph.add_nodeZ   s2    --'''Cy!c#

3r   c                 V    [        XU5      nX@R                  U'   S U R                  U'   g r   )rC   rS   rT   )r   r   rG   rH   rX   s        r   add_dummy_nodeMigrationGraph.add_dummy_node`   s'    m4!c

3r   c                    X R                   ;  a  SU< SU< 3nU R                  X!U5        X0R                   ;  a  SU< SU< 3nU R                  X1U5        U R                  U   R                  U R                  U   5        U R                  U   R	                  U R                  U   5        U(       d  U R                  5         gg)z
This may create dummy nodes if they don't yet exist. If
`skip_validation=True`, validate_consistency() should be called
afterward.
z
Migration z/ dependencies reference nonexistent child node z0 dependencies reference nonexistent parent node N)rT   r\   rS   r:   r6   validate_consistency)r   rW   r5   r9   skip_validationrH   s         r   add_dependencyMigrationGraph.add_dependencye   s     

" %.u6  -@# &/8  =Ae''f(=>f''e(<=%%' r   c                    [        U5      n U R                  U   nU GH	  nU R                  R                  US5        U R                  R                  US5      nU(       d  ME  UR                   HR  nUR                  R                  U5        UR                  U;  d  M0  UR                  U5        UR                  U5        MT     UR                   HR  nUR                  R                  U5        UR                  U;  d  M0  UR                  U5        UR                  U5        MT     GM     g! [         a  n[        SU< S3U5      UeSnAff = f)z
Remove each of the `replaced` nodes (when they exist). Any
dependencies that were referencing them are changed to reference the
`replacement` node instead.
z Unable to find replacement node zH. It was either never added to the migration graph, or has been removed.N)r   rS   KeyErrorr   rT   popr   r   remover   r6   r:   )	r   replacementreplacedreplacement_nodeerrreplaced_keyreplaced_noder5   r9   s	            r   remove_replaced_nodes$MigrationGraph.remove_replaced_nodes|   s$    x=	#}}[9 %LJJNN<. MM--lDAM}*33EMM((7 yy0(2259(()9: 4 ,33FOO**=9zz1(33F;(()9: 4 %  	#CNQ 		s   D- -
E7E		Ec                    U R                   R                  US5         U R                  R                  U5      n[        5       n[        5       nU HF  nU R                  R                  U5      nU(       d  M'  UR                  U5        XhR                  -  nMH     XV-  nUR                   HI  n	U	R                  R                  U5        U H%  nUR                  U	5        U	R                  U5        M'     MK     UR                   H  n
U
R                  R                  U5        M      g! [         a  n[	        SU< S3U5      UeSnAff = f)a  
The inverse operation to `remove_replaced_nodes`. Almost. Remove the
replacement node `replacement` and remap its child nodes to `replaced`
- the list of nodes it would have replaced. Don't remap its parent
nodes as they are expected to be correct already.
Nz"Unable to remove replacement node zP. It was either never added to the migration graph, or has been removed already.)rT   re   rS   rd   r   r   getr4   r   r   rf   r6   r:   )r   rg   rh   ri   rj   replaced_nodesreplaced_nodes_parentsr   rl   r5   r9   s              r   remove_replacement_node&MigrationGraph.remove_replacement_node   s,    	

{D)	#}}00= !$C MM--c2M}""=1&*?*??&	  	0%..EMM  !12!/''.  / "0 /
 '..FOO""#34 /-  	# ! 	
 	s   D% %
E/EEc                     U R                   R                  5        Vs/ s H*  n[        U[        5      (       d  M  UR	                  5       PM,       ngs  snf )z7Ensure there are no dummy nodes remaining in the graph.N)rS   values
isinstancerC   rK   )r   ns     r   r_   #MigrationGraph.validate_consistency   s7    "&--"6"6"8U"8QJq)<T"8UUs
   AAc                 ~    XR                   ;  a  [        SU< S3U5      eU R                  U R                  U   5      $ )z
Given a node, return a list of which previous nodes (dependencies) must
be applied, ending with the node itself. This is the list you would
follow if applying the migrations to a database.
Node  not a valid noderT   r   iterative_dfsrS   r   targets     r   forwards_planMigrationGraph.forwards_plan   s8     ##&$JFSS!!$--"788r   c                 |    XR                   ;  a  [        SU< S3U5      eU R                  U R                  U   SS9$ )z
Given a node, return a list of which dependent nodes (dependencies)
must be unapplied, ending with the node itself. This is the list you
would follow if removing the migrations from a database.
r{   r|   F)forwardsr}   r   s     r   backwards_planMigrationGraph.backwards_plan   s=     ##&$JFSS!!$--"7%!HHr   c                    / n[        5       nUS4/nU(       a  UR                  5       u  pgXd;   a  OU(       a-  UR                  U5        UR                  UR                  5        OQUR                  US45        U[        U(       a  UR                  OUR                  5       Vs/ s H  nUS4PM	     sn-  nU(       a  M  U$ s  snf )z6Iterative depth-first search for finding dependencies.FT)r   re   r4   appendr   sortedr   r   )	r   startr   visitedvisited_setstackrX   	processedrx   s	            r   r~   MigrationGraph.iterative_dfs   s    e #iikOD"%txx(dD\*#HDLL$--PP JP  e 	s   #CNc                    ^ [        5       nU R                   HY  m[        U4S jU R                  T   R                   5       5      (       d  M6  U(       a  UTS   :X  d  MH  UR                  T5        M[     [        U5      $ )zx
Return all root nodes - that is, nodes with no dependencies inside
their app. These are the starting point for an app.
c              3   8   >#    U  H  oS    TS    :g  v   M     g7fr   NrA   .0r   rX   s     r   	<genexpr>,MigrationGraph.root_nodes.<locals>.<genexpr>   s     L0Kq6T!W$0K   r   )r   rT   allrS   r   r4   r   )r   approotsrX   s      @r   
root_nodesMigrationGraph.root_nodes   s^    
 JJDLd0C0K0KLLL3$q'>		$	 
 e}r   c                    ^ [        5       nU R                   HY  m[        U4S jU R                  T   R                   5       5      (       d  M6  U(       a  UTS   :X  d  MH  UR                  T5        M[     [        U5      $ )a=  
Return all leaf nodes - that is, nodes with no dependents in their app.
These are the "most current" version of an app's schema.
Having more than one per app is technically an error, but one that
gets handled further up, in the interactive command - it's usually the
result of a VCS merge and needs some user input.
c              3   8   >#    U  H  oS    TS    :g  v   M     g7fr   rA   r   s     r   r   ,MigrationGraph.leaf_nodes.<locals>.<genexpr>  s     M0Lq6T!W$0Lr   r   )r   rT   r   rS   r   r4   r   )r   r   leavesrX   s      @r   
leaf_nodesMigrationGraph.leaf_nodes   s_     JJDMd0C0L0LMMM3$q'>

4 	 
 f~r   c                    [        U R                  5      nU(       a  UR                  5       nU/nU(       a  US   nU R                  U   R                   Hq  nUR
                  nX#;   a4  X3R                  U5      S  n[        SR                  S U 5       5      5      eX!;   d  MO  UR                  U5        UR                  U5          O   UR                  5       nU(       a  M  U(       a  M  g g )Nr.   c              3   ,   #    U  H
  nS U-  v   M     g7f)z%s.%sNrA   )r   rx   s     r   r   3MigrationGraph.ensure_not_cyclic.<locals>.<genexpr>  s     %A5agk5s   )r   rT   re   rS   r   r   indexr   joinr   rf   )r   todorX   r   topr5   cycles          r   ensure_not_cyclic MigrationGraph.ensure_not_cyclic  s     4::88:DFEBi!]]3/88E !99D} %kk$&7&9 :5 II%A5%AA  |T*D) 9 !99;D! % dr   c                 (    SU R                  5       -  $ )NzGraph: %s nodes, %s edges)_nodes_and_edgesr!   s    r   r*   MigrationGraph.__str__&  s    *T-B-B-DDDr   c                 l    U R                  5       u  pSU R                  R                  < SU< SU< S3$ )Nr-   z: nodes=z, edges=>)r   r/   r0   )r   rT   edgess      r   r1   MigrationGraph.__repr__)  s+    ,,.u-1^^-D-DeUSSr   c                     [        U R                  5      [        S U R                  R	                  5        5       5      4$ )Nc              3   L   #    U  H  n[        UR                  5      v   M     g 7fr   )lenr   )r   rX   s     r   r   2MigrationGraph._nodes_and_edges.<locals>.<genexpr>.  s       $
*@$C*@s   "$)r   rT   sumrS   rv   r!   s    r   r   MigrationGraph._nodes_and_edges-  s8    4:: $
*.--*>*>*@$
 !
 
 	
r   c                     / nU HA  nU R                  U5       H)  nXS;  d  M
  U(       d  XQ;  d  M  UR                  U5        M+     MC     U$ r   )r   r   )r   rT   at_endplanrX   rW   s         r   _generate_planMigrationGraph._generate_plan2  sF    D!//5	(f	8NKK	* 6  r   c                    Uc  [        U R                  5       5      nU(       d
  [        5       $ [        US   [        5      (       d  U/nU R                  X5      n[        US9nU H   nU R                  U   R                  USS9nM"     U$ )z
Given a migration node or nodes, return a complete ProjectState for it.
If at_end is False, return the state before the migration has run.
If nodes is not provided, return the overall most current project state.
r   )	real_appsF)preserve)listr   r   rw   tupler   rT   mutate_state)r   rT   r   r   r   project_staterX   s          r   
make_stateMigrationGraph.make_state:  s     =*+E>!%(E**GE""51$y9D JJt,99-RW9XM r   c                     XR                   ;   $ r   )rT   )r   rX   s     r   __contains__MigrationGraph.__contains__L  s    zz!!r   rR   )F)Tr   )NTN)r0   r<   r=   r>   r?   r   rY   r\   ra   rm   rs   r_   r   r   r~   r   r   r   r*   r1   r   r   r   r   r@   rA   r   r   rO   rO   ?   sl    ,$
(.!;F!5JV9I( '2ET

$"r   rO   N)
	functoolsr   django.db.migrations.stater   
exceptionsr   r   r	   rC   rO   rA   r   r   <module>r      sB    $ 3 B !! !! !!HR R$N" N"r   