Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #19152 -- Allowed PostGIS tests to be run without template

From version 2, a PostGIS template is no longer required to create
a GIS-enabled database.
  • Loading branch information...
commit fbd1df8e16fc17a5d73f36136298c20b8ec3d618 1 parent 9d4baf8
@fcurella fcurella authored claudep committed
Showing with 27 additions and 2 deletions.
  1. +27 −2 django/contrib/gis/db/backends/postgis/creation.py
View
29 django/contrib/gis/db/backends/postgis/creation.py
@@ -1,12 +1,23 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation
+from django.utils.functional import cached_property
+
class PostGISCreation(DatabaseCreation):
geom_index_type = 'GIST'
geom_index_ops = 'GIST_GEOMETRY_OPS'
geom_index_ops_nd = 'GIST_GEOMETRY_OPS_ND'
+ @cached_property
+ def template_postgis(self):
+ template_postgis = getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis')
+ cursor = self.connection.cursor()
+ cursor.execute('SELECT 1 FROM pg_database WHERE datname = %s LIMIT 1;', (template_postgis,))
+ if cursor.fetchone():
+ return template_postgis
+ return None
+
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
@@ -67,5 +78,19 @@ def sql_indexes_for_field(self, model, f, style):
return output
def sql_table_creation_suffix(self):
- postgis_template = getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis')
- return ' TEMPLATE %s' % self.connection.ops.quote_name(postgis_template)
+ if self.template_postgis is not None:
+ return ' TEMPLATE %s' % (
+ self.connection.ops.quote_name(self.template_postgis),)
+ return ''
+
+ def _create_test_db(self, verbosity, autoclobber):
+ test_database_name = super(PostGISCreation, self)._create_test_db(verbosity, autoclobber)
+ if self.template_postgis is None:
+ # Connect to the test database in order to create the postgis extension
+ self.connection.close()
+ self.connection.settings_dict["NAME"] = test_database_name
+ cursor = self.connection.cursor()
+ cursor.execute("CREATE EXTENSION postgis")
+ cursor.commit()
+
+ return test_database_name
Please sign in to comment.
Something went wrong with that request. Please try again.