o
    gy                      @   s  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dd Zeddd Ze
ddd Zeddd Zeddd  Zd0d"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$edd.d/ Z%d!S )1   )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)log)post_configure_engine)run_reap_dbs) set_default_schema_on_connection) stop_test_class_outside_fixtures)temp_table_keyword_args)update_db_optsoraclec                 C   s   |  P}|d|  |d|  |d|  |d|f  |d|  |d|  |d|  |d|  |d	|  |d
|  W d    d S 1 sWw   Y  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conn r   ^/var/www/html/api-tag/env/lib/python3.10/site-packages/sqlalchemy/dialects/oracle/provision.py_oracle_create_db   s   
"r   c                 C   s   d| | _ d| | _d S N%s_ts1%s_ts2)test_schematest_schema_2)configr   r   r   r   _oracle_configure_follower/      
r$   c              
   C   sV   z|  d|  td| W dS  tjy* } ztd| W Y d }~dS d }~ww )Nzdrop user %s cascadezReaped db: %sTzcouldn't drop db: %sF)r   r   infor   DatabaseErrorwarning)r   dbnameerrr   r   r   _ora_drop_ignore5   s   r+   c                 C   s   t | t || j d S N)_purge_recyclebinr!   r   r   r   r   r   '_ora_drop_all_schema_objects_pre_tables?   s   r/   c              	   C   s   |  K}|j|d d d D ]}|d|d   q|j|| jd d D ]}|d| j d|d   q%t| D ]
}|d|  q<W d    d S 1 sRw   Y  d S )Nzdrop synonym synonym_name.zdrop table )r   dialect_get_synonymsr   r!   r   get_temp_table_names)r   r   r   syn	tmp_tabler   r   r   (_ora_drop_all_schema_objects_post_tablesE   s   

"r7   c                 C   sT   |  }t|| t|d|  t|d|  W d    d S 1 s#w   Y  d S r   )r   r+   r   r   r   r   _oracle_drop_dbV   s
   

"r8   c              
   C   s   zt | W n tjy } ztd| W Y d }~nd }~ww tD ]}z|j}W n |jjj	y5   Y q"w d|_||_q"t
  d S )Nz#purge recyclebin command failed: %s    )r-   r   r'   r   r(   
_all_connsstmtcachesizer2   dbapiInterfaceErrorclear)r#   dbclsr*   cx_oracle_connscr   r   r   %_ora_stop_test_class_outside_fixturesc   s   
rC   Nc                 C   s   |   >}|d u r|d n#|dd|j|i D ]\}}}|d| d| d| d qW d    d S W d    d S 1 sEw   Y  d S )Nzpurge recyclebinzWselect owner, object_name,type from dba_recyclebin where owner=:schema and type='TABLE'schemazpurge  z."")r   r   r2   denormalize_nameall)r   rD   r   ownerobject_nametype_r   r   r   r-   y   s   
 "r-   c                 C   s8   ddl m} ||ddd }||ddd }d S )	Nr9   )eventcheckoutc                 S   s   t |  d S r,   )r:   add)	dbapi_con
con_record	con_proxyr   r   r   rM      s   z/_oracle_post_configure_engine.<locals>.checkoutcheckinc                 S   s   d|j v r|  d S d S )Ncx_oracle_xid)r&   
invalidate)dbapi_connectionconnection_recordr   r   r   rR      s   
z._oracle_post_configure_engine.<locals>.checkin)
sqlalchemyrL   listens_for)r   enginefollower_identrL   rM   rR   r   r   r   _oracle_post_configure_engine   s
   


r[   c                 C   s  t d|  t| }| r}t dd| |d}dd |D }t }|D ]0}|ds5|dr6q)||v rY|| d	| |v rL|d	|  d
| |v rY|d
|  q)d }}	t	|dD ]\}	}
t
||
rp|d7 }qct d||	 W d    d S 1 sw   Y  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>_ts1_ts2r   r    r9      z-Dropped %d out of %d stale databases detected)r   r&   r   r   joinr   setendswithrN   	enumerater+   )r   identsr   r   to_reap	all_namesto_dropnamedroppedtotalr^   r   r   r   _reap_oracle_dbs   s8   


"rn   c                 C   s   t | } | j|ddS )Nxe)r^   password)sa_urlmake_urlrd   )r   r   r   r   r   _oracle_follower_url_from_main   r%   rs   c                 C   s   dgddS )NzGLOBAL TEMPORARYzPRESERVE ROWS)prefixesoracle_on_commitr   r.   r   r   r   _oracle_temp_table_keyword_args   s   rv   c                 C   s"   |  }|d|  |  d S )Nz#ALTER SESSION SET CURRENT_SCHEMA=%s)cursorexecuteclose)r   rU   schema_namerw   r   r   r   (_oracle_set_default_schema_on_connection   s   r{   c                 C   s,   |j rt|  dkrd|d< dS dS dS )zCSet database options (db_opts) for a test database that we created.oracledbT
thick_modeN)oracledb_thick_moderq   rr   get_driver_name)db_urldb_optsoptionsr   r   r   _update_db_opts   s   r   r,   )& r   r   r   rY   r   rq   testing.provisionr   r   r   r	   r
   r   r   r   r   r   r   r   r   for_dbr   r$   r+   r/   r7   r8   rC   r-   rd   r:   r[   rn   rs   rv   r{   r   r   r   r   r   <module>   sX   









 


