Dropped support for SpatiaLite < 2.4.

This commit is contained in:
Tim Graham 2014-06-09 16:03:04 -04:00
parent 40bfd8561d
commit bc0aba7786
7 changed files with 24 additions and 161 deletions

View File

@ -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.
"""
# Don't import django.core.management if it isn't needed.
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
# Need to load the SpatiaLite initialization SQL before running `migrate`.
self.load_spatialite_sql()
# Report migrate messages at one level lower than that requested.
# This ensures we don't get flooded with messages during testing
# (unless you really ask to be flooded)
call_command('migrate',
verbosity=max(verbosity - 1, 0),
interactive=False,
database=self.connection.alias,
load_initial_data=False)
# We then serialize the current state of the database into a string
# and store it on the connection. This slightly horrific process is so people
# who are testing on databases without transactions or who are using
# a TransactionTestCase still get a clean database on every test run.
if serialize:
self.connection._test_serialized_contents = self.serialize_db_to_string()
# We need to then do a flush to ensure that any data installed by
# custom SQL has been removed. The only test data should come from
# test fixtures, or autogenerated from post_migrate triggers.
# This has the side effect of loading initial data (which was
# intentionally skipped in the migrate).
call_command('flush',
verbosity=max(verbosity - 1, 0),
interactive=False,
database=self.connection.alias)
call_command('createcachetable', database=self.connection.alias)
# Ensure a connection for the side effect of initializing the test database.
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):
# Spatialite >= 2.4 -- No need to load any SQL file, calling
# InitSpatialMetaData() transparently creates the spatial metadata
# tables
cur = self.connection._cursor()
cur.execute("SELECT InitSpatialMetaData()")
else:
# Spatialite < 2.4 -- Load the initial SQL
# Getting the location of the SpatiaLite SQL file, and confirming
# it exists.
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)
# Opening up the SpatiaLite SQL initialization file and executing
# as a script.
with open(spatialite_sql, 'r') as sql_fh:
cur = self.connection._cursor()
cur.executescript(sql_fh.read())
def spatialite_init_file(self):
# SPATIALITE_SQL may be placed in settings to tell GeoDjango
# to use a specific path to the SpatiaLite initialization SQL.
return getattr(settings, 'SPATIALITE_SQL',
'init_spatialite-%s.%s.sql' %
self.connection.ops.spatial_version[:2])
cur = self.connection._cursor()
cur.execute("SELECT InitSpatialMetaData()")

View File

@ -140,17 +140,15 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations):
'database (error was "%s"). Was the SpatiaLite initialization '
'SQL loaded on this database?') % (self.connection.settings_dict['NAME'], msg)
six.reraise(ImproperlyConfigured, ImproperlyConfigured(new_msg), sys.exc_info()[2])
if version < (2, 3, 0):
if version < (2, 4, 0):
raise ImproperlyConfigured('GeoDjango only supports SpatiaLite versions '
'2.3.0 and above')
'2.4.0 and above')
return version
@property
def _version_greater_2_4_0_rc4(self):
if self.spatial_version >= (2, 4, 1):
return True
elif self.spatial_version < (2, 4, 0):
return False
else:
# Spatialite 2.4.0-RC4 added AsGML and AsKML, however both
# RC2 (shipped in popular Debian/Ubuntu packages) and RC4
@ -274,24 +272,7 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations):
Returns the SpatiaLite version as a tuple (version string, major,
minor, subminor).
"""
# Getting the SpatiaLite version.
try:
version = self.spatialite_version()
except DatabaseError:
# The `spatialite_version` function first appeared in version 2.3.1
# of SpatiaLite, so doing a fallback test for 2.3.0 (which is
# used by popular Debian/Ubuntu packages).
version = None
try:
tmp = self._get_spatialite_func("X(GeomFromText('POINT(1 1)'))")
if tmp == 1.0:
version = '2.3.0'
except DatabaseError:
pass
# If no version string defined, then just re-raise the original
# exception.
if version is None:
raise
version = self.spatialite_version()
m = self.version_regex.match(version)
if m:

View File

