U
    dA7  ã                   @  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dGdCdD„ƒZ1ejdEdF„ ƒZ2dS )Hé    )Ú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   ú@/tmp/pip-unpacked-wheel-wbr0uges/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   Zdbname©Údbnamesr   r   r   Údecorate%   s
    
z!register.for_db.<locals>.decorater   )r   r   r    r   r   r   r   $   s    zregister.for_dbc                 G  sn   t |tƒrt |¡}nt |tjƒr(|}n|jj}| ¡ }|| jkrV| j| |f|žŽ S | jd |f|žŽ S d S r   )	Ú
isinstanceÚstrÚ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   r      s   



r   c                 C  s0   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<   s    
r6   c                 C  sŽ   t  | ¡ ¡ }| ¡  |r$t| |ƒ} i }t| ||ƒ d|d< t | |¡}t| ||ƒ | 	¡  
¡  tj ||||¡}|r||t_|rŠt||ƒ |S )NÚglobalZscope)r#   r$   Úget_dialectÚload_provisioningÚfollower_url_from_mainÚupdate_db_optsr   Ztesting_engineÚpost_configure_engineÚconnectÚcloser   ÚConfigr   ÚidentÚconfigure_follower)Údb_urlÚoptionsZfile_configr5   ÚdialectÚdb_optsÚengr(   r   r   r   Úsetup_configB   s     

rG   c                 C  s0   t ƒ D ]$}t d| |jj¡ t||j| ƒ qd S )NzDROP database %s, URI %r)r0   r1   r2   r&   r
   Údrop_dbr4   r   r   r   Údrop_follower_db^   s    
rI   c                 c  sÂ   t ƒ }t t ¡}dd„ dd„ | D ƒD ƒ}|D ]\}}||j  |j¡ q,i }|D ]l\}}|j}| ¡  ||krŠt |ƒ || ¡ ||< }	n|| }	t||	ƒD ]}
|
|krªqœ| |
¡ |
V  qœqPdS )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

    c                 S  s   g | ]}||  ¡ f‘qS r   )r8   )Ú.0Úurl_objr   r   r   Ú
<listcomp>‰   s   ÿz$generate_db_urls.<locals>.<listcomp>c                 S  s   g | ]}t  |¡‘qS r   )r#   r$   )rJ   rB   r   r   r   rL   ‹   s     N)	ÚsetÚcollectionsÚdefaultdictÚnameÚaddÚdriverr9   Ú
differenceÚ_generate_driver_urls)Zdb_urlsÚextra_driversÚurlsZ!backend_to_driver_we_already_haveZurls_plus_dialectsrK   rD   Zbackend_to_driver_we_needr*   Zextra_per_backendZ
driver_urlr   r   r   Úgenerate_db_urlsd   s.    !
þÿþ
rW   c                 c  sx   |   ¡ }| |¡ t| |dƒ} | V  t|ƒD ]F}d|krJ| dd¡\}}n|}d }t| ||ƒ}|r,| |¡ |V  q,d S )NÚ ú?r   )Zget_driver_nameÚdiscardÚgenerate_driver_urlÚlistÚsplitÚremove)r
   rU   Zmain_driverÚdrvZdriver_onlyÚ	query_strÚnew_urlr   r   r   rT   ¥   s    

