B
    ©0H[Ä.  ã               @   sZ   d dl 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
 dZG dd	„ d	ƒZdS )
é    N)ÚStringIO)Úapps)Úsettings)Úserializers)ÚrouterZtest_c               @   s¨   e Zd ZdZdd„ Zedd„ ƒZd(d	d
„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zd)dd„Zd*dd„Zd+dd„Zdd„ Zd,dd„Zd-d d!„Zd"d#„ Zd$d%„ Zd&d'„ ZdS ).ÚBaseDatabaseCreationzs
    Encapsulate backend-specific differences pertaining to creation and
    destruction of the test database.
    c             C   s
   || _ d S )N)Ú
connection)Úselfr   © r
   údC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\db\backends\base\creation.pyÚ__init__   s    zBaseDatabaseCreation.__init__c             C   s   | j jS )zH
        Used to be defined here, now moved to DatabaseWrapper.
        )r   Ú_nodb_connection)r	   r
   r
   r   r      s    z%BaseDatabaseCreation._nodb_connectioné   FTc             C   sÄ   ddl m} |  ¡ }|dkr@d}|r(d}td||  ||¡f ƒ |  |||¡ | j ¡  |tj	| jj
 d< || jjd< |dt|d dƒd	| jj
d
d� |r¦|  ¡ | j_|d| jj
d� | j ¡  |S )z£
        Create a test database, prompting the user for confirmation if the
        database already exists. Return the name of the test database created.
        r   )Úcall_commandr   ZCreatingzUsing existingz %s test database for alias %s...ÚNAMEZmigrateFT)Ú	verbosityÚinteractiveÚdatabaseZ
run_syncdbZcreatecachetable)r   )Zdjango.core.managementr   Ú_get_test_db_nameÚprintÚ_get_database_display_strÚ_create_test_dbr   Úcloser   Ú	DATABASESÚaliasÚsettings_dictÚmaxÚserialize_db_to_stringZ_test_serialized_contentsZensure_connection)r	   r   ÚautoclobberÚ	serializeÚkeepdbr   Útest_database_nameÚactionr
   r
   r   Úcreate_test_db   s0    

z#BaseDatabaseCreation.create_test_dbc             C   s   |d | j jd< dS )z€
        Set this database up to be used in testing as a mirror of a primary
        database whose settings are given.
        r   N)r   r   )r	   Zprimary_settings_dictr
   r
   r   Úset_as_test_mirrorU   s    z'BaseDatabaseCreation.set_as_test_mirrorc                sŒ   ddl m} |ˆjƒ}g ‰ x@t ¡ D ]4}|jdk	r$|j|jkr$|jt	j
