
 XA                 @   s   d  d l  m Z 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 m Z d Z e e Gd d   d e    Z Gd d   d e  Z e Gd d   d e   Z d S)    )unicode_literalsN)deque)total_ordering)ProjectState)six)
OrderedSet)python_2_unicode_compatible   )CircularDependencyErrorNodeNotFoundErrora  Maximum recursion depth exceeded while generating migration graph, falling back to iterative approach. If you're experiencing performance issues, consider squashing migrations as described at https://docs.djangoproject.com/en/dev/topics/migrations/#squashing-migrations.c               @   s   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z d d   Z d S)Nodezp
    A single node in the migration graph. Contains direct links to adjacent
    nodes in either direction.
    c             C   s%   | |  _  t   |  _ t   |  _ d  S)N)keysetchildrenparents)selfr    r   H/home/ubuntu/projects/ifolica/build/django/django/db/migrations/graph.py__init__   s    	zNode.__init__c             C   s   |  j  | k S)N)r   )r   otherr   r   r   __eq__#   s    zNode.__eq__c             C   s   |  j  | k  S)N)r   )r   r   r   r   r   __lt__&   s    zNode.__lt__c             C   s   t  |  j  S)N)hashr   )r   r   r   r   __hash__)   s    zNode.__hash__c             C   s   |  j  | S)N)r   )r   itemr   r   r   __getitem__,   s    zNode.__getitem__c             C   s   t  |  j  S)N)strr   )r   r   r   r   __str__/   s    zNode.__str__c             C   s   d |  j  S)Nz<Node: (%r, %r)>)r   )r   r   r   r   __repr__2   s    zNode.__repr__c             C   s   |  j  j |  d  S)N)r   add)r   childr   r   r   	add_child5   s    zNode.add_childc             C   s   |  j  j |  d  S)N)r   r   )r   parentr   r   r   
add_parent8   s    zNode.add_parentc             C   s{   d |  j  k rp t |  j g  } x0 t |  j  D] } | j t | j     q1 Wt t	 |   |  j  d <n  |  j  d S)N
_ancestors)
__dict__r   r   sortedr   
extendleftreversed	ancestorslistr   )r   r)   r"   r   r   r   r)   =   s    zNode.ancestorsc             C   s{   d |  j  k rp t |  j g  } x0 t |  j  D] } | j t | j     q1 Wt t	 |   |  j  d <n  |  j  d S)N_descendants)
r%   r   r   r&   r   r'   r(   descendantsr*   r   )r   r,   r    r   r   r   r,   I   s    zNode.descendantsN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r!   r#   r)   r,   r   r   r   r   r      s   r   c                   sF   e  Z d  Z   f d d   Z d d   Z d d   Z d d   Z   S)		DummyNodec                s,   t  t |   j |  | |  _ | |  _ d  S)N)superr1   r   originerror_message)r   r   r3   r4   )	__class__r   r   r   U   s    	zDummyNode.__init__c             C   s   d |  j  S)Nz<DummyNode: (%r, %r)>)r   )r   r   r   r   r   Z   s    zDummyNode.__repr__c             C   s   |  `  |  ` t |  _ d S)z
        Transition dummy to a normal node and clean off excess attribs.
        Creating a Node object from scratch would be too much of a
        hassle as many dependendies would need to be remapped.
        N)r3   r4   r   r5   )r   r   r   r   promote]   s    zDummyNode.promotec             C   s"   t  |  j |  j d |  j  d  S)Nr3   )r   r4   r   r3   )r   r   r   r   raise_errorg   s    zDummyNode.raise_error)r-   r.   r/   r   r   r6   r7   r   r   )r5   r   r1   T   s   
r1   c               @   s  e  Z d  Z d Z d d   Z d d   Z d d   Z d d	 d
  Z d d   Z d d   Z	 d d   Z
 d d   Z d d   Z d d   Z d d d  Z d d d  Z d d d  Z d d    Z d! d"   Z d# d$   Z d% d&   Z d d d d' d(  Z d) d*   Z d S)+MigrationGraphaI  
    Represents 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             C   s   i  |  _  i  |  _ d |  _ d  S)NF)node_mapnodescached)r   r   r   r   r      s    		zMigrationGraph.__init__c             C   sY   |  j  j |  } | r% | j   n t |  } | |  j  | <| |  j | <|  j   d  S)N)r9   getr6   r   r:   clear_cache)r   r   	migrationZ