rT   c                 C  sX   |   ¡ }| jd||f d}|r*| |¡}z| ¡  W n tjk
rN   Y d S X |S d S )Nz%s+%s)Z
drivername)r'   rM   Zupdate_query_stringr8   r   ZNoSuchModuleError)r
   rR   r`   r*   ra   r   r   r   r[   ½   s    
ÿ
r[   c                  c  sˆ   t ƒ } tj ¡ D ]}|j ¡  qtj ¡ D ]>}|jj}| ¡ }||j|j	|j
f}|| kr*|V  |  |¡ q*tj ¡ D ]}|j ¡  qtd S r   )rM   r   r?   Zall_configsr&   Zdisposer
   r'   ÚusernameÚhostÚdatabaserQ   )Úhostsr(   r
   r*   Z	host_confr   r   r   r0   Ï   s    r0   c                 C  s   d S r   r   ©r(   rF   r   r   r   Ú"drop_all_schema_objects_pre_tablesâ   s    rg   c                 C  s   d S r   r   rf   r   r   r   Ú#drop_all_schema_objects_post_tablesç   s    rh   c                 C  s  t | |ƒ t| |ƒ tjjjr(t| |ƒ t|ƒ}d}tjj 	| ¡rR|| j
| jf7 }tj|||d t| |ƒ tjj 	| ¡rþ| ¡ v}| ¡ D ]}| t t |¡¡¡ qŒtjj 	| ¡rô| j
| jfD ]0}|j|dD ]}| t tj||d¡¡ qÒqÂW 5 Q R X d S )Nr   )Úconsider_schemasr   )rg   Ú
drop_viewsr   ÚrequirementsZmaterialized_viewsZenabledÚdrop_materialized_viewsr	   ÚschemasÚenabled_for_configÚtest_schemaZtest_schema_2r   Zdrop_all_tablesrh   Ú	sequencesÚbeginZget_sequence_namesÚexecuter   ZDropSequencer   ÚSequence)r(   rF   Ú	inspectorri   ÚconnÚseqÚschema_namer   r   r   Údrop_all_schema_objectsì   s0    





ÿ
ÿÿrx   c                 C  sÞ   t |ƒ}z| ¡ }W n tk
r(   Y n<X | ¡ ,}|D ] }| t t |t 	¡ ¡¡¡ q8W 5 Q R X t
jj | ¡rÚz|j| jd}W n tk
r˜   Y nBX | ¡ 2}|D ]&}| t tj|t 	¡ | jd¡¡ q¨W 5 Q R X d S )Nr   )r	   Zget_view_namesÚNotImplementedErrorrq   rr   r   Z	_DropViewr   ZTableZMetaDatar   rk   rm   rn   ro   )r(   rF   rt   Z
view_namesru   Úvnamer   r   r   rj     s4    
ÿ
ýÿÿrj   c              	   C  s”   t |ƒ}| ¡ }| ¡  }|D ]}| d|› ¡ qW 5 Q R X tjj | ¡r|j| jd}| ¡ (}|D ]}| d| j› d|› ¡ qhW 5 Q R X d S )NzDROP MATERIALIZED VIEW r   Ú.)	r	   Zget_materialized_view_namesrq   Zexec_driver_sqlr   rk   rm   rn   ro   )r(   rF   rt   Zmview_namesru   rz   r   r   r   rl   /  s    
ÿ
ÿrl   c                 C  s   t d|jf ƒ‚dS )z—Dynamically 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: %sN©ry   r
   ©r(   rF   r@   r   r   r   r3   C  s    
ÿr3   c                 C  s   t d|jf ƒ‚dS )z8Drop a database that we dynamically created for testing.zno DB drop routine for cfg: %sNr|   r}   r   r   r   rH   O  s    rH   c                   s,   t  ˆ ¡}t|jƒdkrˆ S ‡ fdd„S d S )Né   c                   s
   ˆ | |ƒS r   r   )rB   rE   Ú_options©r   r   r   Ú<lambda>Z  ó    z'_adapt_update_db_opts.<locals>.<lambda>)r   Zinspect_getfullargspecÚlenÚargs)r   Zinspr   r€   r   Ú_adapt_update_db_optsU  s    
r…   c                 C  s   dS )zCSet database options (db_opts) for a test database that we created.Nr   )rB   rE   rC   r   r   r   r;   ]  s    r;   c                 C  s   dS )z‰Perform extra steps after configuring an engine for testing.

    (For the internal dialects, currently only used by sqlite, oracle)
    Nr   )r
   Úenginer5   r   r   r   r<   b  s    r<   c                 C  s   t  | ¡} | j|dS )zúCreate 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
    )rd   )r#   r$   rM   ©r
   r@   r   r   r   r:   j  s    
r:   c                 C  s   dS )z@Create dialect-specific config settings for a follower database.Nr   )r(   r@   r   r   r   rA   v  s    rA   c                 C  s   dS )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|  s    rˆ   c              	   C  sÚ   t  d¡ t t¡}t t¡}i }t| ƒz}|D ]n}| ¡ }| d¡\}}t 	|¡}||krt| 
¡ ||< ||  ¡  | ¡ |jf}	||	  |¡ ||	  |¡ q0W 5 Q R X |D ]&}	t||	 ƒd }
||	 }t|
|ƒ q®d S )NzReaping databases...ú r   )r1   r2   rN   rO   rM   ÚopenÚstripr]   r#   r$   r8   r9   r'   rc   rQ   r\   rˆ   )Zidents_filerV   ZidentsZdialectsÚfile_ÚlineZdb_namerB   rK   Zurl_keyr
   r@   r   r   r   Úreap_dbsˆ  s&    




rŽ   c                 C  s   t d|jf ƒ‚dS )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: %sNr|   rf   r   r   r   Útemp_table_keyword_args¡  s    	
ÿr   c                 C  s   d S r   r   )r   Ú
connectionr   r   r   Úprepare_for_drop_tables¯  s    r‘   c                 C  s   d S r   r   )r   r&   Ztestclsr   r   r   Ú stop_test_class_outside_fixtures´  s    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(   rF   Ú	base_namer   r   r   Úget_temp_table_name¹  s    r”   c                 C  s   t d| jjf ƒ‚d S )Nz9backend does not implement a schema name set function: %s©ry   r&   r
   )r(   Zdbapi_connectionrw   r   r   r   Ú set_default_schema_on_connectionÈ  s
    ÿÿr–   c                 C  s   t d| jj› ƒ‚dS )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: Nr•   )r(   ÚtableZ	returningZ
set_lambdar   r   r   ÚupsertÐ  s    ÿr˜   c                 C  s   |S )zƒNormalize 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š   )N)3Ú
__future__r   rN   ÚloggingrX   r   r   r   r   r	   r†   r
   r#   Zsqlr   r   Ú	getLoggerr,   r1   ZFOLLOWER_IDENTr   r6   rG   rI   rW   rT   r   r[   r0   rg   rh   rx   rj   rl   r3   rH   r…   r   r;   r<   r:   rA   rˆ   rŽ   r   r‘   r’   r”   r–   r˜   rš   r   r   r   r   Ú<module>   st   
%A


"!











