
 XhC                 @   s   d  d l  Z  d  d l 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 Z d Z Gd	 d
   d
 e  Z d S)    N)settings)BaseDatabaseCreation)DatabaseError)cached_property)inputZtest_ZIm_a_lumberjackc               @   su  e  Z d  Z e d d    Z d d d d d  Z d d   Z d	 d
   Z d d   Z d d d  Z	 d d d  Z
 d d d  Z d d   Z d d   Z d d d  Z d d   Z d d   Z d d d d  Z d  d!   Z d" d#   Z d$ d%   Z d& d'   Z d( d)   Z d* d+   Z d, d-   Z d. d/   Z d0 d1   Z d2 d3   Z d4 d5   Z d6 d7   Z d8 d9   Z d S):DatabaseCreationc             C   s   t  j |  j j } | j d  p) | d } | j d  pB | d } | j   } | j d | d |  t |  j  } | | d |  j j S)a>  
        This is analogous to other backends' `_nodb_connection` property,
        which allows access to an "administrative" connection which can
        be used to manage the test databases.
        For Oracle, the only connection that can be used for that purpose
        is the main (non-test) connection.
        
SAVED_USERUSERSAVED_PASSWORDPASSWORDalias)r   	DATABASES
connectionr   getcopyupdatetype)selfsettings_dictuserpasswordZDatabaseWrapper r   P/home/ubuntu/projects/ifolica/build/django/django/db/backends/oracle/creation.py_maindb_connection   s    	z#DatabaseCreation._maindb_connection   Fc          D   C   s  |  j    } |  j j   } |  j   r)y |  j | | | |  Wq)t k
 r%} z| r` d  St j j d |  | s t	 d | d  } n  | s | d k r| d k r t
 d |  j j  n  y |  j | | |  Wn t k
 rP} zQ d t |  k r|  j | | | |  n! t j j d |  t j d	  WYd  d  } ~ XnF t k
 r} z& t j j d |  t j d	  WYd  d  } ~ Xn Xy |  j | | | |  Wqt k
 r} z& t j j d
 |  t j d	  WYd  d  } ~ XqXn t
 d  t j d  WYd  d  } ~ Xq)Xn  |  j   r| d k rNt
 d  n  y |  j | | | |  Wqt k
 r} z| rd  St j j d |  | st	 d | d  } n  | s| d k ruy_ | d k rt
 d  n  |  j | | |  | d k rt
 d  n  |  j | | | |  Wqt k
 rq} z& t j j d |  t j d	  WYd  d  } ~ XqXn t
 d  t j d  WYd  d  } ~ XqXn  |  j j   |  j |  |  j j d S)Nz,Got an error creating the test database: %s
z^It appears the test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: r   yesr   z.Destroying old test database for alias '%s'...z	ORA-29857z2Got an error destroying the old test database: %s
   z.Got an error recreating the test database: %s
zTests cancelled.zCreating test user...z(Got an error creating the test user: %s
zZIt appears the test user, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: zDestroying old test user...z*Got an error recreating the test user: %s
NAME)_get_test_db_paramsr   cursor_test_database_create_execute_test_db_creation	Exceptionsysstderrwriter   printr   r   _execute_test_db_destructionr   str)_handle_objects_preventing_db_destructionexit_test_user_create_create_test_user_destroy_test_userclose_switch_to_test_userr   )r   	verbosityautoclobberkeepdb
parametersr   econfirmr   r   r   _create_test_db!   s~     #
##
#z DatabaseCreation._create_test_dbc             C   s   t  j |  j j } |  j j d | d <|  j j d <|  j j d | d <|  j j d <| d } |  j j d } | d | d <| d <| d <|  j j d <| d | d <|  j j d <d S)	au  
        Oracle doesn't have the concept of separate databases under the same user.
        Thus, we use a separate user (see _create_test_db). This method is used
        to switch to that user. We will need the main user again for clean-up when
        we end testing, so we keep its credentials in SAVED_USER/SAVED_PASSWORD
        entries in the settings dict.
        r	   r   r   r
   TESTr   r   N)r   r   r   r   r   )r   r3   Zreal_settingsZreal_test_settingsZtest_settingsr   r   r   r/   k   s    ""
,z%DatabaseCreation._switch_to_test_userc             C   s,   | d |  j  j d <| d |  j  j d <d S)z
        Set this database up to be used in testing as a mirror of a primary database
        whose settings are given
        r	   r   N)r   r   )r   Zprimary_settings_dictr   r   r   set_as_test_mirror~   s    z#DatabaseCreation.set_as_test_mirrorc             C   s  t  d  t  d  t  d  |  j   rr| sG t d | d  } n  | sY | d k rXy0 | d k ru t  d  n  |  j | | |  WnF t k
 r } z& t j j d	 |  t j d
  WYd  d  } ~ Xn Xy: | d k r t  d |  j	 j
  n  |  j | | |  Wqot k
 rT} z& t j j d |  t j d
  WYd  d  } ~ XqoXqt  d  t j d  n) t  d | d  t  d  t j d  d  S)NzIThere are objects in the old test database which prevent its destruction.z`If they belong to the test user, deleting the user will allow the test database to be recreated.zbOtherwise, you will need to find and remove each of these objects, or use a different tablespace.
