2008-08-11 20:11:25 +08:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
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
|
|
|
|
|
|
|
class DatabaseCreation(BaseDatabaseCreation):
|
|
|
|
# SQLite doesn't actually support most of these types, but it "does the right
|
|
|
|
# thing" given more verbose field definitions, so leave them as is so that
|
|
|
|
# schema inspection is more useful.
|
|
|
|
data_types = {
|
|
|
|
'AutoField': 'integer',
|
2012-12-14 05:11:06 +08:00
|
|
|
'BinaryField': 'BLOB',
|
2008-08-11 20:11:25 +08:00
|
|
|
'BooleanField': 'bool',
|
|
|
|
'CharField': 'varchar(%(max_length)s)',
|
|
|
|
'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
|
|
|
|
'DateField': 'date',
|
|
|
|
'DateTimeField': 'datetime',
|
|
|
|
'DecimalField': 'decimal',
|
|
|
|
'FileField': 'varchar(%(max_length)s)',
|
|
|
|
'FilePathField': 'varchar(%(max_length)s)',
|
|
|
|
'FloatField': 'real',
|
|
|
|
'IntegerField': 'integer',
|
2009-12-17 23:10:38 +08:00
|
|
|
'BigIntegerField': 'bigint',
|
2008-08-11 20:11:25 +08:00
|
|
|
'IPAddressField': 'char(15)',
|
2011-06-11 21:48:24 +08:00
|
|
|
'GenericIPAddressField': 'char(39)',
|
2008-08-11 20:11:25 +08:00
|
|
|
'NullBooleanField': 'bool',
|
|
|
|
'OneToOneField': 'integer',
|
|
|
|
'PositiveIntegerField': 'integer unsigned',
|
|
|
|
'PositiveSmallIntegerField': 'smallint unsigned',
|
|
|
|
'SlugField': 'varchar(%(max_length)s)',
|
|
|
|
'SmallIntegerField': 'smallint',
|
|
|
|
'TextField': 'text',
|
|
|
|
'TimeField': 'time',
|
|
|
|
}
|
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):
|
2009-12-22 23:18:51 +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:':
|
|
|
|
return test_database_name
|
|
|
|
return ':memory:'
|
|
|
|
|
|
|
|
def _create_test_db(self, verbosity, autoclobber):
|
|
|
|
test_database_name = self._get_test_db_name()
|
|
|
|
if test_database_name != ':memory:':
|
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:
|
2012-08-09 01:08:05 +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):
|
|
|
|
if test_database_name and test_database_name != ":memory:":
|
|
|
|
# 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
|
|
|
|
TEST_NAME. See http://www.sqlite.org/inmemorydb.html
|
|
|
|
"""
|
|
|
|
settings_dict = self.connection.settings_dict
|
|
|
|
test_dbname = self._get_test_db_name()
|
|
|
|
sig = [self.connection.settings_dict['NAME']]
|
|
|
|
if test_dbname == ':memory:':
|
|
|
|
sig.append(self.connection.alias)
|
|
|
|
return tuple(sig)
|