2011-07-13 17:35:51 +08:00
|
|
|
import sys
|
|
|
|
import time
|
2008-08-11 20:11:25 +08:00
|
|
|
from django.db.backends.creation import BaseDatabaseCreation
|
2007-06-23 22:16:00 +08:00
|
|
|
|
|
|
|
TEST_DATABASE_PREFIX = 'test_'
|
|
|
|
PASSWORD = 'Im_a_lumberjack'
|
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseCreation(BaseDatabaseCreation):
|
|
|
|
# This dictionary maps Field objects to their associated Oracle column
|
|
|
|
# types, as strings. Column-type strings can contain format strings; they'll
|
|
|
|
# be interpolated against the values of Field.__dict__ before being output.
|
|
|
|
# If a column type is set to None, it won't be included in the output.
|
|
|
|
#
|
|
|
|
# Any format strings starting with "qn_" are quoted before being used in the
|
|
|
|
# output (the "qn_" prefix is stripped before the lookup is performed.
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
data_types = {
|
|
|
|
'AutoField': 'NUMBER(11)',
|
|
|
|
'BooleanField': 'NUMBER(1) CHECK (%(qn_column)s IN (0,1))',
|
|
|
|
'CharField': 'NVARCHAR2(%(max_length)s)',
|
|
|
|
'CommaSeparatedIntegerField': 'VARCHAR2(%(max_length)s)',
|
|
|
|
'DateField': 'DATE',
|
|
|
|
'DateTimeField': 'TIMESTAMP',
|
|
|
|
'DecimalField': 'NUMBER(%(max_digits)s, %(decimal_places)s)',
|
|
|
|
'FileField': 'NVARCHAR2(%(max_length)s)',
|
|
|
|
'FilePathField': 'NVARCHAR2(%(max_length)s)',
|
|
|
|
'FloatField': 'DOUBLE PRECISION',
|
|
|
|
'IntegerField': 'NUMBER(11)',
|
2009-12-17 23:10:38 +08:00
|
|
|
'BigIntegerField': 'NUMBER(19)',
|
2008-08-11 20:11:25 +08:00
|
|
|
'IPAddressField': 'VARCHAR2(15)',
|
2011-06-11 21:48:24 +08:00
|
|
|
'GenericIPAddressField': 'VARCHAR2(39)',
|
2008-08-11 20:11:25 +08:00
|
|
|
'NullBooleanField': 'NUMBER(1) CHECK ((%(qn_column)s IN (0,1)) OR (%(qn_column)s IS NULL))',
|
|
|
|
'OneToOneField': 'NUMBER(11)',
|
|
|
|
'PositiveIntegerField': 'NUMBER(11) CHECK (%(qn_column)s >= 0)',
|
|
|
|
'PositiveSmallIntegerField': 'NUMBER(11) CHECK (%(qn_column)s >= 0)',
|
2008-12-03 02:40:40 +08:00
|
|
|
'SlugField': 'NVARCHAR2(%(max_length)s)',
|
2008-08-11 20:11:25 +08:00
|
|
|
'SmallIntegerField': 'NUMBER(11)',
|
|
|
|
'TextField': 'NCLOB',
|
|
|
|
'TimeField': 'TIMESTAMP',
|
|
|
|
'URLField': 'VARCHAR2(%(max_length)s)',
|
|
|
|
}
|
2008-08-13 02:49:44 +08:00
|
|
|
|
2010-11-08 04:58:46 +08:00
|
|
|
def __init__(self, connection):
|
|
|
|
super(DatabaseCreation, self).__init__(connection)
|
2008-08-13 02:49:44 +08:00
|
|
|
|
|
|
|
def _create_test_db(self, verbosity=1, autoclobber=False):
|
2010-12-21 09:36:09 +08:00
|
|
|
TEST_NAME = self._test_database_name()
|
2009-12-22 23:18:51 +08:00
|
|
|
TEST_USER = self._test_database_user()
|
|
|
|
TEST_PASSWD = self._test_database_passwd()
|
|
|
|
TEST_TBLSPACE = self._test_database_tblspace()
|
|
|
|
TEST_TBLSPACE_TMP = self._test_database_tblspace_tmp()
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
parameters = {
|
2009-12-22 23:18:51 +08:00
|
|
|
'dbname': TEST_NAME,
|
|
|
|
'user': TEST_USER,
|
|
|
|
'password': TEST_PASSWD,
|
|
|
|
'tblspace': TEST_TBLSPACE,
|
|
|
|
'tblspace_temp': TEST_TBLSPACE_TMP,
|
2008-08-13 02:49:44 +08:00
|
|
|
}
|
2007-06-23 22:16:00 +08:00
|
|
|
|
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:
|
2008-08-11 20:11:25 +08:00
|
|
|
sys.stderr.write("Got an error creating the test database: %s\n" % e)
|
|
|
|
if not autoclobber:
|
2009-12-22 23:18:51 +08:00
|
|
|
confirm = raw_input("It appears the test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_NAME)
|
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 database '%s'..." % self.connection.alias)
|
2008-08-11 20:11:25 +08:00
|
|
|
self._execute_test_db_destruction(cursor, parameters, verbosity)
|
|
|
|
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:
|
2009-12-22 23:18:51 +08:00
|
|
|
confirm = raw_input("It appears the test user, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_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
|
|
|
|
2012-02-29 05:23:45 +08:00
|
|
|
self.connection.settings_dict['SAVED_USER'] = self.connection.settings_dict['USER']
|
|
|
|
self.connection.settings_dict['SAVED_PASSWORD'] = self.connection.settings_dict['PASSWORD']
|
|
|
|
self.connection.settings_dict['TEST_USER'] = self.connection.settings_dict['USER'] = TEST_USER
|
|
|
|
self.connection.settings_dict['PASSWORD'] = TEST_PASSWD
|
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
|
|
|
|
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.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
TEST_NAME = self._test_database_name()
|
|
|
|
TEST_USER = self._test_database_user()
|
|
|
|
TEST_PASSWD = self._test_database_passwd()
|
|
|
|
TEST_TBLSPACE = self._test_database_tblspace()
|
|
|
|
TEST_TBLSPACE_TMP = self._test_database_tblspace_tmp()
|
2007-06-23 22:16:00 +08:00
|
|
|
|
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']
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
parameters = {
|
2009-12-22 23:18:51 +08:00
|
|
|
'dbname': TEST_NAME,
|
|
|
|
'user': TEST_USER,
|
|
|
|
'password': TEST_PASSWD,
|
|
|
|
'tblspace': TEST_TBLSPACE,
|
|
|
|
'tblspace_temp': TEST_TBLSPACE_TMP,
|
2008-08-13 02:49:44 +08:00
|
|
|
}
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
cursor = self.connection.cursor()
|
|
|
|
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:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("_create_test_db(): dbname = %s" % parameters['dbname'])
|
2008-08-11 20:11:25 +08:00
|
|
|
statements = [
|
|
|
|
"""CREATE TABLESPACE %(tblspace)s
|
|
|
|
DATAFILE '%(tblspace)s.dbf' SIZE 20M
|
2010-11-06 12:30:17 +08:00
|
|
|
REUSE AUTOEXTEND ON NEXT 10M MAXSIZE 200M
|
2008-08-11 20:11:25 +08:00
|
|
|
""",
|
|
|
|
"""CREATE TEMPORARY TABLESPACE %(tblspace_temp)s
|
|
|
|
TEMPFILE '%(tblspace_temp)s.dbf' SIZE 20M
|
|
|
|
REUSE AUTOEXTEND ON NEXT 10M MAXSIZE 100M
|
|
|
|
""",
|
|
|
|
]
|
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
|
|
|
|
""",
|
|
|
|
"""GRANT CONNECT, RESOURCE TO %(user)s""",
|
|
|
|
]
|
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 _execute_test_db_destruction(self, cursor, parameters, verbosity):
|
2008-08-11 20:11:25 +08:00
|
|
|
if verbosity >= 2:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("_execute_test_db_destruction(): dbname=%s" % parameters['dbname'])
|
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',
|
|
|
|
]
|
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
|
|
|
|
2008-08-13 02:49:44 +08:00
|
|
|
def _execute_statements(self, cursor, statements, parameters, verbosity):
|
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:
|
2008-08-11 20:11:25 +08:00
|
|
|
sys.stderr.write("Failed (%s)\n" % (err))
|
|
|
|
raise
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2010-12-21 09:36:09 +08:00
|
|
|
def _test_database_name(self):
|
2009-12-22 23:18:51 +08:00
|
|
|
name = TEST_DATABASE_PREFIX + self.connection.settings_dict['NAME']
|
2007-06-23 22:16:00 +08:00
|
|
|
try:
|
2009-12-22 23:18:51 +08:00
|
|
|
if self.connection.settings_dict['TEST_NAME']:
|
|
|
|
name = self.connection.settings_dict['TEST_NAME']
|
2008-08-11 20:11:25 +08:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
return name
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _test_database_create(self):
|
2010-11-08 04:58:46 +08:00
|
|
|
return self.connection.settings_dict.get('TEST_CREATE', True)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _test_user_create(self):
|
2010-11-08 04:58:46 +08:00
|
|
|
return self.connection.settings_dict.get('TEST_USER_CREATE', True)
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _test_database_user(self):
|
|
|
|
name = TEST_DATABASE_PREFIX + self.connection.settings_dict['USER']
|
2008-08-11 20:11:25 +08:00
|
|
|
try:
|
2009-12-22 23:18:51 +08:00
|
|
|
if self.connection.settings_dict['TEST_USER']:
|
|
|
|
name = self.connection.settings_dict['TEST_USER']
|
|
|
|
except KeyError:
|
2008-08-11 20:11:25 +08:00
|
|
|
pass
|
|
|
|
return name
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _test_database_passwd(self):
|
2008-08-11 20:11:25 +08:00
|
|
|
name = PASSWORD
|
|
|
|
try:
|
2009-12-22 23:18:51 +08:00
|
|
|
if self.connection.settings_dict['TEST_PASSWD']:
|
|
|
|
name = self.connection.settings_dict['TEST_PASSWD']
|
|
|
|
except KeyError:
|
2008-08-11 20:11:25 +08:00
|
|
|
pass
|
|
|
|
return name
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _test_database_tblspace(self):
|
|
|
|
name = TEST_DATABASE_PREFIX + self.connection.settings_dict['NAME']
|
2008-08-11 20:11:25 +08:00
|
|
|
try:
|
2009-12-22 23:18:51 +08:00
|
|
|
if self.connection.settings_dict['TEST_TBLSPACE']:
|
|
|
|
name = self.connection.settings_dict['TEST_TBLSPACE']
|
|
|
|
except KeyError:
|
2008-08-11 20:11:25 +08:00
|
|
|
pass
|
|
|
|
return name
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _test_database_tblspace_tmp(self):
|
|
|
|
name = TEST_DATABASE_PREFIX + self.connection.settings_dict['NAME'] + '_temp'
|
2008-08-11 20:11:25 +08:00
|
|
|
try:
|
2009-12-22 23:18:51 +08:00
|
|
|
if self.connection.settings_dict['TEST_TBLSPACE_TMP']:
|
|
|
|
name = self.connection.settings_dict['TEST_TBLSPACE_TMP']
|
|
|
|
except KeyError:
|
2008-08-11 20:11:25 +08:00
|
|
|
pass
|
|
|
|
return name
|
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
|
|
|
)
|
2011-07-07 07:25:30 +08:00
|
|
|
|
|
|
|
def set_autocommit(self):
|
|
|
|
self.connection.connection.autocommit = True
|