3
vÅÏ^€  ã               @   s8   d dl Z d dlZd dlZd dlmZ G dd„ deƒZdS )é    N)ÚBaseDatabaseCreationc               @   sL   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| kS )Nz:memory:zmode=memory© )Zdatabase_namer   r   úG/usr/lib/python3.6/site-packages/django/db/backends/sqlite3/creation.pyÚis_in_memory_db
   s    z DatabaseCreation.is_in_memory_dbc             C   s,   | j jd d pd}|dkr(d| j j S |S )NÚTESTÚNAMEz:memory:z)file:memorydb_%s?mode=memory&cache=shared)Ú
connectionÚsettings_dictÚalias)ÚselfÚtest_database_namer   r   r   Ú_get_test_db_name   s    z"DatabaseCreation._get_test_db_nameFc             C   sÈ   | j ƒ }|r|S | j|ƒsÄ|dkr:| jd| j||ƒf ƒ tj|tjƒrÄ|sXtd| ƒ}|sd|dkr°ytj|ƒ W qÄ t	k
r¬ } z| jd| ƒ t
jdƒ W Y d d }~X qÄX n| jdƒ t
jdƒ |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: Úyesz/Got an error deleting the old test database: %sé   zTests cancelled.)r   r   ÚlogÚ_get_database_display_strÚosÚaccessÚF_OKÚinputÚremoveÚ	ExceptionÚsysÚexit)r   Ú	verbosityZautoclobberÚkeepdbr   ZconfirmÚer   r   r   Ú_create_test_db   s*    


z DatabaseCreation._create_test_dbc             C   sL   | j j}|d }| j|ƒr|S tjj|d ƒ\}}|ddj|||ƒi–S d S )Nr   z{}_{}.{})r	   r
   r   r   ÚpathÚsplitextÚformat)r   ÚsuffixZorig_settings_dictÚsource_database_nameÚrootÚextr   r   r   Úget_test_db_clone_settings0   s    
z+DatabaseCreation.get_test_db_clone_settingsc             C   sô   | j jd }| j|ƒd }| j|ƒsðtj|tjƒr¤|r:d S |dkrZ| jd| j||ƒf ƒ ytj	|ƒ W n: t
k
r¢ } z| jd| ƒ tjdƒ W Y d d }~X nX ytj||ƒ W n: t
k
rî } z| jd| ƒ tjdƒ W Y d d }~X nX d S )Nr   r   z,Destroying old test database for alias %s...z/Got an error deleting the old test database: %sr   z*Got an error cloning the test database: %s)r	   r
   r'   r   r   r   r   r   r   r   r   r   r   ÚshutilÚcopy)r   r#   r   r   r$   Ztarget_database_namer   r   r   r   Ú_clone_test_db9   s&    
zDatabaseCreation._clone_test_dbc             C   s   |r| j |ƒ rtj|ƒ d S )N)r   r   r   )r   r   r   r   r   r   Ú_destroy_test_dbQ   s    z!DatabaseCreation._destroy_test_dbc             C   s6   | j ƒ }| jjd g}| j|ƒr.|j| jjƒ t|ƒS )a   
        Return 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 https://www.sqlite.org/inmemorydb.html
        r   )r   r	   r
   r   Úappendr   Útuple)r   r   Úsigr   r   r   Útest_db_signatureV   s
    
z"DatabaseCreation.test_db_signatureN)F)F)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr   r   r   r'   r*   r+   r/   r   r   r   r   r      s   
	
r   )r   r(   r   Z django.db.backends.base.creationr   r   r   r   r   r   Ú<module>   s   