o
    [h                     @   s   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	 e
eZdZG dd	 d	eZd
d Zdd Zdd Zdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zd d! Zejd"d# ZdS )$    N   )config)engines   )url)compatc                   @   s0   e Zd Zdd Zedd Zdd Zdd Zd	S )
registerc                 C   s
   i | _ d S Nfns)self r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/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                    s    fdd}|S )Nc                    s   | j  < S r	   r
   )r   dbnamer   r   r   decorate   s   
z!register.for_db.<locals>.decorater   )r   r   r   r   r   r   r      s   zregister.for_dbc                 G   sp   t |tjrt|}nt |tjr|}n|jj}| }|| j	v r-| j	| |g|R  S | j	d |g|R  S r   )

isinstancer   string_typessa_urlmake_urlURLdbr   get_backend_namer   )r   cfgargr   backendr   r   r   __call__   s   
zregister.__call__N)__name__
__module____qualname__r   classmethodr   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_db+      
r/   c                 C   sz   t |  }|  |rt| |} i }t| | t| |}t| || |	 
  tj||||}|r;t|| |S r	   )r   r   get_dialectload_provisioningfollower_url_from_mainupdate_db_optsr   Ztesting_enginepost_configure_engineconnectcloser   Configr   configure_follower)db_urloptionsZfile_configr.   dialectdb_optsengr    r   r   r   setup_config1   s   


r?   c                 C   r(   )NzDROP database %s, URI %r)r)   r*   r+   r   r   drop_dbr-   r   r   r   drop_follower_dbF   r0   rA   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	   )setr   r8   Zall_configsr   Zdisposer   r   usernamehostdatabaseadd)hostsr    r   r"   Z	host_confr   r   r   r)   L   s   
r)   c                 C      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: %sNotImplementedErrorr   r    r>   identr   r   r   r,   _   s   r,   c                 C   rH   )z8Drop a database that we dynamically created for testing.zno DB drop routine for cfg: %srI   rK   r   r   r   r@   i   s   r@   c                 C      dS )zCSet database options (db_opts) for a test database that we created.Nr   )r:   r=   r   r   r   r4   o      r4   c                 C   rM   )zPerform extra steps after configuring an engine for testing.

    (For the internal dialects, currently only used by sqlite.)
    Nr   )r   enginer.   r   r   r   r5   u   s   r5   c                 C   s   t | } || _| 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   rE   r   rL   r   r   r   r3   ~   s   
r3   c                 C   rM   )z@Create dialect-specific config settings for a follower database.Nr   )r    rL   r   r   r   r9      rN   r9   c                 C   rM   )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   rP   r   r   r   run_reap_dbs   s   
rQ   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   )r*   r+   collectionsdefaultdictrB   openstripsplitr   r   r1   r2   r   rD   rF   listrQ   )Zidents_fileurlsZidentsZdialectsfile_lineZdb_namer:   Zurl_objZurl_keyr   rL   r   r   r   reap_dbs   s,   




r\   c                 C   rH   )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: %srI   )r    r>   r   r   r   temp_table_keyword_args   s   	r]   )rS   logging r   r   rO   r   r   utilr   	getLoggerr$   r*   ZFOLLOWER_IDENTobjectr   r/   r?   rA   r)   r   r,   r@   r4   r5   r3   r9   rQ   r\   r]   r   r   r   r   <module>   s<    

	





