
    dfW                     
   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(dS )    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                   t    e Zd ZdZd Zed             Zd Zed             Z	d Z
ddZed	             Zd
 ZdS )Commandz"Creates new migration(s) for apps.c                    |                     dddd           |                     ddd	           |                     d
dd	           |                     ddd	           |                     ddddd           |                     ddd           |                     dddd           |                     dddd           |                     dddd            |                     d!dd"d#           d S )$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     jD:\Python Project\djangoTemplates\venv\Lib\site-packages\django/core/management/commands/makemigrations.pyadd_argumentszCommand.add_arguments   s   E	 	 	
 	
 	
 	V 	 	
 	
 	

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

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

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

 	 !G	 	 	
 	
 	
 	 D 	 	
 	
 	
 	@ 	 	
 	
 	
 	( 	 	
 	
 	
 	
 	
    c                 ,    | j         r| j        n| j        S N)r*   stderrstdout)r-   s    r/   
log_outputzCommand.log_outputa   s    "o>t{{4;>r1   c                 :    | j                             |           d S r3   )r6   write)r-   msgs     r/   logzCommand.loge   s    c"""""r1   c                 	   g | _         |d         | _        |d         | _        |d         | _        |d         | _        |d         | _        |d         | _        | j        r(| j                                        st          d          |d         | _	        |d	         }|rd
| _        |d         | _
        |d         | _        | j
        rd | j        _        t                    d}D ]W}	 t          j        |           # t"          $ r3}| j                            t'          |                     d
}Y d }~Pd }~ww xY w|rt)          j        d           t-          d d
          }d t          j                    D             }t0          j        rt4          nt6          g}	t9          |	          D ]}
t4          |
         j        d         dk    rit=          fd|D                       rN	 |                               R# t@          $ r*}tC          j"        d|
d|tF                     Y d }~d }~ww xY w|$                                }r fd|%                                D             }|rJ| j        sCd&                    d |%                                D                       }t          d|z            | j        r|s| '                    d           d S | j        r|r| (                    ||          S | j        rtS          | j        | j*                  }n"tW          | j        | j        | j'                  }tY          |-                                t]          j/        t                    |          }| j        rVst          d          d D             }|0                    ||j1        | j                  }| 2                    |           d S |3                    |j1        pd pd | j                   }|s| j        d!k    rrlti                    d!k    r,| '                    d"5                                z             d S | '                    d#d$&                              z             d S | '                    d%           d S d S | j        r| 6                    |           n| 2                    |           |rt)          j        d!           d S d S )&N	verbosityr&   dry_runmergeemptynamez5The migration name must be a valid Python identifier.r(   r)   Tr*   r+   F   )ignore_no_migrationsc                     h | ]	}|j         
S  )label).0configs     r/   	<setcomp>z!Command.handle.<locals>.<setcomp>   s    #V#V#VVFL#V#V#Vr1   ENGINEzdjango.db.backends.dummyc              3      K   | ]S}t          j        |                                          D ]*}t          j        j        ||j        j                   V  +TdS ))
model_nameN)r   get_app_config
get_modelsr   allow_migratealias_metaobject_name)rF   r   model
connections      r/   	<genexpr>z!Command.handle.<locals>.<genexpr>   s       X X
 !0;;FFHHX X 	 $$iEK<S  X X X X X X Xr1   zXGot an error checking a consistent migration history performed for database connection 'z': c                 $    i | ]\  }}|v 	||S rD   rD   )rF   r   conflict
app_labelss      r/   
<dictcomp>z"Command.handle.<locals>.<dictcomp>   s4       'Ix
** 8***r1   z; c              3   P   K   | ]!\  }}d                      |          d|V  "dS ), z in N)join)rF   appnamess      r/   rT   z!Command.handle.<locals>.<genexpr>   sP       ! !9Cedii....4! ! ! ! ! !r1   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)r^   r=   r<   r:   z:You must supply at least one app label when using --empty.c                 2    i | ]}|t          d |          gS )customr   )rF   r\   s     r/   rX   z"Command.handle.<locals>.<dictcomp>   s'    MMM3sYx556MMMr1   )changesgraphmigration_name)rc   trim_to_appsconvert_appsrd      zNo changes detected in app '%s'z No changes detected in apps '%s'z', 'zNo changes detected)7written_filesr<   r&   r=   r>   r?   rd   isidentifierr   r(   r*   r+   r4   
style_funcsetr   rL   LookupErrorr8   strsysexitr   get_app_configsr   DATABASE_ROUTERSr   r
   sortedsettings_dictanycheck_consistent_historyr   warningswarnRuntimeWarningdetect_conflictsitemsr[   r:   handle_merger   r6   r   r   project_stater   	from_appsarrange_for_graphrc   write_migration_filesrb   lenpopwrite_to_last_migration_files)r-   rW   optionsr)   has_bad_labelsr   errloaderconsistency_check_labelsaliases_to_checkrO   error	conflictsname_str
questionerautodetectorrb   rS   s    `               @r/   handlezCommand.handleh   s    -"=1y)W%
W%
%fo 	Xt':'G'G'I'I 	XVWWW%&670 	 DL!,/h'? 	*%)DK" __
# 	& 	&I&#I.... & & &!!#c((+++!%&  	HQKKK !DAAA $W#Vt?S?U?U#V#V#V  $4LKK;K:L 	 ,-- 	 	E$U+J'15OOOTW X X X X
 ":X X X U UO33J????'   MMHMuuV&        ++--	  	   +4??+<+<  I  	TZ 	yy ! !GPGXGX! ! !  H <>FG   : 	i 	HH6777F : 	8) 	8$$VY777 	7)"o  JJ ;).H	  J -  """4((
 
 : 	 "P   NM*MMMG"44l#2 5  G
 &&w///F &&,#+t#+t.	 ' 
 
  	~"" 	4:!++!BZ^^EUEU!UVVVVV>%{{:668    
 HH233333 #" { 4227;;;;**7333  s0   (C==
D:)D55D:'G==
H1 H,,H1c                 6   t          t          t                             }i }i }|                                D ]H\  }}|j                            |          }t          |          dk    rt          d| d          |d         |j        j                 }|j	        rt          d| d          |j
        v rt          d| d          fd|j                                        D             }	|	r5d	                    d
 |	D                       }
