o
    ïÅ[h  ã                   @   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 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e d¡dd„ ƒZdS )é   )Úcreate_engine)Úexc)Úurl)Úconfigure_follower)Ú	create_db)Údrop_db)Úfollower_url_from_main)Úlog)Úrun_reap_dbs)Útemp_table_keyword_args)Úupdate_db_optsÚoraclec                 C   s’   |  ¡ ;}| d| ¡ | d| ¡ | d| ¡ | d|f ¡ | d| ¡ | d| ¡ | d| ¡ W d   ƒ d S 1 sBw   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_ts2)ÚconnectÚexecute©ÚcfgÚengÚidentÚconn© r   ú‹/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/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)Ztest_schemaZtest_schema_2)Úconfigr   r   r   r   Ú_oracle_configure_follower   s   
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   ZDatabaseErrorÚwarning)r   ZdbnameÚerrr   r   r   Ú_ora_drop_ignore#   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_db-   s
   

"ør!   c                 C   s   d S )Nr   )Zdb_urlZdb_optsr   r   r   Ú_oracle_update_db_opts:   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>L   s    z#_reap_oracle_dbs.<locals>.<setcomp>Z_ts1Z_ts2r   r   é    é   z-Dropped %d out of %d stale databases detected)r	   r   r   r   Újoinr   ÚsetÚendswithÚaddÚ	enumerater    )r   Zidentsr   r   Zto_reapZ	all_namesZto_dropÚnameZdroppedÚtotalr%   r   r   r   Ú_reap_oracle_dbs?   s8   
ÿ
€
€ÿ"çr0   c                 C   s   t  | ¡} || _d| _| S )NZxe)Úsa_urlZmake_urlr%   Úpassword)r   r   r   r   r   Ú_oracle_follower_url_from_maina   s   
r3   c                 C   s   dgddœS )NzGLOBAL TEMPORARYzPRESERVE ROWS)ÚprefixesZoracle_on_commitr   )r   r   r   r   r   Ú_oracle_temp_table_keyword_argsi   s   þr5   N)Ú r   r   Zenginer   r1   Ztesting.provisionr   r   r   r   r	   r
   r   r   Zfor_dbr   r   r    r!   r"   r0   r3   r5   r   r   r   r   Ú<module>   s4    





!
