2008-08-11 20:11:25 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2014-12-04 07:17:59 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2008-08-11 20:11:25 +08:00
|
|
|
from django.db.backends.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
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def sql_for_pending_references(self, model, style, pending_references):
|
|
|
|
"SQLite3 doesn't support constraints"
|
|
|
|
return []
|
|
|
|
|
2008-08-15 15:35:47 +08:00
|
|
|
def sql_remove_table_constraints(self, model, references_to_delete, style):
|
2008-08-11 20:11:25 +08:00
|
|
|
"SQLite3 doesn't support constraints"
|
|
|
|
return []
|
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']
|
2010-12-09 07:48:28 +08:00
|
|
|
if test_database_name and test_database_name != ':memory:':
|
2014-12-04 07:17:59 +08:00
|
|
|
if 'mode=memory' in test_database_name:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"Using `mode=memory` parameter in the database name is not allowed, "
|
|
|
|
"use `:memory:` instead."
|
|
|
|
)
|
2010-12-09 07:48:28 +08:00
|
|
|
return test_database_name
|
2014-12-04 07:17:59 +08:00
|
|
|
if self.connection.features.can_share_in_memory_db:
|
|
|
|
return 'file:memorydb_%s?mode=memory&cache=shared' % self.connection.alias
|
2010-12-09 07:48:28 +08:00
|
|
|
return ':memory:'
|
|
|
|
|
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:
|
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
|
|
|
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
|
|
|
|
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)
|