t          d| d|
 d          |D ]}|j                            |j                   |j        D ]}t#          |t$                    rKt&          j        |j        k    r|j                            d           G|j                            |           b|d         |j        k    r|j                            |           t1                      }|                    |j        |          |_        t5          |          j        }| j        p|                                }|j        d d         d| z   }|j        |k    r|j        dz   }n|}||_        |g||<   |||<   J|                     ||           d S )N)r\   r   zApp z0 has no migration, cannot update last migration.z Cannot update squash migration 'z'.z!Cannot update applied migration 'c                 &    g | ]}|j         v |S rD   )dependencies)rF   	migrationleaf_migration_nodes     r/   
<listcomp>z9Command.write_to_last_migration_files.<locals>.<listcomp>  s1     $ $ $&)*@@@ @@@r1   rZ   c                     g | ]}d | d 	S )'rD   rF   r   s     r/   r   z9Command.write_to_last_migration_files.<locals>.<listcomp>&  s$    LLL)%%%%LLLr1   zCannot update migration 'z' that migrations z depend on.)__setting__AUTH_USER_MODEL   __updated) r   r   r
   rz   rc   
leaf_nodesr   r   nodesreplacesapplied_migrationsdisk_migrationsvaluesr[   
operationsextendr   
isinstancer   r   r   settingappendr   r   optimizer   pathrd   suggest_namer@   r   )r-   rb   r   new_changesupdate_previous_migration_pathsr   app_migrationsleaf_migration_nodesleaf_migrationdepending_migrationsformatted_migrationsr   
dependency	optimizerprevious_migration_pathname_fragmentsuggested_namenew_namer   s                     @r/   r   z%Command.write_to_last_migration_files  sS    -=!>??*,')0 ?	Q ?	Q%I~#)<#:#:y#:#I#I '((A--"V9VVV   #7q"9#\/0CDN & "I~III   #f&???"JJJJ  $ $ $ $!'!7!>!>!@!@$ $ $ 
 $ '+yyLL7KLLL( ($ #9 9 9+9 9 9  
 , G G	)001EFFF"+"8 	G 	GJ!*n== G#3z7III*7>> B    +7>>zJJJJ#A)*===&3:::FFF	G +,,I(1(:(:)9) )N% '6n&E&E&J# /P>3N3N3P3PM+0!47J=7J7JJN"n44).;)"*N&4%5K	"9P+I66"";0OPPPPPr1   Nc                    i }|                                 D ]F\  }}| j        dk    r0|                     | j                            d|z                       |D ]}t          || j                  }| j        dk    r|                     |j                  }|                     d| j        	                    |          z             |j
        D ],}	|                     d|	                                z             -| j        r| j                            |           | j        st           j                            |j                  }
