B
    ©0H[µ7  ã               @   sÈ   d dl Z 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mZ d dlm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 d dlmZ d dlmZ G dd„ de	ƒZdS )é    N)Ú	takewhile)Úapps)Úsettings)ÚBaseCommandÚCommandError)ÚDEFAULT_DB_ALIASÚconnectionsÚrouter)Ú	Migration)ÚMigrationAutodetector)ÚMigrationLoader)ÚInteractiveMigrationQuestionerÚMigrationQuestionerÚ!NonInteractiveMigrationQuestioner)ÚProjectState)Úget_migration_name_timestamp)ÚMigrationWriterc               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚCommandz"Creates new migration(s) for apps.c             C   sˆ   |j ddddd� |j dddd	d
� |j ddddd
� |j ddddd
� |j dddddd
� |j ddddd dd� |j ddddd
� d S )NÚargsÚ	app_labelÚ*z2Specify the app label(s) to create migrations for.)ÚmetavarÚnargsÚhelpz	--dry-runÚ
store_trueÚdry_runzCJust show what migrations would be made; don't actually write them.)ÚactionÚdestr   z--mergeÚmergez%Enable fixing of migration conflicts.z--emptyÚemptyzCreate an empty migration.z	--noinputz
--no-inputÚstore_falseÚinteractivez:Tells Django to NOT prompt the user for input of any kind.z-nz--nameÚstoreÚnamez$Use this name for migration file(s).)r   r   Údefaultr   z--checkÚcheck_changeszDExit with a non-zero status if model changes are missing migrations.)Úadd_argument)ÚselfÚparser© r)   úrC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\core\management\commands\makemigrations.pyÚadd_arguments   s*    
zCommand.add_argumentsc          	      sê  |d | _ |d | _|d | _|d | _|d | _|d | _|d }tˆ ƒ‰ tƒ }x:ˆ D ]2}yt |¡ W qX t	k
rˆ   | 
|¡ Y qXX qXW |rºx|D ]}| j d| ¡ q˜W t d	¡ td d
d�}dd„ t ¡ D ƒ}tjrâtntg}xFt|ƒD ]:}	t|	 ‰ˆjd dkròt‡fdd„|D ƒƒrò| ˆ¡ qòW | ¡ }
ˆ �rT‡ fdd„|
 ¡ D ƒ}
|
�r†| j�s†d dd„ |
 ¡ D ƒ¡}td| ƒ‚| j�r¤|
