Ë
    ¹ûàg^J  ã                   ód   — d dl mZ d dlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ  G d„ d	«      Zy
)é    )Úapps)Ú
migrationsÚrouteré   )ÚInvalidMigrationPlan)ÚMigrationLoader)ÚMigrationRecorder)ÚProjectStatec                   ó^   — e Zd ZdZdd„Zdd„Zdd„Zdd„Zd„ Zd„ Z	dd	„Z
d
„ Zdd„Zd„ Zd„ Zy)ÚMigrationExecutorzu
    End-to-end migration execution - load migrations and run them up or down
    to a specified set of targets.
    Nc                 óˆ   — || _         t        | j                   «      | _        t        | j                   «      | _        || _        y ©N)Ú
connectionr   Úloaderr	   ÚrecorderÚprogress_callback)Úselfr   r   s      ú[/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/django/db/migrations/executor.pyÚ__init__zMigrationExecutor.__init__   s1   € Ø$ˆŒÜ% d§o¡oÓ6ˆŒÜ)¨$¯/©/Ó:ˆŒØ!2ˆÕó    c                 óH  ‡	— g }|ri }nt        | j                  j                  «      }|D �]t  Š	‰	d   €ª| j                  j                  j	                  «       D ]‚  }|d   ‰	d   k(  sŒ| j                  j                  j                  |«      D ]L  }||v sŒ|j                  | j                  j                  j                  |   df«       |j                  |«       ŒN Œ„ Œ³‰	|v �r4| j                  j                  rb‰	| j                  j                  j                  vr@d| j                  _	        | j                  j                  «        | j                  ||¬«      c S t        ˆ	fd„| j                  j                  j                  ‰	   j                  D «       «      }|D ]v  }| j                  j                  j                  |«      D ]L  }||v sŒ|j                  | j                  j                  j                  |   df«       |j                  |«       ŒN Œx �Œì| j                  j                  j                  ‰	«      D ]a  }||vsŒ|j                  | j                  j                  j                  |   df«       | j                  j                  j                  |   ||<   Œc �Œw |S )z\
        Given a set of targets, return a list of (Migration instance, backwards?).
        r   r   TF©Úclean_startc              3   ó8   •K  — | ]  }|d    ‰d    k(  r|–— Œ y­w)r   N© )Ú.0ÚnÚtargets     €r   ú	<genexpr>z3MigrationExecutor.migration_plan.<locals>.<genexpr>8   s)   øè ø€ ò %àØ˜‘t˜v a™yÒ(ô ñ%ùs   ƒ)Ú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+   z MigrationExecutor.migration_plan   sV  ø€ ð ˆÙØ‰Gä˜4Ÿ;™;×9Ñ9Ó:ˆGØó '	PˆFà�a‰yÐ Ø ŸK™K×-Ñ-×8Ñ8Ó:ò 7�DØ˜A‘w &¨¡)Ó+Ø)-¯©×):Ñ):×)IÑ)IÈ$Ó)Oò 7˜IØ(¨GÒ3Ø $§¡¨T¯[©[×->Ñ->×-DÑ-DÀYÑ-OÐQUÐ,VÔ WØ '§¡¨IÕ 6ñ7ñ7ð ˜7Ò"ð —K‘K×2Ò2Ø d§k¡k×&7Ñ&7×&@Ñ&@Ñ@à5:�D—K‘KÔ2Ø—K‘K×+Ñ+Ô-Ø×.Ñ.¨wÀKÐ.ÓPÒPô
 %ó %à!Ÿ[™[×.Ñ.×7Ñ7¸Ñ?×HÑHô%ó �ð
 (ò 3�DØ%)§[¡[×%6Ñ%6×%EÑ%EÀdÓ%Kò 3˜	Ø$¨Ò/Ø ŸK™K¨¯©×):Ñ):×)@Ñ)@ÀÑ)KÈTÐ(RÔSØ#ŸK™K¨	Õ2ñ3ò3ð "&§¡×!2Ñ!2×!@Ñ!@ÀÓ!Hò P�IØ ¨Ò/ØŸ™ T§[¡[×%6Ñ%6×%<Ñ%<¸YÑ%GÈÐ$OÔPØ-1¯[©[×->Ñ->×-DÑ-DÀYÑ-O˜ 	Ò*òPðI'	PðP ˆr   c                 óÎ  — t        | j                  j                  ¬«      }|r½| j                  | j                  j                  j                  «       d¬«      }| j                  j                  D �ch c]G  }|| j                  j                  j                  v r#| j                  j                  j                  |   ’ŒI }}|D ]  \  }}||v sŒ|j                  |d¬«       Œ |S c c}w )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!   r3   Ú_s           r   Ú_create_project_statez'MigrationExecutor._create_project_stateI   sÞ   € ô
  t§{¡{×'BÑ'BÔCˆÙ"à×+Ñ+Ø—‘×!Ñ!×,Ñ,Ó.¸Dð ,ó ˆIð
  Ÿ;™;×9Ñ9ö"àØ˜$Ÿ+™+×+Ñ+×1Ñ1Ñ1ð —‘×!Ñ!×'Ñ'¨Ó,ð"Ðð "ð
 !*ò B‘�	˜1ØÐ 2Ò2Ø×*Ñ*¨5¸5Ð*ÕAðBð ˆùò"s   Á0AC"c                 óf  — |g k(  r,| j                   j                  «       s,| j                  d¬«      S | j                   j                  «        |€| j	                  |«      }| j	                  | j
                  j                  j                  «       d¬«      }t        d„ |D «       «      }t        d„ |D «       «      }|s|€e| j                  d¬«      }nR||k(  rt        d|«      ‚|r+|€| j                  d¬«      }| j                  |||||¬«      }n| j                  |||¬	«      }| j                  «        |S )
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)r=   Tr   c              3   ó(   K  — | ]
  \  }}| –— Œ y ­wr   r   ©r   ÚmigÚ	backwardss      r   r   z,MigrationExecutor.migrate.<locals>.<genexpr>t   s   è ø€ ÒD©^¨S°)˜yœ=ÑDùs   ‚c              3   ó&   K  — | ]	  \  }}|–— Œ y ­wr   r   rE   s      r   r   z,MigrationExecutor.migrate.<locals>.<genexpr>u   s   è ø€ ÒA©.¨#¨yœIÑAùs   ‚z°Migration 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©rJ   )r   Ú	has_tablerB   Úensure_schemar+   r   r"   r;   Úallr   Ú_migrate_all_forwardsÚ_migrate_all_backwardsÚcheck_replacements)	r   r/   r0   r>   rJ   rK   r?   Úall_forwardsÚall_backwardss	            r   ÚmigratezMigrationExecutor.migrate^   sE  € ð �2Š:Ø—=‘=×*Ñ*Ô,Ø×1Ñ1È%Ð1ÓPÐPà�M‰M×'Ñ'Ô)àˆ<Ø×&Ñ& wÓ/ˆDà×'Ñ'Ø�K‰K×Ñ×(Ñ(Ó*¸ð (ó 
