
    VhX                        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e
      Z(y)    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                   d    e Zd ZeZdZd Zed        Zd Z	e
d        Zd ZddZed	        Zd
 Zy)Commandz"Creates new migration(s) for apps.c                    |j                  dddd       |j                  ddd	       |j                  d
dd	       |j                  ddd	       |j                  ddddd       |j                  ddd       |j                  dddd       |j                  dddd       |j                  dddd        |j                  d!dd"d#       y )$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parsers     ^/home/dcms/DCMS/lib/python3.12/site-packages/django/core/management/commands/makemigrations.pyadd_argumentszCommand.add_arguments   sI   E	 	 	
 	V 	 	

 	8 	 	

 	- 	 	

 	 M 	 	
 	7 	 	

 	 !G	 	 	
 	 D 	 	
 	@ 	 	
 	( 	 	
    c                 J    | j                   r| j                  S | j                  S N)r)   stderrstdout)r,   s    r.   
log_outputzCommand.log_outputb   s    "oot{{>4;;>r0   c                 :    | j                   j                  |       y r2   )r5   write)r,   msgs     r.   logzCommand.logf   s    c"r0   c                 	   g | _         |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        | j                  r%| j                  j                         st        d      |d   | _	        |d	   }|rd
| _        |d   | _
        |d   | _        | j                  rd | j                  _        t        |      }d}|D ]  }	 t        j                   |        |rt)        j*                  d       t-        d d
      }t        j.                         D ch c]  }|j0                   }	}t2        j4                  rt6        nt8        g}
t;        |
      D ]E  }t6        |   j<                  d   dk7  st?        fd|	D              s4	 |jA                         G |jK                         }|r'|jM                         D ci c]  \  }}||v r|| }}}|r@| j                  s4djO                  d |jM                         D              }t        d|z        | j                  r|s| jQ                  d       y | j                  r|r| jS                  ||      S | j                  r#tU        || j                  | jV                        }n-tY        || j                  | j                  | jP                        }| j[                  |j]                         t_        j`                  t              |      }| j
                  rb|st        d      |D ci c]  }|tc        d|      g }}|je                  ||jf                  | j                        }| ji                  |       y |jk                  |jf                  |xs d |xs d | j                        }|sy| j                  dk\  ri|rUtm        |      dk(  r#| jQ                  d |jo                         z         y | jQ                  d!d"jO                  |      z         y | jQ                  d#       y y | j                  r| jq                  |       n| ji                  |       |rt)        j*                  d       y y # t"        $ r1}| j                  j%                  t'        |             d
}Y d }~d }~ww xY wc c}w # tB        $ r+}tE        jF                  d|d|tH               Y d }~jd }~ww xY wc c}}w c c}w )$N	verbosityr%   dry_runmergeemptynamez5The migration name must be a valid Python identifier.r'   r(   Tr)   r*   F   )ignore_no_migrationsENGINEzdjango.db.backends.dummyc              3      K   | ]b  }t        j                  |      j                         D ]:  }t        j                  j
                  ||j                  j                          < d yw))
model_nameN)r   get_app_config
get_modelsr   allow_migratealias_metaobject_name).0r   model
connections      r.   	<genexpr>z!Command.handle.<locals>.<genexpr>   sq      X
 !00;FFHX 	 $$$$iEKK<S<S XXs   A(A+zXGot an error checking a consistent migration history performed for database connection 'z': z; c              3   N   K   | ]  \  }}d j                  |      d|  yw), z in N)join)rK   appnamess      r.   rN   z!Command.handle.<locals>.<genexpr>   s'      !9Cedii.4!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_appsr<   prompt_output)rT   r<   r;   r9   z:You must supply at least one app label when using --empty.custom)changesgraphmigration_name)rX   trim_to_appsconvert_appsrY      zNo changes detected in app '%s'z No changes detected in apps '%s'z', 'zNo changes detected)9written_filesr;   r%   r<   r=   r>   rY   isidentifierr   r'   r)   r*   r3   
