|
0
|
1 |
import os |
|
|
2 |
import sys |
|
|
3 |
from django.conf import settings |
|
|
4 |
from django.db.backends.creation import BaseDatabaseCreation |
|
|
5 |
|
|
|
6 |
class DatabaseCreation(BaseDatabaseCreation): |
|
|
7 |
# SQLite doesn't actually support most of these types, but it "does the right |
|
|
8 |
# thing" given more verbose field definitions, so leave them as is so that |
|
|
9 |
# schema inspection is more useful. |
|
|
10 |
data_types = { |
|
|
11 |
'AutoField': 'integer', |
|
|
12 |
'BooleanField': 'bool', |
|
|
13 |
'CharField': 'varchar(%(max_length)s)', |
|
|
14 |
'CommaSeparatedIntegerField': 'varchar(%(max_length)s)', |
|
|
15 |
'DateField': 'date', |
|
|
16 |
'DateTimeField': 'datetime', |
|
|
17 |
'DecimalField': 'decimal', |
|
|
18 |
'FileField': 'varchar(%(max_length)s)', |
|
|
19 |
'FilePathField': 'varchar(%(max_length)s)', |
|
|
20 |
'FloatField': 'real', |
|
|
21 |
'IntegerField': 'integer', |
|
|
22 |
'IPAddressField': 'char(15)', |
|
|
23 |
'NullBooleanField': 'bool', |
|
|
24 |
'OneToOneField': 'integer', |
|
|
25 |
'PositiveIntegerField': 'integer unsigned', |
|
|
26 |
'PositiveSmallIntegerField': 'smallint unsigned', |
|
|
27 |
'SlugField': 'varchar(%(max_length)s)', |
|
|
28 |
'SmallIntegerField': 'smallint', |
|
|
29 |
'TextField': 'text', |
|
|
30 |
'TimeField': 'time', |
|
|
31 |
} |
|
|
32 |
|
|
|
33 |
def sql_for_pending_references(self, model, style, pending_references): |
|
|
34 |
"SQLite3 doesn't support constraints" |
|
|
35 |
return [] |
|
|
36 |
|
|
|
37 |
def sql_remove_table_constraints(self, model, references_to_delete, style): |
|
|
38 |
"SQLite3 doesn't support constraints" |
|
|
39 |
return [] |
|
|
40 |
|
|
|
41 |
def _create_test_db(self, verbosity, autoclobber): |
|
|
42 |
if settings.TEST_DATABASE_NAME and settings.TEST_DATABASE_NAME != ":memory:": |
|
|
43 |
test_database_name = settings.TEST_DATABASE_NAME |
|
|
44 |
# Erase the old test database |
|
|
45 |
if verbosity >= 1: |
|
|
46 |
print "Destroying old test database..." |
|
|
47 |
if os.access(test_database_name, os.F_OK): |
|
|
48 |
if not autoclobber: |
|
|
49 |
confirm = raw_input("Type 'yes' if you would like to try deleting the test database '%s', or 'no' to cancel: " % test_database_name) |
|
|
50 |
if autoclobber or confirm == 'yes': |
|
|
51 |
try: |
|
|
52 |
if verbosity >= 1: |
|
|
53 |
print "Destroying old test database..." |
|
|
54 |
os.remove(test_database_name) |
|
|
55 |
except Exception, e: |
|
|
56 |
sys.stderr.write("Got an error deleting the old test database: %s\n" % e) |
|
|
57 |
sys.exit(2) |
|
|
58 |
else: |
|
|
59 |
print "Tests cancelled." |
|
|
60 |
sys.exit(1) |
|
|
61 |
if verbosity >= 1: |
|
|
62 |
print "Creating test database..." |
|
|
63 |
else: |
|
|
64 |
test_database_name = ":memory:" |
|
|
65 |
return test_database_name |
|
|
66 |
|
|
|
67 |
def _destroy_test_db(self, test_database_name, verbosity): |
|
|
68 |
if test_database_name and test_database_name != ":memory:": |
|
|
69 |
# Remove the SQLite database file |
|
|
70 |
os.remove(test_database_name) |