B
    ©0H[Ü6  ã               @   sÄ   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 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 G dd„ deƒZdS )é    N)ÚOrderedDict)Úimport_module)Úapps)ÚTagsÚ
run_checks)ÚBaseCommandÚCommandError)Úemit_post_migrate_signalÚemit_pre_migrate_signal)ÚDEFAULT_DB_ALIASÚconnectionsÚrouter)ÚMigrationAutodetector)ÚMigrationExecutor)ÚAmbiguityError)Ú
ModelStateÚProjectState)Úmodule_has_submodulec                   sB   e Zd ZdZdd„ Z‡ fdd„Zdd„ Zdd
d„Zdd„ Z‡  Z	S )ÚCommandzMUpdates database schema. Manages both apps with migrations and those without.c             C   s‚   |j dddd� |j dddd� |j ddd	d
dd� |j dddt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ú?z5App label of an application to synchronize the state.)ÚnargsÚhelpÚmigration_namezpDatabase state will be brought to the state after that migration. Use the name "zero" to unapply all migrations.z	--noinputz
--no-inputÚstore_falseÚinteractivez:Tells Django to NOT prompt the user for input of any kind.)ÚactionÚdestr   z
--databaseÚstoreÚdatabasezHNominates a database to synchronize. Defaults to the "default" database.)r   r   Údefaultr   z--fakeÚ
store_trueÚfakez5Mark migrations as run without actually running them.z--fake-initialÚfake_initialzÜDetect 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(   úkC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\core\management\commands\migrate.pyÚadd_arguments   s,    zCommand.add_argumentsc                s&   t tjgd�}| tƒ jf |Ž¡ |S )N)Útags)r   r   r   ÚextendÚsuperÚ_run_checks)r&   ÚkwargsZissues)Ú	__class__r(   r)   r.   8   s    zCommand._run_checksc          	      sr  |d | _ |d | _x(t ¡ D ]}t|jdƒrtd|jƒ qW |d }t| }| 	¡  t
|| jƒ}|j |¡ |j ¡ }|r d dd„ | ¡ D ƒ¡}td	| ƒ‚d
}	|d �rf|d �rf|d |d  ‰ }
ˆ |jjkrâtdˆ  ƒ‚|
dkröˆ d fg}njy|j ˆ |
¡}W nJ tk
�r.   td|
ˆ f ƒ‚Y n& tk
�rR   td|
ˆ f ƒ‚Y nX ˆ |jfg}d}	nT|d �r®|d ‰ ˆ |jjk�r’tdˆ  ƒ‚‡ fdd„|jj ¡ D ƒ}n|jj ¡ }| |¡}|d �oÔ|jj}| j dk�rÆ| j | j d¡¡ |�r"| j | j d¡d t|jjƒ¡ ¡ |	�r\| j | j d¡d tdd„ |D ƒƒ¡�pTd ¡ nj|d d d k�r–| j | j d¡d|d d f  ¡ n0| j | j d ¡d!|d d |d d f  ¡ |j d
d"�}|j}t!| j | j|j"||d#� |�r&| j dk�r| j | j d$¡¡ |  #||jj¡ | j dk�rF| j | j d%¡¡ |�sÂ| j dk�r¸| j d&¡ t$|j %¡ t& 't¡ƒ}|j(|jjd'�}|�r¸| j | j )d(¡¡ | j | j )d)¡¡ d}d}n|d* }|d+ }|j*||| +¡ ||d,�}| ,¡  |j}| -¡ �< g }x0|j.D ]&}|j/|j0f}| 1|¡ |j2|Ž  �qW W d Q R X | 3d-d„ |D ƒ¡ t4| j | j|j"||d#� d S ).NÚ	verbosityr   Z
managementz.managementr   z; c             s   s$   | ]\}}d d  |¡|f V  qdS )z%s in %sz, N)Újoin)Ú.0ZappÚnamesr(   r(   r)   ú	<genexpr>Y   s   z!Command.handle.<locals>.<genexpr>zŒConflicting 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(   )r3   Ú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   h | ]\}}|’qS r(   r(   )r3   Ú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 %s)Zwith_applied_migrations)r   Ú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#   )r<   Ústater"   r#   c             S   s   g | ]}t  tj|Ž ¡‘qS r(   )r   Z
from_modelr   Z	get_model)r3   Úmodelr(   r(   r)   r7   Ù   s    )5r1   r   r   Úget_app_configsr   Úmoduler   Únamer   Zprepare_databaser   Úmigration_progress_callbackÚloaderZcheck_consistent_historyZdetect_conflictsr2   Úitemsr   Zmigrated_appsZget_migration_by_prefixr   ÚKeyErrorr=   Z
leaf_nodesZmigration_planZunmigrated_appsÚstdoutÚwriteÚstyleZMIGRATE_HEADINGZMIGRATE_LABELÚ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_strZtarget_app_labels_onlyr   ÚtargetsÚ	migrationr<   r$   Zpre_migrate_stateZpre_migrate_appsZautodetectorrM   r"   r#   Zpost_migrate_stateZpost_migrate_appsZ
model_keysZmodel_stateZ	model_keyr(   )r   r)   Úhandle=   sÎ    








