a
    [fS                     @   s   d dl Z d dlZd dlmZ d dl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 G dd deZdS )    N)import_module)apps)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                   @   sF   e Zd ZdZg Zdd Zedd Zddd	Zd
d Z	e
dd Zd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 |j dddddd |j dtdd |j dddd |j dddd |j dddd |j dddd |j ddddd |j ddd d!d d S )"Nz--skip-checks
store_truezSkip system checks.)actionhelp	app_label?z5App label of an application to synchronize the state.)nargsr   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.)r   destr   z
--databasezHNominates a database to synchronize. Defaults to the "default" database.)defaultr   z--fakez5Mark 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.z--checkcheck_unappliedzbExits with a non-zero status if unapplied migrations exist and does not actually apply migrations.z--pruneprunez?Delete nonexistent migrations from the django_migrations table.)add_argumentr	   )selfparser r$   yC:\Users\91910\OneDrive\Desktop\vts_admin_rebuild-master\vts\Lib\site-packages\django/core/management/commands/migrate.pyadd_arguments   sv    
	zCommand.add_argumentsc           %   
      s  |d }|d s| j |gd |d | _|d | _t D ]}t|jdr:td|j q:t	| }|
  t|| j}|j| |j }|rdd	d
 | D }td| |d }	d}
|d rN|d  zt  W n2 ty } ztt|W Y d }~n
d }~0 0 |	r4 |jjv rNtd  n |jjvrNtd  |d r$|d r$|d }|dkr d fg}nz|j |}W nF ty   td| f Y n$ ty   td| f Y n0  |jf}||jjjvr||jjv r|jj| }|jd }|g}d}
n2|d rJ fdd|jj D }n|jj }|d r|d srtd| jdkr| j d| j!j" t#|jj$t#|jj% fdd|jj D }|r| j | j!&d |D ]&}|\}}| j d| d |  q| j | j!&d! nt' fd"d
D rD ]l}|\}}| jdkrz| jj | j!(d#| d | d$d% |j)*|| | jdkr>| j | j!+d& q>n| jdkr| j d' |,|}|d( r| j d)| j!j( |s | j d* n||D ]b\}}| j t|| j!j" |j-D ]:}| .||\}}|rJ| j!j/nd }| j d| | q(q|d+ r|t01d, d S |d+ r|rt01d, d S |d rd S |d o|jj2}	| jd,kr| j | j!"d- |	r.|d r| j | j!(d.   n&| j | j!(d/d0t'|jj2  |
rh| j | j!(d1d0t'd2d3 |D p`d4  nh|d d, d u r| j | j!(d5t|d d   n0| j | j!(d6d7|d d, |d d f   |j3dd8}|j}t4| j| j|j5| j||d9 |	rN| jd,kr$| j | j!"d: |d r>| 6| g n| 6||jj2 | jd,krn| j | j!"d; |s| jd,kr| j d< t7|j8 t9:t}|j;|jjd=}|r| j | j!&d>d0d?d
 t'|D   | j | j!&d@ d}d}n|dA }|dB }|j<|||= ||dC} | >  | j}!|!? B g }"|!j@D ]&}#|#jA|#jBf}$|"C|$ |!jD|$  qLW d    n1 s0    Y  |!EdDd |"D  tF| j| j|j5| j|!|d9 d S )ENdatabaseZskip_checks)Z	databases	verbosityr   Z
managementz.managementz; c                 s   s$   | ]\}}d d ||f V  qdS )z%s in %s, N)join).0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   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$   )r+   keyr   r$   r%   
<listcomp>   s   z"Command.handle.<locals>.<listcomp>r    z7Migrations can be pruned only when an app is specified.r   zPruning migrations:c                    s,   g | ]$\}}t  fd d|jD r|qS )c                 3   s   | ]}| v V  qd S Nr$   )r+   Zreplacedto_pruner$   r%   r.          z,Command.handle.<locals>.<listcomp>.<genexpr>)anyreplaces)r+   Zmigration_keyZmigration_objr6   r$   r%   r4      s   z  Cannot use --prune because the following squashed migrations have their 'replaces' attributes and may not be recorded as applied:z    .z|  Re-run 'manage.py migrate' if they are not marked as applied, and remove 'replaces' attributes in their Migration classes.c                 3   s   | ]}|d   kr|V  qdS )r   Nr$   )r+   	migrationr3   r$   r%   r.      s   z
  Pruning  ending OKz  No migrations to prune.planzPlanned operations:z"  No planned migration operations.r      zOperations to perform:z   Synchronize unmigrated app: %sz  Synchronize unmigrated apps: r)   z  Apply all migrations: c                 S   s   h | ]\}}|qS r$   r$   )r+   anr$   r$   r%   	<setcomp>  r8   z!Command.handle.<locals>.<setcomp>z(none)z  Unapply all migrations: z  Target specific migration: z%s, from %s)Zwith_applied_migrations)stdoutr   rA   z&Synchronizing apps without migrations:zRunning migrations:z  No migrations to apply.)graphzl  Your models in app(s): %s have changes that are not yet reflected in a migration, and so won't be applied.c                 s   s   | ]}t |V  qd S r5   )repr)r+   r,   r$   r$   r%   r.   U  r8   zk  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.fakefake_initial)rA   staterI   rJ   c                 S   s   g | ]}t tj| qS r$   )r   Z
