î
Ý^Q\e  ã               @   s~   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
 Gd d „  d e ƒ Z d S)é    N)ÚImproperlyConfigured)ÚBaseDatabaseCreation)Ú
force_text)Úinputc               @   sp   e  Z d  Z e d d „  ƒ Z d d „  Z d d d „ Z d d	 „  Z d d
 d „ Z d d „  Z	 d d „  Z
 d S)ÚDatabaseCreationc             C   s   |  d k p d t  |  ƒ k S)Nz:memory:zmode=memory)r   )Zdatabase_name© r   úI/var/www/dbchiro/venv/build/Django/django/db/backends/sqlite3/creation.pyÚis_in_memory_db   s    z DatabaseCreation.is_in_memory_dbc             C   st   |  j  j d d } |  j  j j } | s2 d } n  | rU | d k rp d |  j  j Sn d | k rp t d ƒ ‚ n  | S)NZTESTÚNAMEz:memory:z)file:memorydb_%s?mode=memory&cache=sharedzmode=memoryz„Using a shared memory database with `mode=memory` in the database name is not supported in your environment, use `:memory:` instead.)Ú
connectionÚsettings_dictÚfeaturesÚcan_share_in_memory_dbÚaliasr   )ÚselfÚtest_database_namer   r   r   r   Ú_get_test_db_name   s    	z"DatabaseCreation._get_test_db_nameFc             C   s  |  j  ƒ  } | r | S|  j | ƒ s| d k rQ t d |  j | | ƒ f ƒ n  t j | t j ƒ r| s t d | ƒ } n  | s‘ | d k rî y t j | ƒ Wqt	 k
 rê } z& t
 j j d | ƒ t
 j d ƒ WYd  d  } ~ XqXqt d ƒ t
 j d ƒ qn  | S)Né   z,Destroying old test database for alias %s...zXType 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: Úyesz0Got an error deleting the old test database: %s
é   zTests cancelled.)r   r	   ÚprintÚ_get_database_display_strÚosÚaccessÚF_OKr   ÚremoveÚ	ExceptionÚsysÚstderrÚwriteÚexit)r   Ú	verbosityZautoclobberÚkeepdbr   ZconfirmÚer   r   r   Ú_create_test_db!   s*    #
z DatabaseCreation._create_test_dbc             C   sr   |  j  j } | d } |  j | ƒ r) | S| j ƒ  } t j j | d ƒ \ } } d j | | | ƒ | d <| Sd  S)Nr
   z{}_{}.{})r   r   r	   Úcopyr   ÚpathÚsplitextÚformat)r   ÚnumberZorig_settings_dictÚsource_database_nameZnew_settings_dictÚrootÚextr   r   r   Úget_test_db_clone_settings=   s    
z+DatabaseCreation.get_test_db_clone_settingsc             C   s>  |  j  j d } |  j | ƒ d } |  j | ƒ s:t j | t j ƒ rÚ | rQ d  S| d k r} t d |  j | | ƒ f ƒ n  y t j	 | ƒ WqÚ t
 k
 rÖ } z& t j j d | ƒ t j d ƒ WYd  d  } ~ XqÚ Xn  y t j | | ƒ Wq:t
 k
 r6} z& t j j d | ƒ t j d ƒ WYd  d  } ~ Xq:Xn  d  S)Nr
   r   z,Destroying old test database for alias %s...z0Got an error deleting the old test database: %s
r   z+Got an error cloning the test database: %s
)r   r   r-   r	   r   r   r   r   r   r   r   r   r   r   r    Úshutilr%   )r   r)   r!   r"   r*   Ztarget_database_namer#   r   r   r   Ú_clone_test_dbH   s&    #zDatabaseCreation._clone_test_dbc             C   s*   | r& |  j  | ƒ r& t j | ƒ n  d  S)N)r	   r   r   )r   r   r!   r   r   r   Ú_destroy_test_db`   s    z!DatabaseCreation._destroy_test_dbc             C   sN   |  j  ƒ  } |  j j d g } |  j | ƒ rD | j |  j j ƒ n  t | ƒ S)a   
        Returns a tuple that uniquely identifies a test database.

        This takes into account the special cases of ":memory:" and "" for
        SQLite since the databases will be distinct despite having the same
        TEST NAME. See http://www.sqlite.org/inmemorydb.html
        r
   )r   r   r   r	   Úappendr   Útuple)r   r   Úsigr   r   r   Útest_db_signaturee   s
    z"DatabaseCreation.test_db_signatureN)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr	   r   r$   r-   r/   r0   r4   r   r   r   r   r      s   r   )r   r.   r   Zdjango.core.exceptionsr   Z django.db.backends.base.creationr   Zdjango.utils.encodingr   Zdjango.utils.six.movesr   r   r   r   r   r   Ú<module>   s   