3
a                 @   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jddd Zejddd Zdd Ze	jddd Zejddd Ze Zejddd Zejddd Ze
jddd Zejddd  Zej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  j}|jd|  |jd|  |jd|  |jd|f  |jd|  |jd|  |jd|  W d 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   J/tmp/pip-build-6_cqtusv/SQLAlchemy/sqlalchemy/dialects/oracle/provision.py_oracle_create_db   s    
r   c             C   s   d| | _ d| | _d S )Nz%s_ts1z%s_ts2)Ztest_schemaZtest_schema_2)configr   r   r   r   _oracle_configure_follower   s    
r   c             C   sR   y| j d|  tjd| dS  tjk
rL } ztjd| dS d }~X 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>   |j  ,}t|| t|d|  t|d|  W d Q R X d S )Nz%s_ts1z%s_ts2)r   r   )r   r   r   r   r   r   r   _oracle_drop_db/   s    

r    c             C   s   y"|j  }|jd W d Q R X W n0 tjk
rR } ztjd| W Y d d }~X nX x>tD ]6}y
|j}W n |jj	j
k
r   Y qZX d|_||_qZW tj  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} |j|ddd }|j|ddd }d S )	Nr!   )eventcheckoutc             S   s   t j|  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|j  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    r-   c             C   s  t jd|  t| }|j }t jddj| |jd}dd |D }t }xj|D ]b}|jdsT|jdrpqTqT||krT|j| d	| |kr|jd	|  d
| |krT|jd
|  qTW d }}	x(t	|dD ]\}	}
t
||
r|d7 }qW t jd||	 W d Q R X d S )Nzdb reaper connecting to %rzidentifiers in file: %sz, zselect 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 | ]\}|j  qS r   )lower).0usernamer   r   r   	<setcomp>w   s    z#_reap_oracle_dbs.<locals>.<setcomp>Z_ts1Z_ts2z%s_ts1z%s_ts2r!      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totalr0   r   r   r   _reap_oracle_dbsj   s.    



r9   c             C   s   t j| } | j|ddS )NZxe)r0   password)sa_urlZmake_urlr4   )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    r>   c             C   s"   |j  }|jd|  |j  d S )Nz#ALTER SESSION SET CURRENT_SCHEMA=%s)cursorexecuteclose)r   r+   Zschema_namer?   r   r   r   (_oracle_set_default_schema_on_connection   s    rB   N) r   r   r,   r   r;   Ztesting.provisionr   r   r   r   r	   r
   r   r   r   r   Zfor_dbr   r   r   r    r4   r"   r-   r9   r<   r>   rB   r   r   r   r   <module>   s.   
"