3
g)2                 @   s\   d dl mZ d dlmZ ddlmZmZ eG dd dZG dd deZG d	d
 d
Z	dS )    )total_ordering)ProjectState   )CircularDependencyErrorNodeNotFoundErrorc               @   sX   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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   X/var/www/tester-filtro-web/env/lib/python3.6/site-packages/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 j| jd | jd f S )Nz<%s: (%r, %r)>r   r   )	__class____name__r   )r   r   r   r   __repr__"   s    zNode.__repr__c             C   s   | j j| d S )N)r
   add)r   childr   r   r   	add_child%   s    zNode.add_childc             C   s   | j j| d S )N)r   r   )r   parentr   r   r   
add_parent(   s    zNode.add_parentN)r   
__module____qualname____doc__r   r   r   r   r   r   r   r   r    r   r   r   r   r      s   r   c                   s(   e Zd ZdZ fddZdd Z  ZS )	DummyNodea  
    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                s   t  j| || _|| _d S )N)superr   originerror_message)r   r   r&   r'   )r   r   r   r   4   s    zDummyNode.__init__c             C   s   t | j| j| jdd S )N)r&   )r   r'   r   r&   )r   r   r   r   raise_error9   s    zDummyNode.raise_error)r   r!   r"   r#   r   r(   __classcell__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	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& Zd/d'd(Zd)d* ZdS )0MigrationGraphaH  
    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             C   s   i | _ i | _d S )N)node_mapnodes)r   r   r   r   r   T   s    zMigrationGraph.__init__c             C   s.   || j kstt|}|| j |< || j|< d S )N)r+   AssertionErrorr   r,   )r   r   	migrationnoder   r   r   add_nodeX   s    
zMigrationGraph.add_nodec             C   s$   t |||}|| j|< d | j|< d S )N)r$   r+   r,   )r   r   r&   r'   r/   r   r   r   add_dummy_node^   s    
zMigrationGraph.add_dummy_nodeFc             C   s   || j kr$d||f }| j||| || j krHd||f }| j||| | j| j| j|  | j| j| j|  |s| j  dS )z
        This may create dummy nodes if they don't yet exist. If
        `skip_validation=True`, validate_consistency() should be called
        afterwards.
        z=Migration %s dependencies reference nonexistent child node %rz>Migration %s dependencies reference nonexistent parent node %rN)r,   r1   r+   r    r   validate_consistency)r   r.   r   r   Zskip_validationr'   r   r   r   add_dependencyc   s    



zMigrationGraph.add_dependencyc       	      C   s   t |}y| j| }W n4 tk
rJ } ztd|f ||W Y dd}~X nX x|D ]}| jj|d | jj|d}|rRx8|jD ].}|jj| |j	|kr~|j
| |j| q~W x8|jD ].}|jj| |j	|kr|j| |j
| qW qRW dS )z
        Remove 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.N)r	   r+   KeyErrorr   r,   popr
   r   remover   r   r    )	r   replacementreplacedreplacement_nodeerrZreplaced_keyreplaced_noder   r   r   r   r   remove_replaced_nodesz   s.    




z$MigrationGraph.remove_replaced_nodesc             C   s   | j j|d y| jj|}W n4 tk
rR } ztd|f ||W Y dd}~X nX t }t }x0|D ](}| jj|}|rf|j| ||jO }qfW ||8 }x<|j	D ]2}	|	jj
| x |D ]}|j|	 |	j| qW qW x|jD ]}
|
j	j
| qW dS )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.
        NztUnable to remove replacement node %r. It was either never added to the migration graph, or has been removed already.)r,   r5   r+   r4   r   r	   getr   r   r
   r6   r   r    )r   r7   r8   r9   r:   Zreplaced_nodesZreplaced_nodes_parentsr   r;   r   r   r   r   r   remove_replacement_node   s0    