style_funcsetr   rE   LookupErrorr7   strsysexitr   get_app_configslabelr   DATABASE_ROUTERSr   r
   sortedsettings_dictanycheck_consistent_historyr   warningswarnRuntimeWarningdetect_conflictsitemsrQ   r9   handle_merger   r5   r   autodetectorproject_stater   	from_appsr   arrange_for_graphrX   write_migration_filesrW   lenpopwrite_to_last_migration_files)r,   
app_labelsoptionsr(   has_bad_labelsr   errloaderconfigconsistency_check_labelsaliases_to_checkrH   error	conflictsconflictname_str
questionerrr   rR   rW   rM   s                       @r.   handlezCommand.handlei   s    -"=1y)W%
W%
%fot':':'G'G'IVWW%&670DL!,/h'??%)DKK" _
# 	&I&##I.	& HHQK !DA @D?S?S?U#VVFLL#V #V $44K;K:L 	 ,- 	E$U+J''15OOTW X
 ":X U33J?	( ++-	  ,5??+<'Ix
* 8#I  TZZyy !GPGX! H <>FG  ::iHH67 ::)$$VY777)"ooJ ;)..HH	J ((  """4(
 ::"P  CMM3sYx566MGM"44ll#22 5 G
 &&w/ &&,,#+t#+t..	 ' 
 ~~":!+!BZ^^EU!UV>%{{:68
 HH23 # {{227;**73 C  &!!#c(+!%& $W" ( MMHMuV& j NsB   Q#RRSS	R"&RR	S$ S

Sc           	         t        t        t                 }i }i }|j                         D ]w  \  }}|j                  j                  |      }t        |      dk(  rt        d| d      |d   }|j                  j                  |   }	|	j                  rt        d|	 d      ||j                  v rt        d|	 d      |j                  j                         D 
cg c]  }
||
j                  v r|
 }}
|r5dj                  |D 
cg c]  }
d	|
 d	
 c}
      }t        d
|	 d| d      |D ]  }
|	j                  j!                  |
j                         |
j                  D ]  }t#        |t$              rUt&        j(                  |j*                  k(  r|	j                  j-                  d       L|	j                  j-                  |       h|d   |
j.                  k7  s{|	j                  j-                  |         t1               }|j3                  |	j                  |      |	_        t5        |	      j6                  }| j8                  xs |	j;                         }|	j<                  d d d| z   }|	j<                  |k(  r|	j<                  dz   }n|}||	_        |	g||<   |||<   z | j?                  ||       y c c}
w c c}
w )N)rR   r   zApp z0 has no migration, cannot update last migration.z Cannot update squash migration 'z'.z!Cannot update applied migration 'rP   'zCannot update migration 'z' that migrations z depend on.)__setting__AUTH_USER_MODEL   __updated) r   r   r
   rp   rX   
leaf_nodesrw   r   nodesreplacesapplied_migrationsdisk_migrationsvaluesdependenciesrQ   
operationsextend
isinstancer   r   r   settingappendr   r   optimizer   pathrY   suggest_namer?   rv   )r,   rW   r~   new_changesupdate_previous_migration_pathsr   app_migrationsleaf_migration_nodesleaf_migration_nodeleaf_migration	migrationdepending_migrationsformatted_migrations
dependency	optimizerprevious_migration_pathname_fragmentsuggested_namenew_names                      r.   ry   z%Command.write_to_last_migration_files  s    -=!>?*,')0 ?	Q%I~#)<<#:#:y#:#I '(A-"9+%UV  #7q"9#\\//0CDN &&"6~6FbI  #f&?&??"77GrJ 
 "(!7!7!>!>!@$&)*@*@@ $  $
 $'+yy7KL)q1%L($ #//??Q+,K9 
 , G	))001E1EF"+"8"8 	GJ!*n=#33z7I7II*77>> B +77>>zJ#A)*=*==&33:::F	GG +,I(1(:(:))9)N% '6n&E&J&J# //P>3N3N3PM+00!4=/7JJN""n4)..;)"*N&4%5K	"9P+I6?	QB 	"";0OP[$ Ms   J9J>
