3
g[                 @   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	e
e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.Nc             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.)r   r   )add_argumentDEFAULT_STATEr   DEFAULT_FILENAME)selfparser r!   o/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django_extensions/management/commands/managestate.pyadd_arguments   s<    zCommand.add_argumentsc             O   s>   || _ || _|d | _t| | _|| _|| _t| || d S )N	verbosity)migrate_argsmigrate_optionsr$   r   conndatabasefilenamegetattr)r   r   r(   r)   r   argsoptionsr!   r!   r"   handleR   s    

zCommand.handle)r   c             C   sL   | j  }|j| j  | j||i | jj| 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   | j  j|}|dkr$td| | j| j| jdkr@| jd ndd}xd|j D ]X\}}| j||rjqT| jdkr| jj	| j
jd| d ||f| j}td|| qTW | jj	| 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   migratezMigrations for "z!" have been successfully applied.)r8   )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j|d}| jdkr<| jjddj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j }t|j 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   | j  j|}||kr@| jdkr<| jj| jjd| d dS dS )z7Check whether a migration for an app is applied or not.r7   zMigrations for "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   sN   t | j}|j  s|j  r.td| j t| j}tj|S Q R X 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   sj   y| j  }W n tk
r$   i }Y nX |j|ttj d t| jd}tj	||ddd W dQ R X 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   r%   r&   rU   r)   intr$   r#   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   