o
    i                     @   sr   d dl Z d dlZd dlZd dlmZmZ d dlmZmZ d dl	m
Z
mZ edZdZdd ZG d	d
 d
eZdS )    N)BaseCommandCommandError)DEFAULT_DB_ALIASconnections)AmbiguityErrorMigrationLoaderz\s+replaces\s*=\s*\[[^\]]+\]\s*z.pycc                 C   s   | d t t  d S )Nz.py)lenPYC)pyc_fn r   /var/www/html/formularioweb/env/lib/python3.10/site-packages/django_extensions/management/commands/delete_squashed_migrations.pypy_from_pyc   s   r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )CommandzDeletes left over migrations that have been replaced by a squashed migration and converts squashed migration into a normal migration. Modifies your source tree! Use with care!c                 C   s`   |j ddd |j dd ddd |j dd	d
dddd |j ddddd |j dtdt d d S )N	app_labelz@App label of the application to delete replaced migrations from.)helpsquashed_migration_name?zPThe squashed migration to replace. If not specified defaults to the first found.)defaultnargsr   z	--noinputz
--no-inputstore_falseinteractiveTz:Tells Django to NOT prompt the user for input of any kind.)actiondestr   r   z	--dry-run
store_trueFz*Do not actually delete or change any files)r   r   r   z
--databasezGNominates a database to run command for. Defaults to the "%s" database.)r   r   )add_argumentr   )selfparserr   r   r   add_arguments   s>   
zCommand.add_argumentsc              
      s  |d | _ |d | _|d | _|d }|d }|d }tt|  | jvr,td| d }|rE|  ||}|jsDtd|j	|j
f n9 jj|d	} j|d
   fdd jj	j
fD }|g }|D ]	jru} nql|s~td| g }	|jD ]?\}
}z	 j|
|f W n ty   | j d
kr| jd|
|f  Y qw tj}|	| |trt|}|	| q| j d
ks| jr| j| jd |	D ]
}| jd|  q|  sd S |	D ]%}z
| jst| W q t y   | j d
kr| jd|f  Y qw t|j}|tr t|}t!|}t"|}W d    n	1 s4w   Y  g }t#|D ]*\}}t$%|rh|| |d
krf||d  & dkrf|'d
|d   nq?|sttd|f | j d
ks~| jr| j| jd|d
 t(|dkrdt)|d  nd|f  |  sd S t*|ddD ]}||= qt!|d}| js|d+| W d    d S W d    d S 1 sw   Y  d S )N	verbosityr   dry_runr   r   databasezVApp '%s' does not have migrations (so delete_squashed_migrations on it makes no sense)z0The migration %s %s is not a squashed migration.)appr   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-Cannot find a squashed migration in app '%s'.z'Couldn't find migration file for %s %s
z Will delete the following files:z - %szCouldn't delete %s
    zOCouldn't find 'replaces =' line in file %s. Please finish cleaning up manually.z"Will delete line %s%s from file %sz and T)reversew),r   r   r   r   r   migrated_appsr   find_migrationreplacesr   namegraph
leaf_nodesr"   forwards_plandisk_migrationsKeyErrorstderrwriteinspectgetfile	__class__appendendswithr	   r   stdoutstyleMIGRATE_HEADINGconfirmosremoveOSErroropenlist	enumerateREPLACES_REGEXmatchstripinsertr   strsortedjoin)r   optionsr   r   r    squashed_migrationr3   previous_migrations
migrationsfiles_to_deleter$   r%   pyc_filepy_filefnsquashed_migration_fnfpsquashed_migration_linesdelete_linesilineline_numr   r&   r   handle=   s   
















  

$zCommand.handlec                 C   sT   | j r(d }|r|dvr$td}|sd}	 |dkS |d  }|r|dvs|dkS dS )NynzDo you wish to proceed? [yN] nr   yT)r   inputlower)r   answerr   r   r   rA      s   zCommand.confirmc                 C   sJ   z| ||W S  ty   td||f  ty$   td||f w )NzJMore than one migration matches '%s' in app '%s'. Please be more specific.z4Cannot find a migration matching '%s' from app '%s'.)get_migration_by_prefixr   r   r6   )r   r'   r   r1   r   r   r   r/      s   zCommand.find_migrationN)__name__
__module____qualname__r   r   r^   rA   r/   r   r   r   r   r      s    $ r   )rB   r9   redjango.core.management.baser   r   	django.dbr   r   django.db.migrations.loaderr   r   compilerH   r	   r   r   r   r   r   r   <module>   s   
