o
    ^"<f  ã                   @   sD   d dl Z d dlZd dlZd dlmZ d dlmZ G dd„ deƒZdS )é    N)ÚPath)Ú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   t | tƒ o| dkpd| v S )Nú:memory:zmode=memory)Ú
isinstancer   )Údatabase_name© r   úZ/var/www/html/kck/venv/lib/python3.10/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ÚNAMEr   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Ì   |   ¡ }|r|S |  |¡sd|dkr|  d|  ||¡f ¡ t |tj¡rd|s,td| ƒ}|s2|dkrZzt |¡ W |S  t	yY } z|  d| ¡ t
 d¡ W Y d }~|S d }~ww |  d¡ t
 d¡ |S )Né   ú,Destroying old test database for alias %s...zXType 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: Úyesú/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   Ú	verbosityÚautoclobberÚkeepdbr   ÚconfirmÚer   r   r	   Ú_create_test_db   s8   

ÿÿÿú€ú

z DatabaseCreation._create_test_dbc                 C   sL   | j j}|d }|  |¡r|S tj |d ¡\}}i |¥dd |||¡i¥S )Nr   z{}_{}.{})r   r   r
   r   ÚpathÚsplitextÚformat)r   ÚsuffixÚorig_settings_dictÚsource_database_nameÚrootÚextr   r   r	   Úget_test_db_clone_settings3   s   
z+DatabaseCreation.get_test_db_clone_settingsc              
   C   sü   | j jd }|  |¡d }|  |¡s|t |tj¡rS|rd S |dkr-|  d|  ||¡f ¡ zt 	|¡ W n t
yR } z|  d| ¡ t d¡ W Y d }~nd }~ww z	t ||¡ W d S  t
y{ } z|  d| ¡ t d¡ W Y d }~d S d }~ww d S )Nr   r   r   r   r   z*Got an error cloning the test database: %s)r   r   r0   r
   r   r   r   r   r   r   r   r    r!   ÚshutilÚcopy)r   r+   r"   r$   r-   Útarget_database_namer&   r   r   r	   Ú_clone_test_db<   s2   

ÿ€þ€þðzDatabaseCreation._clone_test_dbc                 C   s$   |r|   |¡st |¡ d S d S d S )N)r
   r   r   )r   r   r"   r   r   r	   Ú_destroy_test_dbT   s   þz!DatabaseCreation._destroy_test_dbc                 C   sH   |   ¡ }| jjd g}|  |¡r| | jj¡ t|ƒS | |¡ 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_signatureY   s   

ÿz"DatabaseCreation.test_db_signatureN)F)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr
   r   r'   r0   r4   r5   r9   r   r   r   r	   r   	   s    


	r   )r   r1   r    Úpathlibr   Ú django.db.backends.base.creationr   r   r   r   r   r	   Ú<module>   s    