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
|
|
|
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.creation import BaseDatabaseCreation
|
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
|
|
|
|
2016-08-18 08:34:18 +08:00
|
|
|
@staticmethod
|
|
|
|
def is_in_memory_db(database_name):
|
2017-03-04 22:47:49 +08:00
|
|
|
return database_name == ':memory:' or 'mode=memory' in database_name
|
2016-08-18 08:34:18 +08:00
|
|
|
|
2010-12-09 07:48:28 +08:00
|
|
|
def _get_test_db_name(self):
|
2017-11-06 23:23:29 +08:00
|
|
|
test_database_name = self.connection.settings_dict['TEST']['NAME'] or ':memory:'
|
2017-10-04 08:15:44 +08:00
|
|
|
if test_database_name == ':memory:':
|
|
|
|
return 'file:memorydb_%s?mode=memory&cache=shared' % self.connection.alias
|
2016-08-18 04:20:01 +08:00
|
|
|
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
|
2016-08-18 08:34:18 +08:00
|
|
|
if not self.is_in_memory_db(test_database_name):
|
2008-08-11 20:11:25 +08:00
|
|
|
# Erase the old test database
|
|
|
|
if verbosity >= 1:
|
2019-02-14 02:59:44 +08:00
|
|
|
self.log('Destroying old test database for alias %s...' % (
|
2015-07-30 18:56:54 +08:00
|
|
|
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:
|
2018-07-20 06:05:33 +08:00
|
|
|
self.log('Got an error deleting the old test database: %s' % e)
|
2012-10-23 01:23:19 +08:00
|
|
|
sys.exit(2)
|
2008-08-11 20:11:25 +08:00
|
|
|
else:
|
2018-07-20 06:05:33 +08:00
|
|
|
self.log('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
|
|
|
|
2017-09-11 23:32:40 +08:00
|
|
|
def get_test_db_clone_settings(self, suffix):
|
2015-02-10 05:00:09 +08:00
|
|
|
orig_settings_dict = self.connection.settings_dict
|
|
|
|
source_database_name = orig_settings_dict['NAME']
|
2016-08-18 08:34:18 +08:00
|
|
|
if self.is_in_memory_db(source_database_name):
|
2015-02-10 05:00:09 +08:00
|
|
|
return orig_settings_dict
|
|
|
|
else:
|
|
|
|
root, ext = os.path.splitext(orig_settings_dict['NAME'])
|
2018-02-27 01:23:31 +08:00
|
|
|
return {**orig_settings_dict, 'NAME': '{}_{}.{}'.format(root, suffix, ext)}
|
2015-02-10 05:00:09 +08:00
|
|
|
|
2017-09-11 23:32:40 +08:00
|
|
|
def _clone_test_db(self, suffix, verbosity, keepdb=False):
|
2015-02-10 05:00:09 +08:00
|
|
|
source_database_name = self.connection.settings_dict['NAME']
|
2017-09-11 23:32:40 +08:00
|
|
|
target_database_name = self.get_test_db_clone_settings(suffix)['NAME']
|
2015-02-10 05:00:09 +08:00
|
|
|
# Forking automatically makes a copy of an in-memory database.
|
2016-08-18 08:34:18 +08:00
|
|
|
if not self.is_in_memory_db(source_database_name):
|
2015-02-10 05:00:09 +08:00
|
|
|
# Erase the old test database
|
|
|
|
if os.access(target_database_name, os.F_OK):
|
|
|
|
if keepdb:
|
|
|
|
return
|
|
|
|
if verbosity >= 1:
|
2019-02-14 02:59:44 +08:00
|
|
|
self.log('Destroying old test database for alias %s...' % (
|
2015-07-30 18:56:54 +08:00
|
|
|
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:
|
2018-07-20 06:05:33 +08:00
|
|
|
self.log('Got an error deleting the old test database: %s' % e)
|
2015-02-10 05:00:09 +08:00
|
|
|
sys.exit(2)
|
|
|
|
try:
|
|
|
|
shutil.copy(source_database_name, target_database_name)
|
|
|
|
except Exception as e:
|
2018-07-20 06:05:33 +08:00
|
|
|
self.log('Got an error cloning the test database: %s' % e)
|
2015-02-10 05:00:09 +08:00
|
|
|
sys.exit(2)
|
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def _destroy_test_db(self, test_database_name, verbosity):
|
2016-08-18 08:34:18 +08:00
|
|
|
if test_database_name and not self.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):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return a tuple that uniquely identifies a test database.
|
2012-03-14 01:52:48 +08:00
|
|
|
|
|
|
|
This takes into account the special cases of ":memory:" and "" for
|
|
|
|
SQLite since the databases will be distinct despite having the same
|
2018-09-26 14:48:47 +08:00
|
|
|
TEST NAME. See https://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']]
|
2016-08-18 08:34:18 +08:00
|
|
|
if self.is_in_memory_db(test_database_name):
|
2012-03-14 01:52:48 +08:00
|
|
|
sig.append(self.connection.alias)
|
|
|
|
return tuple(sig)
|