�s¤| j d¡ d S | j�r¾|
�r¾|  ||
¡S | j�rÖtˆ | jd�}nt ˆ | jd�}t!| "¡ t# $t¡|ƒ}| j�r@ˆ �stdƒ‚dd„ ˆ D ƒ}|j%||j&| jd�}|  '|¡ d S |j(|j&ˆ �pPd ˆ �pXd | jd�}|�sÌ| j dk�ræt)ˆ ƒdk�r˜| j dˆ  *¡  ¡ n2t)ˆ ƒdk�r¾| j dd  ˆ ¡ ¡ n| j d!¡ n|  '|¡ |�ræt d¡ d S )"NÚ	verbosityr!   r   r   r   r#   r%   z5App '%s' could not be found. Is it in INSTALLED_APPS?é   T)Zignore_no_migrationsc             S   s   h | ]
}|j ’qS r)   )Úlabel)Ú.0Úconfigr)   r)   r*   ú	<setcomp>Q   s    z!Command.handle.<locals>.<setcomp>ZENGINEzdjango.db.backends.dummyc             3   s8   | ]0}t  |¡ ¡ D ]}tjˆ j||jjd �V  qqdS ))Z
model_nameN)r   Úget_app_configZ
get_modelsr	   Zallow_migrateÚaliasZ_metaZobject_name)r/   r   Úmodel)Ú
connectionr)   r*   ú	<genexpr>X   s   z!Command.handle.<locals>.<genexpr>c                s   i | ]\}}|ˆ kr||“qS r)   r)   )r/   r   Úconflict)Ú
app_labelsr)   r*   ú
<dictcomp>d   s   z"Command.handle.<locals>.<dictcomp>z; c             s   s$   | ]\}}d d  |¡|f V  qdS )z%s in %sz, N)Újoin)r/   ÚappÚnamesr)   r)   r*   r6   k   s   zŒConflicting migrations detected; multiple leaf nodes in the migration graph: (%s).
To fix them run 'python manage.py makemigrations --merge'zNo conflicts detected to merge.)Zspecified_appsr   z:You must supply at least one app label when using --empty.c             S   s   i | ]}t d |ƒg|“qS )Zcustom)r
   )r/   r;   r)   r)   r*   r9   Ž   s   )ÚchangesÚgraphÚmigration_name)r>   Ztrim_to_appsZconvert_appsr?   é   zNo changes detected in app '%s'z No changes detected in apps '%s'z', 'zNo changes detected)+r,   r!   r   r   r   r?   Úsetr   r2   ÚLookupErrorÚaddÚstderrÚwriteÚsysÚexitr   Zget_app_configsr   ZDATABASE_ROUTERSr   r   ÚsortedZsettings_dictÚanyZcheck_consistent_historyZdetect_conflictsÚitemsr:   r   ÚstdoutÚhandle_merger   r   r   Zproject_stater   Z	from_appsZarrange_for_graphr>   Úwrite_migration_filesr=   ÚlenÚpop)r'   r8   Úoptionsr%   Zbad_app_labelsr   ÚloaderZconsistency_check_labelsZaliases_to_checkr3   Ú	conflictsZname_strÚ
questionerZautodetectorr=   r)   )r8   r5   r*   Úhandle6   s˜    















zCommand.handlec             C   sÀ  i }�x´|  ¡ D �]¦\}}| jdkr@| j | j d| ¡d ¡ �xt|D �]j}t|ƒ}| jdkrÜytj 	|j¡}W n t
k
rŒ   |j}Y nX | d¡rž|j}| j d| j |¡f ¡ x"|jD ]}| j d| ¡  ¡ qÀW | j�sxtj |j¡}	| |¡�sHtj |	¡�st |	¡ tj |	d¡}
tj |
¡�s@t|
dƒ ¡  d	||< | ¡ }t|jdd
d��}| |¡ W dQ R X qH| jdkrH| j | j d|j ¡d ¡ | j d| ¡  ¡ qHW qW dS )zL
        Take a changes dict and write them out as migration files.
        r@   zMigrations for '%s':Ú
z..z  %s
z	    - %s
z__init__.pyÚwTzutf-8)ÚencodingNé   zFull migrations file '%s':z%s
)rJ   r,   rK   rE   ÚstyleÚMIGRATE_HEADINGr   ÚosÚpathÚrelpathÚ
ValueErrorÚ
startswithÚMIGRATE_LABELÚ
operationsÚdescriber   ÚdirnameÚgetÚisdirÚmkdirr:   ÚisfileÚopenÚcloseÚ	as_stringÚfilename)r'   r=   Zdirectory_createdr   Zapp_migrationsÚ	migrationÚwriterZmigration_stringÚ	operationZmigrations_directoryÚ	init_pathÚfhr)   r)   r*   rM   °   s@    




zCommand.write_migration_filesc          
      sž  | j rtƒ }ntddid�}�xz| ¡ D �]l\‰ }g }xB|D ]:}ˆ ˆ |¡‰‡fdd„ˆj ˆ |f¡D ƒˆ_| ˆ¡ q<W dd„ }t	dd	„ |D ƒŽ }t
