o
    gn9                     @  s  d dl mZ 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 eeZdZG dd dZdd Zdd Zdd Zdd Zdd Zejdd Zdd Zejdd Zejdd  Zd!d" Zd#d$ Z d%d& Z!ejd'd( Z"ejd)d* Z#d+d, Z$e%e$d-d. Z&ejd/d0 Z'ejd1d2 Z(ejd3d4 Z)ejd5d6 Z*d7d8 Z+ejd9d: Z,ejd;d< Z-ejd=d> Z.ejd?d@ Z/ejdAdB Z0ejddCdDdEdFZ1ejdGdH Z2dS )I    )annotationsN   )config)engines)util   )exc)inspect)url)ddlschemac                   @  s>   e Zd ZdddZedd Zedd Zdd	 Zd
d ZdS )registerNc                 C  s   i | _ || _d S N)fns	decorator)selfr    r   ^/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/sqlalchemy/testing/provision.py__init__   s   
zregister.__init__c                 C  s   t  d|S N*r   for_db)clsfnr   r   r   init"   s   zregister.initc                 C  s   t |dS r   r   )r   r   r   r   r   init_decorator&   s   zregister.init_decoratorc                   s    fdd}|S )Nc                   s(   j r | }  D ]}| j|< q
S r   )r   r   )r   dbnamedbnamesr   r   r   decorate+   s
   
z!register.for_db.<locals>.decorater   )r   r    r!   r   r   r   r   *   s   zregister.for_dbc                 O  sz   t |trt|}nt |tjr|}n|jj}| }|| jv r/| j| |g|R i |S | jd |g|R i |S r   )	
isinstancestrsa_urlmake_urlURLdbr
   get_backend_namer   )r   cfgargkwr
   backendr   r   r   __call__4   s   

zregister.__call__r   )	__name__
__module____qualname__r   classmethodr   r   r   r-   r   r   r   r   r      s    



r   c                 C  0   t  D ]}td| |jj t||j|  qd S )NzCREATE database %s, URI %r)_configs_for_db_operationloginfor'   r
   	create_dbfollower_identr)   r   r   r   create_follower_dbB      
r9   c                 C  s   t |  }|  |rt| |} i }t| || d|d< t| |}t| || |	 
  tj||||}|r>|t_|rEt|| |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_configr8   dialectdb_optsengr)   r   r   r   setup_configH   s    

rN   c                 C  r2   )NzDROP database %s, URI %r)r3   r4   r5   r'   r
   drop_dbr7   r   r   r   drop_follower_dbe   r:   rP   c                 c  s    t  }tt }dd dd | D D }|D ]\}}| }||j | qi }|D ]6\}}|j}	|  |	|vrIt |||	  ||	< }
n||	 }
t||
D ]}||v rYqR|| |V  qRq,dS )ar  Generate a set of URLs to test given configured URLs plus additional
    driver names.

    Given:

    .. sourcecode:: text

        --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:

    .. sourcecode:: text

        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::

    .. sourcecode:: text

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

    c                 S  s   g | ]}||  fqS r   )r=   ).0url_objr   r   r   
<listcomp>   s    
z$generate_db_urls.<locals>.<listcomp>c                 S  s   g | ]}t |qS r   )r$   r%   )rQ   rH   r   r   r   rS      s    N)	setcollectionsdefaultdictget_driver_namenameaddr>   
difference_generate_driver_urls)db_urlsextra_driversurls!backend_to_driver_we_already_haveurls_plus_dialectsrR   rK   driver_namebackend_to_driver_we_needr,   extra_per_backend
driver_urlr   r   r   generate_db_urlsk   s6   '


re   c                 c  sz    |   }|| t| |d} | V  t|D ]#}d|v r&|dd\}}n|}d }t| ||}|r:|| |V  qd S )N ?r   )rW   discardgenerate_driver_urllistsplitremove)r
   r]   main_driverdrvdriver_only	query_strnew_urlr   r   r   r[      s    

r[   c                 C  sP   |   }| jd||f d}|r||}z|  W |S  tjy'   Y d S w )Nz%s+%s)
drivername)r(   rT   update_query_stringr=   r   NoSuchModuleError)r
   driverrp   r,   rq   r   r   r   ri      s   


ri   c                  c  s    t  } tj D ]}|j  q	tj D ]}|jj}| }||j|j	|j
f}|| vr5|V  | | qtj D ]}|j  q;d S r   )rT   r   rE   all_configsr'   disposer
   r(   usernamehostdatabaserY   )hostsr)   r
   r,   	host_confr   r   r   r3      s   
