o
    {,h=                     @   sz   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 dlmZ d dlmZ d	ZG d
d dZdS )    N)StringIO)apps)settings)serializers)router)atomic)import_stringtest_c                   @   s   e Zd ZdZdd Z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d0ddZdd Zd/dd Z	!d1d"d#Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd!S )2BaseDatabaseCreationzs
    Encapsulate backend-specific differences pertaining to creation and
    destruction of the test database.
    c                 C   s
   || _ d S N)
connection)selfr    r   V/var/www/html/rh/venv/lib/python3.10/site-packages/django/db/backends/base/creation.py__init__      
zBaseDatabaseCreation.__init__c                 C   s
   | j  S r   )r   _nodb_cursorr   r   r   r   r      r   z!BaseDatabaseCreation._nodb_cursorc                 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   sH  ddl m} |  }|dkr!d}|rd}| d|| ||f  | ||| | j  |tj	| jj
 d< || jjd< z6| jjd d	 d
u rTtj}dd t D t_|dt|d dd
| jj
dd W | jjd d	 d
u rr|t_n| jjd d	 d
u r|t_w |r|  | j_|d| jj
d | j  tjddkr|   |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   CreatingzUsing existing %s test database for alias %s...NAMETESTMIGRATEFc                 S   s   i | ]}|j d qS r   )label).0appr   r   r   
<dictcomp>H   s    z7BaseDatabaseCreation.create_test_db.<locals>.<dictcomp>migrateT)	verbosityinteractivedatabase
run_syncdbcreatecachetable)r)   RUNNING_DJANGOS_TEST_SUITEtrue)django.core.managementr   _get_test_db_namer   _get_database_display_str_create_test_dbr   closer   	DATABASESaliassettings_dictMIGRATION_MODULESr   get_app_configsmaxserialize_db_to_string_test_serialized_contentsensure_connectionr   environget mark_expected_failures_and_skips)	r   r'   autoclobber	serializekeepdbr   test_database_nameactionold_migration_modulesr   r   r   create_test_db    sT   


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   r5   )r   primary_settings_dictr   r   r   set_as_test_mirrorj   s   z'BaseDatabaseCreation.set_as_test_mirrorc                    s.    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.
        c                  3   s    ddl m}  |  j}t D ]G}|jd urW|j|jv rW|jt	j
vrW| D ]/}|j jrVt jj|rV|j jj|jjj}|jrKdnd }|j|dE d H  q'qd S )Nr   )MigrationLoaderi  )
chunk_size)django.db.migrations.loaderrH   r   r   r7   models_moduler"   migrated_appsnamer   TEST_NON_SERIALIZED_APPS
get_models_metacan_migrater   allow_migrate_modelr4   _base_managerusingorder_bypk_prefetch_related_lookupsiterator)rH   loader
app_configmodelquerysetrI   r   r   r   get_objectsy   s.   

z@BaseDatabaseCreation.serialize_db_to_string.<locals>.get_objectsjsonN)indentstream)r   r   r@   getvalue)r   r]   outr   r   r   r9   q   s   z+BaseDatabaseCreation.serialize_db_to_stringc              	   C   s   t |}t }t| jjd? | j " tjd|| jjdD ]}|  |	|j
jjj qW d   n1 s9w   Y  | jj|d W d   dS 1 sPw   Y  dS )zw
        Reload the database with data from a string generated by
        the serialize_db_to_string() method.
        )rT   r^   N)table_names)r   setr   r   r4   constraint_checks_disabledr   deserializesaveaddobject	__class__rP   db_tablecheck_constraints)r   datarc   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   r4   )r   r'   database_namer   r   r   r0      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.
        r    r   )r   r5   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
parametersrA   r   r   r   _execute_create_test_db   s   z,BaseDatabaseCreation._execute_create_test_dbc           	      C   st  |   }| jj||  d}|  }z	| ||| W n ty } zu|r7|W  Y d}~W  d   S | d|  |sFt	d| }|sL|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.
        )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: %srp   zTests cancelled.)r/   r   ops
quote_namesql_table_creation_suffixr   rw   	Exceptionr   inputr0   rt   r   exit)	r   r'   r?   rA   rB   test_db_paramsru   econfirmr   r   r   r1      s`   
	


#
##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   r5   r   r0   _clone_test_db)r   ry   r'   r?   rA   source_database_namerC   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   r5   format)r   ry   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   ry   r'   rA   r   r   r   r     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   
Destroying
Preservingr   )
r   r2   r5   r   r   r0   _destroy_test_dbr   r3   r4   )r   old_database_namer'   rA   ry   rB   rC   r   r   r   destroy_test_db  s*   


z$BaseDatabaseCreation.destroy_test_dbc                 C   sF   |   }|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   rt   r   r{   r|   )r   rB   r'   ru   r   r   r   r   >  s
   
"z%BaseDatabaseCreation._destroy_test_dbc                 C   s   ddl m}m} | jjjD ]'}|d\}}}|dd }|tj	v r4t
|}t||}	t||||	 q| jjj D ]0\}
}|D ])}|d\}}}|dd }|tj	v rkt
|}t||}	t||||
|	 qBq<dS )z
        Mark tests in Django's test suite which are expected failures on this
        database and test which should be skipped on this database.
        r   )expectedFailureskip.N)unittestr   r   r   featuresdjango_test_expected_failures
rpartitionsplitr   INSTALLED_APPSr   getattrsetattrdjango_test_skipsitems)r   r   r   	test_nametest_case_name_test_method_nametest_app	test_casetest_methodreasontestsr   r   r   r>   K  s(   



z5BaseDatabaseCreation.mark_expected_failures_and_skipsc                 C   s   dS )zQ
        SQL to append to the end of the test table creation statements.
        rq   r   r   r   r   r   r}   e  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.
        HOSTPORTENGINE)r   r5   r/   )r   r5   r   r   r   test_db_signaturek  s   z&BaseDatabaseCreation.test_db_signaturec                 C   s*   |  t|}| jj| | j  d S r   )r   strr   r5   updater2   )r   
_worker_idr5   r   r   r   setup_worker_connectiony  s   z,BaseDatabaseCreation.setup_worker_connection)r   FTF)F)r   FF)Nr   FN)__name__
__module____qualname____doc__r   r   r   rE   rG   r9   ro   r0   r/   rw   r1   r   r   r   r   r   r>   r}   r   r   r   r   r   r   r
      s0    
J#	


/


#r
   )r   r   ior   django.appsr   django.confr   django.corer   	django.dbr   django.db.transactionr   django.utils.module_loadingr   rs   r
   r   r   r   r   <module>   s    