Nc                    i }|j                         D ];  \  }}| j                  dk\  r-| j                  | j                  j	                  d|z               |D ]  }t        || j                        }| j                  dk\  r| j                  |j                        }| j                  d| j                  j                  |      z         |j                  D ]$  }	| j                  d|	j                         z         & | j                  r| j                  j                  |       | j                  st         j                  j#                  |j                        }
|j%                  |      sut!        j&                  |
d       t         j                  j)                  |
d      }t         j                  j+                  |      st-        |d      j/                          d||<   |j1                         }t-        |j                  dd	
      5 }|j                  |       | j2                  j5                  |j                         ddd       |s||   }| j                  |      }|j6                  rN| j                  |j                        }| j                  | j                  j9                  d| d| d             bt!        j:                  |       | j                  d|        | j                  dk(  s| j                  | j                  j	                  d|j<                  z               | j                  |j1                                 > t?        | j2                  | j@                         y# 1 sw Y   8xY w)zL
        Take a changes dict and write them out as migration files.
        r\   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':r3   )!rp   r;   r9   styleMIGRATE_HEADINGr   r'   get_relative_pathr   MIGRATE_LABELr   formatted_descriptionr)   r4   r7   r<   osdirnamegetmakedirsrQ   isfileopenclose	as_stringr]   r   needs_manual_portingWARNINGremovefilenamer	   r3   )r,   rW   r   directory_createdr   r   r   writermigration_string	operationmigrations_directory	init_pathfh	prev_pathrel_prev_pathmigration_paths                   r.   rv   zCommand.write_migration_filesO  s    )0 6	1%I~~~"334JY4VWX+ 31	(D4G4GH>>Q& (,'='=fkk'J$HHX

(@(@AQ(RRS%.%9%9 O	I,K,K,M!MNO))*:;||+-77??6;;+G(,00;$84H$&GGLL1E}$U	!ww~~i8 C06687;))4'-'7'7'9$fkk3A ?R!12**11&++>? 7$CI$N	(,(>(>y(I!66-1-C-CFKK-PN HH $

 2 2&88H I::G IX%Y!" IIi0 HHx%?@^^q( HH

2286??J
 HHV--/0g316	1n 	t))$++>;? ?s   7M44M>c                     	 t         j                  j                  |       }|j	                  d      r| }|S # t        $ r | }Y "w xY w)Nz..)r   r   relpath
ValueError
startswith)r   r   s     r.   r   zCommand.get_relative_path  sM    	$!wwt4 &&t,#	  	$#	$s   6 AAc                 B	   | j                   rt        | j                        }nt        ddi      }|j	                         D ]+  \  }}g }|D ]g  }j                  ||      }j                  j                  ||f      D 	cg c]  }	|	d   |j                  k(  r|	 c}	|_	        |j                  |       i d }
t        d |D         }t        d t        |
|      D              }|st        d	|z        |D ]<  }|j                  |d
 |_        fd|j                  D        }t        |g       |_        > | j"                  dkD  r| j%                  | j&                  j)                  d|z               |D ]l  }| j%                  | j&                  j+                  d|j,                  z               |j                   D ]$  }| j%                  d|j/                         z         & n |j1                  |      s|D cg c]'  }| j2                  j5                  |j,                        ) }}	 t7        d |D              }t9        dt:        fd|D cg c]  }||j,                  f c}i      }d|dz   z  g}| j<                  r|j                  | j<                         nk|j                  d       dj?                  tA        d |D                    }tC        |      dkD  r|j                  tE                      n|j                  |       dj?                  |      } |||      }tG        || jH                        }| jJ                  stM        |jN                  dd      5 }|jQ                  |jS                                d
d
d
       tU        |jN                  g| jV                         | j"                  dkD  sr| j%                  d|jN                  z         | jX                  s| jZ                  jQ                  |jN                         | j"                  dk(  s| j%                  | j&                  j)                  d|j\                  z               | j%                  |jS                                . y
