a
    [fW                     @   s   d dl Z 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mZ d dlmZ d dlmZ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 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no_translations)run_formatters)DEFAULT_DB_ALIASOperationalErrorconnectionsrouter	Migration)MigrationAutodetector)MigrationLoader)SwappableTuple)MigrationOptimizer)InteractiveMigrationQuestionerMigrationQuestioner!NonInteractiveMigrationQuestioner)ProjectState)get_migration_name_timestamp)MigrationWriterc                   @   s^   e Zd ZdZdd Zedd Zdd Zedd	 Z	d
d Z
dddZe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	 |j d
ddd	 |j dddd	 |j dddddd |j 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 S )$Nargs	app_label*z2Specify the app label(s) to create migrations for.)metavarnargshelpz	--dry-run
store_truezCJust show what migrations would be made; don't actually write them.)actionr   z--mergez%Enable fixing of migration conflicts.z--emptyzCreate an empty migration.z	--noinputz
--no-inputstore_falseinteractivez:Tells Django to NOT prompt the user for input of any kind.)r!   destr   z-nz--namez$Use this name for migration file(s).)r   z--no-headerinclude_headerz4Do not add header comments to new migration file(s).z--checkcheck_changeszuExit with a non-zero status if model changes are missing migrations and don't actually write them. Implies --dry-run.z--scriptable
scriptableziDivert log output and input prompts to stderr, writing only paths of generated migration files to stdout.z--updateupdatezTMerge model changes into the latest migration and optimize the resulting operations.)add_argument)selfparser r,   C:\Users\91910\OneDrive\Desktop\vts_admin_rebuild-master\vts\Lib\site-packages\django/core/management/commands/makemigrations.pyadd_arguments   sr    		zCommand.add_argumentsc                 C   s   | j r| jS | jS N)r'   stderrstdout)r*   r,   r,   r-   
log_outputa   s    zCommand.log_outputc                 C   s   | j | d S r/   )r2   write)r*   msgr,   r,   r-   loge   s    zCommand.logc                    s  g | _ |d | _|d | _|d | _|d | _|d | _|d | _| jrZ| j sZtd|d | _	|d	 }|rvd
| _|d | _
|d | _| j
rd | j_t  d} D ]L}zt| W q ty } z | jt| d
}W Y d }~qd }~0 0 q|rtd td d
d}dd t D }tjr0tntg}	t|	D ]}
t|
 jd dkr>tfdd|D r>z| W n: t y } z t!"d|
|f t# W Y d }~n
d }~0 0 q>|$ } r fdd|% D }|r| jsd&dd |% D }td| | jr2|s2| 'd d S | jrL|rL| (||S | jrht) | j| j*d}nt+ | j| j| j'd}t,|- t./t|}| jrڈ stdd d  D }|j0||j1| jd!}| 2| d S |j3|j1 pd  pd | jd"}|sX| jd#kr rLt4 d#kr6| 'd$ 5   n| 'd%d&&   n
| 'd' n.| jrl| 6| n
| 2| |rtd# d S )(N	verbosityr#   dry_runmergeemptynamez5The migration name must be a valid Python identifier.r%   r&   Tr'   r(   F   )Zignore_no_migrationsc                 S   s   h | ]
}|j qS r,   )label).0configr,   r,   r-   	<setcomp>       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alias_metaZobject_name)r=   r   model)
connectionr,   r-   	<genexpr>   s
   z!Command.handle.<locals>.<genexpr>z_Got an error checking a consistent migration history performed for database connection '%s': %sc                    s   i | ]\}}| v r||qS r,   r,   )r=   r   conflict)
