
    +h&/                     
   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dlmZ ddlmZ  ej                  e      ZdZ G d de      Zd Zd Zd Zd Zd Zej4                  d        Zd Zej4                  d        Zej4                  d        Zd Zej4                  d        Z ej4                  d        Z!ej4                  d        Z"ej4                  d        Z#ej4                  d        Z$ej4                  d        Z%ej4                  d        Z&d  Z'ej4                  d!        Z(ej4                  d"        Z)ej4                  d#        Z*ej4                  d$        Z+ej4                  d%        Z,y)&    N   )config)engines)util   )exc)inspect)url)ddlschema)compatc                   .    e Zd Zd Zed        Zd Zd Zy)registerc                     i | _         y Nfns)selfs    O/var/www/html/venv/lib/python3.12/site-packages/sqlalchemy/testing/provision.py__init__zregister.__init__   s	        c                 @     t               j                  d      |      S N*)r   for_db)clsfns     r   initzregister.init   s    %xz  %b))r   c                       fd}|S )Nc                 4    D ]  }| j                   |<    S r   r   )r   dbnamedbnamesr   s     r   decoratez!register.for_db.<locals>.decorate   s%    ! &#% &Kr    )r   r#   r$   s   `` r   r   zregister.for_db   s    	
 r   c                 \   t        |t        j                        rt        j                  |      }n3t        |t        j
                        r|}n|j                  j                  }|j                         }|| j                  v r | j                  |   |g| S  | j                  d   |g| S r   )

isinstancer   string_typessa_urlmake_urlURLdbr
   get_backend_namer   )r   cfgargr
   backends        r   __call__zregister.__call__$   s    c6..///#&CVZZ(C&&**C&&(dhh$488G$S/3// 488C=+s++r   N)__name__
__module____qualname__r   classmethodr   r   r1   r%   r   r   r   r      s%     * *,r   r   c                     t               D ]D  }t        j                  d| |j                  j                         t        ||j                  |        F y )NzCREATE database %s, URI %r)_configs_for_db_operationloginfor,   r
   	create_dbfollower_identr.   s     r   create_follower_dbr=   2   s=    (* /-~svvzzJ#svv~./r   c                    t        j                  |       j                         }|j                          |rt	        | |      } i }t        | |       d|d<   t        j                  | |      }t        | ||       |j                         j                          t        j                  j                  ||||      }|r|t        _        |rt        ||       |S )Nglobalscope)r)   r*   get_dialectload_provisioningfollower_url_from_mainupdate_db_optsr   testing_enginepost_configure_engineconnectcloser   Configr   identconfigure_follower)db_urloptionsfile_configr<   dialectdb_optsengr.   s           r   setup_configrR   8   s     oof%113G'?G67#GG

 
 
1C&#~6KKM
--
 
 gw
DC %3/Jr   c                     t               D ]D  }t        j                  d| |j                  j                         t        ||j                  |        F y )NzDROP database %s, URI %r)r7   r8   r9   r,   r
   drop_dbr;   s     r   drop_follower_dbrU   T   s=    (* -+^SVVZZHSVV^,-r   c              #   H  K   t               }t        j                  t               }| D cg c]  }t        j                  |       c}D cg c]  }||j                         f }}|D ]-  \  }}||j                     j                  |j                         / i }|D ]x  \  }}|j                  }	|j                          |	|vr#t        |      j                  ||	         x||	<   }
n||	   }
t        ||
      D ]  }||v r|j                  |       |  z yc c}w c c}w w)a&  Generate a set of URLs to test given configured URLs plus additional
    driver names.

    Given::

        --dburi postgresql://db1          --dburi postgresql://db2          --dburi postgresql://db2          --dbdriver=psycopg2 --dbdriver=asyncpg?async_fallback=true

    Noting that the default postgresql driver is psycopg2,  the output
    would be::

        postgresql+psycopg2://db1
        postgresql+asyncpg://db1
        postgresql+psycopg2://db2
        postgresql+psycopg2://db3

    That is, for the driver in a --dburi, we want to keep that and use that
    driver for each URL it's part of .   For a driver that is only
    in --dbdrivers, we want to use it just once for one of the URLs.
    for a driver that is both coming from --dburi as well as --dbdrivers,
    we want to keep it in that dburi.

    Driver specific query options can be specified by added them to the
    driver name. For example, to enable the async fallback option for
    asyncpg::

        --dburi postgresql://db1          --dbdriver=asyncpg?async_fallback=true

    N)setcollectionsdefaultdictr)   r*   rA   nameadddriverrB   