@ -369,6 +369,8 @@ class BaseDatabaseCreation(object):
settings.DATABASES[self.connection.alias]["NAME"] = test_database_name
self.connection.settings_dict["NAME"] = test_database_name
self._create_test_db_pre_migrate_sql()
# We report migrate messages at one level lower than that requested.
# This ensures we don't get flooded with messages during testing
# (unless you really ask to be flooded).
@ -402,6 +404,12 @@ class BaseDatabaseCreation(object):
return test_database_name
def _create_test_db_pre_migrate_sql(self):
"""
Hook for databases to load SQL before creating the test DB.
"""
pass
def serialize_db_to_string(self):
"""
Serializes all data in the database into a JSON string.

View File

@ -12,10 +12,10 @@ Program Description Required
======================== ==================================== ================================ ==========================
:ref:`GEOS <ref-geos>` Geometry Engine Open Source Yes 3.4, 3.3, 3.2
`PROJ.4`_ Cartographic Projections library Yes (PostgreSQL and SQLite only) 4.8, 4.7, 4.6, 4.5, 4.4
:ref:`GDAL <ref-gdal>` Geospatial Data Abstraction Library No (but, required for SQLite) 1.10, 1.9, 1.8, 1.7
:ref:`GDAL <ref-gdal>` Geospatial Data Abstraction Library Yes (SQLite only) 1.10, 1.9, 1.8, 1.7
:ref:`GeoIP <ref-geoip>` IP-based geolocation library No 1.4
`PostGIS`__ Spatial extensions for PostgreSQL Yes (PostgreSQL only) 2.1, 2.0, 1.5, 1.4, 1.3
`SpatiaLite`__ Spatial extensions for SQLite Yes (SQLite only) 4.1, 4.0, 3.0, 2.4, 2.3
`SpatiaLite`__ Spatial extensions for SQLite Yes (SQLite only) 4.1, 4.0, 3.0, 2.4
======================== ==================================== ================================ ==========================
Note that older or more recent versions of these libraries *may* also work
@ -35,7 +35,6 @@ totally fine with GeoDjango. Your mileage may vary.
PostGIS 1.5.0 2010-02-04
PostGIS 2.0.0 2012-04-03
PostGIS 2.1.0 2013-08-17
SpatiaLite 2.3.0 2009-04-06
Spatialite 2.4.0 2010-11-14
Spatialite 3.0.0 2011-12-30
Spatialite 4.0.0 2012-11-25

View File

@ -73,7 +73,7 @@ Prior to attempting to build, please read the important notes below to see if
customization of the ``configure`` command is necessary. If not, then run the
``configure`` script, make, and install for the SpatiaLite library::
$ cd libspatialite-amalgamation-2.3.1
$ cd libspatialite-amalgamation-2.4.0-5
$ ./configure # May need to be modified, see notes below.
$ make
$ sudo make install
@ -83,7 +83,7 @@ customization of the ``configure`` command is necessary. If not, then run the
Finally, do the same for the SpatiaLite tools::
$ cd spatialite-tools-2.3.1
$ cd spatialite-tools-2.4.0-5
$ ./configure # May need to be modified, see notes below.
$ make
$ sudo make install
@ -196,33 +196,18 @@ Creating a spatial database for SpatiaLite
After you've installed SpatiaLite, you'll need to create a number of spatial
metadata tables in your database in order to perform spatial queries.
If you're using SpatiaLite 2.4 or newer, use the ``spatialite`` utility to
call the ``InitSpatialMetaData()`` function, like this::
Use the ``spatialite`` utility to call the ``InitSpatialMetaData()`` function,
like this::
$ spatialite geodjango.db "SELECT InitSpatialMetaData();"
the SPATIAL_REF_SYS table already contains some row(s)
InitSpatiaMetaData ()error:"table spatial_ref_sys already exists"
0
You can safely ignore the error messages shown. When you've done this, you can
skip the rest of this section.
If you're using SpatiaLite 2.3, you'll need to download a
database-initialization file and execute its SQL queries in your database.
First, get it from the `SpatiaLite Resources`__ page::
$ wget http://www.gaia-gis.it/spatialite-2.3.1/init_spatialite-2.3.sql.gz
$ gunzip init_spatialite-2.3.sql.gz
Then, use the ``spatialite`` command to initialize a spatial database::
$ spatialite geodjango.db < init_spatialite-2.3.sql
You can safely ignore the error messages shown.
.. note::
The parameter ``geodjango.db`` is the *filename* of the SQLite database
you want to use. Use the same in the :setting:`DATABASES` ``"name"`` key
inside your ``settings.py``.
__ http://www.gaia-gis.it/spatialite-2.3.1/resources.html

View File

@ -120,24 +120,6 @@ database, as described in :ref:`create_spatialite_db`. Then just do this::
$ python manage.py test
Settings
--------
.. setting:: SPATIALITE_SQL
``SPATIALITE_SQL``
^^^^^^^^^^^^^^^^^^
Only relevant when using a SpatiaLite version 2.3.
By default, the GeoDjango test runner looks for the :ref:`file containing the
SpatiaLite database-initialization SQL code <create_spatialite_db>` in the
same directory where it was invoked (by default the same directory where
``manage.py`` is located). To use a different location, add the following to
your settings::
SPATIALITE_SQL='/path/to/init_spatialite-2.3.sql'
.. _geodjango-tests:
GeoDjango tests

View File

@ -280,6 +280,8 @@ Miscellaneous
* The :tfilter:`length` template filter now returns ``0`` for an undefined
variable, rather than an empty string.
* Support for SpatiaLite < 2.4 has been dropped.
.. _deprecated-features-1.8:
Features deprecated in 1.8