2
from django.conf import settings
3
from django.core.exceptions import ImproperlyConfigured
4
from django.core.management import call_command
5
from django.db.backends.sqlite3.creation import DatabaseCreation
7
class SpatiaLiteCreation(DatabaseCreation):
9
def create_test_db(self, verbosity=1, autoclobber=False):
11
Creates a test database, prompting the user for confirmation if the
12
database already exists. Returns the name of the test database created.
14
This method is overloaded to load up the SpatiaLite initialization
15
SQL prior to calling the `syncdb` command.
18
print "Creating test database '%s'..." % self.connection.alias
20
test_database_name = self._create_test_db(verbosity, autoclobber)
22
self.connection.close()
24
self.connection.settings_dict["NAME"] = test_database_name
25
can_rollback = self._rollback_works()
26
self.connection.settings_dict["SUPPORTS_TRANSACTIONS"] = can_rollback
27
# Need to load the SpatiaLite initialization SQL before running `syncdb`.
28
self.load_spatialite_sql()
29
call_command('syncdb', verbosity=verbosity, interactive=False, database=self.connection.alias)
31
if settings.CACHE_BACKEND.startswith('db://'):
32
from django.core.cache import parse_backend_uri
33
_, cache_name, _ = parse_backend_uri(settings.CACHE_BACKEND)
34
call_command('createcachetable', cache_name)
36
# Get a cursor (even though we don't need one yet). This has
37
# the side effect of initializing the test database.
38
cursor = self.connection.cursor()
40
return test_database_name
42
def sql_indexes_for_field(self, model, f, style):
43
"Return any spatial index creation SQL for the field."
44
from django.contrib.gis.db.models.fields import GeometryField
46
output = super(SpatiaLiteCreation, self).sql_indexes_for_field(model, f, style)
48
if isinstance(f, GeometryField):
49
gqn = self.connection.ops.geo_quote_name
50
qn = self.connection.ops.quote_name
51
db_table = model._meta.db_table
53
output.append(style.SQL_KEYWORD('SELECT ') +
54
style.SQL_TABLE('AddGeometryColumn') + '(' +
55
style.SQL_TABLE(gqn(db_table)) + ', ' +
56
style.SQL_FIELD(gqn(f.column)) + ', ' +
57
style.SQL_FIELD(str(f.srid)) + ', ' +
58
style.SQL_COLTYPE(gqn(f.geom_type)) + ', ' +
59
style.SQL_KEYWORD(str(f.dim)) + ', ' +
60
style.SQL_KEYWORD(str(int(not f.null))) +
64
output.append(style.SQL_KEYWORD('SELECT ') +
65
style.SQL_TABLE('CreateSpatialIndex') + '(' +
66
style.SQL_TABLE(gqn(db_table)) + ', ' +
67
style.SQL_FIELD(gqn(f.column)) + ');')
71
def load_spatialite_sql(self):
73
This routine loads up the SpatiaLite SQL file.
75
# Getting the location of the SpatiaLite SQL file, and confirming
77
spatialite_sql = self.spatialite_init_file()
78
if not os.path.isfile(spatialite_sql):
79
raise ImproperlyConfigured('Could not find the required SpatiaLite initialization '
80
'SQL file (necessary for testing): %s' % spatialite_sql)
82
# Opening up the SpatiaLite SQL initialization file and executing
84
sql_fh = open(spatialite_sql, 'r')
86
cur = self.connection._cursor()
87
cur.executescript(sql_fh.read())
91
def spatialite_init_file(self):
92
# SPATIALITE_SQL may be placed in settings to tell GeoDjango
93
# to use a specific path to the SpatiaLite initilization SQL.
94
return getattr(settings, 'SPATIALITE_SQL',
95
'init_spatialite-%s.%s.sql' %
96
self.connection.ops.spatial_version[:2])