app_labelsr,   r-   
<dictcomp>   s   z"Command.handle.<locals>.<dictcomp>z; c                 s   s$   | ]\}}d d ||f V  qdS )z%s in %s, N)join)r=   appnamesr,   r,   r-   rF      s   zConflicting 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.)specified_appsr7   prompt_output)rN   r7   r6   r5   z:You must supply at least one app label when using --empty.c                 S   s   i | ]}|t d |gqS )Zcustomr   )r=   rL   r,   r,   r-   rI      r@   )changesgraphmigration_name)rQ   Ztrim_to_appsZconvert_appsrR      zNo changes detected in app '%s'z No changes detected in apps '%s'z', 'zNo changes detected)7written_filesr6   r#   r7   r8   r9   rR   isidentifierr   r%   r'   r(   r0   Z
style_funcsetr   rA   LookupErrorr3   strsysexitr   Zget_app_configsr   ZDATABASE_ROUTERSr   r	   sortedZsettings_dictanyZcheck_consistent_historyr
   warningswarnRuntimeWarningZdetect_conflictsitemsrK   r5   handle_merger   r2   r   r   Zproject_stater   Z	from_appsZarrange_for_graphrQ   write_migration_filesrP   lenpopwrite_to_last_migration_files)r*   rH   optionsr&   Zhas_bad_labelsr   errloaderZconsistency_check_labelsZaliases_to_checkrB   error	conflictsZname_str
questionerZautodetectorrP   r,   )rH   rE   r-   handleh   s    















zCommand.handlec                    s  t tt }i }i }| D ]\}}|jj|d}t|dkrPtd| d|d  |jj  }|j	rztd| d |j
v rtd| d fdd	|j D }	|	rd
dd	 |	D }
td| d|
 d|D ]n}|j|j |jD ]T}t|tr0tj|jkr"|jd n|j| q|d |jkr|j| qqt }||j||_t|j}| jp|| }|jd d d|  }|j|kr|jd }n|}||_|g||< |||< q| || d S )N)rL   r   zApp z0 has no migration, cannot update last migration.z Cannot update squash migration 'z'.z!Cannot update applied migration 'c                    s   g | ]} |j v r|qS r,   )dependenciesr=   	migrationZleaf_migration_noder,   r-   
<listcomp>  s   
z9Command.write_to_last_migration_files.<locals>.<listcomp>rJ   c                 S   s   g | ]}d | d qS )'r,   rn   r,   r,   r-   rq   &  r@   zCannot update migration 'z' that migrations z depend on.)Z__setting__AUTH_USER_MODEL   _Z_updated) r   r   r	   r`   rQ   Z
leaf_nodesrc   r   nodesZreplacesZapplied_migrationsZdisk_migrationsvaluesrK   
operationsextendrm   
isinstancer   r   rs   Zsettingappendr   r   optimizer   pathrR   Zsuggest_namer:   rb   )r*   rP   rh   Znew_changesupdate_previous_migration_pathsr   app_migrationsZleaf_migration_nodesZleaf_migrationZdepending_migrationsZformatted_migrationsro   
dependencyZ	optimizerZprevious_migration_pathZname_fragmentZsuggested_namenew_namer,   rp   r-   re     sp    








z%Command.write_to_last_migration_filesNc                 C   s  i }|  D ]\}}| jdkr6| | jd|  |D ]}t|| j}| jdkr| |j}| d| j	|  |j
D ]}	| d|	   q~| jr| j| | jstj|j}
||s
tj|
dd tj|
d}tj|st|d  d||< | }t|jdd	d
(}|| | j|j W d   n1 sR0    Y  |r|| }| |}|jr| |j}| | jd| d| d nt| | d|  q:| jdkr:| | jd|j  | |  q:qt| j dS )zL
        Take a changes dict and write them out as migration files.
        rS   zMigrations for '%s':z  %s
    - %sT)exist_okz__init__.pywutf-8encodingNzUpdated migration z- requires manual porting.
