a
    Lcòf™S  ã                   @   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-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--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$   úd/home/httpd/docs/test/DocsMgr/lib/python3.9/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 ¡ }|r²d 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}nžz|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!¡¡ n¨t'‡ 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|t0 1d,¡ d S |d+ �rž|�ršt0 1d,¡ d S |d �r¬d 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 �sŠ0    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>zŒConflicting 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à|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 )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
 ˆ¡s²q | jd
krÔ| j d||	j
jf ¡ | jdkrò| j d|	j
j ¡ | |	¡ q q”| 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|r„d}d	}|r�d
| }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   