Ran 'CREATE EXTENSION postgis' during prepare_database hook

DatabaseWrapper.prepare_database has been introduced in 307de67073.
This commit is contained in:
Claude Paroz 2014-12-06 15:11:20 +01:00
parent 8f97413fae
commit bac7664f27
4 changed files with 31 additions and 27 deletions

View File

@ -1,3 +1,4 @@
from django.conf import settings
from django.db.backends.creation import NO_DB_ALIAS from django.db.backends.creation import NO_DB_ALIAS
from django.db.backends.postgresql_psycopg2.base import ( from django.db.backends.postgresql_psycopg2.base import (
DatabaseWrapper as Psycopg2DatabaseWrapper, DatabaseWrapper as Psycopg2DatabaseWrapper,
@ -8,6 +9,7 @@ from django.contrib.gis.db.backends.postgis.creation import PostGISCreation
from django.contrib.gis.db.backends.postgis.introspection import PostGISIntrospection from django.contrib.gis.db.backends.postgis.introspection import PostGISIntrospection
from django.contrib.gis.db.backends.postgis.operations import PostGISOperations from django.contrib.gis.db.backends.postgis.operations import PostGISOperations
from django.contrib.gis.db.backends.postgis.schema import PostGISSchemaEditor from django.contrib.gis.db.backends.postgis.schema import PostGISSchemaEditor
from django.utils.functional import cached_property
class DatabaseFeatures(BaseSpatialFeatures, Psycopg2DatabaseFeatures): class DatabaseFeatures(BaseSpatialFeatures, Psycopg2DatabaseFeatures):
@ -25,3 +27,20 @@ class DatabaseWrapper(Psycopg2DatabaseWrapper):
self.creation = PostGISCreation(self) self.creation = PostGISCreation(self)
self.ops = PostGISOperations(self) self.ops = PostGISOperations(self)
self.introspection = PostGISIntrospection(self) self.introspection = PostGISIntrospection(self)
@cached_property
def template_postgis(self):
template_postgis = getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis')
with self.cursor() as cursor:
cursor.execute('SELECT 1 FROM pg_database WHERE datname = %s LIMIT 1;', (template_postgis,))
if cursor.fetchone():
return template_postgis
return None
def prepare_database(self):
super(DatabaseWrapper, self).prepare_database()
if self.template_postgis is None:
# Check that postgis extension is installed on PostGIS >= 2
with self.cursor() as cursor:
cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")
cursor.connection.commit()

View File

@ -1,6 +1,4 @@
from django.conf import settings
from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation
from django.utils.functional import cached_property
class PostGISCreation(DatabaseCreation): class PostGISCreation(DatabaseCreation):
@ -8,15 +6,6 @@ class PostGISCreation(DatabaseCreation):
geom_index_ops = 'GIST_GEOMETRY_OPS' geom_index_ops = 'GIST_GEOMETRY_OPS'
geom_index_ops_nd = 'GIST_GEOMETRY_OPS_ND' geom_index_ops_nd = 'GIST_GEOMETRY_OPS_ND'
@cached_property
def template_postgis(self):
template_postgis = getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis')
with self.connection.cursor() as 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): def sql_indexes_for_field(self, model, f, style):
"Return any spatial index creation SQL for the field." "Return any spatial index creation SQL for the field."
from django.contrib.gis.db.models.fields import GeometryField from django.contrib.gis.db.models.fields import GeometryField
@ -77,21 +66,7 @@ class PostGISCreation(DatabaseCreation):
return output return output
def sql_table_creation_suffix(self): def sql_table_creation_suffix(self):
if self.template_postgis is not None: if self.connection.template_postgis is not None:
return ' TEMPLATE %s' % ( return ' TEMPLATE %s' % (
self.connection.ops.quote_name(self.template_postgis),) self.connection.ops.quote_name(self.connection.template_postgis),)
return '' return ''
def _create_test_db(self, verbosity, autoclobber, keepdb=False):
test_database_name = super(PostGISCreation, self)._create_test_db(verbosity, autoclobber, keepdb)
if keepdb:
return test_database_name
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
with self.connection.cursor() as cursor:
cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")
cursor.connection.commit()
return test_database_name

View File

@ -68,6 +68,12 @@ spatial functionality::
No PostGIS topology functionalities are yet available from GeoDjango, so the No PostGIS topology functionalities are yet available from GeoDjango, so the
creation of the ``postgis_topology`` extension is entirely optional. creation of the ``postgis_topology`` extension is entirely optional.
.. versionchanged:: 1.8
The ``CREATE EXTENSION postgis`` command is now automatically run during
the :djadmin:`migrate` process. You can still create it manually if you
wish.
.. _spatialdb_template_earlier: .. _spatialdb_template_earlier:
Creating a spatial database template for earlier versions Creating a spatial database template for earlier versions

View File

@ -135,6 +135,10 @@ Minor features
* The Spatialite backend now supports ``Collect`` and ``Extent`` aggregates * The Spatialite backend now supports ``Collect`` and ``Extent`` aggregates
when the database version is 3.0 or later. when the database version is 3.0 or later.
* The PostGIS 2 ``CREATE EXTENSION postgis`` and the Spatialite
``SELECT InitSpatialMetaData`` initialization commands are now automatically
run by :djadmin:`migrate`.
* Compatibility shims for ``SpatialRefSys`` and ``GeometryColumns`` changed in * Compatibility shims for ``SpatialRefSys`` and ``GeometryColumns`` changed in
Django 1.2 have been removed. Django 1.2 have been removed.