
v^8                 @   s  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 m Z 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 Gd d   d e	  Z  d S)    N)	takewhile)apps)settings)BaseCommandCommandErrorno_translations)DEFAULT_DB_ALIASconnectionsrouter)	Migration)MigrationAutodetector)MigrationLoader)InteractiveMigrationQuestionerMigrationQuestioner!NonInteractiveMigrationQuestioner)ProjectState)get_migration_name_timestamp)MigrationWriterc               @   sL   e  Z d  Z d Z d d   Z e d d    Z d d   Z d d	   Z d
 S)Commandz"Creates new migration(s) for apps.c          	   C   s   | j  d d d d d d d | j  d d	 d
 d d | j  d d	 d
 d d | j  d d	 d
 d d | j  d d d	 d d d d d | j  d d d d | j  d d	 d d d d d | j  d d	 d
 d d d d d  S)Nargsmetavar	app_labelnargs*helpz2Specify the app label(s) to create migrations for.z	--dry-runaction
store_truezCJust show what migrations would be made; don't actually write them.z--mergez%Enable fixing of migration conflicts.z--emptyzCreate an empty migration.z	--noinputz
--no-inputstore_falsedestinteractivez:Tells Django to NOT prompt the user for input of any kind.z-nz--namez$Use this name for migration file(s).z--no-headerinclude_headerz4Do not add header comments to new migration file(s).z--checkcheck_changeszDExit with a non-zero status if model changes are missing migrations.)add_argument)selfparser r%   P/tmp/pip-build-8lau8j11/django/django/core/management/commands/makemigrations.pyadd_arguments   s0    	zCommand.add_argumentsc                s  | d |  _  | d |  _ | d |  _ | d |  _ | d |  _ | d |  _ |  j rs |  j j   rs t d   | d |  _ | d	 } t	      d
 } xc   D][ } y t
 j |  Wq t k
 r } z! |  j j t |   d } WYd  d  } ~ Xq Xq W| rt j d  t d  d d } d d   t
 j   D } t j rOt n t g }	 x] t |	  D]O }
 t |
   j d d k ret  f d d   | D  re| j   qeW| j   }   r  f d d   | j   D } | r+|  j r+d j d d   | j   D  } t d |   |  j rO| rO|  j j d  d  S|  j rn| rn|  j  | |  S|  j rt! d   d |  j  } n t" d   d |  j  } t# | j$   t% j& t
  |  } |  j r.  st d   d d     D } | j' d | d | j( d |  j  } |  j) |  d  S| j* d | j( d    pId  d!   pUd  d |  j  } | s|  j  d" k r  rt+    d" k r|  j j d#   j,    q|  j j d$ d% j     q|  j j d&  n  |  j) |  | rt j d"  d  S)'N	verbosityr   dry_runmergeemptynamez5The migration name must be a valid Python identifier.r    r!   FT   Zignore_no_migrationsc             S   s   h  |  ] } | j   q Sr%   )label).0configr%   r%   r&   	<setcomp>Z   s   	 z!Command.handle.<locals>.<setcomp>ZENGINEzdjango.db.backends.dummyc             3   sL   |  ]B } t  j |  j   D]& } t j   j | d  | j j Vq q d S)Z
model_nameN)r   get_app_configZ
get_modelsr
   Zallow_migratealiasZ_metaZobject_name)r/   r   model)
connectionr%   r&   	<genexpr>a   s   z!Command.handle.<locals>.<genexpr>c                s+   i  |  ]! \ } } |   k r | |  q Sr%   r%   )r/   r   conflict)
app_labelsr%   r&   
<dictcomp>m   s   		z"Command.handle.<locals>.<dictcomp>z; c             s   s.   |  ]$ \ } } d  d j  |  | f Vq d S)z%s in %sz, N)join)r/   appnamesr%   r%   r&   r6   t   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.Zspecified_appsz:You must supply at least one app label when using --empty.c             S   s%   i  |  ] } t  d  |  g |  q S)Zcustom)r   )r/   r;   r%   r%   r&   r9      s   	changesgraphmigration_nameZtrim_to_appsZconvert_apps   zNo changes detected in app '%s'z No changes detected in apps '%s'z', 'zNo changes detected)-r(   r   r)   r*   r+   r?   isidentifierr   r    setr   r2   LookupErrorstderrwritestrsysexitr   Zget_app_configsr   ZDATABASE_ROUTERSr	   r   sortedZsettings_dictanyZcheck_consistent_historyZdetect_conflictsitemsr:   stdouthandle_merger   r   r   Zproject_stater   Z	from_appsZarrange_for_graphr>   write_migration_filesr=   lenpop)r#   r8   optionsr!   Zhas_bad_labelsr   errloaderZconsistency_check_labelsZaliases_to_checkr3   	conflictsZname_str
questionerZautodetectorr=   r%   )r8   r5   r&   handle<   s    

	
					
				 zCommand.handlec             C   s>  i  } x1| j    D]#\ } } |  j d k rR |  j j |  j j d |  d  x| D]} t | |  j  } |  j d k r y t j	 j
 | j	  } Wn t k
 r | j	 } Yn X| j d  r | j	 } |  j j d |  j j |  f  x+ | j D]  } |  j j d | j    q W|  j st j	 j | j	  }	 | j |  st j |	 d d t j	 j |	 d	  }
 t j	 j |
  st |
 d
  j   d | | <| j   } t | j	 d
 d d  } | j |  Wd QRXqY |  j d k rY |  j j |  j j d | j  d  |  j j d | j    qY Wq Wd S)zL
        Take a changes dict and write them out as migration files.
        r@   zMigrations for '%s':
