B
    ©0H[”<  ã               @   st   d dl 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G dd	„ d	ƒƒZG d
d„ deƒZG dd„ dƒZdS )é    N)Útotal_ordering)ÚProjectState)Ú
OrderedSeté   )Ú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               @   sh   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   ú^C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\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 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  |¡ d S )N)r   Úadd)r   Úchildr   r   r   Ú	add_child.   s    zNode.add_childc             C   s   | j  |¡ d S )N)r   r   )r   Úparentr   r   r   Ú
add_parent1   s    zNode.add_parentc             C   sZ   d| j krPg }x"t| jdd�D ]}|| ¡ 7 }qW | | j¡ tt|ƒƒ| j d< | j d S )NÚ
_ancestorsT)Úreverse)Ú__dict__Úsortedr   Ú	ancestorsÚappendr	   Úlistr   )r   r&   r    r   r   r   r&   6   s    
zNode.ancestorsc             C   sZ   d| j krPg }x"t| jdd�D ]}|| ¡ 7 }qW | | j¡ tt|ƒƒ| j d< | j d S )NÚ_descendantsT)r#   )r$   r%   r   Údescendantsr'   r	   r(   r   )r   r*   r   r   r   r   r*   C   s    
zNode.descendantsN)r   Ú
__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‡ fdd„Zdd„ Zdd„ Z‡  ZS )Ú	DummyNodec                s   t ƒ  |¡ || _|| _d S )N)Úsuperr   ÚoriginÚerror_message)r   r	   r0   r1   )r   r   r   r   P   s    zDummyNode.__init__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)r0   r1   r   r   )r   r   r   r   ÚpromoteU   s    zDummyNode.promotec             C   s   t | j| j| jd�‚d S )N)r0   )r   r1   r	   r0   )r   r   r   r   Úraise_error_   s    zDummyNode.raise_error)r   r+   r,   r   r2   r3   Ú__classcell__r   r   )r   r   r.   O   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„ 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(„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| _d S )NF)Únode_mapÚnodesÚcached)r   r   r   r   r   z   s    zMigrationGraph.__init__c             C   sB   | j  |¡}|r| ¡  nt|ƒ}|| j |< || j|< |  ¡  d S )N)r6   Úgetr2   r   r7   Úclear_cache)r   r	   Ú	migrationZ
dummy_nodeÚnoder   r   r   Úadd_node   s    


zMigrationGraph.add_nodec             C   s$   t |||ƒ}|| j|< d | j|< d S )N)r.   r6   r7   )r   r	   r0   r1   r<   r   r   r   Úadd_dummy_node‹   s    
zMigrationGraph.add_dummy_nodeFc             C   sŒ   || j kr$d||f }|  |||¡ || j krHd||f }|  |||¡ | j|  | j| ¡ | j|  | j| ¡ |s€|  ¡  |  ¡  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)r7   r>   r6   r!   r   Úvalidate_consistencyr:   )r   r;   r   r    Zskip_validationr1   r   r   r   Úadd_dependency�   s    



zMigrationGraph.add_dependencyc       	   
   C   sú   t |ƒ}y| j| }W n4 tk
rJ } ztd|f |ƒ|‚W dd}~X Y nX x |D ]˜}| j |d¡ | j |d¡}|rRx8|jD ].}|j |¡ |j	|kr~| 
|¡ | |¡ q~W x8|jD ].}|j |¡ |j	|kr¸| |¡ | 
|¡ q¸W 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
   r6   ÚKeyErrorr   r7   Úpopr   r   Úremover	   r   r!   r:   )	r   ÚreplacementÚreplacedÚreplacement_nodeÚerrZreplaced_keyÚreplaced_noder   r    r   r   r   Úremove_replaced_nodes¨   s0    




z$MigrationGraph.remove_replaced_nodesc          
   C   s   | j  |d¡ y| j |¡}W n4 tk
rR } ztd|f |ƒ|‚W dd}~X Y nX tƒ }tƒ }x0|D ](}| j |¡}|rf| |¡ ||jO }qfW ||8 }x<|j	D ]2}	|	j 
|¡ x |D ]}| |	¡ |	 |¡ q¸W q¢W x|jD ]}
|
j	 
|¡ qàW |  ¡  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.)r7   rB   r6   rA   r   r
   r9   r   r   r   rC   r   r!   r:   )r   rD   rE   rF   rG   Zreplaced_nodesZreplaced_nodes_parentsr	   rH   r   r    r   r   r   Úremove_replacement_nodeÌ   s2    



z&MigrationGraph.remove_replacement_nodec             C   s   dd„ | j  ¡ D ƒ dS )z7Ensure there are no dummy nodes remaining in the graph.c             S   s   g | ]}t |tƒr| ¡ ‘qS r   )Ú
isinstancer.   r3   )Ú.0Únr   r   r   ú
<listcomp>ó   s    z7MigrationGraph.validate_consistency.<locals>.<listcomp>N)r6   Úvalues)r   r   r   r   r?   ñ   s    z#MigrationGraph.validate_consistencyc             C   sH   | j rDx6| jD ],}| j| j dd ¡ | j| j dd ¡ qW d| _ d S )Nr"   r)   F)r8   r7   r6   r$   rB   )r   r<   r   r   r   r:   õ   s
    zMigrationGraph.clear_cachec                sr   |ˆ j krtd|f |ƒ‚ˆ  |‡ fdd„¡ dˆ _ˆ j| }y| ¡ S  tk
