|
@@ -1,76 +1,8 @@
|
|
|
-import os
|
|
|
-
|
|
|
-from django.conf import settings
|
|
|
-from django.core.exceptions import ImproperlyConfigured
|
|
|
from django.db.backends.sqlite3.creation import DatabaseCreation
|
|
|
|
|
|
|
|
|
class SpatiaLiteCreation(DatabaseCreation):
|
|
|
|
|
|
- def create_test_db(self, verbosity=1, autoclobber=False, serialize=True, keepdb=False):
|
|
|
- """
|
|
|
- Creates a test database, prompting the user for confirmation if the
|
|
|
- database already exists. Returns the name of the test database created.
|
|
|
-
|
|
|
- This method is overloaded to load up the SpatiaLite initialization
|
|
|
- SQL prior to calling the `migrate` command.
|
|
|
- """
|
|
|
-
|
|
|
- from django.core.management import call_command
|
|
|
-
|
|
|
- test_database_name = self._get_test_db_name()
|
|
|
-
|
|
|
- if verbosity >= 1:
|
|
|
- test_db_repr = ''
|
|
|
- action = 'Creating'
|
|
|
- if verbosity >= 2:
|
|
|
- test_db_repr = " ('%s')" % test_database_name
|
|
|
- if keepdb:
|
|
|
- action = 'Using existing'
|
|
|
- print("%s test database for alias '%s'%s..." % (
|
|
|
- action, self.connection.alias, test_db_repr))
|
|
|
-
|
|
|
- self._create_test_db(verbosity, autoclobber, keepdb)
|
|
|
-
|
|
|
- self.connection.close()
|
|
|
- self.connection.settings_dict["NAME"] = test_database_name
|
|
|
-
|
|
|
-
|
|
|
- self.load_spatialite_sql()
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- call_command('migrate',
|
|
|
- verbosity=max(verbosity - 1, 0),
|
|
|
- interactive=False,
|
|
|
- database=self.connection.alias,
|
|
|
- load_initial_data=False)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if serialize:
|
|
|
- self.connection._test_serialized_contents = self.serialize_db_to_string()
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- call_command('flush',
|
|
|
- verbosity=max(verbosity - 1, 0),
|
|
|
- interactive=False,
|
|
|
- database=self.connection.alias)
|
|
|
-
|
|
|
- call_command('createcachetable', database=self.connection.alias)
|
|
|
-
|
|
|
-
|
|
|
- self.connection.ensure_connection()
|
|
|
-
|
|
|
- return test_database_name
|
|
|
-
|
|
|
def sql_indexes_for_field(self, model, f, style):
|
|
|
"Return any spatial index creation SQL for the field."
|
|
|
from django.contrib.gis.db.models.fields import GeometryField
|
|
@@ -99,35 +31,9 @@ class SpatiaLiteCreation(DatabaseCreation):
|
|
|
|
|
|
return output
|
|
|
|
|
|
- def load_spatialite_sql(self):
|
|
|
+ def _create_test_db_pre_migrate_sql(self):
|
|
|
"""
|
|
|
- This routine loads up the SpatiaLite SQL file.
|
|
|
+ Creates the spatial metadata tables.
|
|
|
"""
|
|
|
- if self.connection.ops.spatial_version[:2] >= (2, 4):
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- cur = self.connection._cursor()
|
|
|
- cur.execute("SELECT InitSpatialMetaData()")
|
|
|
- else:
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- spatialite_sql = self.spatialite_init_file()
|
|
|
- if not os.path.isfile(spatialite_sql):
|
|
|
- raise ImproperlyConfigured('Could not find the required SpatiaLite initialization '
|
|
|
- 'SQL file (necessary for testing): %s' % spatialite_sql)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- with open(spatialite_sql, 'r') as sql_fh:
|
|
|
- cur = self.connection._cursor()
|
|
|
- cur.executescript(sql_fh.read())
|
|
|
-
|
|
|
- def spatialite_init_file(self):
|
|
|
-
|
|
|
-
|
|
|
- return getattr(settings, 'SPATIALITE_SQL',
|
|
|
- 'init_spatialite-%s.%s.sql' %
|
|
|
- self.connection.ops.spatial_version[:2])
|
|
|
+ cur = self.connection._cursor()
|
|
|
+ cur.execute("SELECT InitSpatialMetaData()")
|