dummy_nodenoder   r   r   add_node   s    zMigrationGraph.add_nodec             C   s0   t  | | |  } | |  j | <d  |  j | <d  S)N)r1   r9   r:   )r   r   r3   r4   r?   r   r   r   add_dummy_node   s    zMigrationGraph.add_dummy_nodeFc             C   s   | |  j  k r5 d | | f } |  j | | |  n  | |  j  k rj d | | f } |  j | | |  n  |  j | j |  j |  |  j | j |  j |  | s |  j   n  |  j   d S)z
        This may create dummy nodes if they don't yet exist.
        If `skip_validation` is set, validate_consistency should be called afterwards.
        z=Migration %s dependencies reference nonexistent child node %rz>Migration %s dependencies reference nonexistent parent node %rN)r:   rA   r9   r#   r!   validate_consistencyr=   )r   r>   r    r"   Zskip_validationr4   r   r   r   add_dependency   s    zMigrationGraph.add_dependencyc       
      C   s  t  |  } y |  j | } Wn t k
 r } zf t d | f |  } | | _ t | d  sv t j   d | _ n  t	 j
 t | t j   d  WYd d } ~ Xn Xx | D] } |  j j | d  |  j j | d  } | r xM | j D]B } | j j |  | j | k r | j |  | j |  q q WxP | j D]B }	 |	 j j |  |	 j | k r;| j |	  |	 j |  q;q;Wq q W|  j   d S)z
        Removes each of the `replaced` nodes (when they exist). Any
        dependencies that were referencing them are changed to reference the
        `replacement` node instead.
        zjUnable to find replacement node %r. It was either never added to the migration graph, or has been removed.__traceback__   N)r   r9   KeyErrorr   	__cause__hasattrsysexc_inforD   r   reraiser:   popr   r   remover   r!   r#   r=   )
r   replacementreplacedreplacement_nodeexc	exc_valueZreplaced_keyreplaced_noder    r"   r   r   r   remove_replaced_nodes   s6    		0z$MigrationGraph.remove_replaced_nodesc             C   s  |  j  j | d  y |  j j |  } Wn t k
 r } zf t d | f |  } | | _ t | d  s t j   d | _	 n  t
 j t | t j   d  WYd d } ~ Xn Xt   } t   } xC | D]; } |  j j |  }	 |	 r | j |	  | |	 j O} q q W| | 8} xL | j D]A }
 |
 j j |  x( | D]  }	 |	 j |
  |
 j |	  q;WqWx! | j D] } | j j |  qmW|  j   d S)a1  
        The inverse operation to `remove_replaced_nodes`. Almost. Removes the
        replacement node `replacement` and remaps its child nodes to
        `replaced` - the list of nodes it would have replaced. Its parent
        nodes are not remapped as they are expected to be correct already.
        NztUnable to remove replacement node %r. It was either never added to the migration graph, or has been removed already.rD   rE   )r:   rL   r9   rF   r   rG   rH   rI   rJ   rD   r   rK   r   r<   r   r   r   rM   r!   r#   r=   )r   rN   rO   rP   rQ   rR   Zreplaced_nodesZreplaced_nodes_parentsr   rS   r    r"   r   r   r   remove_replacement_node   s8    		0		
z&MigrationGraph.remove_replacement_nodec             C   s   d d   |  j  j   D d S)zI
        Ensure there are no dummy nodes remaining in the graph.
        c             S   s+   g  |  ]! } t  | t  r | j    q Sr   )
