2008-08-11 20:11:25 +08:00
|
|
|
import os
|
2015-02-10 05:00:09 +08:00
|
|
|
import shutil
|
2008-08-11 20:11:25 +08:00
|
|
|
import sys
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2014-12-04 07:17:59 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.creation import BaseDatabaseCreation
|
2012-08-09 01:08:05 +08:00
|
|
|
from django.utils.six.moves import input
|
2008-08-11 20:11:25 +08:00
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseCreation(BaseDatabaseCreation):
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2010-12-09 07:48:28 +08:00
|
|
|
def _get_test_db_name(self):
|
2014-01-20 08:45:29 +08:00
|
|
|
test_database_name = self.connection.settings_dict['TEST']['NAME']
|
2015-10-06 01:25:06 +08:00
|
|
|
can_share_in_memory_db = self.connection.features.can_share_in_memory_db
|
2016-08-18 04:20:01 +08:00
|
|
|
if not test_database_name:
|
|
|
|
test_database_name = ':memory:'
|
2015-10-06 01:25:06 +08:00
|
|
|
if can_share_in_memory_db:
|
2016-08-18 04:20:01 +08:00
|
|
|
if test_database_name == ':memory:':
|
|
|
|
return 'file:memorydb_%s?mode=memory&cache=shared' % self.connection.alias
|
|
|
|
elif 'mode=memory' in test_database_name:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"Using a shared memory database with `mode=memory` in the "
|
|
|
|
"database name is not supported in your environment, "
|
|
|
|
"use `:memory:` instead."
|
|
|
|
)
|
|
|
|
return test_database_name
|
2010-12-09 07:48:28 +08:00
|
|
|
|
2014-05-28 05:13:08 +08:00
|
|
|
def _create_test_db(self, verbosity, autoclobber, keepdb=False):
|
2010-12-09 07:48:28 +08:00
|
|
|
test_database_name = self._get_test_db_name()
|
2014-12-04 07:17:59 +08:00
|
|
|
|
2014-05-28 05:13:08 +08:00
|
|
|
if keepdb:
|
|
|
|
return test_database_name
|
2014-12-04 07:17:59 +08:00
|
|
|
if not self.connection.is_in_memory_db(test_database_name):
|
2008-08-11 20:11:25 +08:00
|
|
|
# Erase the old test database
|
|
|
|
if verbosity >= 1:
|
2015-07-30 18:56:54 +08:00
|
|
|
print("Destroying old test database for alias %s..." % (
|
|
|
|
self._get_database_display_str(verbosity, test_database_name),
|
|
|
|
))
|
2008-08-11 20:11:25 +08:00
|
|
|
if os.access(test_database_name, os.F_OK):
|
|
|
|
if not autoclobber:
|
2014-09-04 20:15:09 +08:00
|
|
|
confirm = input(
|
|
|
|
"Type 'yes' if you would like to try deleting the test "
|
|
|
|
"database '%s', or 'no' to cancel: " % test_database_name
|
|
|
|
)
|
2008-08-11 20:11:25 +08:00
|
|
|
if autoclobber or confirm == 'yes':
|
2012-10-23 01:23:19 +08:00
|
|
|
try:
|
|
|
|
os.remove(test_database_name)
|
|
|
|
except Exception as e:
|
|
|
|
sys.stderr.write("Got an error deleting the old test database: %s\n" % e)
|
|
|
|
sys.exit(2)
|
2008-08-11 20:11:25 +08:00
|
|
|
else:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("Tests cancelled.")
|
2008-08-11 20:11:25 +08:00
|
|
|
sys.exit(1)
|
|
|
|
return test_database_name
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2015-02-10 05:00:09 +08:00
|
|
|
def get_test_db_clone_settings(self, number):
|
|
|
|
orig_settings_dict = self.connection.settings_dict
|
|
|
|
source_database_name = orig_settings_dict['NAME']
|
|
|
|
if self.connection.is_in_memory_db(source_database_name):
|
|
|
|
return orig_settings_dict
|
|
|
|
else:
|
|
|
|
new_settings_dict = orig_settings_dict.copy()
|
|
|
|
root, ext = os.path.splitext(orig_settings_dict['NAME'])
|
|
|
|
new_settings_dict['NAME'] = '{}_{}.{}'.format(root, number, ext)
|
|
|
|
return new_settings_dict
|
|
|
|
|
|
|
|
def _clone_test_db(self, number, verbosity, keepdb=False):
|
|
|
|
source_database_name = self.connection.settings_dict['NAME']
|
|
|
|
target_database_name = self.get_test_db_clone_settings(number)['NAME']
|
|
|
|
# Forking automatically makes a copy of an in-memory database.
|
|
|
|
if not self.connection.is_in_memory_db(source_database_name):
|
|
|
|
# Erase the old test database
|
|
|
|
if os.access(target_database_name, os.F_OK):
|
|
|
|
if keepdb:
|
|
|
|
return
|
|
|
|
if verbosity >= 1:
|
2015-07-30 18:56:54 +08:00
|
|
|
print("Destroying old test database for alias %s..." % (
|
|
|
|
self._get_database_display_str(verbosity, target_database_name),
|
|
|
|
))
|
2015-02-10 05:00:09 +08:00
|
|
|
try:
|
|
|
|
os.remove(target_database_name)
|
|
|
|
except Exception as e:
|
|
|
|
sys.stderr.write("Got an error deleting the old test database: %s\n" % e)
|
|
|
|
sys.exit(2)
|
|
|
|
try:
|
|
|
|
shutil.copy(source_database_name, target_database_name)
|
|
|
|
except Exception as e:
|
|
|
|
sys.stderr.write("Got an error cloning the test database: %s\n" % e)
|
|
|
|
sys.exit(2)
|
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def _destroy_test_db(self, test_database_name, verbosity):
|
2014-12-04 07:17:59 +08:00
|
|
|
if test_database_name and not self.connection.is_in_memory_db(test_database_name):
|
2008-08-11 20:11:25 +08:00
|
|
|
# Remove the SQLite database file
|
2008-08-16 12:48:57 +08:00
|
|
|
os.remove(test_database_name)
|
2011-07-07 07:25:30 +08:00
|
|
|
|
2012-03-14 01:52:48 +08:00
|
|
|
def test_db_signature(self):
|
|
|
|
"""
|
|
|
|
Returns a tuple that uniquely identifies a test database.
|
|
|
|
|
|
|
|
This takes into account the special cases of ":memory:" and "" for
|
|
|
|
SQLite since the databases will be distinct despite having the same
|
2014-01-20 08:45:29 +08:00
|
|
|
TEST NAME. See http://www.sqlite.org/inmemorydb.html
|
2012-03-14 01:52:48 +08:00
|
|
|
"""
|
2014-12-04 07:17:59 +08:00
|
|
|
test_database_name = self._get_test_db_name()
|
2012-03-14 01:52:48 +08:00
|
|
|
sig = [self.connection.settings_dict['NAME']]
|
2014-12-04 07:17:59 +08:00
|
|
|
if self.connection.is_in_memory_db(test_database_name):
|
2012-03-14 01:52:48 +08:00
|
|
|
sig.append(self.connection.alias)
|
|
|
|
return tuple(sig)
|