ˆ	ô ÑD¸tÔDÓDˆÜÑA¸DÔAÓAˆáØˆ}à×2Ñ2È4Ð2ÓP‘Ø˜]Ò*ä&ðKð ó	ð ñ Øˆ}à×2Ñ2È4Ð2ÓP�Ø×.Ñ.Ø�t˜Y¨TÀð /ó ‰Eð ×/Ñ/°°iÀdÐ/ÓKˆEà×ÑÔ!àˆr   c                 óP  — |D �ch c]  }|d   ’Œ	 }}|D ]‰  \  }}	|s |S ||v sŒd|j                   vrF| j                  r| j                  d«       |j                   | j                  r| j                  d«       | j                  ||||¬«      }|j	                  |«       Œ‹ |S c c}w )z’
        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_successrI   )Ú__dict__r   r   Úapply_migrationÚremove)
r   r>   r0   r?   rJ   rK   ÚmÚmigrations_to_runr3   rA   s
             r   rP   z'MigrationExecutor._migrate_all_forwards“   sÅ   € ð
 ,0Ö0 a˜Q˜q›TÐ0ÐÐ0Ø%ò 	4‰LˆI�qÙ$ð
 ð ˆð Ð-Ò-Ø §¡Ñ/Ø×-Ò-Ø×.Ñ.¨~Ô>Ø—J’JØ×-Ò-Ø×.Ñ.Ð/?Ô@Ø×,Ñ,Ø˜9¨4¸lð -ó �ð "×(Ñ(¨Õ3ð#	4ð& ˆùò) 1s   …B#c                 ó†  — |D �ch c]  }|d   ’Œ	 }}i }| j                  «       }| j                  j                  D �ch c]G  }|| j                  j                  j                  v r#| j                  j                  j                  |   ’ŒI }	}| j
                  r| j                  d«       |D ]i  \  }
}|s nb|
|v rDd|j                  vr|j                   |||
<   |
j                  |d¬«      }|j                  |
«       ŒR|
|	v sŒW|
j                  |d¬«       Œk | j
                  r| j                  d«       |D ]-  \  }
}| j                  ||
   |
