
    dfS                         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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                   V    e Zd ZdZg Zd Zed             Zd	dZd Z	e
d             ZdS )
CommandzMUpdates database schema. Manages both apps with migrations and those without.c                 (   |                     ddd           |                     ddd           |                     d	dd
           |                     ddddd           |                     dt          d           |                     ddd           |                     ddd           |                     ddd           |                     ddd           |                     ddd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parsers     cD:\Python Project\djangoTemplates\venv\Lib\site-packages\django/core/management/commands/migrate.pyadd_argumentszCommand.add_arguments   s   & 	 	
 	
 	

 	H 	 	
 	
 	

 	H 	 	
 	
 	
 	 M 	 	
 	
 	
 	$	 	 	
 	
 	
 	H 	 	
 	
 	

 	'	 	 		
 		
 		
 	P 	 	
 	
 	

 	> 	 	
 	
 	

 	"1 	 	
 	
 	
 	R	 	 	
 	
 	
 	
 	
    c                   %& |d         }|d         s|                      |g           |d         | _        |d         | _        t          j                    D ],}t          |j        d          rt          d|j                   -t          |         }|
                                 t          || j                  }|j                            |           |j                                        }|rCd                    d	 |                                D                       }t%          d
|z            |d         }	d}
|d         r|d         %	 t          j        %           n.# t(          $ r!}t%          t+          |                    d }~ww xY w|	r!%|j        j        v rt%          d%z            n %|j        j        vrt%          d%z            |d         r|d         r|d         }|dk    r%d fg}n	 |j                            %|          }nC# t0          $ r t%          d|d%d          t2          $ r t%          d|d%d          w xY w%|j        f}||j        j        j        vr-||j        j        v r|j        j        |         }|j        d         }|g}d}
nQ|d         r+%fd|j        j                                        D             }n|j        j                                        }|d         r|d         st%          d          | j        dk    r%| j                             d| j!        j"                   tG          |j        j$                  tG          |j        j%                  z
  &&fd|j        j                                        D             }|r| j                             | j!        &                    d                      |D ]'}|\  }}| j                             d!| d"|            (| j                             | j!        &                    d#                     ntO          %fd$&D                       &&r&D ]}|\  }}| j        dk    r:| j                             | j!        (                    d%| d"|           d&'           |j)        *                    ||           | j        dk    r2| j                             | j!        +                    d(                     n%| j        dk    r| j                             d)           |,                    |          }|d*         r| j                             d+| j!        j(                   |s| j                             d,           n|D ]\  }}| j                             t+          |          | j!        j"                   |j-        D ]I}| .                    ||          \  }}|r| j!        j/        nd }| j                             d!|z   |           J|d-         rta          j1        d.           d S |d-         r|rta          j1        d.           d S |d         rd S |d         o|j        j2        }	| j        d.k    r| j                             | j!        "                    d/                     |	r|d         r6| j                             | j!        (                    d0%z                       n_| j                             | j!        (                    d1          d2                    tO          |j        j2                            z              |
rb| j                             | j!        (                    d3          d2                    tO          d4 |D                                 pd5z              n|d         d.         O| j                             | j!        (                    d6          t+          |d         d                   z              nR| j                             | j!        (                    d7          |d         d.         d8|d         d         z              |3                    d9          }|j        }ti          | j        | j        |j5        | j        ||:           |	r}| j        d.k    r2| j                             | j!        "                    d;                     |d         r| 6                    |%g           n | 6                    ||j        j2                   | j        d.k    r2| j                             | j!        "                    d<                     |s| j        d.k    r| j                             d=           to          |j        8                                ts          j:        t                              }|;                    |j        j        >          }|r| j                             | j!        &                    d?d2                    d@ tO          |          D                       z                       | j                             | j!        &                    dA                     d}d}n|dB         }|dC         }|<                    |||=                                ||D          } | >                                 | j        }!|!?                                5  g }"|!j@        D ]/}#|#jA        |#jB        f}$|"C                    |$            |!jD        |$  0	 d d d            n# 1 swxY w Y   |!E                    dE |"D                        t          | j        | j        |j5        | j        |!|:           d S )FNdatabaseskip_checks)	databases	verbosityr   
managementz.managementz; c              3   P   K   | ]!\  }}d                      |          d|V  "dS ), z in N)join).0appnamess      r'   	<genexpr>z!Command.handle.<locals>.<genexpr>~   sP       ! !9Cedii....4! ! ! ! ! !r)   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   zeroz!More than one migration matches 'z
' in app 'z'. Please be more specific.z"Cannot find a migration matching 'z' from app 'z'.Fc                 ,    g | ]}|d          k    |S )r    )r3   keyr   s     r'   
<listcomp>z"Command.handle.<locals>.<listcomp>   s.       SVyEXEXEXEXEXr)   r#   z7Migrations can be pruned only when an app is specified.r   zPruning migrations:c                 Z    g | ]'\  }}t          fd |j        D                       %|(S )c              3       K   | ]}|v V  	d S Nr;   )r3   replacedto_prunes     r'   r6   z,Command.handle.<locals>.<listcomp>.<genexpr>   s(      SSx8+SSSSSSr)   )anyreplaces)r3   migration_keymigration_objrB   s      r'   r=   z"Command.handle.<locals>.<listcomp>   sZ     D D D0M=SSSSM<RSSSSSDD D Dr)   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   4   K   | ]}|d          k    |V  dS )r   Nr;   )r3   	migrationr   s     r'   r6   z!Command.handle.<locals>.<genexpr>   s:       " ""+9Q<9;T;TI;T;T;T;T" "r)   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: r1   z  Apply all migrations: c                     h | ]\  }}|S r;   r;   )r3   ans      r'   	<setcomp>z!Command.handle.<locals>.<setcomp>  s    (?(?(?tq!(?(?(?r)   z(none)z  Unapply all migrations: z  Target specific migration: z, from )with_applied_migrations)stdoutr   rN   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              3   4   K   | ]}t          |          V  d S r@   )repr)r3   r4   s     r'   r6   z!Command.handle.<locals>.<genexpr>U  s(      2X2X4992X2X2X2X2X2Xr)   zk  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.fakefake_initial)rN   staterY   rZ   c                 N    g | ]"}t          j        t          j        |           #S r;   )r   
from_modelr   	get_model)r3   models     r'   r=   z"Command.handle.<locals>.<listcomp>z  s*    SSSuZ"4>5#9::SSSr)   )Gcheckr.   r   r   get_app_configsr   moduler   namer   prepare_databaser   migration_progress_callbackloadercheck_consistent_historydetect_conflictsr2   itemsr   get_app_configLookupErrorstrmigrated_appsget_migration_by_prefixr   KeyErrorrV   nodesreplacementsrD   
leaf_nodesrU   writestyleMIGRATE_HEADINGsetapplied_migrationsdisk_migrationsNOTICEsortedMIGRATE_LABELrecorderrecord_unappliedSUCCESSmigration_plan
operationsdescribe_operationWARNINGsysexitunmigrated_apps_create_project_stater	   alias	sync_appsr   project_stater   	from_appschangesmigratecloneclear_delayed_apps_cachebulk_updatereal_modelsr   
name_lowerappendunregister_modelrender_multipler   )'r%   argsoptionsr+   
app_config
connectionexecutor	conflictsname_strr7   target_app_labels_onlyerrr   targetsrI   targetincomplete_migration4squashed_migrations_with_deleted_replaced_migrationsr4   rc   rN   	backwards	operationmessageis_errorrt   pre_migrate_statepre_migrate_appsautodetectorr   rY   rZ   post_migrate_statepost_migrate_apps
model_keysmodel_state	model_keyr   rB   s'                                        @@r'   handlezCommand.handle`   s   :&}% 	-JJ(J,,, -"=1 .00 	> 	>J#J$5|DD >mZ_=== !*
 	##%%%$Z1QRR 	00<<< O4466	 	yy ! !GPGXGX! ! !  H <>FG   \*
!%; 	U,I-#I.... - - -"3s88,,,- U ===&R#$   >
 (/"???"#G)#STTT; #	9G,<$= #	9$%56N''%t,- ( G G!>! !II &   &,6DnniiiQ       &,)>>9996  
 $Y^4 (/"7"===(/">>>+3?+G+O(1:2>F!(%*""[! 	9   '4??AA  GG o+6688G7 1	C;' "M   ~!!!!"79STTT8?=>>/B B HD D D D4<O4P4V4V4X4XD D D@
 D "C!!J%%2    "V ; ;I )ICK%%&9S&9&94&9&9::::!!J%%-     " " " " "/7" " "    C%- 	I 	I	$-	T>A-- K-- $
 8 89Rc9R9RD9R9R S S') .    !)::3EEE>A-- K--dj.@.@.G.GHHH	I ^a''K%%&ABBB&&w//6? 	K3TZ5MNNN  !!"FGGGG,0 C C(IyK%%c)nndj6PQQQ%.%9 C C	,0,C,C%y- -) 7? H
 2 2D))&7*:EBBBBC ,-  HQKKKF$% 	 F7 	F \*Nx/N
>QKdj889QRRSSS ;' 
K%%
00>J     K%%
001RSS99VHO,K%L%LMMO   & !!J,,-GHHyy(?(?w(?(?(?!@!@AAMXO   
 1:a=(K%%
001MNNgajm,,-   
 K%%
001PQQ+21:a==='!*Q--HI  
 %::SW:XX,1N;!	
 	
 	
 	
  	L~""!!J../WXX   {# LzI;7777z8?+JKKK >QKdj889NOOPPP 	3~""!!"=>>>  5O1133 *400    '..X_5J.KK K%%
))')-2X2Xw2X2X2X)X)XY    K%%
))*    D LL6?D">2L%--#))++% . 
 
 	33555.3
 **,, 	? 	?J0< ? ?'1;3II	!!),,,2!2I>>>?	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	))SS
SSS	
 	
 	
 	!N;"	
 	
 	
 	
 	
 	
s7   E" "
F,FF8H A I!:l((l,/l,NFc                 l   | j         dk    r| j         dk    }|dk    rT|rt          j                    | _        | j                            d|z  d           | j                                         d S |dk    r|rdt          j                    | j        z
  z  nd}|r7| j                            | j                            d|z                        d S | j                            | j                            d	|z                        d S |d
k    rT|rt          j                    | _        | j                            d|z  d           | j                                         d S |dk    r|rdt          j                    | j        z
  z  nd}|r7| j                            | j                            d|z                        d S | j                            | j                            d	|z                        d S |dk    rQ|rt          j                    | _        | j                            dd           | j                                         d S |dk    r[|rdt          j                    | j        z
  z  nd}| j                            | j                            d|z                        d S d S d S )NrO   apply_startz  Applying %s...rJ   rK   apply_successz (%.3fs)z FAKEDrM   unapply_startz  Unapplying %s...unapply_successrender_startz  Rendering model states...render_successz DONE)	r.   time	monotonicstartrU   rs   flushrt   r~   )r%   r   rI   rY   compute_timeelapseds         r'   re   z#Command.migration_progress_callback  s   >Q>A-L&& 2!%!1!1DJ!!"4y"@!LLL!!#####?**DPXJ$."2"2TZ"?@@VX   KK%%dj&8&8G9K&L&LMMMMMK%%dj&8&8&I&IJJJJJ?** 2!%!1!1DJ!!"6"B2!NNN!!#####,,,DPXJ$."2"2TZ"?@@VX   KK%%dj&8&8G9K&L&LMMMMMK%%dj&8&8&I&IJJJJJ>)) 2!%!1!1DJ!!"?!KKK!!#####+++DPXJ$."2"2TZ"?@@VX  !!$*"4"4Ww5F"G"GHHHHHK B ,+r)   c           
      ^  
                                  5 }j                            |          ddd           n# 1 swxY w Y   fdt          j                    D             }fd

fd|D             }| j        dk    r| j                            d                                           5 }|	                                D ]\  }}|D ]}	|	j
                                      s| j        dk    r+| j                            d|d	|	j
        j        d
           | j        dk    r'| j                            d|	j
        j        z             |                    |	           | j        dk    r| j                            d           ddd           dS # 1 swxY w Y   dS )z;Run the old syncdb-style operation on a list of app_labels.Nc                 v    g | ]5}|j         	|j        v |j        t          j        |j        d          f6S )NF)include_auto_created)models_modulelabelr   get_migratable_modelsr   )r3   r   
app_labelsr   s     r'   r=   z%Command.sync_apps.<locals>.<listcomp>  se     	
 	
 	
 '3
8HJ8V8V  ,
 0u   9W8V8Vr)   c                     | j         }j        j        } ||j                  v p"|j        o ||j        j         j                  v  S r@   )_metaintrospectionidentifier_converterdb_tableauto_created)r_   opts	converterr   tabless      r'   model_installedz*Command.sync_apps.<locals>.model_installed  s_    ;D"0EI4=))V3 % N!	$"3"9"BCCvM	 r)   c           	      R    i | ]#\  }}|t          t          |                    $S r;   )listfilter)r3   app_name
model_listr   s      r'   
<dictcomp>z%Command.sync_apps.<locals>.<dictcomp>  sA     
 
 
$* d6/:>>??
 
 
r)   rO   z  Creating tables...   z    Processing rG   z modelz    Creating table %sz    Running deferred SQL...)cursorr   table_namesr   ra   r.   rU   rs   schema_editorri   r   can_migrateobject_namer   create_model)r%   r   r   r   
all_modelsmanifesteditorr   r   r_   r   r   s    ``       @@r'   r   zCommand.sync_apps  s      	BF-99&AAF	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B	
 	
 	
 	
 	
 #244	
 	
 	

		 		 		 		 		 		
 
 
 
(2
 
 
 >QK4555%%'' 	A6(0(8(8 / /$*' / /E ;22:>> ! ~**)))'xx)@)@)@B   ~**))3ek6JJ   ''..../  ~""!!"?@@@'	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	As$   A  AA8CF""F&)F&c                    d}d}t          | d          r|r| j        n| j        }|r	|j        pdnd}n't          | d          r|r| j        n| j        }nd}|rd}|$t          |                              dd          }n|rd}d	}|rd
|z   }t          |          }|| 	                                z   |
                    d          z   |fS )z@Return a string that describes a migration operation for --plan.rJ   FcodeNsqlzUndo 
IRREVERSIBLETz -> (   )hasattrreverse_coder   __doc__reverse_sqlr   rl   replacer   describechars)r   r   prefixr   r   r   	truncateds          r'   r   zCommand.describe_operation  s    9f%% 	!-6J9))IND-1;dl(btFFY&& 	!.7JY**Y]FFF ! [[((r22FF 	#FH 	%f_Ff%%		**,,,yr/B/BBHLLr)   )NF)__name__
__module____qualname__r   requires_system_checksr(   r   r   re   r   staticmethodr   r;   r)   r'   r   r      s        W 	  G
 G
 G
R e
 e
 _e
N	&I &I &I &IP7A 7A 7Ar M M \M M Mr)   r   )r   r   	importlibr   django.appsr   django.core.management.baser   r   r   django.core.management.sqlr   r	   	django.dbr
   r   r   !django.db.migrations.autodetectorr   django.db.migrations.executorr   django.db.migrations.loaderr   django.db.migrations.stater   r   django.utils.module_loadingr   django.utils.textr   r   r;   r)   r'   <module>r      sI   



  # # # # # #       R R R R R R R R R R X X X X X X X X ; ; ; ; ; ; ; ; ; ; C C C C C C ; ; ; ; ; ; 6 6 6 6 6 6 ? ? ? ? ? ? ? ? < < < < < < ' ' ' ' ' 'nM nM nM nM nMk nM nM nM nM nMr)   