summaryrefslogtreecommitdiff
path: root/django/db
diff options
context:
space:
mode:
authorGreg Chapple <gregchapple1@gmail.com>2014-05-27 22:13:08 +0100
committerGreg Chapple <gregchapple1@gmail.com>2014-05-28 16:22:46 +0100
commitb7aa7c4ab4372d2b7994d252c8bc87f77dd217ae (patch)
tree2466569c39afe585f6870dcf26bb9ec42f052c2b /django/db
parent2e613ea5c52d4d12e071987178af18c0371d6e2f (diff)
Fixed #20550 -- Added ability to preserve test db between runs
Diffstat (limited to 'django/db')
-rw-r--r--django/db/backends/creation.py24
-rw-r--r--django/db/backends/oracle/creation.py6
-rw-r--r--django/db/backends/sqlite3/creation.py4
3 files changed, 27 insertions, 7 deletions
diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
index b2b82c13c2..72fea241dc 100644
--- a/django/db/backends/creation.py
+++ b/django/db/backends/creation.py
@@ -332,7 +332,7 @@ class BaseDatabaseCreation(object):
";",
]
- def create_test_db(self, verbosity=1, autoclobber=False):
+ def create_test_db(self, verbosity=1, autoclobber=False, keepdb=False):
"""
Creates a test database, prompting the user for confirmation if the
database already exists. Returns the name of the test database created.
@@ -344,12 +344,21 @@ class BaseDatabaseCreation(object):
if verbosity >= 1:
test_db_repr = ''
+ action = 'Creating'
if verbosity >= 2:
test_db_repr = " ('%s')" % test_database_name
- print("Creating test database for alias '%s'%s..." % (
- self.connection.alias, test_db_repr))
+ if keepdb:
+ action = "Using existing"
+
+ print("%s test database for alias '%s'%s..." % (
+ action, self.connection.alias, test_db_repr))
- self._create_test_db(verbosity, autoclobber)
+ # We could skip this call if keepdb is True, but we instead
+ # give it the keepdb param. This is to handle the case
+ # where the test DB doesn't exist, in which case we need to
+ # create it, then just not destroy it. If we instead skip
+ # this, we will get an exception.
+ self._create_test_db(verbosity, autoclobber, keepdb)
self.connection.close()
settings.DATABASES[self.connection.alias]["NAME"] = test_database_name
@@ -393,7 +402,7 @@ class BaseDatabaseCreation(object):
return self.connection.settings_dict['TEST']['NAME']
return TEST_DATABASE_PREFIX + self.connection.settings_dict['NAME']
- def _create_test_db(self, verbosity, autoclobber):
+ def _create_test_db(self, verbosity, autoclobber, keepdb=False):
"""
Internal implementation - creates the test db tables.
"""
@@ -409,6 +418,11 @@ class BaseDatabaseCreation(object):
cursor.execute(
"CREATE DATABASE %s %s" % (qn(test_database_name), suffix))
except Exception as e:
+ # 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 test_database_name
+
sys.stderr.write(
"Got an error creating the test database: %s\n" % e)
if not autoclobber:
diff --git a/django/db/backends/oracle/creation.py b/django/db/backends/oracle/creation.py
index 90cc83fd58..d93aac4d77 100644
--- a/django/db/backends/oracle/creation.py
+++ b/django/db/backends/oracle/creation.py
@@ -56,7 +56,7 @@ class DatabaseCreation(BaseDatabaseCreation):
def __init__(self, connection):
super(DatabaseCreation, self).__init__(connection)
- def _create_test_db(self, verbosity=1, autoclobber=False):
+ def _create_test_db(self, verbosity=1, autoclobber=False, keepdb=False):
TEST_NAME = self._test_database_name()
TEST_USER = self._test_database_user()
TEST_PASSWD = self._test_database_passwd()
@@ -76,6 +76,10 @@ class DatabaseCreation(BaseDatabaseCreation):
try:
self._execute_test_db_creation(cursor, parameters, verbosity)
except Exception as e:
+ # 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
sys.stderr.write("Got an error creating the test database: %s\n" % e)
if not autoclobber:
confirm = input("It appears the test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_NAME)
diff --git a/django/db/backends/sqlite3/creation.py b/django/db/backends/sqlite3/creation.py
index abcd068952..2519add07c 100644
--- a/django/db/backends/sqlite3/creation.py
+++ b/django/db/backends/sqlite3/creation.py
@@ -52,8 +52,10 @@ class DatabaseCreation(BaseDatabaseCreation):
return test_database_name
return ':memory:'
- def _create_test_db(self, verbosity, autoclobber):
+ def _create_test_db(self, verbosity, autoclobber, keepdb=False):
test_database_name = self._get_test_db_name()
+ if keepdb:
+ return test_database_name
if test_database_name != ':memory:':
# Erase the old test database
if verbosity >= 1: