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   ú`/var/www/html/premium_crap/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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: %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    