
v^?                 @   s  d  d l  Z  d  d l m Z d  d l m Z d  d l m Z m Z d  d l m	 Z	 m
 Z
 m Z d  d l m Z m Z d  d l m Z m Z 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  d l m Z d  d l m Z Gd d   d e	  Z  d S)    N)import_module)apps)Tags
run_checks)BaseCommandCommandErrorno_translations)emit_post_migrate_signalemit_pre_migrate_signal)DEFAULT_DB_ALIASconnectionsrouter)MigrationAutodetector)MigrationExecutor)AmbiguityError)
ModelStateProjectState)module_has_submodule)	Truncatorc                   sv   e  Z d  Z d Z d d   Z   f d d   Z e d d    Z d d	 d
 d  Z d d   Z	 e
 d d    Z   S)CommandzMUpdates database schema. Manages both apps with migrations and those without.c          	   C   s   | j  d d d d d | j  d d d d d | j  d d	 d
 d d d d d | j  d d t d d | j  d d
 d d d | j  d d
 d d d | j  d d
 d d d | j  d d
 d d d d  S)N	app_labelnargs?helpz5App label of an application to synchronize the state.migration_namezpDatabase state will be brought to the state after that migration. Use the name "zero" to unapply all migrations.z	--noinputz
--no-inputactionstore_falsedestinteractivez:Tells Django to NOT prompt the user for input of any kind.z
--databasedefaultzHNominates a database to synchronize. Defaults to the "default" database.z--fake
store_truez5Mark migrations as run without actually running them.z--fake-initialzDetect if tables already exist and fake-apply initial migrations if so. Make sure that the current database schema matches your initial migration before using this flag. Django will only check for an existing table name.z--planz=Shows a list of the migration actions that will be performed.z--run-syncdbz+Creates tables for apps without migrations.)add_argumentr   )selfparser r$   I/tmp/pip-build-8lau8j11/django/django/core/management/commands/migrate.pyadd_arguments   s2    zCommand.add_argumentsc                s2   t  d t j g  } | j t   j |    | S)Ntags)r   r   databaseextendsuper_run_checks)r"   kwargsZissues)	__class__r$   r%   r+   >   s    zCommand._run_checksc           %      s[  | d |  _  | d |  _ x6 t j   D]( } t | j d  r' t d | j  q' W| d } t | } | j	   t
 | |  j  } | j j |  | j j   } | r d j d d   | j   D  } t d	 |   | d
 }	 d }
 | d r| d   y t j    Wn7 t k
 rH} z t t |    WYd  d  } ~ Xn X|	 rt  | j j k rt d     n"   | j j k rt d     | d rX| d rX| d } | d k r  d  f g } n} y | j j   |  } WnO t k
 rt d |   f   Yn( t k
 r<t d |   f   Yn X  | j f g } d }
 nD | d r  f d d   | j j j   D } n | j j j   } | j |  } | d ry|  j j d |  j j  | s|  j j d  x | D] \ } } |  j j t |  |  j j   xX | j! D]M } |  j" | |  \ } } | rP|  j j# n d  } |  j j d | |  q WqWd  S| d
 o| j j$ }	 |  j  d k r|  j j |  j j  d   |	 r"| d r|  j j |  j j d     n5 |  j j |  j j d  d j t% | j j$    |
 rm|  j j |  j j d  d j t% d  d!   | D   ped"  n | d# d d  k r|  j j |  j j d$  d% | d# d# f  n: |  j j |  j j d&  d' | d# d | d# d# f  | j& d( d  } | j } t' |  j  |  j | j( d) | d | |	 r|  j  d k r^|  j j |  j j  d*   | d r~|  j) |   g  n |  j) | | j j$  |  j  d k r|  j j |  j j  d+   | sj|  j  d k r[|  j j d,  t* | j j+   t, j- t   } | j. d- | j j  } | r[|  j j |  j j/ d.   |  j j |  j j/ d/   d } d } n | d0 } | d1 } | j0 | d | d2 | j1   d0 | d1 | } | j2   | j } | j3   K g  } x= | j4 D]2 } | j5 | j6 f } | j7 |  | j8 |   qWWd  QRX| j9 d3 d   | D  t: |  j  |  j | j( d) | d | d  S)4N	verbosityr   Z