zType 'yes' to delete user %s: r   r   r   zDestroying old test user...z*Got an error destroying the test user: %s
r   z.Destroying old test database for alias '%s'...z.Got an error destroying the test database: %s
z5Tests cancelled -- test database cannot be recreated.z\Django is configured to use pre-existing test user '%s', and will not attempt to delete it.
)r&   r+   r   r-   r"   r#   r$   r%   r*   r   r   r'   )r   r   r3   r0   r1   r5   r4   r   r   r   r)      s6    


 #

z:DatabaseCreation._handle_objects_preventing_db_destructionc             C   s   |  j  j d |  j  j d <|  j  j d |  j  j d <|  j  j   |  j   } |  j j   } t j d  |  j   r | d k r t	 d  n  |  j
 | | |  n  |  j   r | d k r t	 d  n  |  j | | |  n  |  j j   d S)	z
        Destroy a test database, prompting the user for confirmation if the
        database already exists. Returns the name of the test database created.
        r   r	   r
   r   r   zDestroying test user...z"Destroying test database tables...N)r   r   r.   r   r   r   timesleepr+   r&   r-   r    r'   )r   Ztest_database_namer0   r3   r   r   r   r   _destroy_test_db   s    z!DatabaseCreation._destroy_test_dbc             C   s\   | d k r! t  d | d  n  d d g } | r9 d n d  } |  j | | | | |  d  S)Nr   z_create_test_db(): dbname = %sr   zCREATE TABLESPACE %(tblspace)s
               DATAFILE '%(datafile)s' SIZE 20M
               REUSE AUTOEXTEND ON NEXT 10M MAXSIZE %(maxsize)s
            zCREATE TEMPORARY TABLESPACE %(tblspace_temp)s
               TEMPFILE '%(datafile_tmp)s' SIZE 20M
               REUSE AUTOEXTEND ON NEXT 10M MAXSIZE %(maxsize_tmp)s
            z	ORA-01543)r&   _execute_allow_fail_statements)r   r   r3   r0   r2   
statementsacceptable_ora_errr   r   r   r!      s    	z*DatabaseCreation._execute_test_db_creationc       	      C   s   | d k r! t  d | d  n  d d g } | r9 d n d  } |  j | | | | |  d } |  j | | g | | d  } | r | d k r t  d	  n  d  S)
Nr   z"_create_test_user(): username = %sr   zCREATE USER %(user)s
               IDENTIFIED BY %(password)s
               DEFAULT TABLESPACE %(tblspace)s
               TEMPORARY TABLESPACE %(tblspace_temp)s
               QUOTA UNLIMITED ON %(tblspace)s
            zGRANT CREATE SESSION,
                     CREATE TABLE,
                     CREATE SEQUENCE,
                     CREATE PROCEDURE,
                     CREATE TRIGGER
               TO %(user)sz	ORA-01920zGRANT CREATE VIEW TO %(user)sz	ORA-01031zDFailed to grant CREATE VIEW permission to test user. This may be ok.)r&   r<   )	r   r   r3   r0   r2   r=   r>   extrasuccessr   r   r   r,      s    	z"DatabaseCreation._create_test_userc             C   sG   | d k r! t  d | d  n  d d g } |  j | | | |  d  S)Nr   z)_execute_test_db_destruction(): dbname=%sr   zQDROP TABLESPACE %(tblspace)s INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTSzVDROP TABLESPACE %(tblspace_temp)s INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS)r&   _execute_statements)r   r   r3   r0   r=   r   r   r   r'      s
    	z-DatabaseCreation._execute_test_db_destructionc             C   sN   | d k r+ t  d | d  t  d  n  d g } |  j | | | |  d  S)Nr   z_destroy_test_user(): user=%sr   z'Be patient.  This can take some time...zDROP USER %(user)s CASCADE)r&   rA   )r   r   r3   r0   r=   r   r   r   r-      s
    	z#DatabaseCreation._destroy_test_userc       	      C   s   x | D] } | | } | d k r0 t  |  n  y | j |  Wq t k
 r } z2 | si | d k r t j j d |  n    WYd  d  } ~ Xq Xq Wd  S)Nr   zFailed (%s)
)r&   executer"   r#   r$   r%   )	r   r   r=   r3   r0   allow_quiet_failtemplatestmterrr   r   r   rA      s    
z$DatabaseCreation._execute_statementsc       	      C   s   yB | d k	 o t  |  d k } |  j | | | | d | d SWnS t k
 r } z3 t |  } | d k s{ | | k r   n  d SWYd d } ~ Xn Xd S)z
        Execute statements which are allowed to fail silently if the Oracle
        error code given by `acceptable_ora_err` is raised. Return True if the
        statements execute without an exception, or False otherwise.
        Nr   rC   TF)lenrA   r   r(   )	r   r   r=   r3   r0   r>   rC   rF   descriptionr   r   r   r<     s    z/DatabaseCreation._execute_allow_fail_statementsc             C   sy   i	 |  j    d 6|  j   d 6|  j   d 6|  j   d 6|  j   d 6|  j   d 6|  j   d 6|  j   d 6|  j   d	 6S)
