Ticket #3174: patch-boulder-oracle-sprint.diff

File patch-boulder-oracle-sprint.diff, 11.3 KB (added by andreas.mock@…, 18 years ago)
  • django_src/django/db/backends/oracle/creation.py

     
    3535PASSWORD = 'Im_a_lumberjack'
    3636OLD_DATABASE_USER = None
    3737OLD_DATABASE_PASSWORD = None
     38REMEMBER = {}
    3839
    3940def create_test_db(settings, connection, backend, verbosity=1, autoclobber=False):
    40     if verbosity >= 1:
    41         print "Creating test database..."
    4241
    4342    TEST_DATABASE_NAME = _test_database_name(settings)
     43    TEST_DATABASE_USER = _test_database_user(settings)
     44    TEST_DATABASE_PASSWD = _test_database_passwd(settings)
     45    TEST_DATABASE_TBLSPACE = _test_database_tblspace(settings)
     46    TEST_DATABASE_TBLSPACE_TMP = _test_database_tblspace_tmp(settings)
    4447
     48    parameters = {
     49        'dbname': TEST_DATABASE_NAME,
     50        'user': TEST_DATABASE_USER,
     51        'password': TEST_DATABASE_PASSWD,
     52        'tblspace': TEST_DATABASE_TBLSPACE,
     53        'tblspace_temp': TEST_DATABASE_TBLSPACE_TMP,
     54    }
     55
     56    REMEMBER['user'] = settings.DATABASE_USER
     57    REMEMBER['passwd'] = settings.DATABASE_PASSWORD
     58
    4559    cursor = connection.cursor()
    46     try:
    47         _create_test_db(cursor, TEST_DATABASE_NAME, verbosity)
    48     except Exception, e:
    49         sys.stderr.write("Got an error creating the test database: %s\n" % e)
    50         if not autoclobber:
    51             confirm = raw_input("It appears the test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_NAME)
    52         if autoclobber or confirm == 'yes':
    53             try:
    54                 if verbosity >= 1:
    55                     print "Destroying old test database..."
    56                 _destroy_test_db(cursor, TEST_DATABASE_NAME, verbosity)
    57                 if verbosity >= 1:
    58                     print "Creating test database..."
    59                 _create_test_db(cursor, TEST_DATABASE_NAME, verbosity)
    60             except Exception, e:
    61                 sys.stderr.write("Got an error recreating the test database: %s\n" % e)
    62                 sys.exit(2)
    63         else:
    64             print "Tests cancelled."
    65             sys.exit(1)
    6660
     61    if _test_database_create(settings):
     62        if verbosity >= 1:
     63            print "Creating test database..."
     64        try:
     65            _create_test_db(cursor, parameters, verbosity)
     66        except Exception, e:
     67            sys.stderr.write("Got an error creating the test database: %s\n" % e)
     68            if not autoclobber:
     69                confirm = raw_input("It appears the test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_NAME)
     70            if autoclobber or confirm == 'yes':
     71                try:
     72                    if verbosity >= 1:
     73                        print "Destroying old test database..."
     74                    _destroy_test_db(cursor, parameters, verbosity)
     75                    if verbosity >= 1:
     76                        print "Creating test database..."
     77                    _create_test_db(cursor, parameters, verbosity)
     78                except Exception, e:
     79                    sys.stderr.write("Got an error recreating the test database: %s\n" % e)
     80                    sys.exit(2)
     81            else:
     82                print "Tests cancelled."
     83                sys.exit(1)
     84
     85    if _test_user_create(settings):
     86        if verbosity >= 1:
     87            print "Creating test user..."
     88        try:
     89            _create_test_user(cursor, parameters, verbosity)
     90        except Exception, e:
     91            sys.stderr.write("Got an error creating the test user: %s\n" % e)
     92            if not autoclobber:
     93                confirm = raw_input("It appears the test user, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_USER)
     94            if autoclobber or confirm == 'yes':
     95                try:
     96                    if verbosity >= 1:
     97                        print "Destroying old test user..."
     98                    _destroy_test_user(cursor, parameters, verbosity)
     99                    if verbosity >= 1:
     100                        print "Creating test user..."
     101                    _create_test_user(cursor, parameters, verbosity)
     102                except Exception, e:
     103                    sys.stderr.write("Got an error recreating the test user: %s\n" % e)
     104                    sys.exit(2)
     105            else:
     106                print "Tests cancelled."
     107                sys.exit(1)
     108
    67109    connection.close()
    68     settings.DATABASE_USER = TEST_DATABASE_NAME
    69     settings.DATABASE_PASSWORD = PASSWORD
     110    settings.DATABASE_USER = TEST_DATABASE_USER
     111    settings.DATABASE_PASSWORD = TEST_DATABASE_PASSWD
    70112
    71113    # Get a cursor (even though we don't need one yet). This has
    72114    # the side effect of initializing the test database.
    73115    cursor = connection.cursor()
    74116
    75117def destroy_test_db(settings, connection, backend, old_database_name, verbosity=1):
    76     if verbosity >= 1:
    77         print "Destroying test database..."
    78118    connection.close()
    79119
    80120    TEST_DATABASE_NAME = _test_database_name(settings)
     121    TEST_DATABASE_USER = _test_database_user(settings)
     122    TEST_DATABASE_PASSWD = _test_database_passwd(settings)
     123    TEST_DATABASE_TBLSPACE = _test_database_tblspace(settings)
     124    TEST_DATABASE_TBLSPACE_TMP = _test_database_tblspace_tmp(settings)
    81125    settings.DATABASE_NAME = old_database_name
    82126    #settings.DATABASE_USER = 'old_user'
    83127    #settings.DATABASE_PASSWORD = 'old_password'
     128    settings.DATABASE_USER = REMEMBER['user']
     129    settings.DATABASE_PASSWORD = REMEMBER['passwd']
    84130
     131    parameters = {
     132        'dbname': TEST_DATABASE_NAME,
     133        'user': TEST_DATABASE_USER,
     134        'password': TEST_DATABASE_PASSWD,
     135        'tblspace': TEST_DATABASE_TBLSPACE,
     136        'tblspace_temp': TEST_DATABASE_TBLSPACE_TMP,
     137    }
     138
    85139    cursor = connection.cursor()
    86140    time.sleep(1) # To avoid "database is being accessed by other users" errors.
    87     _destroy_test_db(cursor, TEST_DATABASE_NAME, verbosity)
     141    if _test_user_create(settings):
     142        if verbosity >= 1:
     143            print "Destroying test user..."
     144        _destroy_test_user(cursor, parameters, verbosity)
     145    if _test_database_create(settings):
     146        if verbosity >= 1:
     147            print "Destroying test database..."
     148        _destroy_test_db(cursor, parameters, verbosity)
    88149    connection.close()
    89150
    90 def _create_test_db(cursor, dbname, verbosity):
     151def _create_test_db(cursor, parameters, verbosity):
    91152    if verbosity >= 2:
    92         print "_create_test_db(): dbname = %s" % dbname
     153        print "_create_test_db(): dbname = %s" % parameters['dbname']
    93154    statements = [
    94         """CREATE TABLESPACE %(user)s
    95            DATAFILE '%(user)s.dbf' SIZE 10M AUTOEXTEND ON NEXT 10M MAXSIZE 20M
     155        """CREATE TABLESPACE %(tblspace)s
     156           DATAFILE '%(tblspace)s.dbf' SIZE 10M AUTOEXTEND ON NEXT 10M MAXSIZE 20M
    96157        """,
    97         """CREATE TEMPORARY TABLESPACE %(user)s_temp
    98            TEMPFILE '%(user)s_temp.dbf' SIZE 10M AUTOEXTEND ON NEXT 10M MAXSIZE 20M
     158        """CREATE TEMPORARY TABLESPACE %(tblspace_temp)s
     159           TEMPFILE '%(tblspace_temp)s.dbf' SIZE 10M AUTOEXTEND ON NEXT 10M MAXSIZE 20M
    99160        """,
     161    ]
     162    _execute_statements(cursor, statements, parameters, verbosity)
     163
     164def _create_test_user(cursor, parameters, verbosity):
     165    if verbosity >= 2:
     166        print "_create_test_user(): username = %s" %  parameters['user']
     167    statements = [
    100168        """CREATE USER %(user)s
    101169           IDENTIFIED BY %(password)s
    102            DEFAULT TABLESPACE %(user)s
    103            TEMPORARY TABLESPACE %(user)s_temp
     170           DEFAULT TABLESPACE %(tblspace)s
     171           TEMPORARY TABLESPACE %(tblspace_temp)s
    104172        """,
    105173        """GRANT CONNECT, RESOURCE TO %(user)s""",
    106174    ]
    107     _execute_statements(cursor, statements, dbname, verbosity)
     175    _execute_statements(cursor, statements, parameters, verbosity)
    108176
    109 def _destroy_test_db(cursor, dbname, verbosity):
     177def _destroy_test_db(cursor, parameters, verbosity):
    110178    if verbosity >= 2:
    111         print "_destroy_test_db(): dbname=%s" % dbname
     179        print "_destroy_test_db(): dbname=%s" % parameters['dbname']
    112180    statements = [
     181        'DROP TABLESPACE %(tblspace)s INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS',
     182        'DROP TABLESPACE %(tblspace_temp)s INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS',
     183        ]
     184    _execute_statements(cursor, statements, parameters, verbosity)
     185
     186def _destroy_test_user(cursor, parameters, verbosity):
     187    if verbosity >= 2:
     188        print "_destroy_test_user(): user=%s" % parameters['user']
     189        print "Be patient: This can take some time..."
     190    statements = [
    113191        'DROP USER %(user)s CASCADE',
    114         'DROP TABLESPACE %(user)s INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS',
    115         'DROP TABLESPACE %(user)s_TEMP INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS',
    116192        ]
    117     _execute_statements(cursor, statements, dbname, verbosity)
     193    _execute_statements(cursor, statements, parameters, verbosity)
    118194
    119 def _execute_statements(cursor, statements, dbname, verbosity):
     195def _execute_statements(cursor, statements, parameters, verbosity):
    120196    for template in statements:
    121         stmt = template % {'user': dbname,
    122                            'password': PASSWORD}
     197        stmt = template % parameters
    123198        if verbosity >= 2:
    124199            print stmt
    125200        try:
     
    129204            raise
    130205
    131206def _test_database_name(settings):
    132     if settings.TEST_DATABASE_NAME:
    133         name = settings.TEST_DATABASE_NAME
    134     else:
    135         name = TEST_DATABASE_PREFIX + settings.DATABASE_NAME
     207    name = TEST_DATABASE_PREFIX + settings.DATABASE_NAME
     208    try:
     209        if settings.TEST_DATABASE_NAME:
     210            name = settings.TEST_DATABASE_NAME
     211    except AttributeError:
     212        pass
     213    except:
     214        raise
    136215    return name
     216
     217def _test_database_create(settings):
     218    name = True
     219    try:
     220        if settings.TEST_DATABASE_CREATE
     221            name = True
     222        else:
     223            name = False
     224    except AttributeError:
     225        pass
     226    except:
     227        raise
     228    return name
     229
     230def _test_user_create(settings):
     231    name = True
     232    try:
     233        if settings.TEST_USER_CREATE
     234            name = True
     235        else:
     236            name = False
     237    except AttributeError:
     238        pass
     239    except:
     240        raise
     241    return name
     242
     243def _test_database_user(settings):
     244    name = TEST_DATABASE_PREFIX + settings.DATABASE_NAME
     245    try:
     246        if settings.TEST_DATABASE_USER:
     247            name = settings.TEST_DATABASE_USER
     248    except AttributeError:
     249        pass
     250    except:
     251        raise
     252    return name
     253
     254def _test_database_passwd(settings):
     255    name = PASSWORD
     256    try:
     257        if settings.TEST_DATABASE_PASSWD:
     258            name = settings.TEST_DATABASE_PASSWD
     259    except AttributeError:
     260        pass
     261    except:
     262        raise
     263    return name
     264
     265def _test_database_tblspace(settings):
     266    name = TEST_DATABASE_PREFIX + settings.DATABASE_NAME
     267    try:
     268        if settings.TEST_DATABASE_TBLSPACE:
     269            name = settings.TEST_DATABASE_TBLSPACE
     270    except AttributeError:
     271        pass
     272    except:
     273        raise
     274    return name
     275
     276def _test_database_tblspace_tmp(settings):
     277    name = TEST_DATABASE_PREFIX + settings.DATABASE_NAME + '_temp'
     278    try:
     279        if settings.TEST_DATABASE_TBLSPACE_TMP:
     280            name = settings.TEST_DATABASE_TBLSPACE_TMP
     281    except AttributeError:
     282        pass
     283    except:
     284        raise
     285    return name
Back to Top