
v^                 @   sl   d  d l  Z  d  d l m Z d  d l m Z d  d l m Z m Z d  d l m	 Z	 Gd d   d e  Z
 d S)    N)apps)BaseCommand)DEFAULT_DB_ALIASconnections)MigrationLoaderc               @   sX   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	 d
  Z d d d  Z d S)Commandz6Shows all available migrations for the current projectc             C   s   | j  d d d d d | j  d d t d d | j   } | 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  S)N	app_labelnargs*helpz2App labels of applications to limit the output to.z
--databasedefaultzHNominates a database to synchronize. Defaults to the "default" database.z--listz-lactionstore_constdestformatconstlistzShows a list of all migrations and which are applied. With a verbosity level of 2 or above, the applied datetimes will be included.z--planz-pplanzShows all migrations in the order they will be applied. With a verbosity level of 2 or above all direct migration dependencies and reverse dependencies (run_before) will be included.)add_argumentr   add_mutually_exclusive_groupset_defaults)selfparserformats r   P/tmp/pip-build-8lau8j11/django/django/core/management/commands/showmigrations.pyadd_arguments   s    zCommand.add_argumentsc             O   s]   | d |  _  | d } t | } | d d k rE |  j | | d  S|  j | | d  Sd  S)N	verbosityZdatabaser   r   r   )r   r   	show_plan	show_list)r   argsoptionsdb
connectionr   r   r   handle*   s    

zCommand.handlec             C   s   d } xc | D][ } y t  j |  Wq t k
 rg } z! |  j j t |   d } WYd  d  } ~ Xq Xq W| r t j d  d  S)NFT   )r   Zget_app_configLookupErrorstderrwritestrsysexit)r   loader	app_namesZhas_bad_namesapp_nameerrr   r   r   _validate_app_names6   s    zCommand._validate_app_namesNc             C   s  t  | d d } | j } | r4 |  j | |  n t | j  } xV| D]N} |  j j | |  j j  t	   } x| j
 |  D] } x | j |  D] } | | k r | d | k r | d }	 | j | j r |	 d t | j | j  7}	 | j j |  }
 |
 rPd |	 } |  j d k r=| d |
 j j d	  7} |  j j |  n |  j j d
 |	  | j |  q Wq W| sJ |  j j d |  j j  qJ Wd S)zh
        Show a list of all migrations on the system, or only those of
        some named apps.
        Zignore_no_migrationsTr      z (%s squashed migrations)z [X] %sr%   z (applied at %s)z%Y-%m-%d %H:%M:%Sz [ ] %sz (no migrations)N)r   graphr0   sortedZmigrated_appsstdoutr(   styleZMIGRATE_LABELset
leaf_nodesforwards_planZnodesZreplaceslenapplied_migrationsgetr   ZappliedstrftimeaddERROR)r   r#   r-   r,   r2   r.   ZshownnodeZ	plan_nodetitleZapplied_migrationoutputr   r   r   r   A   s0    		

zCommand.show_listc                s  t  |  } | j }   rM |  j |      f d d   | j   D } n | j   } g  } t   } x[ | D]S } xJ | j |  D]9 }	 |	 | k r | j |	 }
 | j |
  | j |	  q Wqo Wd d   } x | D] }
 d } |  j	 d k r | |
  } |
 j
 | j k r@|  j j d |
 j
 d |
 j
 d	 | f  q |  j j d
 |
 j
 d |
 j
 d	 | f  q W| s|  j j d |  j j  d S)z
        Show all known migrations (or only those of the specified app_names)
        in the order they will be applied.
        c                s&   g  |  ] } | d    k r |  q S)r   r   ).0key)r-   r   r   
<listcomp>s   s   	 z%Command.show_plan.<locals>.<listcomp>c             S   sO   g  } x+ t  |  j  D] } | j d | j  q W| rK d d j |  Sd S)Nz%s.%sz	 ... (%s)z,  )r3   parentsappendrC   join)r?   outparentr   r   r   
print_deps   s    z%Command.show_plan.<locals>.print_depsrE   r%   z[X]  %s.%s%sr   r1   z[ ]  %s.%s%sz(no migrations)N)r   r2   r0   r7   r6   r8   Znode_maprG   r=   r   rC   r:   r4   r(   r5   r>   )r   r#   r-   r,   r2   targetsr   seentargetZ	migrationr?   rK   depsr   )r-   r   r   i   s0    	"	./zCommand.show_plan)	__name__
__module____qualname__r   r   r$   r0   r   r   r   r   r   r   r   	   s   (r   )r*   Zdjango.appsr   Zdjango.core.management.baser   Z	django.dbr   r   Zdjango.db.migrations.loaderr   r   r   r   r   r   <module>   s
   