a
    [ŠÝ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º   |  ¡ ž}| d| ¡ | d| ¡ | d| ¡ | d|f ¡ | d| ¡ | d| ¡ | d| ¡ | d| ¡ | d	| ¡ | d
| ¡ W d   ƒ n1 s¬0    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   úr/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/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/   s    
r#   c              
   C   sZ   z |   d| ¡ t d|¡ W dS  tjyT } zt d|¡ W Y d }~dS d }~0 0 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_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°   |  ¡ ”}|j |d d d ¡D ]}| d|d › ¡ q|j || jd d ¡D ] }| d| j› d|d › ¡ qJt|ƒ ¡ D ]}| d|› ¡ qxW d   ƒ n1 s¢0    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_tablesE   s    

ÿÿr/   c                 C   sR   |  ¡ 6}t||ƒ t|d| ƒ t|d| ƒ W d   ƒ n1 sD0    Y  d S r   )r   r(   r   r   r   r   Ú_oracle_drop_dbV   s    

r0   c              
   C   s†   zt |ƒ W n2 tjy> } zt d|¡ W Y d }~n
d }~0 0 tD ]4}z
|j}W n |jjj	yj   Y qD0 d|_||_qDt 
¡  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_fixturesc   s    "
r7   Nc                 C   s†   |   ¡ j}|d u r| d¡ nF| dd|j |¡i¡ ¡ D ](\}}}| d|› d|› d|› d¡ q:W d   ƒ n1 sx0    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*   y   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 )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| ƒ}| ¡ ä}t  dd |¡¡ | d¡}dd„ |D ƒ}tƒ }|D ]b}| d¡sR| d¡rnqRqR||v rR| |¡ d	| |v rš| d	| ¡ d
| |v rR| d
| ¡ qRd }}	t	|dƒD ]\}	}
t
||
ƒrÈ|d7 }qÈt  d||	¡ W d   ƒ n1 s0    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>©   ó    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   s2    
ÿ


ÿrQ   c                 C   s   t  | ¡} | j|ddS )NZxe)rG   Úpassword)Úsa_urlÚmake_urlrL   )r   r   r   r   r   Ú_oracle_follower_url_from_main¾   s    
rU   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    þrW   c                 C   s"   |  ¡ }| d| ¡ | ¡  d S )Nz#ALTER SESSION SET CURRENT_SCHEMA=%s)ÚcursorÚexecuteÚclose)r   rB   Zschema_namerX   r   r   r   Ú(_oracle_set_default_schema_on_connectionÌ   s    r[   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.ZoracledbTZ
thick_modeN)Zoracledb_thick_moderS   rT   Zget_driver_name)Zdb_urlZdb_optsÚoptionsr   r   r   Ú_update_db_optsÕ   s
    ÿþr]   )N)&Ú r   r   r   rC   r   rS   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*   rL   r2   rD   rQ   rU   rW   r[   r]   r   r   r   r   Ú<module>	   sV   









 