Previous migration z? was kept and must be deleted after porting functions manually.zDeleted    zFull migrations file '%s':) r`   r6   r5   styleMIGRATE_HEADINGr   r%   get_relative_pathr}   MIGRATE_LABELrx   describer'   r1   r3   r7   osdirnamegetmakedirsrK   isfileopenclose	as_stringrT   r{   Zneeds_manual_portingWARNINGremovefilenamer   )r*   rP   r~   Zdirectory_createdr   r   ro   writermigration_string	operationZmigrations_directoryZ	init_pathfhZ	prev_pathZrel_prev_pathZmigration_pathr,   r,   r-   rb   N  s^    




.
	

zCommand.write_migration_filesc                 C   s:   zt j| }W n ty&   | }Y n0 |dr6| }|S )Nz..)r   r}   relpath
ValueError
startswith)r}   r   r,   r,   r-   r     s    

zCommand.get_relative_pathc              	      s  | j rt| jd}ntddid}| D ]\ }g }|D ]:} |fddj |fD _|	 q<dd }t
d	d
 |D  }tdd
 t||D }	|	std| |D ]4j|	d _fdd
jD }
t|
g _q| jdkrZ| | jd   |D ]>| | jdj  jD ]}| d|   q<q| r*dd |D }ztdd
 |D }W n ty   d}Y n0 tdtfd fdd|D i}d|d  g}| jr|	| j nH|	d dtdd
 |D }t|dkr$|	t  n
|	| d|}|| }t || j!}| j"st#|j$ddd }|%|&  W d   n1 s0    Y  t'|j$g | jdkr| d!|j$  | j(r| j)%|j$ q*| jd"kr*| | jd#|j*  | |&  q*dS )$z
        Handles merging together conflicted migrations interactively,
        if it's safe; otherwise, advises on how to fix it.
        )rO   	ask_mergeT)defaultsc                    s   g | ]}|d   j kr|qS )r   r   )r=   Zmig)ro   r,   r-   rq     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-   rF     r@   z@Command.handle_merge.<locals>.all_items_equal.<locals>.<genexpr>rS   )allr   r,   r   r-   all_items_equal  s    z-Command.handle_merge.<locals>.all_items_equalc                 s   s   | ]}|j V  qd S r/   )ancestry)r=   mr,   r,   r-   rF     r@   z'Command.handle_merge.<locals>.<genexpr>c                 s   s   | ]
}d V  qdS )rS   Nr,   )r=   Zcommon_ancestor_generationr,   r,   r-   rF     s   z$Could not find common ancestor of %sNc                 3   s    | ]\}}  ||jV  qd S r/   )get_migrationrx   )r=   Znode_appZ	node_name)rh   r,   r-   rF     s   r   z
Merging %sz  Branch %sr   c                 S   s   g | ]}t |jqS r,   )r   Zparse_numberr:   rn   r,   r,   r-   rq     s   c                 s   s   | ]}|d ur|V  qd S r/   r,   )r=   xr,   r,   r-   rF     r@   rS   r   rm   c                    s   g | ]} |j fqS r,   r:   rn   r   r,   r-   rq     s   z%04ir8   ru   c                 s   s   | ]}|j V  qd S r/   r   rn   r,   r,   r-   rF     r@   /   r   r   r   z
Created new merge migration %sr   z Full merge migrations file '%s':)+r#   r   r2   r   r`   r   rQ   Zforwards_planr   r{   zipsumr   r   branchZmerged_operationsr6   r5   r   r   r   r:   r   r   maxtyper   rR   rK   r[   rc   r   r   r%   r7   r   r}   r3   r   r   r'   r1   r   )r*   rh   rj   rk   Zmigration_namesZmerge_migrationsrR   r   Zmerge_migrations_generationsZcommon_ancestor_countZmigrations_opsr   numbersZbiggest_numbersubclasspartsZ
leaf_namesZnew_migrationr   r   r,   )r   rh   ro   r-   ra     s    









.
zCommand.handle_merge)N)__name__
__module____qualname__r   r.   propertyr2   r5   r   rl   re   rb   staticmethodr   ra   r,   r,   r,   r-   r      s   D

 G
>
	r   ))r   rY   r]   	itertoolsr   Zdjango.appsr   Zdjango.confr   Zdjango.core.management.baser   r   r   Zdjango.core.management.utilsr   Z	django.dbr	   r
   r   r   Zdjango.db.migrationsr   Z!django.db.migrations.autodetectorr   Zdjango.db.migrations.loaderr   Zdjango.db.migrations.migrationr   Zdjango.db.migrations.optimizerr   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$   