difference_generate_driver_urls)db_urlsextra_driversurls!backend_to_driver_we_already_haverL   url_objurls_plus_dialectsrO   backend_to_driver_we_needr0   extra_per_backend
driver_urls               r   generate_db_urlsrh   Z   sJ    B 5D(3(?(?(D% ?FFF/F 
'%%'( 
 / L)',,7;;GNNKL !#. ,,!!#33EHFj:7CDE%g.1B !:' B/9JK 	JT!HHZ 		 Gs   (D"DD"D$B>D"c              #   :  K   | j                         }|j                  |       t        | |d      } t        |        t	        |      D ]N  }d|v r|j                  dd      \  }}n|}d }t        | ||      }|s1|j                  |       t        |       P y w)N ?r   )get_driver_namediscardgenerate_driver_urlstrlistsplitremove)r
   r`   main_driverdrvdriver_only	query_strnew_urls          r   r^   r^      s     %%'K+&
c;
3C
c(NM" #:%(YYsA%6"K KI%c;	B  %g,s   A7B:!Bc                     | j                         }| j                  |d|      }|r|j                  |      }	 |j                          |S # t        j
                  $ r Y y w xY w)N+)
drivername)r-   rW   update_query_stringrA   r   NoSuchModuleError)r
   r\   rv   r0   rw   s        r   rn   rn      sr    ""$Ggg%v.  G --i8     s   A A$#A$c               #   "  K   t               } t        j                  j                         D ]  }|j                  j                           t        j                  j                         D ]f  }|j                  j                  }|j                         }||j                  |j                  |j                  f}|| vsR| | j                  |       h t        j                  j                         D ]  }|j                  j                           y wr   )rW   r   rI   all_configsr,   disposer
   r-   usernamehostdatabaser[   )hostsr.   r
   r0   	host_confs        r   r7   r7      s     EE}}((*  }}((* !ffjj&&(cllCHHcllC	E!IIIi ! }}((* s   B7D:ADc                      y r   r%   r.   rQ   s     r   "drop_all_schema_objects_pre_tablesr          r   c                      y r   r%   r   s     r   #drop_all_schema_objects_post_tablesr      r   r   c                 p   t        | |       t        |      }	 |j                         }|j                         5 }|D ]L  }|j	                  t        j                  t        j                  |t        j                                            N 	 d d d        t        j                  j                  j                  |       r	 |j                  d      }|j                         5 }|D ]N  }|j	                  t        j                  t        j                  |t        j                         d                   P 	 d d d        t        j                   ||       t        j                  j                  j                  |       rDt        j                   ||| j"                         t        j                   ||| j$                         t'        | |       t        j                  j(                  j                  |       r|j                         5 }|j+                         D ]9  }|j	                  t        j,                  t        j.                  |                   ; t        j                  j                  j                  |       rm| j"                  | j$                  fD ]R  }|j+                  |      D ];  }|j	                  t        j,                  t        j.                  ||                   = T d d d        y y # 1 sw Y   hxY w# t        $ r Y ww xY w# 1 sw Y   xY w# t        $ r Y w xY w# 1 sw Y   y xY w)Ntest_schemar   )r   r	   get_view_namesbeginexecuter   	_DropViewr   TableMetaDataNotImplementedErrorr   requirementsschemasenabled_for_configr   drop_all_tablesr   test_schema_2r   	sequencesget_sequence_namesDropSequenceSequence)r.   rQ   	inspector
view_namesconnvnameseqschema_names           r   drop_all_schema_objectsr      s   &sC0I	--/
 YY[ 	D# MM&,,ufoo6G"HI	 ""55c:	"111GJ  
' 	ELL"LL % & 1'4	
 	i(""55c:S)COODS)C4E4EF'S1$$77<YY[ 	D 335 ES--fooc.BCDE""**==cB$'OOS5F5F#G K(;;*  <    ,, &K H		 	 =?	 	  
 
 # 		.	 	sI   K? AK2>L  ALC#L,2K<?	LLL	L)(L),L5c                 2    t        d|j                        )zDynamically create a database for testing.

    Used when a test run will employ multiple processes, e.g., when run
    via `tox` or `pytest -n4`.
    z no DB creation routine for cfg: r   r
   r.   rQ   rJ   s      r   r:   r:     s     039 r   c                 2    t        d|j                        )z8Drop a database that we dynamically created for testing.zno DB drop routine for cfg: r   r   s      r   rT   rT   '  s     #''K
LLr   c                      y)zCSet database options (db_opts) for a test database that we created.Nr%   )rL   rP   s     r   rD   rD   -       	r   c                      y)zPerform extra steps after configuring an engine for testing.

    (For the internal dialects, currently only used by sqlite, oracle)
    Nr%   )r
   enginer<   s      r   rF   rF   3  s     	r   c                 P    t        j                  |       } | j                  |      S )zCreate a connection URL for a dynamically-created test database.

    :param url: the connection URL specified when the test run was invoked
    :param ident: the pytest-xdist "worker identifier" to be used as the
                  database name
    )r   )r)   r*   rW   r
   rJ   s     r   rC   rC   <  s#     //#
C77E7""r   c                      y)z@Create dialect-specific config settings for a follower database.Nr%   )r.   rJ   s     r   rK   rK   H  r   r   c                      y)a[  Remove databases that were created during the test process, after the
    process has ended.

    This is an optional step that is invoked for certain backends that do not
    reliably release locks on the database as long as a process is still in
    use. For the internal dialects, this is currently only necessary for
    mssql and oracle.
    Nr%   r   s     r   run_reap_dbsr   N  s     	r   c                    t         j                  d       t        j                  t              }t        j                  t              }i }t        |       5 }|D ]  }|j                         }|j                  d      \  }}t        j                  |      }||vr&|j                         ||<   ||   j                          |j                         |j                  f}	||	   j                  |       ||	   j                  |        	 d d d        |D ]$  }	t        ||	         d   }
||	   }t!        |
|       & y # 1 sw Y   3xY w)NzReaping databases... r   )r8   r9   rX   rY   rW   openstriprq   r)   r*   rA   rB   r-   r   r[   rp   r   )idents_filera   identsdialectsfile_linedb_namerL   rc   url_keyr
   rJ   s               r   reap_dbsr   [  s*   HH#$""3'D$$S)FH	k	 
)e 		)D::<D"jjoOGVoof-Gh&$+$7$7$9!!335//17<<@GMf%7O(		)
)  !4=!!$wS% !
) 
)s   B/D77E c                 2    t        d|j                        )aO  Specify keyword arguments for creating a temporary Table.

    Dialect-specific implementations of this method will return the
    kwargs that are passed to the Table method when creating a temporary
    table for testing, e.g., in the define_temp_tables method of the
    ComponentReflectionTest class in suite/test_reflection.py
    z,no temp table keyword args routine for cfg: r   r   s     r   temp_table_keyword_argsr   t  s     <?GGE r   c                      y r   r%   )r   
connections     r   prepare_for_drop_tablesr     r   r   c                      y r   r%   )r   r,   testclss      r    stop_test_class_outside_fixturesr     r   r   c                     |S )a  Specify table name for creating a temporary Table.

    Dialect-specific implementations of this method will return the
    name to use when creating a temporary table for testing,
    e.g., in the define_temp_tables method of the
    ComponentReflectionTest class in suite/test_reflection.py

    Default to just the base name since that's what most dialects will
    use. The mssql dialect's implementation will need a "#" prepended.
    r%   )r.   rQ   	base_names      r   get_temp_table_namer     s
     r   c                 F    t        d| j                  j                        )Nz7backend does not implement a schema name set function: )r   r,   r
   )r.   dbapi_connectionr   s      r    set_default_schema_on_connectionr     s    
66::	 r   )-rX   loggingrj   r   r   r   r   r	   r   r
   r)   sqlr   r   r   	getLoggerr2   r8   FOLLOWER_IDENTobjectr   r=   rR   rU   rh   r^   r   rn   r7   r   r   r   r:   rT   rD   rF   rC   rK   r   r   r   r   r   r   r   r%   r   r   <module>r      s          "    g!,v ,</8->B0 
 "& 
	 	 
	 	6r 
  
M M
 
	 	
 
	 	 
# # 
	 	
 
		 		!2 

 
 
	 	 
	 	 
  
 r   