3
W­E^�.  ã               @   sb   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	 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„ 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 )0ÚBaseDatabaseCreationzs
    Encapsulate backend-specific differences pertaining to creation and
    destruction of the test database.
    c             C   s
   || _ d S )N)Ú
connection)Úselfr   © r
   úB/tmp/pip-build-78s3knkw/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_connectionc             C   s   t jj|tj ƒ d S )N)ÚsysÚstderrÚwriteÚosÚlinesep)r	   Úmsgr
   r
   r   Úlog   s    zBaseDatabaseCreation.logé   FTc             C   sÆ   ddl m} | jƒ }|dkrBd}|r(d}| jd|| j||ƒf ƒ | j|||ƒ | jjƒ  |tj	| jj
 d< || jjd< |dt|d dƒd	| jj
d
d� |r¨| jƒ | j_|d| jj
d� | j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_namer   Ú_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_mirrorY   s    z'BaseDatabaseCreation.set_as_test_mirrorc                sŒ   ddl m} |ˆjƒ}g ‰ x@tjƒ D ]4}|jdk	r$|j|jkr$|jt	j
kr$ˆ j|dfƒ q$W ‡ ‡fdd„}tƒ }tjd|ƒ d|d� |jƒ 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 jˆ ƒD ]L} | jjˆjƒrtjˆjj| ƒr| jj	ˆj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_objectss   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_configr2   Úoutr
   )r1   r	   r   r#   `   s    

z+BaseDatabaseCreation.serialize_db_to_stringc             C   s2   t |ƒ}x$tjd|| jjd�D ]}|jƒ  qW dS )zw
        Reload the database with data from a string generated by
        the serialize_db_to_string() method.
        r3   )r,   N)r   r   Zdeserializer   r    Úsave)r	   ÚdataÚobjr
   r
   r   Údeserialize_db_from_string~   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   |j 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jj|ƒ| jƒ dœ}| jjƒ ��}y| j|||ƒ W nì tk
�r, } zÎ|r\|S | j	d| ƒ |szt
d| ƒ}|sˆ|dk�ry@|dkrª| j	d| j||ƒf ƒ |jd| ƒ | j|||ƒ W n< tk
�r } z| j	d| ƒ tjd	ƒ W Y d
d
}~X nX n| j	dƒ tjdƒ W Y d
d
}~X 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: %szXType '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: %sr?   NzTests cancelled.)r   r   ÚopsÚ
quote_nameÚsql_table_creation_suffixr   rC   rE   Ú	Exceptionr   Úinputr   rB   r   Úexit)	r	   r   r$   r&   r'   Ztest_db_paramsrC   ÚeZconfirmr
   r
   r   r   ž   s6    
&z$BaseDatabaseCreation._create_test_dbc             C   sL   | j jd }|dkr:d}|r d}| jd|| j||ƒf ƒ | j|||ƒ 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	   rF   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j|d |ƒi–S )zX
        Return a modified connection settings dict for the n-th clone of a DB.
        r   z{}_{})r   r!   Úformat)r	   rF   Zorig_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	   rF   r   r&   r
   r
   r   rO   ä   s    z#BaseDatabaseCreation._clone_test_dbNc             C   s–   | j jƒ  |dkr | j jd }n| j|ƒd }|dkr\d}|rBd}| jd|| j||ƒf ƒ |sl| j||ƒ |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!   rR   r   r   Ú_destroy_test_dbr   r   r    )r	   Zold_database_namer   r&   rF   r'   r(   r
   r
   r   Údestroy_test_dbì   s     
z$BaseDatabaseCreation.destroy_test_dbc             C   s4   | j jjƒ �}|jd| j jj|ƒ ƒ W dQ R X dS )zF
        Internal implementation - remove the test db tables.
        zDROP DATABASE %sN)r   r   rC   rB   rH   rI   )r	   r'   r   rC   r
   r
   r   rT   
  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   rJ     s    z.BaseDatabaseCreation.sql_table_creation_suffixc             C   s$   | j j}|d |d |d | jƒ 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*   r#   r>   r   r   rE   r   rP   rR   rO   rU   rT   rJ   rV   r
   r
   r
   r   r      s&   
8		

)



r   )r   r   Úior   Zdjango.appsr   Zdjango.confr   Zdjango.corer   Z	django.dbr   rA   r   r
   r
   r
   r   Ú<module>   s   