o
    بf.                     @   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 ).BaseDatabaseCreationzs
    Encapsulate backend-specific differences pertaining to creation and
    destruction of the test database.
    c                 C   s
   || _ d S N)
connection)selfr	    r   X/var/www/dscantool/venv/lib/python3.10/site-packages/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|tj  d S r   )sysstderrwriteoslinesep)r
   msgr   r   r   log   s   zBaseDatabaseCreation.log   FTc                 C   s   ddl m} |  }|dkr!d}|rd}| d|| ||f  | ||| | j  |tj	| jj
 d< || jjd< |dt|d dd	| jj
d
d |rT|  | 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 existing %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!   s4   



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  t D ]}|jdur+|j|jv r+|jt	j
vr+ |df q 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^    t  D ]&} | jjr,tjj| r,| j	jj
| jjj}| E d H  qd S r   )r   Zsort_dependencies_metaZcan_migrater	   r   Zallow_migrate_modelr#   Z_default_managerusingZorder_bypknameiterator)modelZquerysetZ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_appsr2   r   ZTEST_NON_SERIALIZED_APPSappendr   r   r(   getvalue)r
   r.   loaderZ
app_configr6   outr   r5   r   r&   `   s   

z+BaseDatabaseCreation.serialize_db_to_stringc                 C   s.   t |}tjd|| jjdD ]}|  qdS )zw
        Reload the database with data from a string generated by
        the serialize_db_to_string() method.
        r7   )r0   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| f S 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.
        TESTr   )r	   r$   TEST_DATABASE_PREFIXr   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   sv  |   }| jj||  d}| j }z	| ||| W n ty } zu|r8|W  Y d}~W  d   S | 	d|  |sGt
d| }|sM|dkrz |dkr^| 	d| ||f  |d|  | ||| W n( ty } z| 	d	|  td
 W Y d}~nd}~ww | 	d td W Y d}~nd}~ww W d   |S W d   |S 1 sw   Y  |S )zF
        Internal implementation - create the test db tables.
        )ZdbnamesuffixNz+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: %srC   zTests cancelled.)r   r	   ops
quote_namesql_table_creation_suffixr   rH   rJ   	Exceptionr   inputr   rG   r   exit)	r
   r   r'   r)   r*   Ztest_db_paramsrH   eZconfirmr   r   r   r       sV   	




z$BaseDatabaseCreation._create_test_dbc                 C   sL   | j jd }|dkrd}|rd}| 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
   rK   r   r'   r)   Zsource_database_namer+   r   r   r   clone_test_db   s   
z"BaseDatabaseCreation.clone_test_dbc                 C   s$   | j j}i |dd|d |iS )zX
        Return a modified connection settings dict for the n-th clone of a DB.
        r   z{}_{})r	   r$   format)r
   rK   Zorig_settings_dictr   r   r   get_test_db_clone_settings   s   z/BaseDatabaseCreation.get_test_db_clone_settingsc                 C   s   t d)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.)NotImplementedError)r
   rK   r   r)   r   r   r   rT      s   z#BaseDatabaseCreation._clone_test_dbNc                 C   s   | j   |du r| j jd }n| |d }|dkr.d}|r!d}| d|| ||f  |s6| || |durK|tj| j j	 d< || j jd< dS dS )zv
        Destroy a test database, prompting the user for confirmation if the
        database already exists.
        Nr   r   Z
DestroyingZ
Preservingr   )
r	   r!   r$   rW   r   r   _destroy_test_dbr   r"   r#   )r
   Zold_database_namer   r)   rK   r*   r+   r   r   r   destroy_test_db   s$   

z$BaseDatabaseCreation.destroy_test_dbc                 C   sJ   | j j }|d| j j|  W d   dS 1 sw   Y  dS )zF
        Internal implementation - remove the test db tables.
        zDROP DATABASE %sN)r	   r   rH   rG   rM   rN   )r
   r*   r   rH   r   r   r   rY   
  s
   "z%BaseDatabaseCreation._destroy_test_dbc                 C   s   dS )zQ
        SQL to append to the end of the test table creation statements.
        rD   r   r   r   r   r   rO     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)r   FF)Nr   FN)__name__
__module____qualname____doc__r   propertyr   r   r,   r-   r&   rB   r   r   rJ   r    rU   rW   rT   rZ   rY   rO   r[   r   r   r   r   r      s*    

8		


)


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