From 57e40551e41eaf31ff96672beea9b1f732f2e331 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 30 Oct 2014 13:52:42 -0400 Subject: [PATCH] Fixed #23152 -- Added support for transactional Spatialite metadata initialization. Thanks Doug Goldstein for the initial patch. --- django/contrib/gis/db/backends/spatialite/base.py | 8 ++++++++ django/contrib/gis/db/backends/spatialite/creation.py | 3 ++- docs/releases/1.7.2.txt | 3 +++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/django/contrib/gis/db/backends/spatialite/base.py b/django/contrib/gis/db/backends/spatialite/base.py index 571d37cec9..db6895814a 100644 --- a/django/contrib/gis/db/backends/spatialite/base.py +++ b/django/contrib/gis/db/backends/spatialite/base.py @@ -13,6 +13,7 @@ from django.contrib.gis.db.backends.spatialite.introspection import SpatiaLiteIn from django.contrib.gis.db.backends.spatialite.operations import SpatiaLiteOperations from django.contrib.gis.db.backends.spatialite.schema import SpatialiteSchemaEditor from django.utils import six +from django.utils.functional import cached_property class DatabaseFeatures(BaseSpatialFeatures, SQLiteDatabaseFeatures): @@ -20,6 +21,13 @@ class DatabaseFeatures(BaseSpatialFeatures, SQLiteDatabaseFeatures): # SpatiaLite can only count vertices in LineStrings supports_num_points_poly = False + @cached_property + def supports_initspatialmetadata_with_transactions(self): + # SpatiaLite 4.1+ support initializing all metadata in one transaction + # which can result in a significant performance improvement when + # creating the database. + return self.connection.ops.spatial_version >= (4, 1, 0) + class DatabaseWrapper(SQLiteDatabaseWrapper): SchemaEditorClass = SpatialiteSchemaEditor diff --git a/django/contrib/gis/db/backends/spatialite/creation.py b/django/contrib/gis/db/backends/spatialite/creation.py index 1592ae50b9..54f4cd0b89 100644 --- a/django/contrib/gis/db/backends/spatialite/creation.py +++ b/django/contrib/gis/db/backends/spatialite/creation.py @@ -36,4 +36,5 @@ class SpatiaLiteCreation(DatabaseCreation): Creates the spatial metadata tables. """ cur = self.connection._cursor() - cur.execute("SELECT InitSpatialMetaData()") + arg = "1" if self.connection.features.supports_initspatialmetadata_with_transactions else "" + cur.execute("SELECT InitSpatialMetaData(%s)" % arg) diff --git a/docs/releases/1.7.2.txt b/docs/releases/1.7.2.txt index 9f20e0a01b..5d4ea56850 100644 --- a/docs/releases/1.7.2.txt +++ b/docs/releases/1.7.2.txt @@ -34,3 +34,6 @@ Bugfixes ``AlterIndexTogether`` or ``AlterUniqueTogether`` (:ticket:`23614`). * Updated the first day of the week in the Ukrainian locale to Monday. + +* Added support for transactional spatial metadata initialization on + SpatiaLite 4.1+ (:ticket:`23152`).