o
    E6dM                      @   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_optsZ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   {C:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\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_schemaZ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   Zdbnameerrr   r   r   _ora_drop_ignore/   s   r(   c                 C   s   t | t || j d S N)_purge_recyclebinr    r   r   r   r   r   '_ora_drop_all_schema_objects_pre_tables9   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 Zsynonym_name.zdrop table )r   dialectZ_get_synonymsr   r    r   Zget_temp_table_names)r   r   r   ZsynZ	tmp_tabler   r   r   (_ora_drop_all_schema_objects_post_tables?   s   

"r/   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_dbP   s
   

"r0   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_connsZstmtcachesizer.   ZdbapiZInterfaceErrorclear)r!   dbclsr'   Zcx_oracle_connscr   r   r   %_ora_stop_test_class_outside_fixtures]   s   
r7   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   r.   Zdenormalize_nameall)r   r8   r   ownerZobject_nametype_r   r   r   r*   s   s   
 "r*   c                 C   s8   ddl m} ||ddd }||ddd }d S )	Nr1   )eventcheckoutc                 S   s   t |  d S r)   )r2   add)Z	dbapi_conZ
con_recordZ	con_proxyr   r   r   r?      s   z/_oracle_post_configure_engine.<locals>.checkoutcheckinc                 S   s   d|j v r|  d S d S )NZcx_oracle_xid)r$   Z
invalidate)dbapi_connectionZconnection_recordr   r   r   rA      s   
z._oracle_post_configure_engine.<locals>.checkin)Z
sqlalchemyr>   Zlistens_for)r   engineZfollower_identr>   r?   rA   r   r   r   _oracle_post_configure_engine   s
   


rD   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>Z_ts1Z_ts2r   r   r1      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totalrG   r   r   r   _reap_oracle_dbs   s8   


"rP   c                 C   s   t | } | j|ddS )NZxe)rG   password)sa_urlmake_urlrK   )r   r   r   r   r   _oracle_follower_url_from_main   r#   rT   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   rV   c                 C   s"   |  }|d|  |  d S )Nz#ALTER SESSION SET CURRENT_SCHEMA=%s)cursorexecuteclose)r   rB   Zschema_namerW   r   r   r   (_oracle_set_default_schema_on_connection   s   rZ   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.ZoracledbTZ
thick_modeN)Zoracledb_thick_moderR   rS   Zget_driver_name)Zdb_urlZdb_optsoptionsr   r   r   _update_db_opts   s   r\   r)   )& r   r   r   rC   r   rR   Ztesting.provisionr   r   r   r	   r
   r   r   r   r   r   r   r   r   Zfor_dbr   r"   r(   r,   r/   r0   r7   r*   rK   r2   rD   rP   rT   rV   rZ   r\   r   r   r   r   <module>   sX   



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