o
    uvXj�.  ã                   @   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)ÚrouterÚ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   ú]/var/www/html/myproject/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   ÚCreatingzUsing existingú %s test database for alias %s...ÚNAMEÚmigrateFT)Ú	verbosityÚinteractiveÚdatabaseÚ
run_syncdbÚcreatecachetable)r    )Údjango.core.managementr   Ú_get_test_db_namer   Ú_get_database_display_strÚ_create_test_dbr
   Úcloser   Ú	DATABASESÚaliasÚsettings_dictÚmaxÚserialize_db_to_stringÚ_test_serialized_contentsÚ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   Ú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   Úsort_dependenciesÚ_metaÚcan_migrater
   r   Úallow_migrate_modelr)   Ú_default_managerÚusingÚorder_byÚpkÚnameÚiterator)ÚmodelÚqueryset©Úapp_listr   r   r   Úget_objectss   s   €ÿ€üz@BaseDatabaseCreation.serialize_db_to_string.<locals>.get_objectsÚjson)ÚindentÚstream)Údjango.db.migrations.loaderr7   r
   r   Úget_app_configsÚmodels_moduleÚlabelÚmigrated_appsr@   r   ÚTEST_NON_SERIALIZED_APPSÚappendr   r   r0   Úgetvalue)r   r7   ÚloaderÚ
app_configrF   Úoutr   rD   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.
        rG   )r=   N)r   r   Ú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   Ú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Ú
parametersr1   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rŒz |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 s´w   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: %srZ   zTests cancelled.)r$   r
   ÚopsÚ
quote_nameÚsql_table_creation_suffixr   r`   rb   Ú	Exceptionr   Úinputr%   r_   r   Úexit)	r   r   r/   r1   r2   Útest_db_paramsr`   ÚeÚ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   rd   r   r/   r1   Úsource_database_namer3   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   rd   Ú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   rd   r   r1   r   r   r   ro   ä   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
   r'   r*   rt   r   r%   Ú_destroy_test_dbr   r(   r)   )r   Úold_database_namer   r1   rd   r2   r3   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   r`   r_   rf   rg   )r   r2   r   r`   r   r   r   rx   
  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   rh     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
   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   r4   r6   r,   rY   r%   r$   rb   r&   rq   rt   ro   rz   rx   rh   r~   r   r   r   r   r      s*    

8		


)


r   )r   r   Úior   Údjango.appsr   Údjango.confr   Údjango.corer   Ú	django.dbr   r^   r   r   r   r   r   Ú<module>   s    