U
    ø×cc®  ã                   @   sP  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 d d
lmZ d dlmZ d dlmZ d dlmZ e d¡dd„ ƒZe d¡dd„ ƒZdd„ Ze	 d¡dd„ ƒZe d¡dd„ ƒZeƒ Ze d¡dd„ ƒZe d¡dd„ ƒZe
 d¡dd„ ƒZe d¡dd „ ƒZe d¡d!d"„ ƒZd#S )$é   )Úcreate_engine)Úexc)Úurl)Úconfigure_follower)Ú	create_db)Údrop_db)Úfollower_url_from_main)Úlog)Úpost_configure_engine)Úrun_reap_dbs)Ú set_default_schema_on_connection)Ú stop_test_class_outside_fixtures)Útemp_table_keyword_argsÚoraclec              	   C   s|   |  ¡ j}| d| ¡ | d| ¡ | d| ¡ | d|f ¡ | d| ¡ | d| ¡ | d| ¡ W 5 Q R X d S )Nzcreate user %s identified by xez#create user %s_ts1 identified by xez#create user %s_ts2 identified by xezgrant dba to %sz grant unlimited tablespace to %sz$grant unlimited tablespace to %s_ts1z$grant unlimited tablespace to %s_ts2)ÚbeginÚexec_driver_sql©ÚcfgÚengÚidentÚconn© r   úH/tmp/pip-unpacked-wheel-8u86ls_i/sqlalchemy/dialects/oracle/provision.pyÚ_oracle_create_db   s    
r   c                 C   s   d| | _ d| | _d S ©Nú%s_ts1ú%s_ts2)Ztest_schemaZtest_schema_2)Úconfigr   r   r   r   Ú_oracle_configure_follower   s    
r   c              
   C   s\   z |   d| ¡ t d|¡ W dS  tjk
rV } zt d|¡ W Y ¢dS d }~X Y nX d S )Nzdrop user %s cascadezReaped db: %sTzcouldn't drop db: %sF)r   r	   Úinfor   ÚDatabaseErrorÚwarning)r   ZdbnameÚerrr   r   r   Ú_ora_drop_ignore%   s    r#   c              	   C   s>   |  ¡ ,}t||ƒ t|d| ƒ t|d| ƒ W 5 Q R X d S r   )r   r#   r   r   r   r   Ú_oracle_drop_db/   s    

r$   c              
   C   sœ   z"|  ¡ }| d¡ W 5 Q R X W n0 tjk
rR } zt d|¡ W 5 d }~X Y nX tD ]6}z
|j}W n |jj	j
k
r€   Y qXX d|_||_qXt ¡  d S )Nzpurge recyclebinz#purge recyclebin command failed: %sé    )r   r   r   r    r	   r!   Ú
_all_connsZstmtcachesizeÚdialectZdbapiZInterfaceErrorÚclear)r   ÚdbÚclsr   r"   Zcx_oracle_connZscr   r   r   r   <   s    

r   c                 C   s8   ddl m} | |d¡dd„ ƒ}| |d¡dd„ ƒ}d S )	Nr%   )ÚeventÚcheckoutc                 S   s   t  | ¡ d S )N)r&   Úadd)Z	dbapi_conZ
con_recordZ	con_proxyr   r   r   r,   ]   s    z/_oracle_post_configure_engine.<locals>.checkoutÚcheckinc                 S   s   d|j kr| ¡  d S )NZcx_oracle_xid)r   Z
invalidate)Údbapi_connectionZconnection_recordr   r   r   r.   a   s    
z._oracle_post_configure_engine.<locals>.checkin)Z
sqlalchemyr+   Zlistens_for)r   ÚengineZfollower_identr+   r,   r.   r   r   r   Ú_oracle_post_configure_engineY   s
    


r1   c              	   C   s   t  d| ¡ t| ƒ}| ¡ Ú}t  dd |¡¡ | d¡}dd„ |D ƒ}tƒ }|D ]b}| d¡sR| d¡rnqRqR||krR| |¡ d	| |krš| d	| ¡ d
| |krR| d
| ¡ qRd }}	t	|dƒD ]\}	}
t
||
ƒrÈ|d7 }qÈt  d||	¡ W 5 Q R X d S )Nzdb reaper connecting to %rzidentifiers in file: %sz, z‰select u.username from all_users u where username like 'TEST_%' and not exists (select username from v$session where username=u.username)c                 S   s   h | ]\}|  ¡ ’qS r   )Úlower)Ú.0Úusernamer   r   r   Ú	<setcomp>w   s     z#_reap_oracle_dbs.<locals>.<setcomp>Z_ts1Z_ts2r   r   r%   é   z-Dropped %d out of %d stale databases detected)r	   r   r   r   Újoinr   ÚsetÚendswithr-   Ú	enumerater#   )r   Zidentsr   r   Zto_reapZ	all_namesZto_dropÚnameZdroppedÚtotalr4   r   r   r   Ú_reap_oracle_dbsj   s6    
ÿ


  ÿr=   c                 C   s   t  | ¡} | j|ddS )NZxe)r4   Úpassword)Úsa_urlZmake_urlr8   )r   r   r   r   r   Ú_oracle_follower_url_from_mainŒ   s    
r@   c                 C   s   dgddœS )NzGLOBAL TEMPORARYzPRESERVE ROWS)ÚprefixesZoracle_on_commitr   )r   r   r   r   r   Ú_oracle_temp_table_keyword_args’   s    þrB   c                 C   s"   |  ¡ }| d| ¡ | ¡  d S )Nz#ALTER SESSION SET CURRENT_SCHEMA=%s)ÚcursorÚexecuteÚclose)r   r/   Zschema_namerC   r   r   r   Ú(_oracle_set_default_schema_on_connectionš   s    rF   N)Ú r   r   r0   r   r?   Ztesting.provisionr   r   r   r   r	   r
   r   r   r   r   Zfor_dbr   r   r#   r$   r8   r&   r1   r=   r@   rB   rF   r   r   r   r   Ú<module>   s@   






!

