
    VheS                         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y)    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                   Z     e Zd ZeZdZd Z fdZed        Z	ddZ
d Zed        Z xZS )	CommandzMUpdates database schema. Manages both apps with migrations and those without.c                    |j                  ddd       |j                  ddd       |j                  ddd	d
d       |j                  dt        t        t              d       |j                  ddd       |j                  ddd       |j                  ddd       |j                  ddd       |j                  dddd       |j                  dddd       y ) 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
--databasezHNominates a database to synchronize. Defaults to the "default" database.)defaultchoicesr   z--fake
store_truez5Mark migrations as run without actually running them.)r   r   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
   tupler   )selfparsers     W/home/dcms/DCMS/lib/python3.12/site-packages/django/core/management/commands/migrate.pyadd_argumentszCommand.add_arguments   sI   H 	 	

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

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

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

 	"1 	 	
 	R	 	 	
    c                 :    t         |   |      }i |d|d   giS )N	databasesdatabase)superget_check_kwargs)r'   optionskwargs	__class__s      r)   r0   zCommand.get_check_kwargs\   s,    )'2=&=+
(;'<==r+   c                 ^  +, |d   }|d   | _         |d   | _        t        j                         D ]/  }t	        |j
                  d      st        d|j                         1 t        |   }|j                          t        || j                        }|j                  j                  |       |j                  j                         }|r4dj                  d |j!                         D              }t#        d|z        |d	   }	d
}
|d   ri|d   +	 t        j$                  +       |	r&+|j                  j*                  v r4t#        d+z        +|j                  j*                  vrt#        d+z        |d   r|d   r|d   }|dk(  r+d fg}n	 |j                  j-                  +|      }+|j                  f}||j                  j2                  j4                  vr@||j                  j6                  v r(|j                  j6                  |   }|j8                  d   }|g}d}
nd|d   r;|j                  j2                  j;                         D cg c]  }|d   +k(  s| }}n$|j                  j2                  j;                         }|d   rp|d   st#        d      | j                   dkD  r0| j<                  j?                  d| j@                  jB                         tE        +fdtG        |j                  jH                        tG        |j                  jJ                        z
  D              ,|j                  j6                  j!                         D cg c]%  \  }}tM        ,fd|j8                  D              r|' }}}|r| j<                  j?                  | j@                  jO                  d             |D ](  }|\  }}| j<                  j?                  d| d |        * | j<                  j?                  | j@                  jO                  d!             n,r,D ]  }|\  }}| j                   dkD  r<| j<                  j?                  | j@                  jQ                  d"| d |       d#$       |jR                  jU                  ||       | j                   dkD  s| j<                  j?                  | j@                  jW                  d%              n*| j                   dkD  r| j<                  j?                  d&       |jY                  |      }|d'   r| j<                  j?                  d(| j@                  jP                         |s| j<                  j?                  d)       y |D ]  \  }}| j<                  j?                  t)        |      | j@                  jB                         |jZ                  D ]P  }| j]                  ||      \  }}|r| j@                  j^                  nd }| j<                  j?                  d|z   |       R  |d*   rta        jb                  d+       y |d*   r|rta        jb                  d+       y |d   ry |d	   xr |j                  jd                  }	| j                   d+k\  r| j<                  j?                  | j@                  jC                  d,             |	r|d   r8| j<                  j?                  | j@                  jQ                  d-+z               nc| j<                  j?                  | j@                  jQ                  d.      d/j                  tE        |j                  jd                              z          |