c c}	w c c}w # t        $ r d}Y Ow xY wc c}w # 1 sw Y   xY w)z
        Handles merging together conflicted migrations interactively,
        if it's safe; otherwise, advises on how to fix it.
        )rU   	ask_mergeT)defaultsr   c                 2     t         fd dd  D              S )Nc              3   .   K   | ]  }|d    k(    yw)r   N )rK   itemseqs     r.   rN   z@Command.handle_merge.<locals>.all_items_equal.<locals>.<genexpr>  s     >d43q6>>s   r\   )all)r   s   `r.   all_items_equalz-Command.handle_merge.<locals>.all_items_equal  s    >c!"g>>>r0   c              3   4   K   | ]  }|j                     y wr2   )ancestry)rK   ms     r.   rN   z'Command.handle_merge.<locals>.<genexpr>  s     0V0V   c              3       K   | ]  }d   yw)r\   Nr   )rK   common_ancestor_generations     r.   rN   z'Command.handle_merge.<locals>.<genexpr>  s      (. (s   z$Could not find common ancestor of %sNc              3   \   K   | ]#  \  }}j                  ||      j                   % y wr2   )get_migrationr   )rK   node_app	node_namer~   s      r.   rN   z'Command.handle_merge.<locals>.<genexpr>  s0      "+) ((9=HH"s   ),z
Merging %sz  Branch %sr   c              3   &   K   | ]	  }||  y wr2   r   )rK   xs     r.   rN   z'Command.handle_merge.<locals>.<genexpr>  s     (Mqq}(Ms   r\   r   r   z%04ir=   r   c              3   4   K   | ]  }|j                     y wr2   )r?   )rK   r   s     r.   rN   z'Command.handle_merge.<locals>.<genexpr>  s     P)y~~Pr   /   r   r   r   r   z
Created new merge migration %sr   z Full merge migrations file '%s':)/r%   r   r5   r   rp   r   rX   forwards_planr   r   r   zipsumr   r   branchmerged_operationsr;   r9   r   r   r   r?   r   r   rr   parse_numbermaxtyper   rY   rQ   rh   rw   r   r   r'   r<   r   r   r7   r   r	   r3   r)   r4   r   )r,   r~   r   r   r   migration_namesmerge_migrationsrY   r   migr   merge_migrations_generationscommon_ancestor_countmigrations_opsr   numbersbiggest_numbersubclassparts
leaf_namesnew_migrationr   r   s    `                     r.   rq   zCommand.handle_merge  sJ   
 7dooVJ,{D6IJJ*3//*; c	1&I  ""1 3"00NK	  &||999n:UV&1v!4!44 &	"
 !''	23? ,/0VEU0V+W($' (2;#%A3( %! ) :_L  . F	#,#5#56K6L#M	 "/8/?/?" /2.".E	+F ~~!33L94LMN!1 OIHHTZZ55minn6TUV%.%@%@ O	I,K,K,M!MNOO ##I.
 &6! %%229>>B '%((MG(M%MN  L&-=) ) '	7)	  >A#567&&LL!4!45LL)!$P?OPP"J :+%A%CDZ0!$% ( C(8K8KL||fkk3A 5R!1!1!345"FKK=E~~)!Cfkk!QR?? KK--fkk:^^q( HH

22>P
 HHV--/0Gc	1&R " '%&N')05 5s0   Q3;,Q8*Q=R R=RRR	r2   )__name__
__module____qualname__r   rr   r!   r/   propertyr5   r9   r   r   ry   rv   staticmethodr   rq   r   r0   r.   r   r      sk    (L/DB
H ? ?# \ \|EQN<?|    m1r0   r   ))r   rc   rl   	itertoolsr   django.appsr   django.confr   django.core.management.baser   r   r   django.core.management.utilsr	   	django.dbr
   r   r   r   django.db.migrationsr   !django.db.migrations.autodetectorr   django.db.migrations.loaderr   django.db.migrations.migrationr   django.db.migrations.optimizerr   django.db.migrations.questionerr   r   r   django.db.migrations.stater   django.db.migrations.utilsr   django.db.migrations.writerr   r   r   r0   r.   <module>r     sX    	 
      R R 7 M M * C 7 9 = 
 4 C 7j1k j1r0   