r3   c                 C     d S r   r   r)   rM   r   r   r   "drop_all_schema_objects_pre_tables      r   c                 C  r}   r   r   r~   r   r   r   #drop_all_schema_objects_post_tables   r   r   c                 C  s,  t | | t| | tjjjrt| | t|}d}tjj	| r)|| j
| jf7 }tj|||d t| | tjj	| r| I}| D ]}|tt| qFtjj	| rz| j
| jfD ] }|j|dD ]}|ttj||d qiqaW d    d S W d    d S 1 sw   Y  d S d S )Nr   )consider_schemasr   )r   
drop_viewsr   requirementsmaterialized_viewsenableddrop_materialized_viewsr	   schemasenabled_for_configtest_schematest_schema_2r   drop_all_tablesr   	sequencesbeginget_sequence_namesexecuter   DropSequencer   Sequence)r)   rM   	inspectorr   connseqschema_namer   r   r   drop_all_schema_objects   s<   






"r   c                 C  s
  t |}z| }W n	 ty   Y n(w | }|D ]}|tt|t	  qW d    n1 s6w   Y  t
jj| rz	|j| jd}W n
 tyU   Y d S w | }|D ]}|ttj|t	 | jd q]W d    d S 1 s|w   Y  d S d S )Nr   )r	   get_view_namesNotImplementedErrorr   r   r   	_DropViewr   TableMetaDatar   r   r   r   r   )r)   rM   r   
view_namesr   vnamer   r   r   r     sB   

"r   c                 C  s   t |}| }| }|D ]
}|d|  qW d    n1 s$w   Y  tjj| r_|j| jd}| }|D ]}|d| j d|  q>W d    d S 1 sXw   Y  d S d S )NzDROP MATERIALIZED VIEW r   .)	r	   get_materialized_view_namesr   exec_driver_sqlr   r   r   r   r   )r)   rM   r   mview_namesr   r   r   r   r   r   <  s&   

"r   c                 C     t d|jf )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: %sr   r
   r)   rM   rF   r   r   r   r6   P  s   
r6   c                 C  r   )z8Drop a database that we dynamically created for testing.zno DB drop routine for cfg: %sr   r   r   r   r   rO   \  s   rO   c                   s(   t  }t|jdkr S  fddS )N   c                   s
    | |S r   r   )rH   rL   _optionsr   r   r   <lambda>g  s   
 z'_adapt_update_db_opts.<locals>.<lambda>)r   inspect_getfullargspeclenargs)r   inspr   r   r   _adapt_update_db_optsb  s   
r   c                 C     dS )zCSet database options (db_opts) for a test database that we created.Nr   )rH   rL   rI   r   r   r   r@   j      r@   c                 C  r   )zPerform extra steps after configuring an engine for testing.

    (For the internal dialects, currently only used by sqlite, oracle, mssql)
    Nr   )r
   enginer8   r   r   r   rB   o  r   rB   c                 C  s   t | } | j|d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
    )rz   )r$   r%   rT   r
   rF   r   r   r   r?   w  s   
r?   c                 C  r   )z@Create dialect-specific config settings for a follower database.Nr   )r)   rF   r   r   r   rG     s   rG   c                 C  r   )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   r   r   r   run_reap_dbs  r   r   c                 C  s   t d tt}tt}i }t| B}|D ]7}| }|d\}}t	|}||vr:|
 ||< ||   | |jf}	||	 | ||	 | qW d    n1 sZw   Y  |D ]}	t||	 d }
||	 }t|
| qad S )NzReaping databases... r   )r4   r5   rU   rV   rT   openstriprk   r$   r%   r=   r>   r(   ry   rY   rj   r   )idents_filer^   identsdialectsfile_linedb_namerH   rR   url_keyr
   rF   r   r   r   reap_dbs  s,   




r   c                 C  r   )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: %sr   r~   r   r   r   temp_table_keyword_args  s   	
r   c                 C  r}   r   r   )r   
connectionr   r   r   prepare_for_drop_tables  r   r   c                 C  r}   r   r   )r   r'   testclsr   r   r    stop_test_class_outside_fixtures  r   r   c                 C  s   |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)   rM   	base_namer   r   r   get_temp_table_name  s   r   c                 C  s   t d| jjf )Nz9backend does not implement a schema name set function: %sr   r'   r
   )r)   dbapi_connectionr   r   r   r    set_default_schema_on_connection  s
   r   F)
set_lambdasort_by_parameter_orderc                C  s   t d| jj )a  return the backends insert..on conflict / on dupe etc. construct.

    while we should add a backend-neutral upsert construct as well, such as
    insert().upsert(), it's important that we continue to test the
    backend-specific insert() constructs since if we do implement
    insert().upsert(), that would be using a different codepath for the things
    we need to test like insertmanyvalues, etc.

    z3backend does not include an upsert implementation: r   )r)   table	returningr   r   r   r   r   upsert  s   r   c                 C  s   |S )zNormalize sequence parameters for dialect that don't start with 1
    by default.

    The default implementation does nothing
    r   )r)   sequencer   r   r   normalize_sequence  s   r   )3
__future__r   rU   loggingrf   r   r   r   r   r	   r   r
   r$   sqlr   r   	getLoggerr.   r4   FOLLOWER_IDENTr   r9   rN   rP   re   r[   r   ri   r3   r   r   r   r   r   r6   rO   r   r   r@   rB   r?   rG   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sx   
%J


!!











