
    h3                     t    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Z G d	 d
      Zy)    N)StringIO)apps)settings)serializers)router)atomictest_c                       e Zd ZdZd Zd Zd ZddZd Zd Z	d Z
d	 Zd
 ZddZddZddZd ZddZddZd Zd Zd Zy)BaseDatabaseCreationzs
    Encapsulate backend-specific differences pertaining to creation and
    destruction of the test database.
    c                     || _         y N)
connection)selfr   s     c/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/django/db/backends/base/creation.py__init__zBaseDatabaseCreation.__init__   s	    $    c                 6    | j                   j                         S r   )r   _nodb_cursorr   s    r   r   z!BaseDatabaseCreation._nodb_cursor   s    ++--r   c                 d    t         j                  j                  |t        j                  z          y r   )sysstderrwriteoslinesep)r   msgs     r   logzBaseDatabaseCreation.log   s    

rzz)*r   c                 
   ddl m} | j                         }|dk\  r-d}|rd}| j                  |d| j	                  ||      d       | j                  |||       | j                  j                          |t        j                  | j                  j                     d<   || j                  j                  d<   	 | j                  j                  d	   d
   du rFt        j                  }t        j                         D 	ci c]  }	|	j                  d c}	t        _         |dt!        |dz
  d      d| j                  j                  d       | j                  j                  d	   d
   du rt        _        	 |r| j#                         | j                  _         |d| j                  j                         | j                  j'                          |S c c}	w # | j                  j                  d	   d
   du rt        _        w w xY w)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_command   CreatingzUsing existing test database for alias ...NAMETESTMIGRATEFNmigrateT)	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MIGRATION_MODULESr   get_app_configslabelmaxserialize_db_to_string_test_serialized_contentsensure_connection)
r   r(   autoclobber	serializekeepdbr   test_database_nameactionold_migration_modulesapps
             r   create_test_dbz#BaseDatabaseCreation.create_test_db   s    	8!335>F)HH..y:LM  	YV<<N4??001&90B%%f-	C,,V4Y?5H(0(B(B%  $335. IItO.* i!mQ/!.. ,,V4Y?5H-B* 8<8S8S8UDOO5'$//2G2GH 	))+!!=. ,,V4Y?5H-B* Is   :AG >G:G G ,Hc                 <    |d   | j                   j                  d<   y)z
        Set this database up to be used in testing as a mirror of a primary
        database whose settings are given.
        r$   N)r   r4   )r   primary_settings_dicts     r   set_as_test_mirrorz'BaseDatabaseCreation.set_as_test_mirrora   s    
 1Ff0M%%f-r   c                 ~      fd}t               }t        j                  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.
        c               3     K   ddl m}   | j                        }t        j                         D ]   }|j
                  |j                  |j                  v s*|j                  t        j                  vsG|j                         D ]  }|j                  j                  j                        s)t        j                  j                  j                   |      sT|j"                  j%                  j                  j                         j'                  |j                  j(                  j                        }|j+                         E d {     # y 7 
