
    h^J                     d    S SK Jr  S SKJrJr  SSKJr  SSKJ	r	  SSK
Jr  SSKJr   " S S	5      rg
)    )apps)
migrationsrouter   )InvalidMigrationPlan)MigrationLoader)MigrationRecorder)ProjectStatec                   r    \ rS rSrSrSS jrSS jrSS jrSS jrS r	S	 r
SS
 jrS rSS jrS rS rSrg)MigrationExecutor
   zi
End-to-end migration execution - load migrations and run them up or down
to a specified set of targets.
Nc                     Xl         [        U R                   5      U l        [        U R                   5      U l        X l        g N)
connectionr   loaderr	   recorderprogress_callback)selfr   r   s      O/var/www/html/env/lib/python3.13/site-packages/django/db/migrations/executor.py__init__MigrationExecutor.__init__   s-    $%doo6)$//:!2    c                   ^	 / nU(       a  0 nO[        U R                  R                  5      nU GH  m	T	S   c  U R                  R                  R	                  5        H  nUS   T	S   :X  d  M  U R                  R                  R                  U5       HO  nXd;   d  M
  UR                  U R                  R                  R                  U   S45        UR                  U5        MQ     M     M  T	U;   Ga?  U R                  R                  (       a`  T	U R                  R                  R                  ;  a<  SU R                  l	        U R                  R                  5         U R                  XS9s  $ [        U	4S jU R                  R                  R                  T	   R                   5       5      nU H{  nU R                  R                  R                  U5       HO  nXd;   d  M
  UR                  U R                  R                  R                  U   S45        UR                  U5        MQ     M}     GM  U R                  R                  R                  T	5       Hc  nXd;  d  M
  UR                  U R                  R                  R                  U   S45        U R                  R                  R                  U   XF'   Me     GM     U$ )zL
Given a set of targets, return a list of (Migration instance, backwards?).
r   r   TFclean_startc              3   D   >#    U  H  nUS    TS    :X  d  M  Uv   M     g7f)r   N ).0ntargets     r   	<genexpr>3MigrationExecutor.migration_plan.<locals>.<genexpr>8   s*      %Htvay( AHs    	 )dictr   applied_migrationsgraph
root_nodesbackwards_planappendnodespopreplace_migrationsnode_mapbuild_graphmigration_plansortedchildrenforwards_plan)
r   targetsr   planappliedroot	migrationnext_in_appnoder    s
            @r   r.    MigrationExecutor.migration_plan   s-    G4;;99:GFay  KK--88:DAw&)+)-):):)I)I$)OI(3 $T[[->->-D-DY-OQU,V W 'I 6 *P ; 7" KK22dkk&7&7&@&@@5:DKK2KK++-..w.PP
 % %![[..77?HH% 
 (D%)[[%6%6%E%Ed%K	$/ KK):):)@)@)KT(RS#KK	2 &L ( "&!2!2!@!@!HI /T[[%6%6%<%<Y%G$OP-1[[->->-D-DY-O* "II P r   c                    [        U R                  R                  S9nU(       a  U R                  U R                  R                  R                  5       SS9nU R                  R                   Vs1 s HK  nX@R                  R                  R                  ;   d  M(  U R                  R                  R                  U   iMM     nnU H  u  pgXe;   d  M  UR                  USS9  M     U$ s  snf )z
Create a project state including all the applications without
migrations and applied migrations if with_applied_migrations=True.
)	real_appsTr   Fpreserve)	r
   r   unmigrated_appsr.   r%   
leaf_nodesr$   r)   mutate_state)r   with_applied_migrationsstate	full_plankeyr$   r6   _s           r   _create_project_state'MigrationExecutor._create_project_stateI   s    
 t{{'B'BC"++!!,,.D , I
  ;;99"9C++++111 -!!'',9  "
 !*	2**55*A !* "s   /'C''C'c                 d   U/ :X  a/  U R                   R                  5       (       d  U R                  SS9$ OU R                   R                  5         Uc  U R	                  U5      nU R	                  U R
                  R                  R                  5       SS9n[        S U 5       5      n[        S U 5       5      nU(       d  Uc  U R                  SS9nOLXx:X  a  [        SU5      eU(       a$  Uc  U R                  SS9nU R                  X2XdUS9nOU R                  X&US	9nU R                  5         U$ )
z
Migrate the database up to the given targets.

Django first needs to create all project states before a migration is
(un)applied and in a second step run all the database operations.
F)rA   Tr   c              3   4   #    U  H  u  pU(       + v   M     g 7fr   r   r   mig	backwardss      r   r!   ,MigrationExecutor.migrate.<locals>.<genexpr>t   s     Dt^Sy==ts   c              3   *   #    U  H	  u  pUv   M     g 7fr   r   rJ   s      r   r!   rM   u   s     AD.#IDs   zMigration plans with both forwards and backwards migrations are not supported. Please split your migration process into separate plans of only forwards OR backwards migrations.fakefake_initialrP   )r   	has_tablerF   ensure_schemar.   r   r%   r?   allr   _migrate_all_forwards_migrate_all_backwardscheck_replacements)	r   r2   r3   rB   rP   rQ   rC   all_forwardsall_backwardss	            r   migrateMigrationExecutor.migrate^   sH    2:==**,,11%1PP - MM'')<&&w/D''KK((* ( 
	 DtDDADAA}2242P*&K 	  }2242P..Y / E //d/KE!r   c                 r   U Vs1 s H  ofS   iM	     nnU H  u  pU(       d    U$ X;   d  M  SUR                   ;  aP  U R                  (       a  U R                  S5        UR                    U R                  (       a  U R                  S5        U R                  XXES9nUR	                  U5        M     U$ s  snf )zz
Take a list of 2-tuples of the form (migration instance, False) and
apply them in the order they occur in the full_plan.
r   r   render_startrender_successrO   )__dict__r   r   apply_migrationremove)
r   rB   r3   rC   rP   rQ   mmigrations_to_runr6   rE   s
             r   rV   'MigrationExecutor._migrate_all_forwards   s    
 ,004aqT40%LI$
   -/--..~>JJ--../?@,,4 -  "((3# && ) 1s   B4c                    U Vs1 s H  oDS   iM	     nn0 nU R                  5       nU R                  R                   Vs1 s HK  nXR                  R                  R                  ;   d  M(  U R                  R                  R                  U   iMM     n	nU R
                  (       a  U R                  S5        U Hm  u  pU(       d    OcX;   aC  SUR                  ;  a  UR                    XvU
'   U
R                  USS9nUR                  U
5        MV  X;   d  M]  U
R                  USS9  Mo     U R
                  (       a  U R                  S5        U H(  u  pU R                  Xj   XS9  U	R                  U
5        M*     US	   S   nXl   nU?[        U5       H4  u  nu  pX:X  d  M  X-S
  H  u  pX;   d  M  U
R                  USS9  M       U$    U$ s  snf s  snf )a`  
Take a list of 2-tuples of the form (migration instance, True) and
unapply them in reverse order they occur in the full_plan.

Since unapplying a migration requires the project state prior to that
migration, Django will compute the migration states before each of them
in a first run over the plan and then unapply them in a second run over
the plan.
r   r^   r   Tr<   Fr_   rR   N)rF   r   r$   r%   r)   r   r`   r   r@   rb   unapply_migration	enumerate)r   r3   rC   rP   rc   rd   statesrB   rD   r$   r6   rE   last_unapplied_migrationindexs                 r   rW   (MigrationExecutor._migrate_all_backwards   s    ,004aqT40**, {{55
5kk''--- )DKK##C(5 	 

 !!"">2%LI$
 -/JJ$)y!!..ut.D!((30 &&uu&=' &( !!""#34 LI""6#4i"K%%i0 ! $(8A; 0 J%.y%9!E>I4$-f$5LI 6!..uu.E %6  &: k 1
s   G'G*'Gc                    SnU R                   (       a  U R                  SX#5        U(       d  U(       a  U R                  X5      u  paU(       a  SnU(       da  U R                  R                  UR                  S9 nUR                  X5      nUR                  (       d  U R                  U5        SnSSS5        U(       d  U R                  U5        U R                   (       a  U R                  SX#5        U$ ! , (       d  f       NK= f)zRun a migration forwards.Fapply_startTatomicNapply_success)r   detect_soft_appliedr   schema_editorrq   applydeferred_sqlrecord_migration)r   rB   r6   rP   rQ   migration_recordedr4   rt   s           r   ra   !MigrationExecutor.apply_migration   s    "!!""=)B!%!9!9%!KD__22$++ 3 "%OOEAE(55--i8-1* "!!),!!""?ID s   :6C55
Dc                     UR                   (       a1  UR                    H   u  p#U R                  R                  X#5        M"     g U R                  R                  UR                  UR                  5        g r   )replacesr   record_applied	app_labelname)r   r6   r}   r~   s       r   rw   "MigrationExecutor.record_migration
  sO    #,#5#5	,,Y= $6 MM(()<)<innMr   c                    U R                   (       a  U R                  SX#5        U(       d=  U R                  R                  UR                  S9 nUR	                  X5      nSSS5        UR
                  (       a0  UR
                   H   u  pVU R                  R                  XV5        M"     U R                  R                  UR                  UR                  5        U R                   (       a  U R                  SX#5        U$ ! , (       d  f       N= f)zRun a migration backwards.unapply_startrp   Nunapply_success)
r   r   rt   rq   unapplyr{   r   record_unappliedr}   r~   )r   rB   r6   rP   rt   r}   r~   s          r   rh   #MigrationExecutor.unapply_migration  s    !!""?ID.. '' / !))%?
 #,#5#5	..y? $6&&y':':INNK!!""#4iF s   C>>
Dc                 &  ^ U R                   R                  5       mU R                  R                  R	                  5        HN  u  p[        U4S jUR                   5       5      nU(       d  M-  UT;  d  M5  U R                   R                  " U6   MP     g)a  
Mark replacement migrations applied if their replaced set all are.

Do this unconditionally on every migrate, rather than just when
migrations are applied or unapplied, to correctly handle the case
when a new squash migration is pushed to a deployment that already had
all its replaced migrations applied. In this case no new migration will
be applied, but the applied state of the squashed migration must be
maintained.
c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r   )r   rc   r4   s     r   r!   7MigrationExecutor.check_replacements.<locals>.<genexpr>2  s     G4Fq7l4Fs   N)r   r$   r   replacementsitemsrU   r{   r|   )r   rD   r6   all_appliedr4   s       @r   rX   $MigrationExecutor.check_replacements%  sj     --224"kk66<<>NCGI4F4FGGK{s'1,,c2 ?r   c                 r  ^ ^ U 4S jnTR                   c)  [        U4S jTR                   5       5      (       a  SU4$ OTR                   SL a  SU4$ Uc1  T R                  R	                  TR
                  TR                  4SS9nOTR                  U5      nUR                  nSnSnT R                  R                  R                  nT R                  R                  5        n	[        T R                  R                  R                  U	5      5      n
U(       a  U
 Vs1 s H  oR!                  5       iM     n
nSSS5        TR"                   GH  n[%        U[&        R(                  5      (       a  UR+                  TR
                  UR                  5      nUR,                  R.                  (       a*  [0        R*                  " UR,                  R.                  5      nU" TU5      (       a  M  UR,                  R2                  nU(       a  UR!                  5       nUW
;  a  SU4s  $ SnM  [%        U[&        R4                  5      (       d  M  UR+                  TR
                  UR6                  5      nUR,                  R.                  (       a*  [0        R*                  " UR,                  R.                  5      nU" TU5      (       a  GMx  UR,                  R2                  nUR,                  R9                  UR                  5      nUR:                  (       aR  UR<                  R>                  R,                  R2                  nU(       a  UR!                  5       nUW
;  a  SU4s  $ SnGM  T R                  R                  5        n	T R                  R                  RA                  X5      nSSS5        W HN  nURB                  nUR                  nU(       a   UR!                  5       nUR!                  5       nUU:X  d  MJ  Sn  GM     SU4s  $    U=(       d    UU4$ s  snf ! , (       d  f       GN= f! , (       d  f       N= f)z
Test whether a migration has been implicitly applied - that the
tables or columns it would create exist. This is intended only for use
on initial migrations (as it only looks for CreateModel and AddField).
c                   > UR                   R                  =(       do    UR                   R                  (       + =(       dM    [        R                  " TR
                  R                  U R                  UR                   R                  S9(       + $ )zx
No need to detect tables for proxy models, unmanaged models, or
models that can't be migrated on the current database.
)
model_name)	_metaproxymanagedr   allow_migrater   aliasr}   r   )r6   modelr   s     r   should_skip_detecting_modelJMigrationExecutor.detect_soft_applied.<locals>.should_skip_detecting_model=  sg     !! {{***++OO))''${{55 r   Nc              3   F   >#    U  H  u  pUTR                   :H  v   M     g 7fr   )r}   )r   appr~   r6   s      r   r!   8MigrationExecutor.detect_soft_applied.<locals>.<genexpr>N  s      V?U)#3)---?Us   !FT)at_end)"initialanydependenciesr   project_stater}   r~   r@   r   r   featuresignores_table_name_casecursorsetintrospectiontable_namescasefold
operations
isinstancer   CreateModel	get_modelr   swappedglobal_appsdb_tableAddFieldr   	get_fieldmany_to_manyremote_fieldthroughget_table_descriptioncolumn)r   r   r6   r   after_stater   found_create_model_migrationfound_add_field_migrationfold_identifier_caser   existing_table_namesr~   	operationr   r   tablefieldthrough_db_tablecolumnsr   field_columncolumn_names   ` `                   r   rs   %MigrationExecutor.detect_soft_applied6  s   	 $Vy?U?UVVVm++ W%'-'' ++33$$inn5d 4 K $00?K',$$)!#77OO__##%#&--99&A$  $0D(0DMMO0D % ( & #--I)Z%;%;<<y':':INNK;;&& (11%++2E2EFE.y%@@ ;;//''002H#77 -///3,Iz':':;;y':':I<P<PQ;;&& (11%++2E2EFE.y%@@,,--inn= %%','9'9'A'A'G'G'P'P$++;+D+D+F('/CC$m33481 __++-"oo;;QQG . &F#(<<L"(++K+&1&:&:&<'3'<'<'>"l2481 & !-//g .l -I0IKWWu( &%\ .-s*   !:PP4P:&P(P
P%(
P6	)r   r   r   r   r   )F)NNFF)FF)__name__
__module____qualname____firstlineno____doc__r   r.   rF   r[   rV   rW   ra   rw   rh   rX   rs   __static_attributes__r   r   r   r   r   
   sD    
31f*3j6?B6N&3"gXr   r   N)django.apps.registryr   r   	django.dbr   r   
exceptionsr   r   r   r   r	   rB   r
   r   r   r   r   <module>r      s%    4 ( , # ' SX SXr   