rl   t t	t
¡ ˆ  |¡S X d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 nodec                s   dd„ ˆ j |  jD ƒS )Nc             s   s   | ]}|j V  qd S )N)r	   )rL   r    r   r   r   ú	<genexpr>  s    zAMigrationGraph.forwards_plan.<locals>.<lambda>.<locals>.<genexpr>)r6   r   )Úx)r   r   r   Ú<lambda>  s    z.MigrationGraph.forwards_plan.<locals>.<lambda>TN)r7   r   Úensure_not_cyclicr8   r6   r&   ÚRuntimeErrorÚwarningsÚwarnÚRECURSION_DEPTH_WARNINGÚRuntimeWarningÚiterative_dfs)r   Útargetr<   r   )r   r   Úforwards_planü   s    

zMigrationGraph.forwards_planc                sv   |ˆ j krtd|f |ƒ‚ˆ  |‡ fdd„¡ dˆ _ˆ j| }y| ¡ S  tk
rp   t t	t
¡ ˆ j|dd�S X 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 nodec                s   dd„ ˆ j |  jD ƒS )Nc             s   s   | ]}|j V  qd S )N)r	   )rL   r   r   r   r   rP     s    zBMigrationGraph.backwards_plan.<locals>.<lambda>.<locals>.<genexpr>)r6   r   )rQ   )r   r   r   rR     s    z/MigrationGraph.backwards_plan.<locals>.<lambda>TF)ÚforwardsN)r7   r   rS   r8   r6   r*   rT   rU   rV   rW   rX   rY   )r   rZ   r<   r   )r   r   Úbackwards_plan  s    

zMigrationGraph.backwards_planTc             C   sN   g }|g}x2|r<|  ¡ }| |¡ |t|r0|jn|jƒ7 }qW ttt|ƒƒƒS )z6Iterative depth-first search for finding dependencies.)rB   r'   r%   r   r   r(   r   Úreversed)r   Ústartr\   ÚvisitedÚstackr<   r   r   r   rY   "  s    
zMigrationGraph.iterative_dfsNc                sV   t ƒ }xF| jD ]<‰ t‡ fdd„| jˆ  jD ƒƒs|r@|ˆ d kr| ˆ ¡ 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   )rL   r	   )r<   r   r   rP   3  s    z,MigrationGraph.root_nodes.<locals>.<genexpr>r   )r
   r7   Úanyr6   r   r   r%   )r   ÚappÚrootsr   )r<   r   Ú
root_nodes,  s
    .zMigrationGraph.root_nodesc                sV   t ƒ }xF| jD ]<‰ t‡ fdd„| jˆ  jD ƒƒs|r@|ˆ d kr| ˆ ¡ 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   )rL   r	   )r<   r   r   rP   A  s    z,MigrationGraph.leaf_nodes.<locals>.<genexpr>r   )r
   r7   rb   r6   r   r   r%   )r   rc   Zleavesr   )r<   r   Ú
leaf_nodes7  s
    .zMigrationGraph.leaf_nodesc             C   s¢   t | jƒ}x’|rœ| ¡ }|g}xz|r˜|d }xh||ƒD ]T}||krl|| |¡d … }td dd„ |D ƒ¡ƒ‚||kr6| |¡ | |¡ P q6W | ¡ }q W qW d S )Néÿÿÿÿz, c             s   s   | ]}d | V  qdS )z%s.%sNr   )rL   rM   r   r   r   rP   Q  s    z3MigrationGraph.ensure_not_cyclic.<locals>.<genexpr>)r
   r7   rB   Úindexr   Újoinr'   rC   )r   r_   Zget_childrenÚtodor<   ra   ÚtopÚcycler   r   r   rS   E  s    


z MigrationGraph.ensure_not_cyclicc             C   s   d|   ¡  S )NzGraph: %s nodes, %s edges)Ú_nodes_and_edges)r   r   r   r   r   Y  s    zMigrationGraph.__str__c             C   s   |   ¡ \}}d| jj||f S )Nz<%s: nodes=%s, edges=%s>)rm   r   r   )r   r7   Úedgesr   r   r   r   \  s    zMigrationGraph.__repr__c             C   s"   t | jƒtdd„ | j ¡ D ƒƒfS )Nc             s   s   | ]}t |jƒV  qd S )N)Úlenr   )rL   r<   r   r   r   rP   a  s    z2MigrationGraph._nodes_and_edges.<locals>.<genexpr>)ro   r7   Úsumr6   rO   )r   r   r   r   rm   `  s    zMigrationGraph._nodes_and_edgesc             C   s°   |dkrt |  ¡ ƒ}t|ƒdkr&tƒ S t|d tƒs:|g}g }x@|D ]8}x2|  |¡D ]$}||krT|sn||krnqT| |¡ qTW qDW t|d�}x |D ]}| j| j	|dd�}q�W |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)
r(   rf   ro   r   rK   Útupler[   r'   r7   Zmutate_state)r   r7   Zat_endrq   Zplanr<   r;   Zproject_stater   r   r   Ú
make_statec  s"    


zMigrationGraph.make_statec             C   s
   || j kS )N)r7   )r   r<   r   r   r   Ú__contains__{  s    zMigrationGraph.__contains__)F)T)N)N)NTN)r   r+   r,   r-   r   r=   r>   r@   rI   rJ   r?   r:   r[   r]   rY   re   rf   rS   r   r   rm   rs   rt   r   r   r   r   r5   c   s(   
$%




r5   )rU   Ú	functoolsr   Zdjango.db.migrations.stater   Zdjango.utils.datastructuresr   Ú
exceptionsr   r   rW   r   r.   r5   r   r   r   r   Ú<module>   s   >