rg| j<                  j?                  | j@                  jQ                  d0      d/j                  tE        |D ch c]  \  }}|	 c}}            xs d1z          n|d   d+   G| j<                  j?                  | j@                  jQ                  d2      t)        |d   d         z          nH| j<                  j?                  | j@                  jQ                  d3      |d   d+   d4|d   d   z          |jg                  d
5      }|j                  } ti        | j                   | j                  |jj                  | j<                  | |6       |	r| j                   d+k\  r4| j<                  j?                  | j@                  jC                  d7             |d   r| jm                  |+g       n&| jm                  ||j                  jd                         | j                   d+k\  r4| j<                  j?                  | j@                  jC                  d8             |s#| j                   d+k\  r| j<                  j?                  d9       | jo                  |j                  jq                         ts        jt                  t                    }!|!jw                  |j                  j2                  :      }"|"r| j<                  j?                  | j@                  jO                  d;d/j                  d< tE        |"      D              z               | j<                  j?                  | j@                  jO                  d=             d}#d}$n
|d>   }#|d?   }$|jy                  |||j{                         |#|$@      }%|%j}                          |%j                  }&|&j                         5  g }'|&j                  D ]:  }(|(j                  |(j                  f})|'j                  |)        |&j                  |)  < 	 d d d        |&j                  'D *cg c]'  }*t        j                  t        j                  |*       ) c}*       t        | j                   | j                  |jj                  | j<                  |&|6       y # t&        $ r}t#        t)        |            d }~ww xY w# t.        $ r t#        d|d+d      t0        $ r t#        d|d+d      w xY wc c}w c c}}w c c}}w # 1 sw Y   xY wc c}*w )ANr.   	verbosityr   
managementz.managementz; c              3   N   K   | ]  \  }}d j                  |      d|  yw), z in N)join).0appnamess      r)   	<genexpr>z!Command.handle.<locals>.<genexpr>{   s'      !9Cedii.4!s   #%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'.Fr   r$   z7Migrations can be pruned only when an app is specified.zPruning migrations:c              3   2   K   | ]  }|d    k(  r|  yw)r   N )r:   	migrationr   s     r)   r=   z!Command.handle.<locals>.<genexpr>   s%      Q<9, s   c              3   &   K   | ]  }|v  
 y wNrB   )r:   replacedto_prunes     r)   r=   z!Command.handle.<locals>.<genexpr>   s     Sx8+Ss   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.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: r8   z  Apply all migrations: z(none)z  Unapply all migrations: z  Target specific migration: z, from )with_applied_migrations)stdoutr   rM   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   2   K   | ]  }t        |        y wrE   )repr)r:   r;   s     r)   r=   z!Command.handle.<locals>.<genexpr>R  s     2X492Xs   zk  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.fakefake_initial)rM   staterT   rU   )Jr5   r   r   get_app_configsr   moduler   namer   prepare_databaser   migration_progress_callbackloadercheck_consistent_historydetect_conflictsr9   itemsr   get_app_configLookupErrorstrmigrated_appsget_migration_by_prefixr   KeyErrorrQ   nodesreplacementsreplaces
leaf_nodesrP   writestyleMIGRATE_HEADINGsortedsetapplied_migrationsdisk_migrationsanyNOTICEMIGRATE_LABELrecorderrecord_unappliedSUCCESSmigration_plan
operationsdescribe_operationWARNINGsysexitunmigrated_apps_create_project_stater	   alias	sync_appsautodetectorproject_stater   	from_appschangesmigratecloneclear_delayed_apps_cachebulk_updatereal_modelsr   
name_lowerappendunregister_modelrender_multipler   
from_model	get_modelr   )-r'   argsr1   r.   
app_config
connectionexecutor	conflictsname_strr>   target_app_labels_onlyerrr   targetsrC   targetincomplete_migrationkeymigration_keymigration_obj4squashed_migrations_with_deleted_replaced_migrationsr;   rY   rM   	backwards	operationmessageis_errorrk   anpre_migrate_statepre_migrate_appsr   r   rT   rU   post_migrate_statepost_migrate_apps
model_keysmodel_state	model_keymodelr   rG   s-                                              @@r)   handlezCommand.handle`   s)   :& -"=1 ..0 	>J#J$5$5|DmZ__=	>
 !*
 	##%$Z1Q1QR 	00< OO446	yy !GPGX! H <>FG  \*
!%;,I-##I.  = ==&R#$  (//"?"??"#G)#STT;G,<$=$%56N'%t,- ( G G!>!I $Y^^4 (//"7"7"="==(//">">>+3??+G+G+O(1::2>F!(%*"[!'44??ASVyEXG  oo++668G7;'"M  ~~!!!"79S9ST !$X__%G%G!Hhoo556"7 H 5=OO4P4P4V4V4XD0M=SM<R<RSS D@ D
 D!!JJ%%2 "V ;I )ICKK%%SE4&&9:; !!JJ%%- %- 	I	$-	T>>A- KK-- $

 8 8:cU!D69R S') .  !))::3E>>A- KK--djj.@.@.GH	I ^^a'KK%%&AB&&w/6?KK3TZZ5M5MN!!"FG  -1 C(IyKK%%c)ndjj6P6PQ%.%9%9 C	,0,C,C%y-) 7?

 2 2D))&7*:EBCC ,-HHQK$%7 \*Nx/N/N
>>QKKdjj889QRS;'KK%%

00>J KK%%

001RS99VHOO,K,K%LMO &!!JJ,,-GHyyw(?tq!(?!@AMXO
 1:a=(KK%%

001MNgajm,-
 KK%%

001PQ+21:a='!*Q-HI
 %::SW:X,11NN;;!	
 ~~"!!JJ../WX {#zI;7z8??+J+JK >>QKKdjj889NOP~~"!!"=>  $00OO113 **40  '..X__5J5J.KKK%%

))')-2Xw2X)XY KK%%

))* D L6?D">2L%--#))+% . 
 	335.33
 **, 	?J0<< ?'11;3I3II	!!),2!22I>?	? 	))HRSuZ""4>>5#9:S	
 	!NN;;"	
a  -"3s8,,-( & &6DiQ    &)96 "&Df )@h	? 	? TsO   n/ o 	pp*ppAp,p*/	o8oo6p
p'c                    | j                   dk\  r| j                   dkD  }|dk(  rV|rt        j                         | _        | j                  j                  d|z  d       | j                  j                          y |dk(  r|r$dt        j                         | j                  z
  z  nd}|r8| j                  j                  | j                  j                  d|z                y | j                  j                  | j                  j                  d	|z                y |d
k(  rV|rt        j                         | _        | j                  j                  d|z  d       | j                  j                          y |dk(  r|r$dt        j                         | j                  z
  z  nd}|r8| j                  j                  | j                  j                  d|z                y | j                  j                  | j                  j                  d	|z                y |dk(  rS|rt        j                         | _        | j                  j                  dd       | j                  j                          y |dk(  r`|r$dt        j                         | j                  z
  z  nd}| j                  j                  | j                  j                  d|z                y y y )NrN   apply_startz  Applying %s...rI   rJ   apply_successz (%.3fs)z FAKEDrL   unapply_startz  Unapplying %s...unapply_successrender_startz  Rendering model states...render_successz DONE)	r5   time	monotonicstartrP   rj   flushrk   rv   )r'   r   rC   rT   compute_timeelapseds         r)   r[   z#Command.migration_progress_callback  s<   >>Q>>A-L&!%!1DJ!!"4y"@!L!!#?*DPJ$.."2TZZ"?@VX  KK%%djj&8&8G9K&LMKK%%djj&8&8&IJ?*!%!1DJ!!"6"B2!N!!#,,DPJ$.."2TZZ"?@VX  KK%%djj&8&8G9K&LMKK%%djj&8&8&IJ>)!%!1DJ!!"?!K!!#++DPJ$.."2TZZ"?@VX  !!$**"4"4Ww5F"GH	 ,C r+   c                 `   j                         5 }j                  j                  |      ddd       t        j                         D cg c]J  }|j
                  <|j                  |v r.|j                  t        j                  |j                  d      fL }}fd}|D ci c]  \  }}|t        t        ||             }	}}| j                  dk\  r| j                  j                  d       j                         5 }