|¬«       |	j                  |
«       Œ/ |d	   d   }||   }|`t        |«      D ]5  \  }\  }
}|
|k(  sŒ||d
 D ]  \  }
}|
|	v sŒ|
j                  |d¬«       Œ  |S  |S c c}w c c}w )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   rW   r   Tr8   FrX   rL   éÿÿÿÿN)rB   r   r!   r"   r&   r   rY   r   r<   r[   Úunapply_migrationÚ	enumerate)r   r0   r?   rJ   r\   r]   Ústatesr>   r@   r!   r3   rA   Úlast_unapplied_migrationÚindexs                 r   rQ   z(MigrationExecutor._migrate_all_backwards®   s  € ð ,0Ö0 a˜Q˜q›TÐ0ÐÐ0àˆØ×*Ñ*Ó,ˆð —{‘{×5Ñ5ö
àØ�d—k‘k×'Ñ'×-Ñ-Ñ-ð �K‰K×Ñ×#Ñ# CÓ(ð
Ðð 
ð
 ×!Ò!Ø×"Ñ" >Ô2Ø%ò 	>‰LˆI�qÙ$ñ
 ØÐ-Ñ-Ø §¡Ñ/Ø—J’Jà$)��yÑ!à!×.Ñ.¨u¸tÐ.ÓD�Ø!×(Ñ(¨Õ3ØÐ0Ò0ð ×&Ñ& u°uÐ&Õ=ð'	>ð( ×!Ò!Ø×"Ñ"Ð#3Ô4à ò 	1‰LˆI�qØ×"Ñ" 6¨)Ñ#4°iÀdÐ"ÔKØ×%Ñ% iÕ0ð	1ð $(¨¡8¨A¡;Ð ØÐ/Ñ0ˆð ˆJÜ%.¨yÓ%9ò 	Ñ!ˆE‘>�I˜qØÐ4Ó4Ø$-¨e¨fÐ$5ò F‘L�I˜qØ Ð$6Ò6Ø!×.Ñ.¨u¸uÐ.ÕEðFð àˆð	ð ˆùòk 1ùò
s   …F9½AF>c                 óÂ  — d}| j                   r| j                  d||«       |s}|r| j                  ||«      \  }}|rd}|s`| j                  j                  |j                  ¬«      5 }|j                  ||«      }|j                  s| j                  |«       d}ddd«       |s| j                  |«       | j                   r| j                  d||«       |S # 1 sw Y   Œ=xY w)zRun a migration forwards.FÚapply_startT©ÚatomicNÚapply_success)r   Údetect_soft_appliedr   Úschema_editorrh   ÚapplyÚdeferred_sqlÚrecord_migration)r   r>   r3   rJ   rK   Úmigration_recordedr1   rk   s           r   rZ   z!MigrationExecutor.apply_migrationï   sâ   € à"ÐØ×!Ò!Ø×"Ñ" =°)¸TÔBÙÙà!%×!9Ñ!9¸%ÀÓ!K‘�˜ÙØ�DÙà—_‘_×2Ñ2Ø$×+Ñ+ð 3ó ð 2à"Ø%ŸO™O¨E°=ÓA�EØ(×5Ò5Ø×-Ñ-¨iÔ8Ø-1Ð*÷2ñ "Ø×!Ñ! )Ô,à×!Ò!Ø×"Ñ" ?°I¸tÔDØˆ÷2ð 2ús   Á'2CÃCc                 óÞ   — |j                   r1|j                   D ]!  \  }}| j                  j                  ||«       Œ# y | j                  j                  |j                  |j                  «       y r   )Úreplacesr   Úrecord_appliedÚ	app_labelÚname)r   r3   rs   rt   s       r   rn   z"MigrationExecutor.record_migration
  sX   € à×ÒØ#,×#5Ñ#5ò >‘�	˜4Ø—‘×,Ñ,¨Y¸Õ=ñ>ð �M‰M×(Ñ(¨×)<Ñ)<¸i¿n¹nÕMr   c                 óø  — | j                   r| j                  d||«       |sA| j                  j                  |j                  ¬«      5 }|j	                  ||«      }ddd«       |j
                  r0|j
                  D ]!  \  }}| j                  j                  ||«       Œ# | j                  j                  |j                  |j                  «       | j                   r| j                  d||«       |S # 1 sw Y   Œ–xY w)zRun a migration backwards.Úunapply_startrg   NÚunapply_success)
