
    ]jI                         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j                  d      ZdZd Z G d d	e      Zy)
    N)BaseCommandCommandError)DEFAULT_DB_ALIASconnections)AmbiguityErrorMigrationLoaderz\s+replaces\s*=\s*\[[^\]]+\]\s*z.pycc                 .    | d t        t                dz   S )Nz.py)lenPYC)pyc_fns    j/root/env/lib/python3.12/site-packages/django_extensions/management/commands/delete_squashed_migrations.pypy_from_pycr      s    *CH9%%    c                   ,    e Zd ZdZ	 	 d Zd Zd Zd Zy)Commandz:Deletes left over migrations that have been replaced by a c                     |j                  dd       |j                  dd dd       |j                  dd	d
ddd       |j                  dddd       |j                  dt        dt        z         y )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parsers     r   add_argumentszCommand.add_arguments   s    S 	 	
 	%t3A 	 	

 	m-Y]M 	 	
 	e= 	 	? 	"2Z]mm 	 	
r   c                 	   |d   | _         |d   | _        |d   | _        |d   }|d   }|d   }t        t        |         }||j
                  vrt        d|z        d }|rE| j                  |||      }|j                  st        d|j                  d	|j                  d
      |j                  j                  |      } |j                  |d    }|j                  j                  |j                  |j                  f      D 	
cg c]&  \  }	}
|	|j                  k(  r|j                  |	|
      ( }}	}
||gz   }|D ]  }|j                  s|} n |st        d|z        g }|j                  D ]y  \  }	}
	 |j                  |	|
f   }t!        j"                  |j$                        }|j'                  |       |j)                  t*              s^t-        |      }|j'                  |       { | j                   dkD  s| j                  rj| j4                  j3                  | j6                  j9                  d             |D ]   }| j4                  j3                  d|z         " | j;                         sy |D ]$  }	 | j                  st=        j>                  |       & t!        j"                  |j$                        }|j)                  t*              rt-        |      }tC        |      5 }tE        |      }d d d        g }tG              D ]`  \  }}tH        jK                  |      s|j'                  |       |dkD  r.||dz
     jM                         dk(  r|jO                  d|dz
          n |st        d|d      | j                   dkD  s| j                  ro| j4                  j3                  | j6                  j9                  d|d   tQ        |      dkD  rdtS        |d         z   ndd|             | j;                         sy tU        |d      D ]  }||=  tC        |d      5 }| j                  s |j3                  djW                  |             d d d        y c c}
}	w # t.        $ r5 | j                   dkD  r"| j0                  j3                  d|	d	|
d       Y -w xY w# t@        $ r2 | j                   dkD  r| j0                  j3                  d|d       Y hw xY w# 1 sw Y   xY w# 1 sw Y   y xY w)N	verbosityr   dry_runr   r   databasezVApp '%s' does not have migrations (so delete_squashed_migrations on it makes no sense)zThe migration  z is not a squashed migration.)appr   z-Cannot find a squashed migration in app '%s'.z!Couldn't find migration file for 
z Will delete the following files:z - %szCouldn't delete     z(Couldn't find 'replaces =' line in file z%. Please finish cleaning up manually.zWill delete line z and z from file T)reversew),r#   r   r$   r   r   migrated_appsr   find_migrationreplacesr   namegraph
leaf_nodesget_migrationforwards_plandisk_migrationsinspectgetfile	__class__appendendswithr   r   KeyErrorstderrwritestdoutstyleMIGRATE_HEADINGconfirmosremoveOSErroropenlist	enumerateREPLACES_REGEXmatchstripinsertr
   strsortedjoin)r   optionsr   r   r%   loadersquashed_migrationr2   	migrationalmnprevious_migrations
migrationsfiles_to_deletepyc_filepy_filefnsquashed_migration_fnfpsquashed_migration_linesdelete_linesilineline_nums                           r   handlezCommand.handle-   s    -"=1y)K(	")*C"D:& !X!67F000%'01 
 ""!%!4!4VYH_!`%.."'113E3J3JL 
  00Y0?J,,,jm<I %ll88):M:My~~9^_#B,,, $$R,# #
 -	{:J' 	%%)2&
 &"C  
 (11 	4FB4"222r6:	 #??9+>+>?&&x0$$S))(3G#**73	4 >>A!1!1KKdjj889[\]% 0!!'B,/0 <<>! 	FBF||IIbM	F !(0B0L0L M ))#.$/0E$F!'( 	0B'+Bx$	0  !9: 	GAt##D)##A&q55a!e<BBDJ ''1q51	 ;PS 
 >>A!1!1KKdjj88a36|3Dq3H3|A//bP&() * <<>|T: 	3H(2	3 '- 	<<<!9:;	< 	<e#*  2>>A%KK%%)+R'1 220  F>>A%KK%%&DEF	0 	0:	< 	<sB   +Q7Q 7!R!$S$-S, :RR!7SSS),S5c                     | j                   r=d }|r|dvr0t        d      }|sd}	 |dk(  S |d   j                         }|s+|dvr0|dk(  S y)NynzDo you wish to proceed? [yN] nr   yT)r   inputlower)r   answers     r   rA   zCommand.confirm   si    Fd 2>? F S=  $AY__.F d 2 S= r   c                     	 |j                  ||      S # t        $ r t        d|d|d      t        $ r t        d|d|d      w xY w)Nz!More than one migration matches 'z
' in app 'z'. Please be more specific.z"Cannot find a migration matching 'z' from app 'z'.)get_migration_by_prefixr   r   r;   )r   rP   r   r0   s       r   r.   zCommand.find_migration   sa    	11)TBB 	$()5   	y" 	s	    6A
N)__name__
__module____qualname__r   r!   rb   rA   r.    r   r   r   r      s#    GDG:
,o<br   r   )rB   r6   redjango.core.management.baser   r   	django.dbr   r   django.db.migrations.loaderr   r   compilerH   r   r   r   ro   r   r   <module>ru      s@    	  	 A 3 G>?&ek er   