|                    |          s|t!          j        |
d           t           j                            |
d          }t           j                            |          s"t-          |d                                           d||<   |                                }t-          |j        dd	
          5 }|                    |           | j                            |j                   ddd           n# 1 swxY w Y   |r||         }|                     |          }|j        rP|                     |j                  }|                     | j                            d| d| d                     lt!          j        |           |                     d|            | j        dk    r\|                     | j                            d|j        z                       |                     |                                           Ht?          | j                   dS )zL
        Take a changes dict and write them out as migration files.
        rg   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':) rz   r<   r:   styleMIGRATE_HEADINGr   r(   get_relative_pathr   MIGRATE_LABELr   describer*   r5   r8   r=   osdirnamegetmakedirsr[   isfileopenclose	as_stringrh   r   needs_manual_portingWARNINGremovefilenamer	   )r-   rb   r   directory_createdr   r   r   writermigration_string	operationmigrations_directory	init_pathfh	prev_pathrel_prev_pathmigration_paths                   r/   r   zCommand.write_migration_filesN  s    )0 6	1 6	1%I~~""334JY4VWWXXX+ 31 31	(D4GHH>Q&& (,'='=fk'J'J$HHX
(@(@AQ(R(RRSSS%.%9 D D	i.@.@.B.B!BCCCC <))*:;;;| '1+-7??6;+G+G(,00;; <$84HHHH$&GLL1E}$U$U	!w~~i88 9 C00668887;))4'-'7'7'9'9$fk3AAA ?R!1222*11&+>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 7 A$CI$N	(,(>(>y(I(I!6 A-1-C-CFK-P-PN HH $
 2 2%Y %Y %Y:G%Y %Y %Y!" !"    Ii000 HH%?%?%?@@@^q(( HH
2286?J   
 HHV--//000g31h 	t)*****s   5II	I	c                     	 t           j                            |           }n# t          $ r | }Y nw xY w|                    d          r| }|S )Nz..)r   r   relpath
ValueError
startswith)r   r   s     r/   r   zCommand.get_relative_path  sg    	$!wt44 	$ 	$ 	$#	$&&t,, 	$#s   " 11c                    | j         rt          | j                  }nt          ddi          }|                                D ])\  }g }|D ]Z}                    |          fdj                            |f          D             _        |	                               [d }t          d |D              }t          d t          ||          D                       }	|	st          d	|z            |D ]>j        |	d
         _        fdj        D             }
t          |
g           _        ?| j        dk    r|                     | j                            dz                       |D ]k|                     | j                            dj        z                       j        D ],}|                     d|                                z             -l|                              rsd |D             }	 t1          d |D                       }n# t          $ r d}Y nw xY wt3          dt4          fdfd|D             i          }d|dz   z  g}| j        r|	                    | j                   n|	                    d           d                    t;          d |D                                 }t=          |          dk    r"|	                    t?                                 n|	                    |           d                    |          } ||          }tA          || j!                  }| j"        stG          |j$        dd          5 }|%                    |&                                           d
d
d
           n# 1 swxY w Y   tO          |j$        g           | j        dk    rC|                     d|j$        z             | j(        r| j)        %                    |j$                   | j        dk    r\|                     | j                            d |j*        z                       |                     |&                                           +d
S )!z
        Handles merging together conflicted migrations interactively,
        if it's safe; otherwise, advises on how to fix it.
        )r_   	ask_mergeT)defaultsc                 6    g | ]}|d          j         k    |S )r   )r   )rF   migr   s     r/   r   z(Command.handle_merge.<locals>.<listcomp>  s5     & & &1v!444 444r1   c                 J     t           fd dd          D                       S )Nc              3   0   K   | ]}|d          k    V  dS )r   NrD   )rF   itemseqs     r/   rT   z@Command.handle_merge.<locals>.all_items_equal.<locals>.<genexpr>  s+      >>d43q6>>>>>>>r1   rg   )all)r   s   `r/   all_items_equalz-Command.handle_merge.<locals>.all_items_equal  s.    >>>>c!""g>>>>>>r1   c              3   $   K   | ]}|j         V  d S r3   )ancestry)rF   ms     r/   rT   z'Command.handle_merge.<locals>.<genexpr>  s$      0V0V0V0V0V0V0V0Vr1   c              3      K   | ]}d V  dS )rg   NrD   )rF   common_ancestor_generations     r/   rT   z'Command.handle_merge.<locals>.<genexpr>  s6       ( (. ( ( ( ( ( (r1   z$Could not find common ancestor of %sNc              3   T   K   | ]"\  }}                     ||          j        V  #d S r3   )get_migrationr   )rF   node_app	node_namer   s      r/   rT   z'Command.handle_merge.<locals>.<genexpr>  sN       " "+) ((9==H" " " " " "r1   r   z
Merging %sz  Branch %sr   c                 @    g | ]}t          j        |j                  S rD   )r   parse_numberr@   r   s     r/   r   z(Command.handle_merge.<locals>.<listcomp>  s5       ! *6y~FF  r1   c              3      K   | ]}||V  	d S r3   rD   )rF   xs     r/   rT   z'Command.handle_merge.<locals>.<genexpr>  s"      (M(Mqq}}}}}(M(Mr1   rg   r   r   c                 "    g | ]}|j         fS rD   r@   )rF   r   r   s     r/   r   z(Command.handle_merge.<locals>.<listcomp>  s0     ) ) ) ) '	7) ) )r1   z%04ir>   r   c              3   $   K   | ]}|j         V  d S r3   r   r   s     r/   rT   z'Command.handle_merge.<locals>.<genexpr>  s$      PP)y~PPPPPPr1   /   r   r   r   z
Created new merge migration %sr   z Full merge migrations file '%s':)+r&   r   r6   r   rz   r   rc   forwards_planr   r   zipsumr   r   branchmerged_operationsr<   r:   r   r   r   r@   r   r   maxtyper   rd   r[   rr   r   r   r   r(   r=   r   r   r8   r   r	   r*   r5   r   )r-   r   r   r   migration_namesmerge_migrationsrd   r   merge_migrations_generationscommon_ancestor_countmigrations_opsr   numbersbiggest_numbersubclassparts
leaf_namesnew_migrationr   r   r   r   s    `                  @@r/   r{   zCommand.handle_merge  s   
  	K7doVVVJJ,{D6IJJJJ*3//*;*; c	1 c	1&I  ""1 3 3"00NKK	& & & &%|999n:UVV& & &	"
 !''	2222? ? ? ,/0V0VEU0V0V0V+W($' ( (2;#%A3 3( ( ( % %! )  :_L   . F F	#,#56K6L6L#M	 " " " "/8/?" " " /2.".E.E	++ ~!!33L94LMMNNN!1 D DIHHTZ55min6TUUVVV%.%@ D D	i.@.@.B.B!BCCCCD##I.. 71 %5  '%((M(MG(M(M(M%M%MNN! ' ' '%&NNN'L& ) ) ) )-=) ) )	 	  >A#567& 
1LL!45555LL)))!$PP?OPPPPP" "J :++%A%C%CDDDDZ000!$% ( C C(8KLL| 1fk3AAA 5R!1!1!3!34445 5 5 5 5 5 5 5 5 5 5 5 5 5 5"FK=111~))!Cfk!QRRR? ; K--fk:::^q(( HH
22>P   
 HHV--//000Gc	1 c	1s$   
H$$H32H3%(NN	 N	r3   )__name__
__module____qualname__r"   r0   propertyr6   r:   r   r   r   r   staticmethodr   r{   rD   r1   r/   r   r      s        /DB
 B
 B
H ? ? X?# # # \ \ _\|EQ EQ EQN<+ <+ <+ <+|     \ m1 m1 m1 m1 m1r1   r   ))r   rn   rv   	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   rD   r1   r/   <module>r!     s   				 



                          R R R R R R R R R R 7 7 7 7 7 7 M M M M M M M M M M M M * * * * * * C C C C C C 7 7 7 7 7 7 9 9 9 9 9 9 = = = = = =         
 4 3 3 3 3 3 C C C C C C 7 7 7 7 7 7i1 i1 i1 i1 i1k i1 i1 i1 i1 i1r1   