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/flaskapp/venv/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 sƒw   Y  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>©   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   



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