
 X9                 @   sL  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 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 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)DEFAULT_DB_ALIASconnectionsrouter)	Migration)MigrationAutodetector)MigrationLoader)InteractiveMigrationQuestionerMigrationQuestioner!NonInteractiveMigrationQuestioner)ProjectState)get_migration_name_timestamp)MigrationWriter)RemovedInDjango20Warning)	iteritems)zipc               @   sF   e  Z d  Z d Z d d   Z 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 d d d d | j  d d	 d
 d d d d d d | j  d d	 d
 d d d d d d | j  d d d	 d d d d d d d | j  d d d	 d d d d d  d d  | j  d! d" d	 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_truedestdry_rundefaultFzCJust show what migrations would be made; don't actually write them.z--mergemergez%Enable fixing of migration conflicts.z--emptyemptyzCreate an empty migration.z	--noinputz
--no-inputstore_falseinteractiveTz:Tells Django to NOT prompt the user for input of any kind.z-nz--namestorenamez$Use this name for migration file(s).z-ez--exit	exit_codeznExit with error code 1 if no changes needing migrations are found. Deprecated, use the --check option instead.z--checkcheck_changeszDExit with a non-zero status if model changes are missing migrations.)add_argument)selfparser r-   \/home/ubuntu/projects/ifolica/build/django/django/core/management/commands/makemigrations.pyadd_arguments   s2    	zCommand.add_argumentsc                sB  | d |  _  | d |  _ | d |  _ | d |  _ | d |  _ | d |  _ | d |  _ | d } |  j r t j d	 t	  n  t
      t
   } xA   D]9 } y t j |  Wq t k
 r | j |  Yq Xq W| rx" | D] } |  j j d
 |  q Wt j d  n  t d  d d } t
 d d   t j   D  } t j rUt n t g } x` t |  D]R }	 t |	   j d d k rkt  f d d   | D  rk| j   qkqkW| j   }
   r  f d d   t |
  D }
 n  |
 r:|  j r:d j d d   |
 j    D  } t! d |   n  |  j r^|
 r^|  j" j d  d  S|  j r}|
 r}|  j# | |
  S|  j rt$ d   d |  j  } n t% d   d |  j  } t& | j'   t( j) t  |  } |  j r@  st! d   n  d d     D } | j* d | d | j+ d |  j  } |  j, |  d  S| j- d | j+ d   p[d  d    pgd  d |  j  } | s|  j  d! k rt.    d! k r|  j" j d"   j/    qt.    d! k r|  j" j d# d$ j     q|  j" j d%  n  |  j r>t j d!  q>n# |  j, |  | r>t j d!  n  d  S)&N	verbosityr%   r    r"   r#   r'   r(   r)   z?The --exit option is deprecated in favor of the --check option.z5App '%s' could not be found. Is it in INSTALLED_APPS?   Zignore_no_migrationsTc             s   s   |  ] } | j  Vq d  S)N)label).0configr-   r-   r.   	<genexpr>b   s    z!Command.handle.<locals>.<genexpr>ZENGINEzdjango.db.backends.dummyc             3   s$   |  ] } t  j   j |  Vq d  S)N)r	   Zallow_migratealias)r3   r2   )
connectionr-   r.   r5   i   s    c                s+   i  |  ]! \ } } |   k r | |  q Sr-   r-   )r3   r   conflict)
app_labelsr-   r.   
<dictcomp>s   s   	 	z"Command.handle.<locals>.<dictcomp>z; c             s   s.   |  ]$ \ } } d  d j  |  | f Vq d S)z%s in %sz, N)join)r3   appnamesr-   r-   r.   r5   y   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
   )r3   r<   r-   r-   r.   r:      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)0r0   r%   r    r"   r#   r@   r(   warningswarnr   setr   Zget_app_configLookupErroraddstderrwritesysexitr   Zget_app_configsr   ZDATABASE_ROUTERSr   r   sortedZsettings_dictanyZcheck_consistent_historyZdetect_conflictsr   r;   itemsr   stdouthandle_merger   r   r   Zproject_stater   Z	from_appsZarrange_for_graphr?   write_migration_filesr>   lenpop)r+   r9   optionsr)   Zbad_app_labelsr   loaderZconsistency_check_labelsZaliases_to_checkr6   	conflictsZname_str
questionerZautodetectorr>   r-   )r9   r7   r.   handle@   s    
	
	
"						
				 	zCommand.handlec             C   s0  i  } x#| j    D]\ } } |  j d k rU |  j j |  j j d |  d  n  x| D]} t |  } |  j d k rt j j	 | j  } | j
 d  r | j } n  |  j j d |  j j |  f  x. | j D]  } |  j j d | j    q Wn  |  j st j j | j  }	 | j |  st j j |	  sPt j |	  n  t j j |	 d  }
 t j j |
  st |
 d  j   n  d	 | | <n  | j   } t | j d
   } | j |  Wd QXq\ |  j d k r\ |  j j |  j j d | j  d  |  j j d | j    q\ q\ Wq Wd S)zN
        Takes a changes dict and writes them out as migration files.
        rA   zMigrations for '%s':
z..z  %s:
z	    - %s
z__init__.pywTwbN   zFull migrations file '%s':z%s
)rM   r0   rN   rH   styleMIGRATE_HEADINGr   ospathrelpath
startswithMIGRATE_LABEL
operationsdescriber    dirnamegetisdirmkdirr;   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.   rP      s:    '#!	zCommand.write_migration_filesc                sz  |  j  r t   } n t d i d d 6 } xH| j   D]:\   } g  } x[ | D]S }  j   |    f d d    j j   | f  D  _ | j   qQ Wd d   } t	 d d   | D   } t
 d	 d
   t | |  D  }	 |	 st d |   n  xR | D]J   j |	 d   _  f d d
    j D }
 t
 |
 g    _ qW|  j d k r|  j j |  j j d     xb | D]W  |  j j |  j j d  j   x+  j D]  } |  j j d | j    qWqWn  | j    r8 d d   | D } y t d d
   | D  } Wn t k
 rJd } Yn Xt d t f i   f d d   | D d 6 } d | d |  j pd t   f } | |    } t |  } |  j st | j  d   } | j | j!    Wd QX|  j d k ro|  j j d | j   qoqr|  j d k rr|  j j |  j j d | j"  d  |  j j d | j!    qrq8 q8 Wd S) z
        Handles merging together conflicted migrations interactively,
        if it's safe; otherwise, advises on how to fix it.
        defaultsT	ask_mergec                s)   g  |  ] } | d    j  k r |  q S)r   )r   )r3   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-   )r3   item)seqr-   r.   r5     s    z@Command.handle_merge.<locals>.all_items_equal.<locals>.<genexpr>rA   )all)rv   r-   )rv   r.   all_items_equal  s    z-Command.handle_merge.<locals>.all_items_equalc             S   s   g  |  ] } | j   q Sr-   )ancestry)r3   mr-   r-   r.   rt     s   	 c             s   s   |  ] } d  Vq d S)rA   Nr-   )r3   Zcommon_ancestor_generationr-   r-   r.   r5     s    z'Command.handle_merge.<locals>.<genexpr>z$Could not find common ancestor of %sNc             3   s*   |  ]  \ } }   j  | |  j Vq d  S)N)get_migrationrc   )r3   Znode_appZ	node_name)rT   r-   r.   r5     s   r   z
Merging %sz  Branch %sz	    - %s
c             S   s"   g  |  ] } t  j | j   q Sr-   )r   Zparse_numberr'   )r3   rn   r-   r-   r.   rt     s   	c             s   s!   |  ] } | d  k	 r | Vq d  S)Nr-   )r3   xr-   r-   r.   r5   !  s    rA   r
   c                s   g  |  ] }   | j  f  q Sr-   )r'   )r3   rn   )r   r-   r.   rt   %  s   	 Zdependenciesz%04i_%szmerge_%srZ   z
Created new merge migration %sr[   z Full merge migrations file '%s':rX   z%s
)#r%   r   r   rM   r{   r?   Zforwards_planry   appendr   sumr   
ValueErrorbranchZmerged_operationsr0   rN   rH   r\   r]   rb   r'   rd   rs   maxtyper
   r@   r   r   r    rj   r_   rl   rm   )r+   rT   rU   rV   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   rT   rn   r.   rO      sb    	. #%	
 	zCommand.handle_mergeN)__name__
__module____qualname__r   r/   rW   rP   rO   r-   r-   r-   r.   r      s
   $+r   )'r^   rI   rB   	itertoolsr   Zdjango.appsr   Zdjango.confr   Zdjango.core.management.baser   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   Zdjango.utils.deprecationr   Zdjango.utils.sixr   Zdjango.utils.six.movesr   r   r-   r-   r-   r.   <module>   s$   