z..z  %s
z	    - %s
exist_okTz__init__.pywencodingzutf-8N   zFull migrations file '%s':z%s
)rK   r(   rL   rE   styleMIGRATE_HEADINGr   r    ospathrelpath
ValueError
startswithMIGRATE_LABEL
operationsdescriber)   dirnamegetmakedirsr:   isfileopenclose	as_stringfilename)r#   r=   Zdirectory_createdr   Zapp_migrations	migrationwriterZmigration_string	operationZmigrations_directoryZ	init_pathfhr%   r%   r&   rN      s>    $	#	
zCommand.write_migration_filesc                sv  |  j  r t   } n t d d d i  } xE| j   D]7\   } g  } x[ | D]S }  j   |    f d d    j j   | f  D  _ | j   qP Wd d   } t	 d d	   | D   } t
 d
 d	   t | |  D  }	 |	 st d |   xR | D]J   j |	 d   _  f d d	    j D }
 t
 |
 g    _ qW|  j d k r|  j j |  j j d     x_ | D]W  |  j j |  j j d  j   x+  j D]  } |  j j d | j    qWqW| j    r7 d d   | D } y t d d	   | D  } Wn t k
 rCd } Yn Xt d t f d   f d d   | D i  } d | d |  j pd t   f } | |    } t | |  j  } |  j st  | j! d d d  } | j | j"    Wd QRX|  j d k rn|  j j d | j!  q7 |  j d k r7 |  j j |  j j d | j#  d   |  j j d! | j"    q7 Wd S)"z
        Handles merging together conflicted migrations interactively,
        if it's safe; otherwise, advises on how to fix it.
        defaults	ask_mergeTc                s)   g  |  ] } | d    j  k r |  q S)r   )r   )r/   Zmig)rn   r%   r&   
<listcomp>   s   	 z(Command.handle_merge.<locals>.<listcomp>c                s'   t    f d d     d d   D  S)Nc             3   s   |  ] } |   d  k Vq d S)r   Nr%   )r/   item)seqr%   r&   r6      s    z@Command.handle_merge.<locals>.all_items_equal.<locals>.<genexpr>r@   )all)rv   r%   )rv   r&   all_items_equal   s    z-Command.handle_merge.<locals>.all_items_equalc             s   s   |  ] } | j  Vq d  S)N)ancestry)r/   mr%   r%   r&   r6      s    z'Command.handle_merge.<locals>.<genexpr>c             s   s   |  ] } d  Vq d S)r@   Nr%   )r/   Zcommon_ancestor_generationr%   r%   r&   r6     s    z$Could not find common ancestor of %sNc             3   s*   |  ]  \ } }   j  | |  j Vq d  S)N)get_migrationrd   )r/   Znode_appZ	node_name)rS   r%   r&   r6     s   r   z
Merging %sz  Branch %sz	    - %s
c             S   s"   g  |  ] } t  j | j   q Sr%   )r   Zparse_numberr,   )r/   rn   r%   r%   r&   rt     s   	c             s   s!   |  ] } | d  k	 r | Vq d  S)Nr%   )r/   xr%   r%   r&   r6     s    r@   r   Zdependenciesc                s   g  |  ] }   | j  f  q Sr%   )r,   )r/   rn   )r   r%   r&   rt      s   	 z%04i_%szmerge_%srY   rZ   zutf-8z
Created new merge migration %sr[   z Full merge migrations file '%s':rW   z%s
)$r   r   r   rK   r{   r>   Zforwards_planry   appendzipsumr   ra   branchZmerged_operationsr(   rL   rE   r\   r]   rc   r,   re   rs   maxtyper   r?   r   r   r    r)   rj   r_   rl   rm   )r#   rS   rT   rU   Zmigration_namesZmerge_migrationsr?   rx   Zmerge_migrations_generationsZcommon_ancestor_countZmigrations_opsrp   ZnumbersZbiggest_numbersubclassZnew_migrationro   rq   r%   )r   rS   rn   r&   rM      sb    	. #"	
"	zCommand.handle_mergeN)	__name__
__module____qualname__r   r'   r   rV   rN   rM   r%   r%   r%   r&   r      s
   "~-r   )!r^   rG   	itertoolsr   Zdjango.appsr   Zdjango.confr   Zdjango.core.management.baser   r   r   Z	django.dbr   r	   r
   Zdjango.db.migrationsr   Z!django.db.migrations.autodetectorr   Zdjango.db.migrations.loaderr   Zdjango.db.migrations.questionerr   r   r   Zdjango.db.migrations.stater   Zdjango.db.migrations.utilsr   Zdjango.db.migrations.writerr   r   r%   r%   r%   r&   <module>   s   