from_modelr   Z	get_model)r+   modelr$   r$   r%   r4   z  r8   )Gcheckr(   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   KeyErrorrG   nodesreplacementsr:   Z
leaf_nodesrF   writestyleZMIGRATE_HEADINGsetZapplied_migrationsZdisk_migrationsZNOTICEsortedZMIGRATE_LABELZrecorderZrecord_unappliedSUCCESSZmigration_plan
operationsdescribe_operationWARNINGsysexitZunmigrated_appsZ_create_project_stater   alias	sync_appsr   Zproject_stater   Z	from_appschangesZmigratecloneZclear_delayed_apps_cacheZbulk_updateZreal_modelsr   Z
name_lowerappendZunregister_modelZrender_multipler   )%r"   argsoptionsr'   
app_config
connectionexecutor	conflictsZname_strr/   Ztarget_app_labels_onlyerrr   targetsr<   targetZincomplete_migrationZ4squashed_migrations_with_deleted_replaced_migrationsr,   rP   rA   	backwards	operationmessageis_errorrZ   Zpre_migrate_stateZpre_migrate_appsZautodetectorre   rI   rJ   Zpost_migrate_stateZpost_migrate_appsZ
model_keysZmodel_stateZ	model_keyr$   )r   r7   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|rt | _| 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|rdt | j  nd}| j| jd|  d S )NrB   Zapply_startz  Applying %s...r=   r>   Zapply_successz (%.3fs)z FAKEDr@   Zunapply_startz  Unapplying %s...Zunapply_successZrender_startz  Rendering model states...Zrender_successz DONE)	r(   time	monotonicstartrF   rY   flushrZ   r]   )r"   r   r<   rI   Zcompute_timeelapsedr$   r$   r%   rQ     sB    






z#Command.migration_progress_callbackc           
         s<    }j|W d   n1 s*0    Y   fddt D }fddfdd|D }| jdkr| jd	  }|	 D ]j\}}|D ]\}	|	j
sq| jd
kr| jd||	j
jf  | jdkr| jd|	j
j  ||	 qq| jdkr| jd W d   n1 s.0    Y  dS )z;Run the old syncdb-style operation on a list of app_labels.Nc                    s8   g | ]0}|j d ur|j v r|jtj|jddfqS )NF)Zinclude_auto_created)Zmodels_modulelabelr   Zget_migratable_modelsrc   )r+   rj   )
app_labelsrk   r$   r%   r4     s   z%Command.sync_apps.<locals>.<listcomp>c                    s6   | j } jj}||jv p2|jo2||jj jv  S r5   )_metaintrospectionZidentifier_converterdb_tableZauto_created)rL   opts	converter)rk   tablesr$   r%   model_installed  s    z*Command.sync_apps.<locals>.model_installedc                    s    i | ]\}}|t t |qS r$   )listfilter)r+   app_name
model_list)r   r$   r%   
<dictcomp>  s   z%Command.sync_apps.<locals>.<dictcomp>rB   z  Creating tables...   z    Processing %s.%s modelz    Creating table %sz    Running deferred SQL...)cursorr~   Ztable_namesr   rN   r(   rF   rY   Zschema_editorrS   r}   Zcan_migrateZobject_namer   Zcreate_model)
r"   rk   r|   r   Z
all_modelsmanifestZeditorr   r   rL   r$   )r|   rk   r   r   r%   rd     s:    
*






zCommand.sync_appsc                 C   s   d}d}t | dr6|r| jn| j}|r0|jp2dnd}n(t | drR|rJ| jn| j}nd}|r^d}|durxt|dd}n|rd}d	}|rd
| }t|}|| 	  |
d |fS )z@Return a string that describes a migration operation for --plan.r=   FcodeNsqlzUndo 
ZIRREVERSIBLETz -> (   )hasattrZreverse_coder   __doc__Zreverse_sqlr   rU   replacer   Zdescribechars)rr   rq   prefixrt   r   r   	truncatedr$   r$   r%   r_     s&    

zCommand.describe_operation)NF)__name__
__module____qualname__r   Zrequires_system_checksr&   r   ru   rQ   rd   staticmethodr_   r$   r$   r$   r%   r      s   I
  )
(9r   )ra   rv   	importlibr   Zdjango.appsr   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   