2011-07-13 17:35:51 +08:00
|
|
|
import sys
|
|
|
|
import time
|
2013-05-21 23:34:08 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.creation import BaseDatabaseCreation
|
2014-09-22 01:15:48 +08:00
|
|
|
from django.db.utils import DatabaseError
|
2012-08-09 01:08:05 +08:00
|
|
|
from django.utils.six.moves import input
|
2007-06-23 22:16:00 +08:00
|
|
|
|
|
|
|
TEST_DATABASE_PREFIX = 'test_'
|
|
|
|
PASSWORD = 'Im_a_lumberjack'
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseCreation(BaseDatabaseCreation):
|
2012-09-08 03:48:22 +08:00
|
|
|
|
2014-05-28 05:13:08 +08:00
|
|
|
def _create_test_db(self, verbosity=1, autoclobber=False, keepdb=False):
|
2014-08-26 13:54:37 +08:00
|
|
|
parameters = self._get_test_db_params()
|
2008-08-11 20:11:25 +08:00
|
|
|
cursor = self.connection.cursor()
|
2009-12-22 23:18:51 +08:00
|
|
|
if self._test_database_create():
|
2008-08-11 20:11:25 +08:00
|
|
|
try:
|
|
|
|
self._execute_test_db_creation(cursor, parameters, verbosity)
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as e:
|
2014-05-28 05:13:08 +08:00
|
|
|
# if we want to keep the db, then no need to do any of the below,
|
|
|
|
# just return and skip it all.
|
|
|
|
if keepdb:
|
|
|
|
return
|
2008-08-11 20:11:25 +08:00
|
|
|
sys.stderr.write("Got an error creating the test database: %s\n" % e)
|
|
|
|
if not autoclobber:
|
2014-09-04 20:15:09 +08:00
|
|
|
confirm = input(
|
|
|
|
"It appears the test database, %s, already exists. "
|
2014-08-26 13:54:37 +08:00
|
|
|
"Type 'yes' to delete it, or 'no' to cancel: " % parameters['user'])
|
2008-08-11 20:11:25 +08:00
|
|
|
if autoclobber or confirm == 'yes':
|
2014-10-15 07:52:21 +08:00
|
|
|
if verbosity >= 1:
|
|
|
|
print("Destroying old test database '%s'..." % self.connection.alias)
|
2008-08-11 20:11:25 +08:00
|
|
|
try:
|
|
|
|
self._execute_test_db_destruction(cursor, parameters, verbosity)
|
2014-10-15 07:52:21 +08:00
|
|
|
except DatabaseError as e:
|
|
|
|
if 'ORA-29857' in str(e):
|
|
|
|
self._handle_objects_preventing_db_destruction(cursor, parameters,
|
|
|
|
verbosity, autoclobber)
|
|
|
|
else:
|
|
|
|
# Ran into a database error that isn't about leftover objects in the tablespace
|
|
|
|
sys.stderr.write("Got an error destroying the old test database: %s\n" % e)
|
|
|
|
sys.exit(2)
|
|
|
|
except Exception as e:
|
|
|
|
sys.stderr.write("Got an error destroying the old test database: %s\n" % e)
|
|
|
|
sys.exit(2)
|
|
|
|
try:
|
2008-08-11 20:11:25 +08:00
|
|
|
self._execute_test_db_creation(cursor, parameters, verbosity)
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as e:
|
2008-08-11 20:11:25 +08:00
|
|
|
sys.stderr.write("Got an error recreating the test database: %s\n" % e)
|
|
|
|
sys.exit(2)
|
|
|
|
else:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("Tests cancelled.")
|
2008-08-11 20:11:25 +08:00
|
|
|
sys.exit(1)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
if self._test_user_create():
|
2008-08-11 20:11:25 +08:00
|
|
|
if verbosity >= 1:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("Creating test user...")
|
2008-08-11 20:11:25 +08:00
|
|
|
try:
|
|
|
|
self._create_test_user(cursor, parameters, verbosity)
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as e:
|
2008-08-11 20:11:25 +08:00
|
|
|
sys.stderr.write("Got an error creating the test user: %s\n" % e)
|
|
|
|
if not autoclobber:
|
2014-09-04 20:15:09 +08:00
|
|
|
confirm = input(
|
|
|
|
"It appears the test user, %s, already exists. Type "
|
2014-08-26 13:54:37 +08:00
|
|
|
"'yes' to delete it, or 'no' to cancel: " % parameters['user'])
|
2008-08-11 20:11:25 +08:00
|
|
|
if autoclobber or confirm == 'yes':
|
|
|
|
try:
|
|
|
|
if verbosity >= 1:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("Destroying old test user...")
|
2008-08-11 20:11:25 +08:00
|
|
|
self._destroy_test_user(cursor, parameters, verbosity)
|
|
|
|
if verbosity >= 1:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("Creating test user...")
|
2008-08-11 20:11:25 +08:00
|
|
|
self._create_test_user(cursor, parameters, verbosity)
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as e:
|
2008-08-11 20:11:25 +08:00
|
|
|
sys.stderr.write("Got an error recreating the test user: %s\n" % e)
|
|
|
|
sys.exit(2)
|
|
|
|
else:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("Tests cancelled.")
|
2008-08-11 20:11:25 +08:00
|
|
|
sys.exit(1)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2014-08-26 01:17:13 +08:00
|
|
|
self.connection.close() # done with main user -- test user and tablespaces created
|
|
|
|
|
2012-11-27 04:45:21 +08:00
|
|
|
real_settings = settings.DATABASES[self.connection.alias]
|
2014-09-04 20:15:09 +08:00
|
|
|
real_settings['SAVED_USER'] = self.connection.settings_dict['SAVED_USER'] = \
|
|
|
|
self.connection.settings_dict['USER']
|
|
|
|
real_settings['SAVED_PASSWORD'] = self.connection.settings_dict['SAVED_PASSWORD'] = \
|
|
|
|
self.connection.settings_dict['PASSWORD']
|
2014-01-20 08:45:29 +08:00
|
|
|
real_test_settings = real_settings['TEST']
|
|
|
|
test_settings = self.connection.settings_dict['TEST']
|
2014-09-04 20:15:09 +08:00
|
|
|
real_test_settings['USER'] = real_settings['USER'] = test_settings['USER'] = \
|
2014-08-26 13:54:37 +08:00
|
|
|
self.connection.settings_dict['USER'] = parameters['user']
|
|
|
|
real_settings['PASSWORD'] = self.connection.settings_dict['PASSWORD'] = parameters['password']
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
return self.connection.settings_dict['NAME']
|
2008-08-13 02:49:44 +08:00
|
|
|
|
2014-10-15 07:52:21 +08:00
|
|
|
def _handle_objects_preventing_db_destruction(self, cursor, parameters, verbosity, autoclobber):
|
|
|
|
# There are objects in the test tablespace which prevent dropping it
|
|
|
|
# The easy fix is to drop the test user -- but are we allowed to do so?
|
|
|
|
print("There are objects in the old test database which prevent its destruction.")
|
|
|
|
print("If they belong to the test user, deleting the user will allow the test "
|
|
|
|
"database to be recreated.")
|
|
|
|
print("Otherwise, you will need to find and remove each of these objects, "
|
|
|
|
"or use a different tablespace.\n")
|
|
|
|
if self._test_user_create():
|
|
|
|
if not autoclobber:
|
|
|
|
confirm = input("Type 'yes' to delete user %s: " % parameters['user'])
|
|
|
|
if autoclobber or confirm == 'yes':
|
|
|
|
try:
|
|
|
|
if verbosity >= 1:
|
|
|
|
print("Destroying old test user...")
|
|
|
|
self._destroy_test_user(cursor, parameters, verbosity)
|
|
|
|
except Exception as e:
|
|
|
|
sys.stderr.write("Got an error destroying the test user: %s\n" % e)
|
|
|
|
sys.exit(2)
|
|
|
|
try:
|
|
|
|
if verbosity >= 1:
|
|
|
|
print("Destroying old test database '%s'..." % self.connection.alias)
|
|
|
|
self._execute_test_db_destruction(cursor, parameters, verbosity)
|
|
|
|
except Exception as e:
|
|
|
|
sys.stderr.write("Got an error destroying the test database: %s\n" % e)
|
|
|
|
sys.exit(2)
|
|
|
|
else:
|
|
|
|
print("Tests cancelled -- test database cannot be recreated.")
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
print("Django is configured to use pre-existing test user '%s',"
|
|
|
|
" and will not attempt to delete it.\n" % parameters['user'])
|
|
|
|
print("Tests cancelled -- test database cannot be recreated.")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def _destroy_test_db(self, test_database_name, verbosity=1):
|
|
|
|
"""
|
|
|
|
Destroy a test database, prompting the user for confirmation if the
|
|
|
|
database already exists. Returns the name of the test database created.
|
|
|
|
"""
|
2012-02-29 05:23:45 +08:00
|
|
|
self.connection.settings_dict['USER'] = self.connection.settings_dict['SAVED_USER']
|
|
|
|
self.connection.settings_dict['PASSWORD'] = self.connection.settings_dict['SAVED_PASSWORD']
|
2014-08-26 13:54:37 +08:00
|
|
|
parameters = self._get_test_db_params()
|
2008-08-11 20:11:25 +08:00
|
|
|
cursor = self.connection.cursor()
|
2013-11-03 05:02:56 +08:00
|
|
|
time.sleep(1) # To avoid "database is being accessed by other users" errors.
|
2009-12-22 23:18:51 +08:00
|
|
|
if self._test_user_create():
|
2008-08-11 20:11:25 +08:00
|
|
|
if verbosity >= 1:
|
2012-04-29 00:02:01 +08:00
|
|
|
print('Destroying test user...')
|
2008-08-11 20:11:25 +08:00
|
|
|
self._destroy_test_user(cursor, parameters, verbosity)
|
2009-12-22 23:18:51 +08:00
|
|
|
if self._test_database_create():
|
2008-08-11 20:11:25 +08:00
|
|
|
if verbosity >= 1:
|
2012-04-29 00:02:01 +08:00
|
|
|
print('Destroying test database tables...')
|
2008-08-11 20:11:25 +08:00
|
|
|
self._execute_test_db_destruction(cursor, parameters, verbosity)
|
|
|
|
self.connection.close()
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2008-08-13 02:49:44 +08:00
|
|
|
def _execute_test_db_creation(self, cursor, parameters, verbosity):
|
2008-08-11 20:11:25 +08:00
|
|
|
if verbosity >= 2:
|
2014-08-26 13:54:37 +08:00
|
|
|
print("_create_test_db(): dbname = %s" % parameters['user'])
|
2008-08-11 20:11:25 +08:00
|
|
|
statements = [
|
|
|
|
"""CREATE TABLESPACE %(tblspace)s
|
2014-08-26 13:54:37 +08:00
|
|
|
DATAFILE '%(datafile)s' SIZE 20M
|
|
|
|
REUSE AUTOEXTEND ON NEXT 10M MAXSIZE %(maxsize)s
|
2008-08-11 20:11:25 +08:00
|
|
|
""",
|
|
|
|
"""CREATE TEMPORARY TABLESPACE %(tblspace_temp)s
|
2014-08-26 13:54:37 +08:00
|
|
|
TEMPFILE '%(datafile_tmp)s' SIZE 20M
|
|
|
|
REUSE AUTOEXTEND ON NEXT 10M MAXSIZE %(maxsize_tmp)s
|
2008-08-11 20:11:25 +08:00
|
|
|
""",
|
|
|
|
]
|
2008-08-13 02:49:44 +08:00
|
|
|
self._execute_statements(cursor, statements, parameters, verbosity)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2008-08-13 02:49:44 +08:00
|
|
|
def _create_test_user(self, cursor, parameters, verbosity):
|
2008-08-11 20:11:25 +08:00
|
|
|
if verbosity >= 2:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("_create_test_user(): username = %s" % parameters['user'])
|
2008-08-11 20:11:25 +08:00
|
|
|
statements = [
|
|
|
|
"""CREATE USER %(user)s
|
|
|
|
IDENTIFIED BY %(password)s
|
|
|
|
DEFAULT TABLESPACE %(tblspace)s
|
|
|
|
TEMPORARY TABLESPACE %(tblspace_temp)s
|
2013-07-06 10:38:33 +08:00
|
|
|
QUOTA UNLIMITED ON %(tblspace)s
|
2008-08-11 20:11:25 +08:00
|
|
|
""",
|
2014-09-22 01:15:48 +08:00
|
|
|
"""GRANT CREATE SESSION,
|
|
|
|
CREATE TABLE,
|
|
|
|
CREATE SEQUENCE,
|
|
|
|
CREATE PROCEDURE,
|
|
|
|
CREATE TRIGGER
|
|
|
|
TO %(user)s""",
|
2008-08-11 20:11:25 +08:00
|
|
|
]
|
2008-08-13 02:49:44 +08:00
|
|
|
self._execute_statements(cursor, statements, parameters, verbosity)
|
2014-09-22 01:15:48 +08:00
|
|
|
# Most test-suites can be run without the create-view privilege. But some need it.
|
|
|
|
extra = "GRANT CREATE VIEW TO %(user)s"
|
|
|
|
try:
|
|
|
|
self._execute_statements(cursor, [extra], parameters, verbosity, allow_quiet_fail=True)
|
|
|
|
except DatabaseError as err:
|
|
|
|
description = str(err)
|
|
|
|
if 'ORA-01031' in description:
|
|
|
|
if verbosity >= 2:
|
|
|
|
print("Failed to grant CREATE VIEW permission to test user. This may be ok.")
|
|
|
|
else:
|
|
|
|
raise
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2008-08-13 02:49:44 +08:00
|
|
|
def _execute_test_db_destruction(self, cursor, parameters, verbosity):
|
2008-08-11 20:11:25 +08:00
|
|
|
if verbosity >= 2:
|
2014-08-26 13:54:37 +08:00
|
|
|
print("_execute_test_db_destruction(): dbname=%s" % parameters['user'])
|
2008-08-11 20:11:25 +08:00
|
|
|
statements = [
|
|
|
|
'DROP TABLESPACE %(tblspace)s INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS',
|
|
|
|
'DROP TABLESPACE %(tblspace_temp)s INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS',
|
2013-07-08 08:39:54 +08:00
|
|
|
]
|
2008-08-13 02:49:44 +08:00
|
|
|
self._execute_statements(cursor, statements, parameters, verbosity)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2008-08-13 02:49:44 +08:00
|
|
|
def _destroy_test_user(self, cursor, parameters, verbosity):
|
2008-08-11 20:11:25 +08:00
|
|
|
if verbosity >= 2:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("_destroy_test_user(): user=%s" % parameters['user'])
|
|
|
|
print("Be patient. This can take some time...")
|
2008-08-11 20:11:25 +08:00
|
|
|
statements = [
|
|
|
|
'DROP USER %(user)s CASCADE',
|
2007-06-23 22:16:00 +08:00
|
|
|
]
|
2008-08-13 02:49:44 +08:00
|
|
|
self._execute_statements(cursor, statements, parameters, verbosity)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2014-09-22 01:15:48 +08:00
|
|
|
def _execute_statements(self, cursor, statements, parameters, verbosity, allow_quiet_fail=False):
|
2008-08-11 20:11:25 +08:00
|
|
|
for template in statements:
|
|
|
|
stmt = template % parameters
|
|
|
|
if verbosity >= 2:
|
2012-04-29 00:02:01 +08:00
|
|
|
print(stmt)
|
2008-08-11 20:11:25 +08:00
|
|
|
try:
|
|
|
|
cursor.execute(stmt)
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception as err:
|
2014-09-22 01:15:48 +08:00
|
|
|
if (not allow_quiet_fail) or verbosity >= 2:
|
|
|
|
sys.stderr.write("Failed (%s)\n" % (err))
|
2008-08-11 20:11:25 +08:00
|
|
|
raise
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2014-08-26 13:54:37 +08:00
|
|
|
def _get_test_db_params(self):
|
|
|
|
return {
|
|
|
|
'dbname': self._test_database_name(),
|
|
|
|
'user': self._test_database_user(),
|
|
|
|
'password': self._test_database_passwd(),
|
|
|
|
'tblspace': self._test_database_tblspace(),
|
|
|
|
'tblspace_temp': self._test_database_tblspace_tmp(),
|
|
|
|
'datafile': self._test_database_tblspace_datafile(),
|
|
|
|
'datafile_tmp': self._test_database_tblspace_tmp_datafile(),
|
|
|
|
'maxsize': self._test_database_tblspace_size(),
|
|
|
|
'maxsize_tmp': self._test_database_tblspace_tmp_size(),
|
|
|
|
}
|
|
|
|
|
2014-01-20 08:45:29 +08:00
|
|
|
def _test_settings_get(self, key, default=None, prefixed=None):
|
|
|
|
"""
|
|
|
|
Return a value from the test settings dict,
|
|
|
|
or a given default,
|
|
|
|
or a prefixed entry from the main settings dict
|
|
|
|
"""
|
|
|
|
settings_dict = self.connection.settings_dict
|
|
|
|
val = settings_dict['TEST'].get(key, default)
|
|
|
|
if val is None:
|
|
|
|
val = TEST_DATABASE_PREFIX + settings_dict[prefixed]
|
|
|
|
return val
|
|
|
|
|
2010-12-21 09:36:09 +08:00
|
|
|
def _test_database_name(self):
|
2014-01-20 08:45:29 +08:00
|
|
|
return self._test_settings_get('NAME', prefixed='NAME')
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _test_database_create(self):
|
2014-01-20 08:45:29 +08:00
|
|
|
return self._test_settings_get('CREATE_DB', default=True)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _test_user_create(self):
|
2014-01-20 08:45:29 +08:00
|
|
|
return self._test_settings_get('CREATE_USER', default=True)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _test_database_user(self):
|
2014-01-20 08:45:29 +08:00
|
|
|
return self._test_settings_get('USER', prefixed='USER')
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _test_database_passwd(self):
|
2014-01-20 08:45:29 +08:00
|
|
|
return self._test_settings_get('PASSWORD', default=PASSWORD)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _test_database_tblspace(self):
|
2014-08-26 13:54:37 +08:00
|
|
|
return self._test_settings_get('TBLSPACE', prefixed='USER')
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _test_database_tblspace_tmp(self):
|
2014-01-20 08:45:29 +08:00
|
|
|
settings_dict = self.connection.settings_dict
|
|
|
|
return settings_dict['TEST'].get('TBLSPACE_TMP',
|
2014-08-26 13:54:37 +08:00
|
|
|
TEST_DATABASE_PREFIX + settings_dict['USER'] + '_temp')
|
|
|
|
|
|
|
|
def _test_database_tblspace_datafile(self):
|
|
|
|
tblspace = '%s.dbf' % self._test_database_tblspace()
|
|
|
|
return self._test_settings_get('DATAFILE', default=tblspace)
|
|
|
|
|
|
|
|
def _test_database_tblspace_tmp_datafile(self):
|
|
|
|
tblspace = '%s.dbf' % self._test_database_tblspace_tmp()
|
|
|
|
return self._test_settings_get('DATAFILE_TMP', default=tblspace)
|
|
|
|
|
|
|
|
def _test_database_tblspace_size(self):
|
|
|
|
return self._test_settings_get('DATAFILE_MAXSIZE', default='500M')
|
|
|
|
|
|
|
|
def _test_database_tblspace_tmp_size(self):
|
|
|
|
return self._test_settings_get('DATAFILE_TMP_MAXSIZE', default='500M')
|
2010-12-21 09:36:09 +08:00
|
|
|
|
|
|
|
def _get_test_db_name(self):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
return self.connection.settings_dict['NAME']
|
2011-02-02 22:02:14 +08:00
|
|
|
|
|
|
|
def test_db_signature(self):
|
|
|
|
settings_dict = self.connection.settings_dict
|
|
|
|
return (
|
|
|
|
settings_dict['HOST'],
|
|
|
|
settings_dict['PORT'],
|
|
|
|
settings_dict['ENGINE'],
|
|
|
|
settings_dict['NAME'],
|
2011-02-19 15:41:17 +08:00
|
|
|
self._test_database_user(),
|
2011-02-02 22:02:14 +08:00
|
|
|
)
|