U
    8ÄT_2   ã                   @   sB   d dl mZ ddlmZ G dd„ dƒZG dd„ deƒZdd	„ Zd
S )é    )Úatomicé   )ÚIrreversibleErrorc                   @   sn   e Zd ZdZg Zg Zg Zg ZdZ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S )Ú	MigrationaŽ  
    The base class for all migrations.

    Migration files will import this from django.db.migrations.Migration
    and subclass it as a class called Migration. It will have one or more
    of the following attributes:

     - operations: A list of Operation instances, probably from django.db.migrations.operations
     - dependencies: A list of tuples of (app_path, migration_name)
     - run_before: A list of tuples of (app_path, migration_name)
     - replaces: A list of migration_names

    Note that all migrations come out of migrations and into the Loader or
    Graph as instances, having been initialized with their app label and name.
    NTc                 C   sH   || _ || _t| jjƒ| _t| jjƒ| _t| jjƒ| _t| jjƒ| _d S ©N)ÚnameÚ	app_labelÚlistÚ	__class__Ú
operationsÚdependenciesÚ
run_beforeÚreplaces)Úselfr   r   © r   úB/tmp/pip-unpacked-wheel-dv63caxw/django/db/migrations/migration.pyÚ__init__3   s    zMigration.__init__c                 C   s"   t |tƒo | j|jko | j|jkS r   )Ú
isinstancer   r   r   )r   Úotherr   r   r   Ú__eq__<   s
    

ÿ
ýzMigration.__eq__c                 C   s   d| j | jf S )Nz<Migration %s.%s>©r   r   ©r   r   r   r   Ú__repr__C   s    zMigration.__repr__c                 C   s   d| j | jf S ©Nz%s.%sr   r   r   r   r   Ú__str__F   s    zMigration.__str__c                 C   s   t d| j| jf ƒS r   )Úhashr   r   r   r   r   r   Ú__hash__I   s    zMigration.__hash__c                 C   s.   |}|r|  ¡ }| jD ]}| | j|¡ q|S )zÏ
        Take a ProjectState and return a new one with the migration's
        operations applied to it. Preserve the original object state by
        default and return a mutated state from a copy.
        )Úcloner   Ústate_forwardsr   )r   Úproject_stateÚpreserveÚ	new_stateÚ	operationr   r   r   Úmutate_stateL   s    
zMigration.mutate_stateFc              
   C   sÎ   | j D ]Â}|rT|j d¡ |js,|j d¡ |j d| ¡  ¡ |j d¡ |jsTq| ¡ }| | j|¡ |jp~| jo~|jdk	}|j	s¶|r¶t|j
jƒ� | | j|||¡ W 5 Q R X q| | j|||¡ q|S )a  
        Take a project_state representing all migrations prior to this one
        and a schema_editor for a live database and apply the migration
        in a forwards order.

        Return the resulting project state for efficient reuse by following
        Migrations.
        ú--úB-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE WRITTEN AS SQL:ú-- %sF)r   Úcollected_sqlÚappendÚreduces_to_sqlÚdescriber   r   r   r   Úatomic_migrationÚ
connectionÚaliasZdatabase_forwards)r   r   Úschema_editorÚcollect_sqlr"   Ú	old_stateÚatomic_operationr   r   r   ÚapplyZ   s&    	
ÿ
zMigration.applyc              
   C   s  g }|}| j D ]J}|js(td|| f ƒ‚| ¡ }| ¡ }| | j|¡ | d|||f¡ q|D ]²\}}}	|r²|j d¡ |j	sŠ|j d¡ |j d| 
¡  ¡ |j d¡ |j	s²q^|jpÆ| joÆ|jdk	}
|jsþ|
rþt|jjƒ� | | j||	|¡ W 5 Q R X q^| | j||	|¡ q^|S )aõ  
        Take a project_state representing all migrations prior to this one
        and a schema_editor for a live database and apply the migration
        in a reverse order.

        The backwards migration process consists of two phases:

        1. The intermediate states from right before the first until right
           after the last operation inside this migration are preserved.
        2. The operations are applied in reverse order using the states
           recorded in step 1.
        z$Operation %s in %s is not reversibler   r$   r%   r&   F)r   Z
reversibler   r   r   r   Úinsertr'   r(   r)   r*   r   r+   r,   r-   Zdatabase_backwards)r   r   r.   r/   Zto_runr!   r"   r0   Zto_stateZ
from_stater1   r   r   r   Úunapply   s4    
ÿ
zMigration.unapply)T)F)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   Úinitialr   r   r   r   r   r   r#   r2   r4   r   r   r   r   r      s   	

%r   c                   @   s   e Zd ZdZdd„ ZdS )ÚSwappableTuplez
    Subclass of tuple so Django can tell this was originally a swappable
    dependency when it reads the migration file.
    c                 C   s   t  | |¡}||_|S r   )ÚtupleÚ__new__Úsetting)ÚclsÚvaluer=   r   r   r   r   r<   ¹   s    zSwappableTuple.__new__N)r5   r6   r7   r8   r<   r   r   r   r   r:   ³   s   r:   c                 C   s   t |  dd¡d df| ƒS )z'Turn a setting value into a dependency.Ú.r   r   Z	__first__)r:   Úsplit)r?   r   r   r   Úswappable_dependency¿   s    rB   N)Zdjango.db.transactionr   Ú
exceptionsr   r   r;   r:   rB   r   r   r   r   Ú<module>   s
    .