z&MigrationGraph.remove_replacement_nodec             C   s   dd | j j D  dS )z7Ensure there are no dummy nodes remaining in the graph.c             S   s   g | ]}t |tr|j qS r   )
isinstancer$   r(   ).0nr   r   r   
<listcomp>   s    z7MigrationGraph.validate_consistency.<locals>.<listcomp>N)r+   values)r   r   r   r   r2      s    z#MigrationGraph.validate_consistencyc             C   s*   || j krtd|f || j| j| S )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.
        zNode %r not a valid node)r,   r   iterative_dfsr+   )r   targetr   r   r   forwards_plan   s    
zMigrationGraph.forwards_planc             C   s.   || j krtd|f || j| j| ddS )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.
        zNode %r not a valid nodeF)forwards)r,   r   rD   r+   )r   rE   r   r   r   backwards_plan   s    
zMigrationGraph.backwards_planTc             C   s   g }t  }|dfg}xj|r~|j \}}||kr0q|rL|j| |j|j q|j|df |dd t|rn|jn|jD 7 }qW |S )z6Iterative depth-first search for finding dependencies.FTc             S   s   g | ]}|d fqS )Fr   )r@   rA   r   r   r   rB      s    z0MigrationGraph.iterative_dfs.<locals>.<listcomp>)r	   r5   r   appendr   sortedr   r
   )r   startrG   visitedZvisited_setstackr/   	processedr   r   r   rD      s    

&zMigrationGraph.iterative_dfsNc                sX   t  }xH| jD ]> t fdd| j  jD r| sB| d kr|j  qW t|S )z
        Return 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   )r/   r   r   	<genexpr>   s    z,MigrationGraph.root_nodes.<locals>.<genexpr>r   )r	   r,   allr+   r   r   rJ   )r   approotsr   )r/   r   
root_nodes   s
    0zMigrationGraph.root_nodesc                sX   t  }xH| jD ]> t fdd| j  jD r| sB| d kr|j  qW t|S )am  
        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   s   | ]}|d   d  kV  qdS )r   Nr   )r@   r   )r/   r   r   rO      s    z,MigrationGraph.leaf_nodes.<locals>.<genexpr>r   )r	   r,   rP   r+   r
   r   rJ   )r   rQ   Zleavesr   )r/   r   
leaf_nodes   s
    0zMigrationGraph.leaf_nodesc             C   s   t | j}x|r|j }|g}x|r|d }xr| j| jD ]Z}|j}||krv||j|d  }tdjdd |D ||kr:|j	| |j
| P q:W |j }q W qW d S )Nr   z, c             s   s   | ]}d | V  qdS )z%s.%sNr   )r@   rA   r   r   r   rO     s    z3MigrationGraph.ensure_not_cyclic.<locals>.<genexpr>)r	   r,   r5   r+   r
   r   indexr   joinrI   r6   )r   todor/   rM   topr   cycler   r   r   ensure_not_cyclic  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\   r   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   )r@   r/   r   r   r   rO   "  s    z2MigrationGraph._nodes_and_edges.<locals>.<genexpr>)r^   r,   sumr+   rC   )r   r   r   r   r\   !  s    zMigrationGraph._nodes_and_edgesc             C   sH   g }x>|D ]6}x0| j |D ]"}||kr|s2||kr|j| qW q
W |S )N)rF   rI   )r   r,   at_endplanr/   r.   r   r   r   _generate_plan$  s    
zMigrationGraph._generate_planc             C   sn   |dkrt | j }|st S t|d ts2|g}| j||}t|d}x |D ]}| j| j|dd}qNW |S )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.
        Nr   )	real_appsF)Zpreserve)listrT   r   r?   tuplerb   r,   Zmutate_state)r   r,   r`   rc   ra   Zproject_stater/   r   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__)F)T)N)N)NTN)r   r!   r"   r#   r   r0   r1   r3   r<   r>   r2   rF   rH   rD   rS   rT   r[   r   r   r\   rb   rf   rg   r   r   r   r   r*   =   s(   
#$





r*   N)
	functoolsr   Zdjango.db.migrations.stater   
exceptionsr   r   r   r$   r*   r   r   r   r   <module>   s
   $