3
gE                 @   s^   d dl mZ d dlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ G dd	 d	Zd
S )    )apps)
migrationsrouter   )InvalidMigrationPlan)MigrationLoader)MigrationRecorder)ProjectStatec               @   st   e Zd ZdZdddZdddZddd	Zdd
dZdd Zdd Z	dddZ
dd ZdddZdd Zdd ZdS ) MigrationExecutorzu
    End-to-end migration execution - load migrations and run them up or down
    to a specified set of targets.
    Nc             C   s(   || _ t| j | _t| j | _|| _d S )N)
connectionr   loaderr   recorderprogress_callback)selfr   r    r   [/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/db/migrations/executor.py__init__   s    zMigrationExecutor.__init__Fc       	         sr  g }|ri }nt | jj}xP|D ]F  d dkrxf| jjj D ]V}|d  d krBx@| jjj|D ].}||krf|j| jjj| df |j| qfW qBW q" |krt	 fdd| jjj
  jD }x|D ]F}x@| jjj|D ].}||kr|j| jjj| df |j| qW qW q"xL| jjj D ]:}||kr,|j| jjj| df | jjj| ||< q,W q"W |S )z\
        Given a set of targets, return a list of (Migration instance, backwards?).
        r   Nr   Tc             3   s"   | ]}|d   d  kr|V  qdS )r   Nr   ).0n)targetr   r   	<genexpr>0   s    z3MigrationExecutor.migration_plan.<locals>.<genexpr>F)dictr   applied_migrationsgraphZ
root_nodesZbackwards_planappendnodespopsortedZnode_mapchildrenZforwards_plan)	r   targetsclean_startplanappliedroot	migrationZnext_in_appnoder   )r   r   migration_plan   s4    



z MigrationExecutor.migration_planc                sn   t t jjd}|rj j jjj dd} fdd jjD }x&|D ]\}}||krH|j|dd qHW |S )z
        Create a project state including all the applications without
        migrations and applied migrations if with_applied_migrations=True.
        )Z	real_appsT)r    c                s(   h | ] }| j jjkr j jj| qS r   )r   r   r   )r   key)r   r   r   	<setcomp>I   s   z:MigrationExecutor._create_project_state.<locals>.<setcomp>F)preserve)	r	   listr   Zunmigrated_appsr&   r   
leaf_nodesr   mutate_state)r   with_applied_migrationsstate	full_planr   r$   _r   )r   r   _create_project_state@   s    
z'MigrationExecutor._create_project_statec       	      C   s   | j j  |dkr| j|}| j| jjj dd}tdd |D }tdd |D }|sp|dkr| jdd}nR||krtd|n>|r|dkr| jdd}| j	|||||d	}n| j
|||d
}| j  |S )z
        Migrate the database up to the given targets.

        Django first needs to create all project states before a migration is
        (un)applied and in a second step run all the database operations.
        NT)r    c             s   s   | ]\}}| V  qd S )Nr   )r   mig	backwardsr   r   r   r   b   s    z,MigrationExecutor.migrate.<locals>.<genexpr>c             s   s   | ]\}}|V  qd S )Nr   )r   r2   r3   r   r   r   r   c   s    )r-   zMigration plans with both forwards and backwards migrations are not supported. Please split your migration process into separate plans of only forwards OR backwards migrations.)fakefake_initial)r4   )r   Zensure_schemar&   r   r   r+   allr1   r   _migrate_all_forwards_migrate_all_backwardscheck_replacements)	r   r   r!   r.   r4   r5   r/   Zall_forwardsZall_backwardsr   r   r   migrateR   s(    	

zMigrationExecutor.migratec       	      C   s~   dd |D }xj|D ]b\}}|s"P ||krd|j krZ| jrD| jd |j | jrZ| jd | j||||d}|j| qW |S )z
        Take a list of 2-tuples of the form (migration instance, False) and
        apply them in the order they occur in the full_plan.
        c             S   s   h | ]}|d  qS )r   r   )r   mr   r   r   r(      s    z:MigrationExecutor._migrate_all_forwards.<locals>.<setcomp>r   render_startrender_success)r4   r5   )__dict__r   r   apply_migrationremove)	r   r.   r!   r/   r4   r5   migrations_to_runr$   r0   r   r   r   r7      s    


z'MigrationExecutor._migrate_all_forwardsc                s\  dd |D }i } j  } fdd jjD } jr@ jd xf|D ]^\}}	|sTP ||krd|jkrl|j |||< |j|dd}|j| qF||krF|j|dd qFW  jr jd	 x.|D ]&\}}	 j|| ||d
 |j| qW |d d }
||
 }xZt	|D ]N\}\}}	||
krx2||d D ]"\}}	||kr*|j|dd q*W P qW |S )a  
        Take a list of 2-tuples of the form (migration instance, True) and
        unapply them in reverse order they occur in the full_plan.

        Since unapplying a migration requires the project state prior to that
        migration, Django will compute the migration states before each of them
        in a first run over the plan and then unapply them in a second run over
        the plan.
        c             S   s   h | ]}|d  qS )r   r   )r   r;   r   r   r   r(      s    z;MigrationExecutor._migrate_all_backwards.<locals>.<setcomp>c                s(   h | ] }| j jjkr j jj| qS r   )r   r   r   )r   r'   )r   r   r   r(      s   r<   r   T)r)   Fr=   )r4   r   r   N)