d
d	„ t||ƒD ƒƒ}	|	s¼td| ƒ‚x<|D ]4‰ˆj|	d… ˆ_‡fdd	„ˆjD ƒ}
t
|
g ƒˆ_qÂW | jdk�rn| j | j dˆ  ¡¡ xN|D ]F‰| j | j dˆj ¡¡ x$ˆjD ]}| j d| ¡  ¡ �qJW �q$W | ˆ ¡r(dd„ |D ƒ}ytdd	„ |D ƒƒ}W n tk
�r¶   d}Y nX tdtfd‡ fdd„|D ƒiƒ}d|d | j�pðdtƒ  f }||ˆ ƒ}t|ƒ}| j�sZt|j ddd��}| | !¡ ¡ W dQ R X | jdk�r–| j d|j  ¡ q(| jdkr(| j | j d|j" ¡d  ¡ | j d!| !¡  ¡ q(W dS )"zŠ
        Handles merging together conflicted migrations interactively,
        if it's safe; otherwise, advises on how to fix it.
        Ú	ask_mergeT)Údefaultsc                s   g | ]}|d  ˆ j kr|‘qS )r   )r   )r/   Zmig)rl   r)   r*   ú
<listcomp>ï   s    z(Command.handle_merge.<locals>.<listcomp>c                s   t ‡ fdd„ˆ dd … D ƒƒS )Nc             3   s   | ]}|ˆ d  kV  qdS )r   Nr)   )r/   Úitem)Úseqr)   r*   r6   õ   s    z@Command.handle_merge.<locals>.all_items_equal.<locals>.<genexpr>r@   )Úall)ru   r)   )ru   r*   Úall_items_equalô   s    z-Command.handle_merge.<locals>.all_items_equalc             s   s   | ]}|j V  qd S )N)Úancestry)r/   Úmr)   r)   r*   r6   ÷   s    z'Command.handle_merge.<locals>.<genexpr>c             s   s   | ]
}d V  qdS )r@   Nr)   )r/   Zcommon_ancestor_generationr)   r)   r*   r6   ø   s    z$Could not find common ancestor of %sNc             3   s    | ]\}}ˆ   ||¡jV  qd S )N)Úget_migrationra   )r/   Znode_appZ	node_name)rQ   r)   r*   r6   ÿ   s   r   z
Merging %sz  Branch %sz	    - %s
c             S   s   g | ]}t  |j¡‘qS r)   )r   Zparse_numberr#   )r/   rl   r)   r)   r*   rs     s   c             s   s   | ]}|d k	r|V  qd S )Nr)   )r/   Úxr)   r)   r*   r6     s    r@   r
   Zdependenciesc                s   g | ]}ˆ |j f‘qS r)   )r#   )r/   rl   )r   r)   r*   rs     s    z%04i_%szmerge_%srV   zutf-8)rW   z
Created new merge migration %srX   z Full merge migrations file '%s':rU   z%s
)#r!   r   r   rJ   rz   r>   Zforwards_planrx   ÚappendÚzipÚsumr   r^   ÚbranchZmerged_operationsr,   rK   rE   rY   rZ   r`   r#   rb   rq   ÚmaxÚtyper
   r?   r   r   r   rh   r\   rj   rk   )r'   rQ   rR   rS   Zmigration_namesZmerge_migrationsr?   rw   Zmerge_migrations_generationsZcommon_ancestor_countZmigrations_opsrn   ZnumbersZbiggest_numberÚsubclassZnew_migrationrm   rp   r)   )r   rQ   rl   r*   rL   Þ   sb    
 



 



zCommand.handle_mergeN)Ú__name__Ú
__module__Ú__qualname__r   r+   rT   rM   rL   r)   r)   r)   r*   r      s
   z.r   ) r[   rF   Ú	itertoolsr   Zdjango.appsr   Zdjango.confr   Zdjango.core.management.baser   r   Z	django.dbr   r   r	   Zdjango.db.migrationsr
   Z!django.db.migrations.autodetectorr   Zdjango.db.migrations.loaderr   Zdjango.db.migrations.questionerr   r   r   Zdjango.db.migrations.stater   Zdjango.db.migrations.utilsr   Zdjango.db.migrations.writerr   r   r)   r)   r)   r*   Ú<module>   s   