
 X"                 @   s   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
 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)    )settings)BaseCommandCommandError)DEFAULT_DB_ALIASconnections
migrations)AmbiguityErrorMigrationLoader)SwappableTuple)MigrationOptimizer)MigrationWriter)six)get_docs_versionc               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	CommandzZSquashes an existing set of migrations (from first until specified) into a single new one.c             C   s   | j  d d d | j  d d d  d d d d | j  d	 d d
 | j  d d d d d d d d d | j  d d d d d d d d d d d  S)N	app_labelhelpz6App label of the application to squash migrations for.start_migration_namedefaultnargs?zGMigrations will be squashed starting from and including this migration.migration_namez?Migrations will be squashed until and including this migration.z--no-optimizeaction
store_truedestno_optimizeFz/Do not try to optimize the squashed operations.z	--noinputz
--no-inputstore_falseinteractiveTz:Tells Django to NOT prompt the user for input of any kind.)add_argument)selfparser r    ^/home/ubuntu/projects/ifolica/build/django/django/core/management/commands/squashmigrations.pyadd_arguments   s    zCommand.add_argumentsc                s  | d |  _  | d |  _ | d } | d } | d } | d } t t t    |   j k rt t d |   n  |  j   | |      f d d	     j j	  j
  j f  D } | rD|  j   | |  }   j | j
 | j  } y# | j |  }	 | |	 d   } WqDt k
 r@t d
 |  | f   YqDXn  |  j  d k s\|  j r|  j j |  j j d   x% | D]  |  j j d  j  qW|  j rd  }
 xI |
 s|
 d k rt j j d  }
 |
 sd }
 Pq|
 d j   }
 qW|
 d k rd  Sqn  g  } t   } d } x | D] } | j rRt d t     n  | j | j  x{ | j D]p } t | t  rt j  | j! k r| j" d+  q| j" |  ql| d | j
 k s| rl| j" |  qlqlWd } q-W| r'|  j  d k r|  j j |  j j d   n  | } n |  j  d k rU|  j j |  j j d   n  t#   } | j$ |  j
  } |  j  d k rt% |  t% |  k r|  j j d  q|  j j d t% |  t% |  f  n  g  } xC | D];   j r| j  j  q| j&  j
  j f  qWt' d t( j) f i | d 6| d 6| d 6 } | rw| d | j  j f |  } n | d   j |  } d | _* t+ |  } t, | j- d!   } | j | j.    Wd  QX|  j  d k r|  j j |  j j d" | j-   |  j j d#  |  j j d$  |  j j d%  |  j j d&  | j/ r|  j j |  j j d'   |  j j d(  |  j j d)  |  j j d*  qn  d  S),N	verbosityr   r   r   r   r   zLApp '%s' does not have migrations (so squashmigrations on it makes no sense)c                s7   g  |  ]- \ } } |  j  k r   j | |   q Sr    )r   get_migration).0almn)loader	migrationr    r!   
<listcomp>:   s   		z"Command.handle.<locals>.<listcomp>zThe migration '%s' cannot be found. Maybe it comes after the migration '%s'?
Have a look at:
  python manage.py showmigrations %s
to debug this issue.r   z%Will squash the following migrations:z - %sZynzDo you wish to proceed? [yN] nyTzYou cannot squash squashed migrations! Please transition it to a normal migration first: https://docs.djangoproject.com/en/%s/topics/migrations/#squashing-migrations__setting__AUTH_USER_MODELFz(Skipping optimization.)zOptimizing...z  No optimizations possible.z0  Optimized from %s operations to %s operations.	Migrationdependencies
operationsreplacesz%s_squashed_%sz0001_squashed_%swbz!Created new squashed migration %szC  You should commit this migration but leave the old ones in place;zD  the new migration will be used for new installs. Once you are surezI  all instances of the codebase have applied the migrations you squashed,z  you can delete them.zManual porting requiredzH  Your migrations contained functions that must be manually copied over,z3  as we could not safely copy their implementation.zC  See the comment at the top of the squashed migration for details.)r-   zAUTH_USER_MODEL)0r#   r   r	   r   r   Zmigrated_appsr   find_migrationgraphZforwards_planr   namer$   index
ValueErrorstdoutwritestyleZMIGRATE_HEADINGr   movesinputlowersetr2   r   extendr1   r0   
isinstancer
   r   r.   Zsettingaddr   optimizelenappendtyper   r/   initialr   openpath	as_stringZneeds_manual_porting)r   optionsr   r   r   r   Zmigrations_to_squashZstart_migrationstartZstart_indexZanswerr1   r0   Zfirst_migrationZ
smigration
dependencyZnew_operationsZ	optimizerr2   subclassZnew_migrationwriterfhr    )r(   r)   r!   handle%   s    



"	
		
				"	#	zCommand.handlec             C   sj   y | j  | |  SWnO t k
 r> t d | | f   Yn( t k
 re t d | | f   Yn Xd  S)NzJMore than one migration matches '%s' in app '%s'. Please be more specific.z4Cannot find a migration matching '%s' from app '%s'.)Zget_migration_by_prefixr   r   KeyError)r   r(   r   r6   r    r    r!   r4      s    zCommand.find_migrationN)__name__
__module____qualname__r   r"   rQ   r4   r    r    r    r!   r      s   r   N)Zdjango.confr   Zdjango.core.management.baser   r   Z	django.dbr   r   r   Zdjango.db.migrations.loaderr   r	   Zdjango.db.migrations.migrationr
   Zdjango.db.migrations.optimizerr   Zdjango.db.migrations.writerr   Zdjango.utilsr   Zdjango.utils.versionr   r   r    r    r    r!   <module>   s   