r1   r   r   r   r>   r   r,   r@   unapply_migration	enumerate)r   r!   r/   r4   rA   Zstatesr.   r   r$   r0   Zlast_unapplied_migrationindexr   )r   r   r8      s@    






z(MigrationExecutor._migrate_all_backwardsc             C   s   d}| j r| j d|| |sx|r8| j||\}}|r8d}|sx| jj|jd&}|j||}|jsn| j| d}W dQ R X |s| j| | j r| j d|| |S )zRun a migration forwards.FZapply_startT)atomicNZapply_success)r   detect_soft_appliedr   schema_editorrF   applyZdeferred_sqlrecord_migration)r   r.   r$   r4   r5   Zmigration_recordedr"   rH   r   r   r   r?      s&    

z!MigrationExecutor.apply_migrationc             C   s@   |j r*x4|j D ]\}}| jj|| qW n| jj|j|j d S )N)replacesr   record_applied	app_labelname)r   r$   rM   rN   r   r   r   rJ      s    z"MigrationExecutor.record_migrationc             C   s   | j r| j d|| |s@| jj|jd}|j||}W dQ R X |jrjx4|jD ]\}}| jj|| qNW n| jj|j|j	 | j r| j d|| |S )zRun a migration backwards.Zunapply_start)rF   NZunapply_success)
r   r   rH   rF   ZunapplyrK   r   Zrecord_unappliedrM   rN   )r   r.   r$   r4   rH   rM   rN   r   r   r   rC      s    z#MigrationExecutor.unapply_migrationc                sX   | j j  xH| jjj D ]8\}}t fdd|jD }|r| kr| j j|  qW dS )a  
        Mark replacement migrations applied if their replaced set all are.

        Do this unconditionally on every migrate, rather than just when
        migrations are applied or unapplied, to correctly handle the case
        when a new squash migration is pushed to a deployment that already had
        all its replaced migrations applied. In this case no new migration will
        be applied, but the applied state of the squashed migration must be
        maintained.
        c             3   s   | ]}| kV  qd S )Nr   )r   r;   )r"   r   r   r     s    z7MigrationExecutor.check_replacements.<locals>.<genexpr>N)r   r   r   replacementsitemsr6   rK   rL   )r   r'   r$   Zall_appliedr   )r"   r   r9     s
    
z$MigrationExecutor.check_replacementsc                s^  fdd} j dkr8t fdd jD rJd|fS n j dkrJd|fS |dkrljj j jfdd}n
 j|}|j}d}d}j	j
j}j	j *}	tj	jj|	}
|rd	d
 |
D }
W dQ R X x jD ]x}t|tjrJ|j j|j}|jjrtj|jj}| |rq|jj}|r2|j }||
krDd|fS d}qt|tjr|j j|j}|jjr~tj|jj}| |rq|jj}|jj|j}|jr|jjjj}|r|j }||
krd|fS d}q҈j	j }	j	jj |	|}W dQ R X xH|D ]8}|j!}|j}|r.|j }|j }||krd}P qW d|fS qW |pX||fS )z
        Test whether a migration has been implicitly applied - that the
        tables or columns it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel and AddField).
        c                s.   |j jp,|j j p,tj jj| j|j jd S )z
            No need to detect tables for proxy models, unmanaged models, or
            models that can't be migrated on the current database.
            )
model_name)	_metaproxyZmanagedr   Zallow_migrater   aliasrM   rQ   )r$   model)r   r   r   should_skip_detecting_model  s    
zJMigrationExecutor.detect_soft_applied.<locals>.should_skip_detecting_modelNc             3   s   | ]\}}| j kV  qd S )N)rM   )r   ZapprN   )r$   r   r   r   .  s    z8MigrationExecutor.detect_soft_applied.<locals>.<genexpr>FT)Zat_endc             S   s   h | ]}|j  qS r   )casefold)r   rN   r   r   r   r(   ?  s    z8MigrationExecutor.detect_soft_applied.<locals>.<setcomp>)"initialanydependenciesr   project_staterM   rN   r,   r   r   featuresZignores_table_name_casecursorsetZintrospectionZtable_names
operations
isinstancer   ZCreateModelZ	get_modelrR   Zswappedglobal_appsdb_tablerW   ZAddFieldrQ   	get_fieldZmany_to_manyZremote_fieldZthroughZget_table_descriptioncolumn)r   r[   r$   rV   Zafter_stater   Zfound_create_model_migrationZfound_add_field_migrationZfold_identifier_caser]   Zexisting_table_namesZ	operationrU   rb   tablefieldZthrough_db_tablecolumnsrd   Zfield_columnZcolumn_namer   )r$   r   r   rG     sv    










z%MigrationExecutor.detect_soft_applied)N)F)F)NNFF)FF)F)__name__
__module____qualname____doc__r   r&   r1   r:   r7   r8   r?   rJ   rC   r9   rG   r   r   r   r   r
   
   s   

*

-=

r
   N)Zdjango.apps.registryr   ra   Z	django.dbr   r   
exceptionsr   r   r   r   r   r.   r	   r
   r   r   r   r   <module>   s   