managementz.managementr(   z; c             s   s.   |  ]$ \ } } d  d j  |  | f Vq d S)z%s in %sz, N)join).0Zappnamesr$   r$   r%   	<genexpr>`   s   z!Command.handle.<locals>.<genexpr>zConflicting migrations detected; multiple leaf nodes in the migration graph: (%s).
To fix them run 'python manage.py makemigrations --merge'
run_syncdbTr   z8Can't use run_syncdb with app '%s' as it has migrations.z"App '%s' does not have migrations.r   ZzerozJMore than one migration matches '%s' in app '%s'. Please be more specific.z4Cannot find a migration matching '%s' from app '%s'.Fc                s&   g  |  ] } | d    k r |  q S)r   r$   )r0   key)r   r$   r%   
<listcomp>   s   	 z"Command.handle.<locals>.<listcomp>planzPlanned operations:z"  No planned migration operations.z       zOperations to perform:z   Synchronize unmigrated app: %sz  Synchronize unmigrated apps: z, z  Apply all migrations: c             S   s   h  |  ] \ } } |  q Sr$   r$   )r0   anr$   r$   r%   	<setcomp>   s   	 z!Command.handle.<locals>.<setcomp>z(none)r   z  Unapply all migrations: z%sz  Target specific migration: z%s, from %sZwith_applied_migrationsr   z&Synchronizing apps without migrations:zRunning migrations:z  No migrations to apply.graphz^  Your models have changes that are not yet reflected in a migration, and so won't be applied.zk  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.fakefake_initialstatec             S   s(   g  |  ] } t  j t j |     q Sr$   )r   Z
from_modelr   Z	get_model)r0   modelr$   r$   r%   r5      s   	 );r.   r   r   get_app_configsr   moduler   namer   Zprepare_databaser   migration_progress_callbackloaderZcheck_consistent_historyZdetect_conflictsr/   itemsr   Zget_app_configLookupErrorstrZmigrated_appsZget_migration_by_prefixr   KeyErrorr;   Z
leaf_nodesZmigration_planstdoutwritestyleZMIGRATE_LABELZMIGRATE_HEADING
operationsdescribe_operationWARNINGZunmigrated_appssortedZ_create_project_stater
   alias	sync_appsr   Zproject_stater   Z	from_appschangesZNOTICEZmigratecloneZclear_delayed_apps_cacheZbulk_updateZreal_modelsr   Z
name_lowerappendZunregister_modelZrender_multipler	   ) r"   argsoptions
app_configdb
connectionexecutorZ	conflictsZname_strr3   Ztarget_app_labels_onlyerrr   targets	migrationr6   	backwards	operationmessageis_errorrK   Zpre_migrate_stateZpre_migrate_appsZautodetectorrR   r<   r=   Zpost_migrate_stateZpost_migrate_appsZ
model_keysZmodel_stateZ	model_keyr$   )r   r%   handleC   s    


	



%
	
(

			- 		"


	

	
	zCommand.handleNFc             C   sD  |  j  d k r@|  j  d k } | d k ri | r? t j   |  _ |  j j d | d d |  j j   n| d k r | r d t j   |  j n d } | r |  j j |  j j d |   q@|  j j |  j j d	 |   n\| d
 k r/| rt j   |  _ |  j j d | d d |  j j   n| d k r| rXd t j   |  j n d } | r|  j j |  j j d |   q@|  j j |  j j d	 |   n | d k r| rt j   |  _ |  j j d d d |  j j   nO | d k r@| rd t j   |  j n d } |  j j |  j j d |   d  S)Nr7   Zapply_startz  Applying %s...Zending Zapply_successz (%.3fs)z FAKEDz OKZunapply_startz  Unapplying %s...Zunapply_successZrender_startz  Rendering model states...Zrender_successz DONE)	r.   time	monotonicstartrI   rJ   flushrK   SUCCESS)r"   r   r]   r<   Zcompute_timeelapsedr$   r$   r%   rC     s<    #######z#Command.migration_progress_callbackc       
         sx   j     }  j j |   Wd QRX   f d d   t j   D }   f d d     f d d   | D } |  j d k r |  j j d	   j    } x | j	   D] \ } } x | D] }	 |	 j
 j   s q |  j d
 k r|  j j d | |	 j
 j f  |  j d k r9|  j j d |	 j
 j  | j |	  q Wq W|  j d k rm|  j j d  Wd QRXd S)z;Run the old syncdb-style operation on a list of app_labels.Nc                sR   g  |  ]H } | j  d  k	 r | j   k r | j t j |  j d d f  q S)NZinclude_auto_createdF)Zmodels_modulelabelr   Zget_migratable_modelsrP   )r0   rW   )
app_labelsrY   r$   r%   r5   ,  s   	z%Command.sync_apps.<locals>.<listcomp>c                sM   |  j  }   j j } | | j   k pK | j oK | | j j  j   k S)N)_metaintrospectionZidentifier_converterdb_tableZauto_created)r?   opts	converter)rY   tablesr$   r%   model_installed4  s    	z*Command.sync_apps.<locals>.model_installedc                s.   i  |  ]$ \ } } t  t   |   |  q Sr$   )listfilter)r0   app_name
model_list)rr   r$   r%   
<dictcomp><  s   	z%Command.sync_apps.<locals>.<dictcomp>r7   z  Creating tables...
   z    Processing %s.%s model
z    Creating table %s
z    Running deferred SQL...
)cursorrm   Ztable_namesr   r@   r.   rI   rJ   Zschema_editorrE   rl   Zcan_migrateZobject_namern   Zcreate_model)
r"   rY   rk   ry   Z
all_modelsmanifestZeditorru   rv   r?   r$   )rk   rY   rr   rq   r%   rQ   %  s,    
	zCommand.sync_appsc             C   s  d } d } t  |  d  rQ | r* |  j n |  j } | rH | j pK d n d } n< t  |  d  r{ | ro |  j n |  j } n d } | r d } | d k	 r t |  j d d  } n | r d } d	 } | r d
 | } t |  } | |  j	   | j
 d  | f S)z@Return a string that describes a migration operation for --plan.rc   FcodeNsqlzUndo 
ZIRREVERSIBLETz -> (   )hasattrZreverse_coder{   __doc__Zreverse_sqlr|   rG   replacer   Zdescribechars)r_   r^   prefixra   r{   r   Z	truncatedr$   r$   r%   rM   V  s&    
zCommand.describe_operation)__name__
__module____qualname__r   r&   r+   r   rb   rC   rQ   staticmethodrM   r$   r$   )r-   r%   r      s   &"1r   )!rd   	importlibr   Zdjango.appsr   Zdjango.core.checksr   r   Zdjango.core.management.baser   r   r   Zdjango.core.management.sqlr	   r
   Z	django.dbr   r   r   Z!django.db.migrations.autodetectorr   Zdjango.db.migrations.executorr   Zdjango.db.migrations.loaderr   Zdjango.db.migrations.stater   r   Zdjango.utils.module_loadingr   Zdjango.utils.textr   r   r$   r$   r$   r%   <module>   s   