
 X{9                 @   s   d  d l  m Z 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  d l m Z m Z d  d l m Z m Z d  d	 l m Z 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! Gd d   d e  Z" d S)    )unicode_literalsN)OrderedDict)import_module)apps)Tags
run_checks)BaseCommandCommandError)emit_post_migrate_signalemit_pre_migrate_signal)DEFAULT_DB_ALIASconnectionsroutertransaction)MigrationAutodetector)MigrationExecutor)AmbiguityError)
ModelStateProjectState)module_has_submodulec                   s^   e  Z d  Z d Z d d   Z   f d d   Z d d   Z d d	 d
 d  Z 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 d d | j  d d
 d d d d t d d | j  d d
 d d d d d d d | j  d d
 d d d d d d d | j  d d
 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interactivedefaultTz:Tells Django to NOT prompt the user for input of any kind.z
--databasestoredatabasezHNominates a database to synchronize. Defaults to the "default" database.z--fake
store_truefakeFz5Mark migrations as run without actually running them.z--fake-initial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--run-syncdb
run_syncdbz+Creates tables for apps without migrations.)add_argumentr   )selfparser r*   U/home/ubuntu/projects/ifolica/build/django/django/core/management/commands/migrate.pyadd_arguments   s.    	zCommand.add_argumentsc                s8   t  d t j g  } | j t t |   j |    | S)Ntags)r   r   r"   extendsuperr   _run_checks)r(   kwargsZissues)	__class__r*   r+   r0   <   s    zCommand._run_checksc                s  | d |  _  | d |  _ x9 t j   D]+ } t | j d  r' t d | j  q' q' W| d } t | } | j	   t
 | |  j  } | j j |  | j j   } | r d j d d   | j   D  } t d	 |   n  d
 }	 | d r| d r| d | d   }
   | j j k r4t d     n  |
 d k rR  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 }	 ns | d r9| d     | j j k rt d     n    f d d   | j j j   D } n | j j j   } | j |  } | d om| j j } |  j  d k r|  j j |  j j d   | r|  j j |  j j d  d j t | j j    n  |	 r*|  j j |  j j d  d j t t  d d   | D    p"d  q| d d d  k rp|  j j |  j j d  d | d d f  q|  j j |  j j d  d  | d d | d d f  n  | j! d! d
  } | j } t" |  j  |  j | j# d" | d# | | r:|  j  d k r!|  j j |  j j d$   n  |  j$ | | j j  n  |  j  d k rh|  j j |  j j d%   n  | s|  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)   q
n  d } d } n | d* } | d+ } | j+ | d# | d, | j,   d* | d+ | } | j } | j-   K g  } x= | j. D]2 } | j/ | j0 f } | j1 |  | j2 |   qWWd  QX| j3 d- d   | D  t4 |  j  |  j | j# d" | d# | d  S).N	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'Tr   r   z"App '%s' does not have migrations.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*   )r5   key)r   r*   r+   
<listcomp>   s   	 z"Command.handle.<locals>.<listcomp>r&      zOperations to perform:z  Synchronize unmigrated apps: z, z  Apply all migrations: c             s   s   |  ] \ } } | Vq d  S)Nr*   )r5   anr*   r*   r+   r7      s    z(none)r   z  Unapply all migrations: z%sz  Target specific migration: z%s, from %sZwith_applied_migrationsr   planz&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.r$   r%   statec             S   s(   g  |  ] } t  j t j |     q Sr*   )r   Z
from_modelr   Z	get_model)r5   modelr*   r*   r+   r9      s   	 )5r3   r   r   get_app_configsr   moduler   namer   Zprepare_databaser   migration_progress_callbackloaderZcheck_consistent_historyZdetect_conflictsr4   itemsr	   Zmigrated_appsZget_migration_by_prefixr   KeyErrorr>   Z
leaf_nodesZmigration_planZunmigrated_appsstdoutwritestyleZMIGRATE_HEADINGZMIGRATE_LABELsortedsetZ_create_project_stater   alias	sync_appsr   Zproject_stater   Z	from_appschangesZNOTICEZmigratecloneZbulk_updateZreal_modelsr   Z
name_lowerappendZunregister_modelZrender_multipler
   )r(   argsoptions
app_configdb
connectionZexecutorZ	conflictsZname_strZtarget_app_labels_onlyr   targets	migrationr=   r&   Zpre_migrate_stateZpre_migrate_appsZautodetectorrO   r$   r%   Zpost_migrate_stateZpost_migrate_appsZ
model_keysZmodel_stateZ	model_keyr*   )r   r+   handleA   s    


		

(	 	3 	"	"
	

		zCommand.handleNFc             C   sS  |  j  d k rO|  j  d k } | d k rl | rB t j   |  _ n  |  j j d | d d |  j j   qO| d k r | r d t j   |  j n d } | r |  j j |  j j d |   qL|  j j |  j j d	 |   qO| d
 k r5| rt j   |  _ n  |  j j d | d d |  j j   qO| d k r| r^d t j   |  j n d } | r|  j j |  j j d |   qL|  j j |  j j d	 |   qO| d k r| rt j   |  _ n  |  j j d d d |  j j   qO| d k rO| r#d t j   |  j n d } |  j j |  j j d |   qOn  d  S)Nr:   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)r3   timestartrH   rI   flushrJ   SUCCESS)r(   r   rX   r$   Zcompute_timeelapsedr*   r*   r+   rD      s<    #######z#Command.migration_progress_callbackc                s   j    } z j j |   t   }    f d d   t j   D }   f d d    t  f d d   | D  } |  j d k r |  j j	 d  n  t
 j d	  j d
  j j  7g  } x | j   D] \ } }	 x |	 D] }
 |
 j j   sq n  |  j d k r:|  j j	 d | |
 j j f  n   j   Y } |  j d k ru|  j j	 d |
 j j  n  | j |
  | j | j  g  | _ Wd QX| j |
  q Wq W|  j d k r|  j j	 d  n  x | D] } | j |  qWWd QXWd | j   X| S)z<Runs the old syncdb-style operation on a list of app_labels.c                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_modelsrM   )r5   rT   )
app_labelsrV   r*   r+   r9     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table_name_converterdb_tableZauto_created)r@   opts	converter)rV   tablesr*   r+   model_installed  s    	z*Command.sync_apps.<locals>.model_installedc             3   s0   |  ]& \ } } | t  t   |   f Vq d  S)N)listfilter)r5   app_name
model_list)rh   r*   r+   r7      s   z$Command.sync_apps.<locals>.<genexpr>r:   z  Creating tables...
ZusingZ	savepoint   z    Processing %s.%s model
z    Creating table %s
Nz    Running deferred SQL...
)cursorrc   Ztable_namesrL   r   rA   r   r3   rH   rI   r   ZatomicrM   featuresZcan_rollback_ddlrF   rb   Zcan_migrateZobject_nameZschema_editorrd   Zcreate_modelr.   deferred_sqladdexecuteclose)r(   rV   ra   rn   Zcreated_modelsZ
all_modelsmanifestrp   rk   rl   r@   Zeditor	statementr*   )ra   rV   rh   rg   r+   rN     sD    		"	zCommand.sync_apps)	__name__
__module____qualname__r   r,   r0   rY   rD   rN   r*   r*   )r2   r+   r      s   #"r   )#
__future__r   r[   collectionsr   	importlibr   Zdjango.appsr   Zdjango.core.checksr   r   Zdjango.core.management.baser   r	   Zdjango.core.management.sqlr
   r   Z	django.dbr   r   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   r   r*   r*   r*   r+   <module>   s   "