
    Vh|                         d dl Z d dlZd dl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 d dlmZ d d	lmZ d d
lmZ  G d de      Zy)    N)apps)BaseCommandCommandError)run_formatters)
migrations)AmbiguityError)MigrationLoader)MigrationOptimizer)MigrationWriter)get_docs_versionc                       e Zd ZdZd Zd Zy)Commandz1Optimizes the operations for the named migration.c                 x    |j                  dd       |j                  dd       |j                  ddd	       y )
N	app_labelz;App label of the application to optimize the migration for.)helpmigration_namez.Migration name to optimize the operations for.z--check
store_truez>Exit with a non-zero status if the migration can be optimized.)actionr   )add_argument)selfparsers     a/home/dcms/DCMS/lib/python3.12/site-packages/django/core/management/commands/optimizemigration.pyadd_argumentszCommand.add_arguments   sT    N 	 	
 	#S 	 	
 	Q 	 	
    c                     |d   }|d   }|d   }|d   }	 t        j                  |       t        d       }||j                  vrt        d| d      	 |j                  ||      }	t               }
|
j                  |	j                  |	j                        }t        |	j                        t        |      k(  r!|dkD  r| j                  j!                  d       y |dkD  r<| j                  j!                  dt        |	j                        t        |      fz         |rt#        j$                  d       ||	_        t'        |	      }|j)                         }|j*                  r|	j,                  rt        dt/               z        t1        dt2        j4                  f|	j6                  ||	j                  |	j8                  fgd      }d|	j8                  z  } |||      }t'        |      }|j)                         }|dkD  r| j                  j!                  | j:                  j=                  d      dz          t?        j@                  d      r4| j                  j!                  | j:                  jC                  d             tE        |jF                  dd      5 }|j!                  |       d d d        tI        |jF                  g| jJ                         |dkD  rB| j                  j!                  | j:                  j=                  d|jF                                y y # t        $ r}t        t	        |            d }~ww xY w# t        $ r t        d| d| d	      t        $ r t        d
| d| d      w xY w# 1 sw Y   xY w)N	verbosityr   r   checkzApp 'z' does not have migrations.z!More than one migration matches 'z
' in app 'z'. Please be more specific.z"Cannot find a migration matching 'z' from app 'z'.r   zNo optimizations possible.z/Optimizing from %d operations to %d operations.   zMigration will require manual porting but is already a squashed migration.
Transition to a normal migration first: https://docs.djangoproject.com/en/%s/topics/migrations/#squashing-migrations	Migration)dependencies
operationsreplacesz%s_optimizedzManual porting requiredz
  Your migrations contained functions that must be manually copied over,
  as we could not safely copy their implementation.
  See the comment at the top of the optimized migration for details.blackz^Optimized migration couldn't be formatted using the "black" command. You can call it manually.wzutf-8)encoding)stderrzOptimized migration )&r   get_app_configLookupErrorr   strr	   migrated_appsget_migration_by_prefixr   KeyErrorr
   optimizer!   r   lenstdoutwritesysexitr   	as_stringneeds_manual_portingr"   r   typer   r   r    namestyleMIGRATE_HEADINGshutilwhichWARNINGopenpathr   r&   )r   argsoptionsr   r   r   r   errloader	migration	optimizernew_operationswritermigration_file_stringsubclassoptimized_migration_nameoptimized_migrationfhs                     r   handlezCommand.handle    s}   K(	K(	 !12 	)	*
 !&F000yk1LMNN	66y.QI '(	"++I,@,@)BUBUVy##$N(;;1}!!">?1}!!E9//0#n2EFG   .	 !+ & 0 0 2&&!!", /?.@A  %%'$-$:$:"0"+"5"5y~~!F GH (6	'F$"*+CY"O$%89F$*$4$4$6!1}!!JJ../HI M  <<(KK%%

**I &++sW5 	,HH*+	,}T[[9q=KK

**-A&+++OP k  	)s3x((	)  	3N3C D;9;   	4^4D E;b" 	D	, 	,s/   L L; M4	L8L33L8;6M14M=N)__name__
__module____qualname__r   r   rK    r   r   r   r      s    >D
ar   r   )r9   r1   django.appsr   django.core.management.baser   r   django.core.management.utilsr   	django.dbr   django.db.migrations.exceptionsr   django.db.migrations.loaderr	   django.db.migrations.optimizerr
   django.db.migrations.writerr   django.utils.versionr   r   rO   r   r   <module>rY      s4     
  A 7   : 7 = 7 1rk rr   