B
    ©0H[‡$  ã               @   s„   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
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
„ d
eƒZdS )é    )Úsettings)ÚBaseCommandÚCommandError)ÚDEFAULT_DB_ALIASÚconnectionsÚ
migrations)ÚAmbiguityErrorÚMigrationLoader)ÚSwappableTuple)ÚMigrationOptimizer)ÚMigrationWriter)Úget_docs_versionc               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚCommandzZSquashes an existing set of migrations (from first until specified) into a single new one.c             C   sh   |j ddd� |j dd ddd� |j dd	d� |j d
dddd� |j dddddd� |j dddd� d S )NÚ	app_labelz6App label of the application to squash migrations for.)ÚhelpÚstart_migration_nameú?zGMigrations will be squashed starting from and including this migration.)ÚdefaultÚnargsr   Úmigration_namez?Migrations will be squashed until and including this migration.z--no-optimizeÚ
store_trueÚno_optimizez/Do not try to optimize the squashed operations.)ÚactionÚdestr   z	--noinputz
--no-inputÚstore_falseÚinteractivez:Tells Django to NOT prompt the user for input of any kind.z--squashed-nameÚsquashed_namez,Sets the name of the new squashed migration.)r   r   )Úadd_argument)ÚselfÚparser© r    útC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\core\management\commands\squashmigrations.pyÚadd_arguments   s$    zCommand.add_argumentsc          	      sn  |d | _ |d | _|d }|d }|d }|d }|d }ttt ƒ‰ |ˆ jkr^td| ƒ‚|  ˆ ||¡‰‡ ‡fd	d
„ˆ j 	ˆj
ˆjf¡D ƒ}|rô|  ˆ ||¡}ˆ  |j
|j¡}	y| |	¡}
||
d … }W n& tk
rò   td|ˆ|f ƒ‚Y nX | j dk�s| j�r’| j | j d¡¡ x |D ]‰| j dˆj ¡ �q"W | j�r’d }x8|�r\|dk�r‚tdƒ}|�srd}P n|d  ¡ }�qLW |dk�r’d S g }tƒ }d}x˜|D ]�}|j�rÀtdtƒ  ƒ‚| |j¡ xb|jD ]X}t|tƒ�r
tj|jk�rþ|  d¡ n
|  |¡ n |d |j
k�s |�rÔ|  |¡ �qÔW d}�q¦W |�rf| j dk�r`| j | j d¡¡ |}n|| j dk�r†| j | j d¡¡ t!ƒ }| "|ˆj
¡}| j dk�rât#|ƒt#|ƒk�rÆ| j d¡ n| j dt#|ƒt#|ƒf ¡ g }x6|D ].‰ˆj�r| ˆj¡ n| $ˆj
ˆjf¡ �qìW t%dt&j'f|||dœƒ}|�r~|�rb|j (dd¡\}}d||f }nd |jˆjf }|||ƒ}n$d!|�pŽd"ˆj  }|||ƒ}d|_)t*|ƒ}t+|j,d#d$d%��}| | -¡ ¡ W d Q R X | j dk�rj| j | j d&|j, ¡¡ | j d'¡ | j d(¡ | j d)¡ | j d*¡ |j.�rj| j | j d+¡¡ | j d,¡ | j d-¡ | j d.¡ d S )/NÚ	verbosityr   r   r   r   r   r   zLApp '%s' does not have migrations (so squashmigrations on it makes no sense)c                s&   g | ]\}}|ˆj krˆ  ||¡‘qS r    )r   Úget_migration)Ú.0ÚalÚmn)ÚloaderÚ	migrationr    r!   ú
<listcomp>>   s   z"Command.handle.<locals>.<listcomp>z–The migration '%s' cannot be found. Maybe it comes after the migration '%s'?
Have a look at:
  python manage.py showmigrations %s
to debug this issue.r   z%Will squash the following migrations:z - %sZynzDo you wish to proceed? [yN] ÚnÚyTz¥You cannot squash squashed migrations! Please transition it to a normal migration first: https://docs.djangoproject.com/en/%s/topics/migrations/#squashing-migrations)Z__setting__ÚAUTH_USER_MODELFz(Skipping optimization.)zOptimizing...z  No optimizations possible.z0  Optimized from %s operations to %s operations.Ú	Migration)ÚdependenciesÚ
operationsÚreplacesÚ_é   z%s_%sz%s_squashed_%sz0001_%szsquashed_%sÚwzutf-8)Úencodingz!Created new squashed migration %szC  You should commit this migration but leave the old ones in place;zD  the new migration will be used for new installs. Once you are surezI  all instances of the codebase have applied the migrations you squashed,z  you can delete them.zManual porting requiredzH  Your migrations contained functions that must be manually copied over,z3  as we could not safely copy their implementation.zC  See the comment at the top of the squashed migration for details.)/r#   r   r	   r   r   Zmigrated_appsr   Úfind_migrationÚgraphZforwards_planr   Únamer$   ÚindexÚ
ValueErrorÚstdoutÚwriteÚstyleZMIGRATE_HEADINGÚinputÚlowerÚsetr1   r   Úextendr0   r/   Ú
isinstancer
   r   r-   ZsettingÚaddr   ÚoptimizeÚlenÚappendÚtyper   r.   ÚsplitÚinitialr   ÚopenÚpathÚ	as_stringZneeds_manual_porting)r   Úoptionsr   r   r   r   r   Zmigrations_to_squashZstart_migrationÚstartZstart_indexZanswerr0   r/   Zfirst_migrationZ
smigrationÚ
dependencyZnew_operationsZ	optimizerr1   ÚsubclassÚprefixr2   r8   Znew_migrationÚwriterÚfhr    )r(   r)   r!   Úhandle(   sÌ    












zCommand.handlec             C   sX   y|  ||¡S  tk
r0   td||f ƒ‚Y n$ tk
rR   td||f ƒ‚Y nX d S )NzJMore than one migration matches '%s' in app '%s'. Please be more specific.z4Cannot find a migration matching '%s' from app '%s'.)Zget_migration_by_prefixr   r   ÚKeyError)r   r(   r   r8   r    r    r!   r6   À   s    zCommand.find_migrationN)Ú__name__Ú
__module__Ú__qualname__r   r"   rT   r6   r    r    r    r!   r      s
    r   N)Zdjango.confr   Zdjango.core.management.baser   r   Z	django.dbr   r   r   Zdjango.db.migrations.loaderr   r	   Zdjango.db.migrations.migrationr
   Zdjango.db.migrations.optimizerr   Zdjango.db.migrations.writerr   Zdjango.utils.versionr   r   r    r    r    r!   Ú<module>   s   