NZdbnamer   r   tblspaceZtblspace_tempZdatafileZdatafile_tmpmaxsizeZmaxsize_tmp)	_test_database_name_test_database_user_test_database_passwd_test_database_tblspace_test_database_tblspace_tmp _test_database_tblspace_datafile$_test_database_tblspace_tmp_datafile_test_database_tblspace_size _test_database_tblspace_tmp_size)r   r   r   r   r     s    z$DatabaseCreation._get_test_db_paramsNc             C   sC   |  j  j } | d j | |  } | d k r? t | | } n  | S)z
        Return a value from the test settings dict,
        or a given default,
        or a prefixed entry from the main settings dict
        r7   N)r   r   r   TEST_DATABASE_PREFIX)r   keydefaultprefixedr   valr   r   r   _test_settings_get%  s
    z#DatabaseCreation._test_settings_getc             C   s   |  j  d d d S)Nr   rW   )rY   )r   r   r   r   rK   1  s    z$DatabaseCreation._test_database_namec             C   s   |  j  d d d S)NZ	CREATE_DBrV   T)rY   )r   r   r   r   r    4  s    z&DatabaseCreation._test_database_createc             C   s   |  j  d d d S)NZCREATE_USERrV   T)rY   )r   r   r   r   r+   7  s    z"DatabaseCreation._test_user_createc             C   s   |  j  d d d S)Nr	   rW   )rY   )r   r   r   r   rL   :  s    z$DatabaseCreation._test_database_userc             C   s   |  j  d d t S)Nr   rV   )rY   r   )r   r   r   r   rM   =  s    z&DatabaseCreation._test_database_passwdc             C   s   |  j  d d d S)NZTBLSPACErW   r	   )rY   )r   r   r   r   rN   @  s    z(DatabaseCreation._test_database_tblspacec             C   s,   |  j  j } | d j d t | d d  S)Nr7   ZTBLSPACE_TMPr	   Z_temp)r   r   r   rT   )r   r   r   r   r   rO   C  s    z,DatabaseCreation._test_database_tblspace_tmpc             C   s#   d |  j    } |  j d d | S)Nz%s.dbfZDATAFILErV   )rN   rY   )r   rI   r   r   r   rP   H  s    z1DatabaseCreation._test_database_tblspace_datafilec             C   s#   d |  j    } |  j d d | S)Nz%s.dbfZDATAFILE_TMPrV   )rO   rY   )r   rI   r   r   r   rQ   L  s    z5DatabaseCreation._test_database_tblspace_tmp_datafilec             C   s   |  j  d d d S)NZDATAFILE_MAXSIZErV   500M)rY   )r   r   r   r   rR   P  s    z-DatabaseCreation._test_database_tblspace_sizec             C   s   |  j  d d d S)NZDATAFILE_TMP_MAXSIZErV   rZ   )rY   )r   r   r   r   rS   S  s    z1DatabaseCreation._test_database_tblspace_tmp_sizec             C   s   |  j  j d S)z
        We need to return the 'production' DB name to get the test DB creation
        machinery to work. This isn't a great deal in this case because DB
        names as handled by Django haven't real counterparts in Oracle.
        r   )r   r   )r   r   r   r   _get_test_db_nameV  s    z"DatabaseCreation._get_test_db_namec             C   s5   |  j  j } | d | d | d | d |  j   f S)NZHOSTZPORTZENGINEr   )r   r   rL   )r   r   r   r   r   test_db_signature^  s    z"DatabaseCreation.test_db_signature)__name__
__module____qualname__r   r   r6   r/   r8   r)   r;   r!   r,   r'   r-   rA   r<   r   rY   rK   r    r+   rL   rM   rN   rO   rP   rQ   rR   rS   r[   r\   r   r   r   r   r      s6   J#		r   )r#   r9   Zdjango.confr   Z django.db.backends.base.creationr   Zdjango.db.utilsr   Zdjango.utils.functionalr   Zdjango.utils.six.movesr   rT   r   r   r   r   r   r   <module>   s   