kr$ˆ  |df¡ q$W ‡ ‡fdd„}tƒ }tjd|ƒ d|d� | ¡ S )z¤
        Serialize all data in the database into a JSON string.
        Designed only for test runner usage; will not handle large
        amounts of data.
        r   )ÚMigrationLoaderNc              3   s`   xZt  ˆ ¡D ]L} | j ˆj¡rt ˆjj| ¡r| j 	ˆjj¡ 
| jjj¡}| ¡ E d H  qW d S )N)r   Zsort_dependenciesZ_metaZcan_migrater   r   Zallow_migrate_modelr   Z_default_managerÚusingZorder_byÚpkÚnameÚiterator)ÚmodelZqueryset)Úapp_listr	   r
   r   Úget_objectso   s
    z@BaseDatabaseCreation.serialize_db_to_string.<locals>.get_objectsÚjson)ÚindentÚstream)Zdjango.db.migrations.loaderr%   r   r   Zget_app_configsZmodels_moduleÚlabelZmigrated_appsr(   r   ZTEST_NON_SERIALIZED_APPSÚappendr   r   r   Úgetvalue)r	   r%   ÚloaderZ
app_configr,   Úoutr
   )r+   r	   r   r   \   s    

z+BaseDatabaseCreation.serialize_db_to_stringc             C   s2   t |ƒ}x$tjd|| jjd�D ]}| ¡  qW dS )zw
        Reload the database with data from a string generated by
        the serialize_db_to_string() method.
        r-   )r&   N)r   r   Zdeserializer   r   Úsave)r	   ÚdataÚobjr
   r
   r   Údeserialize_db_from_stringz   s    z/BaseDatabaseCreation.deserialize_db_from_stringc             C   s    d| j j|dkrd| ndf S )zR
        Return display string for a database for use in various actions.
        z'%s'%sé   z ('%s')Ú )r   r   )r	   r   Zdatabase_namer
   r
   r   r   ƒ   s    z.BaseDatabaseCreation._get_database_display_strc             C   s0   | j jd d r | j jd d S t| j jd  S )zû
        Internal implementation - return the name of the test DB that will be
        created. Only useful when called from create_test_db() and
        _create_test_db() and when no external munging is done with the 'NAME'
        settings.
        ZTESTr   )r   r   ÚTEST_DATABASE_PREFIX)r	   r
   r
   r   r   Œ   s    z&BaseDatabaseCreation._get_test_db_namec             C   s   |  d| ¡ d S )Nz%CREATE DATABASE %(dbname)s %(suffix)s)Úexecute)r	   ÚcursorÚ
parametersr    r
   r
   r   Ú_execute_create_test_db—   s    z,BaseDatabaseCreation._execute_create_test_dbc       	      C   s<  |   ¡ }| jj |¡|  ¡ dœ}| j ¡ ��}y|  |||¡ W nì tk
�r, } zÌ|r\|S t	j
 d| ¡ |s|td| ƒ}|sŠ|dk�r
y>|dkrªtd|  ||¡f ƒ | d| ¡ |  |||¡ W n> tk
�r } zt	j
 d| ¡ t	 d	¡ W d
d
}~X Y nX ntdƒ t	 d¡ W d
d
}~X Y nX W d
Q R X |S )zF
        Internal implementation - create the test db tables.
        )ZdbnameÚsuffixz,Got an error creating the test database: %s
zXType 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: Úyesr   z,Destroying old test database for alias %s...zDROP DATABASE %(dbname)sz.Got an error recreating the test database: %s
r9   NzTests cancelled.)r   r   ÚopsÚ
quote_nameÚsql_table_creation_suffixr   r=   r?   Ú	ExceptionÚsysÚstderrÚwriteÚinputr   r   r<   Úexit)	r	   r   r   r    r!   Ztest_db_paramsr=   ÚeZconfirmr
   r
   r   r   š   s:    

&z$BaseDatabaseCreation._create_test_dbc             C   sJ   | j jd }|dkr8d}|r d}td||  ||¡f ƒ |  |||¡ dS )z(
        Clone a test database.
        r   r   zCloning test databasezUsing existing clonez%s for alias %s...N)r   r   r   r   Ú_clone_test_db)r	   r@   r   r   r    Zsource_database_namer"   r
   r
   r   Úclone_test_dbÅ   s    z"BaseDatabaseCreation.clone_test_dbc             C   s(   | j j}| ¡ }d |d |¡|d< |S )zX
        Return a modified connection settings dict for the n-th clone of a DB.
        z{}_{}r   )r   r   ÚcopyÚformat)r	   r@   Zorig_settings_dictZnew_settings_dictr
   r
   r   Úget_test_db_clone_settingsØ   s    z/BaseDatabaseCreation.get_test_db_clone_settingsc             C   s   t dƒ‚dS )zI
        Internal implementation - duplicate the test db tables.
        znThe database backend doesn't support cloning databases. Disable the option to run tests in parallel processes.N)ÚNotImplementedError)r	   r@   r   r    r
   r
   r   rL   ä   s    z#BaseDatabaseCreation._clone_test_dbNc             C   s”   | j  ¡  |dkr | j jd }n|  |¡d }|dkrZd}|rBd}td||  ||¡f ƒ |sj|  ||¡ |dk	r�|tj| j j	 d< || j jd< dS )zv
        Destroy a test database, prompting the user for confirmation if the
        database already exists.
        Nr   r   Z
DestroyingZ
Preservingz %s test database for alias %s...)
r   r   r   rP   r   r   Ú_destroy_test_dbr   r   r   )r	   Zold_database_namer   r    r@   r!   r"   r
   r
   r   Údestroy_test_dbì   s     
z$BaseDatabaseCreation.destroy_test_dbc          	   C   s4   | j j ¡ �}| d| j j |¡ ¡ W dQ R X dS )zF
        Internal implementation - remove the test db tables.
        zDROP DATABASE %sN)r   r   r=   r<   rB   rC   )r	   r!   r   r=   r
   r
   r   rR   
  s    z%BaseDatabaseCreation._destroy_test_dbc             C   s   dS )zQ
        SQL to append to the end of the test table creation statements.
        r:   r
   )r	   r
   r
   r   rD     s    z.BaseDatabaseCreation.sql_table_creation_suffixc             C   s$   | j j}|d |d |d |  ¡ fS )zÇ
        Return a tuple with elements of self.connection.settings_dict (a
        DATABASES setting value) that uniquely identify a database
        accordingly to the RDBMS particularities.
        ZHOSTZPORTZENGINE)r   r   r   )r	   r   r
   r
   r   Útest_db_signature  s
    z&BaseDatabaseCreation.test_db_signature)r   FTF)F)F)r   FF)F)Nr   FN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr   r#   r$   r   r8   r   r   r?   r   rM   rP   rL   rS   rR   rD   rT   r
   r
   r
   r   r      s$   
8		

+


r   )rF   Úior   Zdjango.appsr   Zdjango.confr   Zdjango.corer   Z	django.dbr   r;   r   r
   r
   r
   r   Ú<module>   s   