U
    ui+                     @   s0  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 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e	ddd Zdd  Zedd!d" Z edd#d$ Z!edd%d& Z"edd'd( Z#d=d)d*Z$edd+d, Z%d-d. Z&edd/d0 Z'edd1d2 Z(edd3d4 Z)edd5d6 Z*edd7d8 Z+edd9d: Z,edd;d< Z-dS )>    N   )create_engine)exc)inspect)url)configure_follower)	create_db)#drop_all_schema_objects_post_tables)"drop_all_schema_objects_pre_tables)drop_db)follower_url_from_main)generate_driver_url)is_preferred_driver)log)post_configure_engine)post_configure_testing_engine)run_reap_dbs) set_default_schema_on_connection) stop_test_class_outside_fixtures)temp_table_keyword_args)update_db_opts)warn_test_suiteoraclec                 C   sp   |   }| jd||f d}|dkr4|ddg}n|ddg}z|  W n tjk
rf   Y d S X |S d S )Nz%s+%s)Z
drivername)oracledbZoracledb_async)retry_count5)retry_delay2r   r   )Zget_backend_namesetZupdate_query_pairsZdifference_update_queryget_dialectr   ZNoSuchModuleError)r   driverZ	query_strbackendnew_url r#   ^/home/lhs5088/backend/venv/lib/python3.8/site-packages/sqlalchemy/dialects/oracle/provision.py_oracle_generate_driver_url"   s     
r%   c              	   C   s   |  }|d|  |d|  |d|  |d|f  |d|  |d|  |d|  |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_ts2zgrant create table to %szgrant create table to %s_ts1zgrant create table to %s_ts2)beginexec_driver_sqlcfgengidentconnr#   r#   r$   _oracle_create_dbC   s    
r-   c                 C   s   d| | _ d| | _d S N%s_ts1%s_ts2)test_schemaZtest_schema_2)configr+   r#   r#   r$   _oracle_configure_followerV   s    
r3   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    r8   c                 C   s   t | t || j d S )N)_purge_recyclebinr1   r)   r*   r#   r#   r$   '_ora_drop_all_schema_objects_pre_tablesf   s    r;   c              	   C   s   |  }|j|d d d D ]}|d|d   q|j|| jd d D ] }|d| j d|d   qJt| D ]}|d|  qxW 5 Q R X d S )Nzdrop synonym Zsynonym_name.zdrop table )r&   dialectZ_get_synonymsr'   r1   r   Zget_temp_table_names)r)   r*   r,   ZsynZ	tmp_tabler#   r#   r$   (_ora_drop_all_schema_objects_post_tablesl   s    
   r>   c              	   C   s>   |  ,}t|| t|d|  t|d|  W 5 Q R X d S r.   )r&   r8   r(   r#   r#   r$   _oracle_drop_db}   s    

r?   c              
   C   sB   zt | W n0 tjk
r< } ztd| W 5 d }~X Y nX d S )Nz#purge recyclebin command failed: %s)r9   r   r5   r   r6   )r2   dbclsr7   r#   r#   r$   %_ora_stop_test_class_outside_fixtures   s    rB   c                 C   sr   |   `}|d kr|d nF|dd|j|i D ](\}}}|d| d| d| d q:W 5 Q R X d S )Nzpurge recyclebinzWselect owner, object_name,type from dba_recyclebin where owner=:schema and type='TABLE'schemazpurge  z."")r&   r'   r=   Zdenormalize_nameall)r*   rC   r,   ownerZobject_nametype_r#   r#   r$   r9      s    
r9   c                 C   s   |j jdko|j j S )ztestablish oracledb as the preferred driver to use for tests, even
    though cx_Oracle is still the "default" driverr   )r=   r    Zis_async)r)   enginer#   r#   r$   _oracle_is_preferred_driver   s    rJ   c                 C   s   | j dkstdd }d }tdD ]r}z| jj||W   S  | jj| jjfk
r } z.|}||rtd t	d W Y 
q"n W 5 d }~X Y q"X q"|d k	rt
d|d S )N	cx_oraclec                 S   s   dt | kpdt | kS )NzDPY-6005z	ORA-12516)str)r7   r#   r#   r$   _is_couldnt_connect   s    z0_connect_with_retry.<locals>._is_couldnt_connect   zOracle database reconnecting...   z"connect failed after five attempts)r    AssertionErrorrangeZloaded_dbapiconnectr5   ZOperationalErrorr   timesleep	Exception)r=   Zconn_recZcargsZcparamsrM   Zerr__r7   r#   r#   r$   _connect_with_retry   s$    

rW   c                 C   s*   ddl m} |jjdkr&||dt d S )Nr   eventrK   Z
do_connect) rY   r=   r    listenrW   )r   rI   optionsscoperY   r#   r#   r$   %_oracle_post_configure_testing_engine   s    r^   c                 C   s$   ddl m} ||ddd }d S )Nr   rX   checkinc                 S   s(   z
| j }W n   Y nX d| _ || _ d S )Nr   )Zstmtcachesize)dbapi_connectionZconnection_recordscr#   r#   r$   r_      s    
z._oracle_post_configure_engine.<locals>.checkin)rZ   rY   Zlistens_for)r   rI   Zfollower_identrY   r_   r#   r#   r$   _oracle_post_configure_engine   s    
rb   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 }qt d||	 W 5 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 | ]\}|  qS r#   )lower).0usernamer#   r#   r$   	<setcomp>   s     z#_reap_oracle_dbs.<locals>.<setcomp>Z_ts1Z_ts2r/   r0   r      z-Dropped %d out of %d stale databases detected)r   r4   r   r&   joinr'   r   endswithadd	enumerater8   )r   Zidentsr*   r,   Zto_reapZ	all_namesZto_dropnameZdroppedtotalre   r#   r#   r$   _reap_oracle_dbs   s6    



  rn   c                 C   s   t | } | j|ddS )NZxe)re   password)sa_urlmake_urlr   )r   r+   r#   r#   r$   _oracle_follower_url_from_main  s    
rr   c                 C   s   dgddS )NzGLOBAL TEMPORARYzPRESERVE ROWS)prefixesZoracle_on_commitr#   r:   r#   r#   r$   _oracle_temp_table_keyword_args  s    rt   c                 C   s"   |  }|d|  |  d S )Nz#ALTER SESSION SET CURRENT_SCHEMA=%s)cursorexecuteclose)r)   r`   Zschema_nameru   r#   r#   r$   (_oracle_set_default_schema_on_connection  s    rx   c                 C   s$   |j r t|  dkr d|d< dS )zCSet database options (db_opts) for a test database that we created.r   TZ
thick_modeN)Zoracledb_thick_moderp   rq   Zget_driver_name)Zdb_urlZdb_optsr\   r#   r#   r$   _update_db_opts"  s
    ry   )N).rS   rZ   r   r   r   rI   r   rp   Ztesting.provisionr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   Ztesting.warningsr   Zfor_dbr%   r-   r3   r8   r;   r>   r?   rB   r9   rJ   rW   r^   rb   rn   rr   rt   rx   ry   r#   r#   r#   r$   <module>	   sl   
 











 