isinstancer1   r7   ).0nr   r   r   
<listcomp>  s   	 z7MigrationGraph.validate_consistency.<locals>.<listcomp>N)r9   values)r   r   r   r   rB     s    z#MigrationGraph.validate_consistencyc             C   sa   |  j  r] xE |  j D]: } |  j | j j d d   |  j | j j d d   q Wd |  _  n  d  S)Nr$   r+   F)r;   r:   r9   r%   rL   )r   r?   r   r   r   r=     s
    	zMigrationGraph.clear_cachec                s   |   j  k r( t d | f |   n    j |   f d d    d   _   j | } y | j   SWn/ t k
 r t j t	 t
    j |  SYn Xd S)z
        Given a node, returns 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.
        zNode %r not a valid nodec                s   d d     j  |  j D S)Nc             s   s   |  ] } | j  Vq d  S)N)r   )rW   r"   r   r   r   	<genexpr>  s    zAMigrationGraph.forwards_plan.<locals>.<lambda>.<locals>.<genexpr>)r9   r   )x)r   r   r   <lambda>  s    z.MigrationGraph.forwards_plan.<locals>.<lambda>TN)r:   r   ensure_not_cyclicr;   r9   r)   RuntimeErrorwarningswarnRECURSION_DEPTH_WARNINGRuntimeWarningiterative_dfs)r   targetr?   r   )r   r   forwards_plan  s    	zMigrationGraph.forwards_planc                s   |   j  k r( t d | f |   n    j |   f d d    d   _   j | } y | j   SWn5 t k
 r t j t	 t
    j | d d SYn Xd S)z
        Given a node, returns 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.
        zNode %r not a valid nodec                s   d d     j  |  j D S)Nc             s   s   |  ] } | j  Vq d  S)N)r   )rW   r    r   r   r   r[   ,  s    zBMigrationGraph.backwards_plan.<locals>.<lambda>.<locals>.<genexpr>)r9   r   )r\   )r   r   r   r]   ,  s    z/MigrationGraph.backwards_plan.<locals>.<lambda>TforwardsFN)r:   r   r^   r;   r9   r,   r_   r`   ra   rb   rc   rd   )r   re   r?   r   )r   r   backwards_plan"  s    	zMigrationGraph.backwards_planTc             C   s   t    } | j |  | r4 t  t | j   } n t  t | j   } xc | r | j   } | j |  | r t | j d d } n t | j d d } | j |  qL Wt t	 |   S)zI
        Iterative depth first search, for finding dependencies.
        reverseT)
r   appendr&   r   r   popleft
appendleftr'   r*   r   )r   startrg   visitedstackr?   r   r   r   r   rd   6  s    		zMigrationGraph.iterative_dfsNc                sx   t    } xb |  j D]W   t   f d d   |  j   j D  r | sZ |   d k r | j    q q Wt |  S)z
        Returns all root nodes - that is, nodes with no dependencies inside
        their app. These are the starting point for an app.
        c             3   s#   |  ] } | d    d  k Vq d S)r   Nr   )rW   r   )r?   r   r   r[   T  s    z,MigrationGraph.root_nodes.<locals>.<genexpr>r   )r   r:   anyr9   r   r   r&   )r   approotsr   )r?   r   
root_nodesM  s
    	AzMigrationGraph.root_nodesc                sx   t    } xb |  j D]W   t   f d d   |  j   j D  r | sZ |   d k r | j    q q Wt |  S)an  
        Returns 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   s#   |  ] } | d    d  k Vq d S)r   Nr   )rW   r   )r?   r   r   r[   b  s    z,MigrationGraph.leaf_nodes.<locals>.<genexpr>r   )r   r:   rp   r9   r   r   r&   )r   rq   Zleavesr   )r?   r   
leaf_nodesX  s
    	AzMigrationGraph.leaf_nodesc             C   s   t  |  j  } x | r | j   } | g } x | r | d } x | |  D]z } | | k r | | j |  d   } t d j d d   | D    n  | | k rM | j |  | j |  PqM qM W| j   } q0 Wq Wd  S)Nr	   z, c             s   s   |  ] } d  | Vq d S)z%s.%sNr   )rW   rX   r   r   r   r[   r  s    z3MigrationGraph.ensure_not_cyclic.<locals>.<genexpr>)r   r:   rL   indexr
   joinrj   rM   )r   rm   Zget_childrentodor?   ro   topcycler   r   r   r^   f  s    			
%z MigrationGraph.ensure_not_cyclicc             C   s   d |  j    S)NzGraph: %s nodes, %s edges)_nodes_and_edges)r   r   r   r   r   z  s    zMigrationGraph.__str__c             C   s)   |  j    \ } } d |  j j | | f S)Nz<%s: nodes=%s, edges=%s>)r{   r5   r-   )r   r:   edgesr   r   r   r   }  s    zMigrationGraph.__repr__c             C   s/   t  |  j  t d d   |  j j   D  f S)Nc             s   s   |  ] } t  | j  Vq d  S)N)lenr   )rW   r?   r   r   r   r[     s    z2MigrationGraph._nodes_and_edges.<locals>.<genexpr>)r}   r:   sumr9   rZ   )r   r   r   r   r{     s    zMigrationGraph._nodes_and_edgesc             C   s   | d k r! t  |  j    } n  t |  d k r: t   St | d t  sY | g } n  g  } x] | D]U } xL |  j |  D]; } | | k r| | r | | k r q| n  | j |  q| q| Wqf Wt d |  } x* | D]" } |  j | j	 | d d } q W| S)z
        Given a migration node or nodes, returns a complete ProjectState for it.
        If at_end is False, returns the state before the migration has run.
        If nodes is not provided, returns the overall most current project state.
        Nr   	real_appsZpreserveF)
r*   rt   r}   r   rV   tuplerf   rj   r:   Zmutate_state)r   r:   at_endr   Zplanr?   r>   Zproject_stater   r   r   
make_state  s"     zMigrationGraph.make_statec             C   s   | |  j  k S)N)r:   )r   r?   r   r   r   __contains__  s    zMigrationGraph.__contains__)r-   r.   r/   r0   r   r@   rA   rC   rT   rU   rB   r=   rf   rh   rd   rs   rt   r^   r   r   r{   r   r   r   r   r   r   r8   k   s(   ()r8   )
__future__r   rI   r`   collectionsr   	functoolsr   Zdjango.db.migrations.stater   Zdjango.utilsr   Zdjango.utils.datastructuresr   Zdjango.utils.encodingr   
exceptionsr
   r   rb   objectr   r1   r8   r   r   r   r   <module>   s    ;