r   r   rk   rh   Úunapplyrq   r   Úrecord_unappliedrs   rt   )r   r>   r3   rJ   rk   rs   rt   s          r   r`   z#MigrationExecutor.unapply_migration  sç   € à×!Ò!Ø×"Ñ" ?°I¸tÔDÙØ—‘×.Ñ.Ø ×'Ñ'ð /ó ð @àØ!×)Ñ)¨%°Ó?�÷@ð
 ×ÒØ#,×#5Ñ#5ò @‘�	˜4Ø—‘×.Ñ.¨y¸$Õ?ð@à�‰×&Ñ& y×':Ñ':¸I¿N¹NÔKà×!Ò!Ø×"Ñ"Ð#4°iÀÔFØˆ÷@ð @ús   ÁC0Ã0C9c                 ó  ‡— | j                   j                  «       Š| j                  j                  j	                  «       D ]D  \  }}t        ˆfd„|j                  D «       «      }|sŒ'|‰vsŒ, | j                   j                  |Ž  ŒF y)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   ó&   •K  — | ]  }|‰v –— Œ
 y ­wr   r   )r   r\   r1   s     €r   r   z7MigrationExecutor.check_replacements.<locals>.<genexpr>2  s   øè ø€ ÒG¨q˜a 7œlÑGùs   ƒN)r   r!   r   ÚreplacementsÚitemsrO   rq   rr   )r   r@   r3   Úall_appliedr1   s       @r   rR   z$MigrationExecutor.check_replacements%  sq   ø€ ð —-‘-×2Ñ2Ó4ˆØ"Ÿk™k×6Ñ6×<Ñ<Ó>ò 	3‰NˆC�ÜÓG°I×4FÑ4FÔGÓGˆKÚ˜s¨'Ò1Ø,�—‘×,Ñ,¨cÒ2ñ	3r   c                 ó´  ‡ ‡— ˆ fd„}‰j                   €"t        ˆfd„‰j                  D «       «      rd|fS ‰j                   du rd|fS |€4‰ j                  j	                  ‰j
                  ‰j                  fd¬«      }n‰j                  |«      }|j                  }d}d}‰ j                  j                  j                  }‰ j                  j                  «       5 }	t        ‰ j                  j                  j                  |	«      «      }
|r|
D �ch c]  }|j!                  «       ’Œ }
}ddd«       ‰j"                  D �]u  }t%        |t&        j(                  «      r¤|j+                  ‰j
                  |j                  «      }|j,                  j.                  r)t1        j*                  |j,                  j.                  «      } |‰|«      rŒ�|j,                  j2                  }|r|j!                  «       }|
vrd|fc S d}ŒÂt%        |t&        j4                  «      sŒÝ|j+                  ‰j
                  |j6                  «      }|j,                  j.                  r)t1        j*                  |j,                  j.                  «      } |‰|«      r�ŒM|j,                  j2                  }|j,                  j9                  |j                  «      }|j:                  rJ|j<                  j>                  j,                  j2                  }|r|j!                  «       }|
vrd|fc S d}�ŒÞ‰ j                  j                  «       5 }	‰ j                  j                  jA                  |	|«      }ddd«       D ]F  }|jB                  }|j                  }|r |j!                  «       }|j!                  «       }||k(  sŒCd} �Œq d|fc S  |xs ||fS c c}w # 1 sw Y   �ŒœxY w# 1 sw Y   ŒuxY w)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                 óü   •— |j                   j                  xsd |j                   j                   xsK t        j                  ‰j
                  j                  | j                  |j                   j                  ¬«       S )zœ
            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   Úaliasrs   r�   )r3   Úmodelr   s     €r   Úshould_skip_detecting_modelzJMigrationExecutor.detect_soft_applied.<locals>.should_skip_detecting_model=  sg   ø€ ð —‘×!Ñ!ò Ø—{‘{×*Ñ*Ð*òä×+Ñ+Ø—O‘O×)Ñ)Ø×'Ñ'Ø$Ÿ{™{×5Ñ5ôð ðr   Nc              3   óB   •K  — | ]  \  }}|‰j                   k(  –— Œ y ­wr   )rs   )r   Úapprt   r3   s      €r   r   z8MigrationExecutor.detect_soft_applied.<locals>.<genexpr>N  s    øè ø€ ÒV±)°#°t�3˜)×-Ñ-Õ-ÑVùs   ƒFT)Úat_end)"ÚinitialÚanyÚdependenciesr   Úproject_staters   rt   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�   r3   rˆ   Úafter_stater   Úfound_create_model_migrationÚfound_add_field_migrationÚfold_identifier_caser’   Úexisting_table_namesrt   Ú	operationr‡   r�   ÚtableÚfieldÚthrough_db_tableÚcolumnsr¤   Úfield_columnÚcolumn_names   ` `                   r   rj   z%MigrationExecutor.detect_soft_applied6  s„  ù€ ô	ð ×ÑÐ$äÓV¸y×?UÑ?UÔVÔVØ˜mÐ+Ð+Ø×Ñ %Ñ'à˜-Ð'Ð'àÐ ØŸ+™+×3Ñ3Ø×$Ñ$ i§n¡nÐ5¸dð 4ó ‰Kð $×0Ñ0°Ó?ˆKØ×ÑˆØ',Ð$Ø$)Ð!Ø#Ÿ™×7Ñ7×OÑOÐØ�_‰_×#Ñ#Ó%ð 	¨Ü#&Ø—‘×-Ñ-×9Ñ9¸&ÓAó$Ð ñ $à0Dö(Ø(,�D—M‘M•Oð(Ð$ð (÷	ð #×-Ñ-ó 3	0ˆIÜ˜)¤Z×%;Ñ%;Ô<ØŸ™ y×':Ñ':¸I¿N¹NÓK�Ø—;‘;×&Ò&ô (×1Ñ1°%·+±+×2EÑ2EÓF�EÙ.¨y¸%Ô@ØØ Ÿ;™;×/Ñ/�Ù'Ø'×0Ñ0Ó2�HØÐ#7Ñ7Ø  -Ð/Ò/Ø/3Ñ,Ü˜I¤z×':Ñ':Õ;ØŸ™ y×':Ñ':¸I×<PÑ<PÓQ�Ø—;‘;×&Ò&ô (×1Ñ1°%·+±+×2EÑ2EÓF�EÙ.¨y¸%Ô@ÙàŸ™×,Ñ,�ØŸ™×-Ñ-¨i¯n©nÓ=�ð ×%Ò%Ø',×'9Ñ'9×'AÑ'A×'GÑ'G×'PÑ'PÐ$Ù+Ø+;×+DÑ+DÓ+FÐ(Ø'Ð/CÑCØ$ mÐ3Ò3à48Ð1Ù Ø—_‘_×+Ñ+Ó-ð °Ø"Ÿo™o×;Ñ;×QÑQØ ó�G÷ð &ò 
0�FØ#(§<¡<�LØ"(§+¡+�KÙ+Ø&1×&:Ñ&:Ó&<˜Ø'3×'<Ñ'<Ó'>˜Ø" lÓ2Ø48Ð1Úð
0ð ! -Ð/Ò/ðg3	0ðl -ÒIÐ0IÈKÐWÐWùòu(÷	ñ 	ú÷\ð ús*   Ã5OÄN<Ä%OÌ3'OÎ<OÏOÏO	r   )F)NNFF)FF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r+   rB   rU   rP   rQ   rZ   rn   r`   rR   rj   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	   r>   r
   r   r   r   r   ú<module>r¸      s%   ðÝ 4ß (å ,Ý #Ý 'Ý ÷SXò SXr   