$

zCommand.handleNFc             C   s¸  | j dk�r´| j dk}|dkrN|r,t ¡ | _| jjd| dd� | j ¡  �nf|dkrª|rldt ¡ | j  nd}|rŽ| j | j d| ¡¡ n| j | j d	| ¡¡ �n
|d
krà|rÀt ¡ | _| jjd| dd� | j ¡  nÔ|dk�r@|�rdt ¡ | j  nd}|�r&| j | j d| ¡¡ n| j | j d	| ¡¡ nt|dk�rv|�rZt ¡ | _| jjddd� | j ¡  n>|dk�r´|�r˜dt ¡ | j  nd}| j | j d| ¡¡ d S )Nr8   Zapply_startz  Applying %s...Ú )ZendingZapply_successz (%.3fs)z FAKEDz OKZunapply_startz  Unapplying %s...Zunapply_successZrender_startz  Rendering model states...Zrender_successz DONE)r1   ÚtimeÚstartrG   rH   ÚflushrI   ÚSUCCESS)r&   r   rW   r"   Zcompute_timeÚelapsedr(   r(   r)   rC   â   s<    






z#Command.migration_progress_callbackc       
   	      s  ˆ  ¡ �}ˆj |¡‰W dQ R X ‡ ‡fdd„t ¡ D ƒ}‡‡fdd„‰t‡fdd„|D ƒƒ}| jdkrr| j d	¡ ˆ 	¡ �š}xz| 
¡ D ]n\}}xd|D ]\}	|	j ˆ¡s¦q”| jd
krÈ| j d||	jjf ¡ | jdkræ| j d|	jj ¡ | |	¡ q”W q†W | jdk�r| j d¡ W dQ R X dS )z;Run the old syncdb-style operation on a list of app_labels.Nc                s8   g | ]0}|j d k	r|jˆ kr|jtj|ˆjdd�f‘qS )NF)Zinclude_auto_created)Zmodels_moduleÚlabelr   Zget_migratable_modelsrK   )r3   rR   )Ú
app_labelsrT   r(   r)   r7     s   z%Command.sync_apps.<locals>.<listcomp>c                s6   | j }ˆ jj}||jƒˆkp2|jo2||jj jƒˆk S )N)Ú_metaÚintrospectionZtable_name_converterÚdb_tableZauto_created)r?   ÚoptsÚ	converter)rT   Útablesr(   r)   Úmodel_installed  s    z*Command.sync_apps.<locals>.model_installedc             3   s$   | ]\}}|t tˆ |ƒƒfV  qd S )N)ÚlistÚfilter)r3   Úapp_nameÚ
model_list)rg   r(   r)   r5     s   z$Command.sync_apps.<locals>.<genexpr>r8   z  Creating tables...
é   z    Processing %s.%s model
z    Creating table %s
z    Running deferred SQL...
)Úcursorrb   Ztable_namesr   r@   r   r1   rG   rH   Zschema_editorrE   ra   Zcan_migrateZobject_namerc   Zcreate_model)
r&   rT   r`   rm   Z
all_modelsÚmanifestÚeditorrj   rk   r?   r(   )r`   rT   rg   rf   r)   rL     s.    







zCommand.sync_apps)NF)
Ú__name__Ú
__module__Ú__qualname__r   r*   r.   rX   rC   rL   Ú__classcell__r(   r(   )r0   r)   r      s   " &
"r   ) rZ   Ú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   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   