

_V*                 @   s   d  d l  m 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 m Z d
 Z e e Gd d   d e    Z e Gd d   d e   Z d S)    )unicode_literalsN)deque)total_ordering)ProjectState)
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   </tmp/pip-build-ghmbqnp_/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__0   s    zNode.__repr__c             C   s   |  j  j |  d  S)N)r   add)r   childr   r   r   	add_child3   s    zNode.add_childc             C   s   |  j  j |  d  S)N)r   r   )r   parentr   r   r   
add_parent6   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+   G   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               @   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 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   j   s    		zMigrationGraph.__init__c             C   s4   t  |  } | |  j | <| |  j | <|  j   d  S)N)r   r1   r2   clear_cache)r   r   implementationnoder   r   r   add_nodeo   s    zMigrationGraph.add_nodec             C   s   | |  j  k r+ t d | | f |   n  | |  j  k rV t d | | f |   n  |  j | j |  j |  |  j | j |  j |  |  j   d  S)Nz=Migration %s dependencies reference nonexistent child node %rz>Migration %s dependencies reference nonexistent parent node %r)r2   r
   r1   r"   r    r4   )r   	migrationr   r!   r   r   r   add_dependencyu   s    zMigrationGraph.add_dependencyc             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)r3   r2   r1   r$   pop)r   r6   r   r   r   r4      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   ).0r!   r   r   r   	<genexpr>   s    zAMigrationGraph.forwards_plan.<locals>.<lambda>.<locals>.<genexpr>)r1   r   )x)r   r   r   <lambda>   s    z.MigrationGraph.forwards_plan.<locals>.<lambda>TN)r2   r
   ensure_not_cyclicr3   r1   r(   RuntimeErrorwarningswarnRECURSION_DEPTH_WARNINGRuntimeWarningiterative_dfs)r   targetr6   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   )r;   r   r   r   r   r<      s    zBMigrationGraph.backwards_plan.<locals>.<lambda>.<locals>.<genexpr>)r1   r   )r=   )r   r   r   r>      s    z/MigrationGraph.backwards_plan.<locals>.<lambda>TforwardsFN)r2   r
   r?   r3   r1   r+   r@   rA   rB   rC   rD   rE   )r   rF   r6   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   startrH   visitedstackr6   r   r   r   r   rE      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   )r;   r   )r6   r   r   r<      s    z,MigrationGraph.root_nodes.<locals>.<genexpr>r   )r   r2   anyr1   r   r   r%   )r   approotsr   )r6   r   
root_nodes   s    	*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   )r;   r   )r6   r   r   r<      s    z,MigrationGraph.leaf_nodes.<locals>.<genexpr>r   )r   r2   rQ   r1   r   r   r%   )r   rR   Zleavesr   )r6   r   
leaf_nodes   s    	*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   )r;   nr   r   r   r<      s    z3MigrationGraph.ensure_not_cyclic.<locals>.<genexpr>)r   r2   r:   indexr	   joinrK   remove)r   rN   Zget_childrentodor6   rP   topcycler   r   r   r?      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      s    zMigrationGraph.__str__c             C   s)   |  j    \ } } d |  j j | | f S)Nz<%s: nodes=%s, edges=%s>)r^   	__class__r,   )r   r2   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   )r;   r6   r   r   r   r<     s    z2MigrationGraph._nodes_and_edges.<locals>.<genexpr>)ra   r2   sumr1   values)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)   rU   ra   r   
isinstancetuplerG   rK   r2   Zmutate_state)r   r2   at_endrd   Zplanr6   r8   Zproject_stater   r   r   
make_state  s"     zMigrationGraph.make_statec             C   s   | |  j  k S)N)r2   )r   r6   r   r   r   __contains__  s    zMigrationGraph.__contains__)r,   r-   r.   r/   r   r7   r9   r4   rG   rI   rE   rT   rU   r?   r   r   r^   rh   ri   r   r   r   r   r0   R   s    r0   )
__future__r   rA   collectionsr   	functoolsr   Zdjango.db.migrations.stater   Zdjango.utils.datastructuresr   Zdjango.utils.encodingr   
exceptionsr	   r
   rC   objectr   r0   r   r   r   r   <module>   s   ;