w)Nr   )MigrationLoader)django.db.migrations.loaderrI   r   r   r6   models_moduler7   migrated_appsnamer   TEST_NON_SERIALIZED_APPS
get_models_metacan_migrater   allow_migrate_modelr3   _default_managerusingorder_bypkiterator)rI   loader
app_configmodelquerysetr   s        r   get_objectsz@BaseDatabaseCreation.serialize_db_to_string.<locals>.get_objectso   s    C$T__5F"224 ;
,,8$$(<(<<OO8+L+LL!+!6!6!8 ;!KK33DOOD"66t7L7LeT','='='C'C $ 5 5(&hu{{~~':':; % (0'8'8':::;; ;s0   =EEE78E0*EA/E
EEjsonN)indentstream)r   r   r=   getvalue)r   r\   outs   `  r   r9   z+BaseDatabaseCreation.serialize_db_to_stringh   s3    	;& jfkmDM||~r   c                 $   t        |      }t               }t        | j                  j                        5  | j                  j                         5  t        j                  d|| j                  j                        D ]K  }|j                          |j                  |j                  j                  j                  j                         M 	 ddd       | j                  j                  |       ddd       y# 1 sw Y   .xY w# 1 sw Y   yxY w)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   r3   constraint_checks_disabledr   deserializesaveaddobject	__class__rP   db_tablecheck_constraints)r   datarc   objs       r   deserialize_db_from_stringz/BaseDatabaseCreation.deserialize_db_from_string   s    
 ~e$////0 		G ;;= I&2264tG\G\] ICHHJOOCJJ$8$8$>$>$G$GHII OO--+-F		G 		GI I		G 		Gs$   DA;C:$D:D	?DDc                 R    d| j                   j                  d|dk\  rd|z  S dS )zR
        Return display string for a database for use in various actions.
        '   z ('%s') )r   r3   )r   r(   database_names      r   r/   z.BaseDatabaseCreation._get_database_display_str   s<    
 OO!!+4>Y&A
 	
?AA
 	
r   c                     | j                   j                  d   d   r| j                   j                  d   d   S t        | j                   j                  d   z   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   r4   TEST_DATABASE_PREFIXr   s    r   r.   z&BaseDatabaseCreation._get_test_db_name   sO     ??((08??008@@#doo&C&CF&KKKr   c                 ,    |j                  d|z         y )Nz%CREATE DATABASE %(dbname)s %(suffix)s)execute)r   cursor
parametersr>   s       r   _execute_create_test_dbz,BaseDatabaseCreation._execute_create_test_db   s    >KLr   c           	          | j                         }| j                  j                  j                  |      | j	                         d}| j                         5 }	 | j                  |||       ddd       |S # t        $ r }|r|cY d}~cddd       S | j                  d|z         |st        d|z        }|sdk(  r	 |dk\  r%| j                  d| j                  ||      d       |j                  d	|z         | j                  |||       ne# t        $ r3}| j                  d
|z         t        j                  d       Y d}~n.d}~ww xY w| j                  d       t        j                  d       Y d}~d}~ww xY w# 1 sw Y   |S xY w)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 r#   zDROP DATABASE %(dbname)sz-Got an error recreating the test database: %srr   zTests cancelled.)r.   r   ops
quote_namesql_table_creation_suffixr   r{   	Exceptionr   inputr/   rx   r   exit)	r   r(   r<   r>   r?   test_db_paramsry   econfirms	            r   r0   z$BaseDatabaseCreation._create_test_db   s    "335oo))445GH446

   	 F ,,V^VL	 : "!5    --	  	  FJK"#=?QRSG 'U"2	$$> HH $ > >yJ\ ]&  'AN'RS44V^VT$ $!PST!TU$ HH/0HHQK1 	 : "!sm   FA66
F  E;F F+E;>ADE;	E)EE;E)E;5F;F  FFc                     | j                   j                  d   }|dk\  r-d}|rd}| j                  |d| j                  ||      d       | j	                  |||       y)z(
        Clone a test database.
        r$   r    zCloning test databasezUsing existing clonez for alias r#   N)r   r4   r   r/   _clone_test_db)r   r~   r(   r<   r>   source_database_namer@   s          r   clone_test_dbz"BaseDatabaseCreation.clone_test_db   sf      $<<VD>,F/HH..y:NO  	FIv6r   c                 d    | j                   j                  }i |ddj                  |d   |      iS )zX
        Return a modified connection settings dict for the n-th clone of a DB.
        r$   z{}_{})r   r4   format)r   r~   orig_settings_dicts      r   get_test_db_clone_settingsz/BaseDatabaseCreation.get_test_db_clone_settings   s:     "__::a$afgnn=OPV=WY_.`aar   c                     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   r~   r(   r>   s       r   r   z#BaseDatabaseCreation._clone_test_db   s     "EF 	Fr   Nc                    | j                   j                          || j                   j                  d   }n| j                  |      d   }|dk\  r-d}|rd}| j	                  |d| j                  ||      d       |s| j                  ||       |D|t        j                  | j                   j                     d<   || j                   j                  d<   yy)zv
        Destroy a test database, prompting the user for confirmation if the
        database already exists.
        Nr$   r    
Destroying
Preservingr"   r#   )
r   r1   r4   r   r   r/   _destroy_test_dbr   r2   r3   )r   old_database_namer(   r>   r~   r?   r@   s          r   destroy_test_dbz$BaseDatabaseCreation.destroy_test_db   s    
 	>!%!>!>v!F!%!@!@!H!P>!F%HH..y:LM  !!"4i@ (@QHt445f=4EDOO))&1 )r   c                     | j                         5 }|j                  d| j                  j                  j	                  |      z         ddd       y# 1 sw Y   yxY w)zF
        Internal implementation - remove the test db tables.
        zDROP DATABASE %sN)r   rx   r   r   r   )r   r?   r(   ry   s       r   r   z%BaseDatabaseCreation._destroy_test_db  sZ       	QFNN-!__00;;<NOP Q	Q 	Q 	Qs   8AAc                      y)zQ
        SQL to append to the end of the test table creation statements.
        rs    r   s    r   r   z.BaseDatabaseCreation.sql_table_creation_suffix(  s     r   c                 h    | 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.
        HOSTPORTENGINE)r   r4   r.   )r   r4   s     r   test_db_signaturez&BaseDatabaseCreation.test_db_signature.  sA     55&!&!(#""$	
 	
r   )r    FTF)F)r    FF)Nr    FN)__name__
__module____qualname____doc__r   r   r   rC   rF   r9   ro   r/   r.   r{   r0   r   r   r   r   r   r   r   r   r   r   r   r      sl    %.+A"FN<G&
	LM'"R7&bFF<
Q
r   r   )r   r   ior   django.appsr   django.confr   django.corer   	django.dbr   django.db.transactionr   rv   r   r   r   r   <module>r      s1    	 
     #  (  j
 j
r   