a
    ÆZEg[  ã                   @   s˜   d dl 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mZ d dlmZ d dlmZ d d	lmZ d d
lmZ dZdZG dd„ deƒZdS )é    N)Ú
itemgetter)ÚPath)Úcall_command)ÚBaseCommandÚCommandError)ÚDEFAULT_DB_ALIASÚconnections)ÚMigrationLoader)ÚMigrationRecorder)Útimezone)Úsignalcommandzmanagestate.jsonÚdefaultc                   @   s¸   e Zd ZU dZd ZZdZeed< eed< e	ed< e
ed< dd„ Zed	d
„ ƒZe	dœdd„Ze	dœdd„Zedœdd„Zedœdd„Ze	e	edœdd„Zedœdd„Zedœdd„ZdS )ÚCommandz,Manage database state in the convenient way.NÚmigrate_argsÚmigrate_optionsÚfilenameÚ	verbosityc                 C   sÈ   |j dddd� |j ddtdt› d�d	� |j d
dtdt› d�d� |j ddtdt› d�d� |j dddd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� d S )%NÚaction)ÚdumpÚloadztAn action to do. Dump action saves applied migrations to a file. Load action applies migrations specified in a file.)ÚchoicesÚhelpÚstateú?z@A name of a state. Usually a name of a git branch. Defaults to "ú")Únargsr   r   z-dz
--databasez6Nominates a database to synchronize. Defaults to the "z" database.)r   r   z-fz
--filenamez!A file to write to. Defaults to "z	--noinputz
--no-inputÚstore_falseÚinteractivez^The argument for "migrate" command. Tells Django to NOT prompt the user for input of any kind.)r   Údestr   z--fakeÚ
store_truezYThe argument for "migrate" command. Mark migrations as run without actually running them.)r   r   z--fake-initiala   The argument for "migrate" command. 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--planzaThe argument for "migrate" command. Shows a list of the migration actions that will be performed.z--run-syncdbzOThe argument for "migrate" command. Creates tables for apps without migrations.z--checkZcheck_unappliedz_The argument for "migrate" command. Exits with a non-zero status if unapplied migrations exist.)Úadd_argumentÚDEFAULT_STATEr   ÚDEFAULT_FILENAME)ÚselfÚparser© r%   új/var/www/sistema_ama/venv/lib/python3.9/site-packages/django_extensions/management/commands/managestate.pyÚadd_arguments   sP    þ
þ
þ
þþþþþþþzCommand.add_argumentsc                 O   s>   || _ || _|d | _t| | _|| _|| _t| |ƒ|ƒ d S )Nr   )r   r   r   r   ÚconnÚdatabaser   Úgetattr)r#   r   r)   r   r   ÚargsÚoptionsr%   r%   r&   ÚhandleR   s    

zCommand.handle)r   c              	   C   sL   |   ¡ }| |  ¡ ¡ |  ||i¡ | j | j d|› d| j› d�¡¡ dS )z"Save applied migrations to a file.zMigrations for state "z"" have been successfully saved to Ú.N)Úget_migrated_appsÚupdateÚget_applied_migrationsÚwriteÚstdoutÚstyleÚSUCCESSr   )r#   r   Úmigrated_appsr%   r%   r&   r   \   s    ÿzCommand.dumpc                 C   sÚ   |   ¡  |¡}|du r$td|› �ƒ‚i | j¥| j| jdkrD| jd nddœ¥}| ¡ D ]b\}}|  ||¡rlqV| jdkr’| j 	| j
 d|› d�¡¡ ||g| j¢R }tdg|¢R i |¤Ž qV| j 	| j
 d	|› d
�¡¡ dS )zApply migrations from a file.NzNo such state saved: é   r   )r)   r   zApplying migrations for "r   ZmigrateúMigrations for "z!" have been successfully applied.)ÚreadÚgetr   r   r)   r   ÚitemsÚ
is_appliedr3   r2   r4   ÚWARNINGr   r   r5   )r#   r   Ú
migrationsÚkwargsÚappÚ	migrationr+   r%   r%   r&   r   e   s&    ÿý

ÿzCommand.load)Úreturnc                 C   s@   t | jƒj}t |d¡}| jdkr<| j dd t	|ƒ¡ ¡ |S )z!Installed apps having migrations.Zzeror7   zApps having migrations: z, )
r	   r(   r6   ÚdictÚfromkeysr   r3   r2   ÚjoinÚsorted)r#   Zappsr6   r%   r%   r&   r/   ~   s
    
zCommand.get_migrated_appsc                 C   s>   | j r| j S t| jƒ ¡ }t| ¡ tdƒd�}t|ƒ| _ | j S )z,Installed apps with last applied migrations.r7   )Úkey)Ú_applied_migrationsr
   r(   Zapplied_migrationsrF   Úkeysr   rC   )r#   r>   Zlast_appliedr%   r%   r&   r1   †   s    
zCommand.get_applied_migrations)r@   rA   rB   c                 C   sD   |   ¡  |¡}||kr@| jdkr<| j | j d|› d�¡¡ dS dS )z7Check whether a migration for an app is applied or not.r7   r8   z" are already applied.TF)r1   r:   r   r3   r2   r4   r=   )r#   r@   rA   Zappliedr%   r%   r&   r<   ‘   s    

ÿzCommand.is_appliedc                 C   sb   t | jƒ}| ¡ r| ¡ s*td| j› �ƒ‚t| jƒ�}t |¡W  d  ƒ S 1 sT0    Y  dS )zGet saved state from the file.zNo such file: N)r   r   ÚexistsÚis_filer   ÚopenÚjsonr   )r#   ÚpathÚfiler%   r%   r&   r9   œ   s
    
zCommand.read)Údatac                 C   s|   z|   ¡ }W n ty"   i }Y n0 |j|tt ¡ ƒd� t| jdƒ�"}tj	||ddd� W d  ƒ n1 sn0    Y  dS )z.Write new data to the file using existent one.)Z
updated_atÚwé   T)ÚindentÚ	sort_keysN)
r9   r   r0   Ústrr   ÚnowrL   r   rM   r   )r#   rP   ZsavedrO   r%   r%   r&   r2   ¥   s    
zCommand.write)Ú__name__Ú
__module__Ú__qualname__r   r(   r)   rH   rC   Ú__annotations__rU   Úintr'   r   r-   r   r   r/   r1   Úboolr<   r9   r2   r%   r%   r%   r&   r      s"   
6
			r   )rM   Úoperatorr   Úpathlibr   Zdjango.core.managementr   Zdjango.core.management.baser   r   Z	django.dbr   r   Zdjango.db.migrations.loaderr	   Zdjango.db.migrations.recorderr
   Zdjango.utilsr   Z"django_extensions.management.utilsr   r"   r!   r   r%   r%   r%   r&   Ú<module>   s   