|	j!                         D ]  \  }}|D ]  }|j"                  j%                        s| j                  dk\  r6| j                  j                  d|d	|j"                  j&                  d
       | j                  dk\  r2| j                  j                  d|j"                  j(                  z         |
j+                  |         | j                  dk\  r| j                  j                  d       ddd       y# 1 sw Y   xY wc c}w c c}}w # 1 sw Y   yxY w)z;Run the old syncdb-style operation on a list of app_labels.NF)include_auto_createdc                     | j                   }j                  j                  } ||j                        v xs6 |j                  xr(  ||j                  j                   j                        v  S rE   )_metaintrospectionidentifier_converterdb_tableauto_created)r   opts	converterr   tabless      r)   model_installedz*Command.sync_apps.<locals>.model_installed  sk    ;;D"00EEI4==)V3 %% N!$"3"3"9"9"B"BCvM	 r+   rN   z  Creating tables...   z    Processing rH   z modelz    Creating table %sz    Running deferred SQL...)cursorr   table_namesr   rW   models_modulelabelr   get_migratable_modelsr   listfilterr5   rP   rj   schema_editorr_   r   can_migrateobject_namer   create_model)r'   r   
app_labelsr   r   
all_modelsr   app_name
model_listmanifesteditorr   r   s    `          @r)   r   zCommand.sync_apps  s     	BF--99&AF	B #224	
 ''3
8H8HJ8V   ,,
 0 0u	

 	
		 )3
$* d6/:>??
 
 >>QKK45%%' 	A6(0(8 /$*' /E ;;22:> ~~*))')@)@B ~~*))3ekk6J6JJ ''.//" ~~"!!"?@'	A 	AE	B 	B	
,
	A 	As%   HAH) HC=H$H$H-c                    d}d}t        | d      r/|r| j                  n| j                  }|r|j                  xs dnd}n-t        | d      r|r| j                  n| j
                  }nd}|rd}|t        |      j                  dd      }n|rd}d	}|rd
|z   }t        |      }|| j                         z   |j                  d      z   |fS )z@Return a string that describes a migration operation for --plan.rI   FcodeNsqlzUndo 
IRREVERSIBLETz -> (   )hasattrreverse_coder   __doc__reverse_sqlr   rb   replacer   describechars)r   r   prefixr   r   r   	truncateds          r)   ry   zCommand.describe_operation  s     9f%-69))INND-1dll(btFY&.7Y**Y]]FF [((r2F#FHf_Ff%		**,,yr/BBHLLr+   )NF)__name__
__module____qualname__r   r   r   r*   r0   r   r   r[   r   staticmethodry   __classcell__)r3   s   @r)   r   r      sW    (LW 	C
J> b
 b
H	&IP7Ar 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   rB   r+   r)   <module>r      sB    
  #  R R X ; ; C ; 6 ? < 'kMk kMr+   