Skip to content

Commit df30ae0

Browse files
committed
Fixed postgis test database initialization
Refs #20968. Allow querying template_postgis presence without existing test database. Thanks Tim Graham for the review.
1 parent c7a19f4 commit df30ae0

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

django/contrib/gis/db/backends/postgis/base.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.conf import settings
2-
from django.db.backends.creation import NO_DB_ALIAS
2+
from django.db.backends import NO_DB_ALIAS
33
from django.db.backends.postgresql_psycopg2.base import (
44
DatabaseWrapper as Psycopg2DatabaseWrapper,
55
DatabaseFeatures as Psycopg2DatabaseFeatures
@@ -31,7 +31,7 @@ def __init__(self, *args, **kwargs):
3131
@cached_property
3232
def template_postgis(self):
3333
template_postgis = getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis')
34-
with self.cursor() as cursor:
34+
with self._nodb_connection.cursor() as cursor:
3535
cursor.execute('SELECT 1 FROM pg_database WHERE datname = %s LIMIT 1;', (template_postgis,))
3636
if cursor.fetchone():
3737
return template_postgis
@@ -43,4 +43,3 @@ def prepare_database(self):
4343
# Check that postgis extension is installed on PostGIS >= 2
4444
with self.cursor() as cursor:
4545
cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")
46-
cursor.connection.commit()

django/contrib/gis/tests/geoapp/tests.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import re
44
from tempfile import NamedTemporaryFile
5-
import unittest
65

76
from django.db import connection
87
from django.contrib.gis import gdal
@@ -292,6 +291,11 @@ def test_left_right_lookups(self):
292291
# Right: A >> B => true if xmin(A) > xmax(B)
293292
# See: BOX2D_left() and BOX2D_right() in lwgeom_box2dfloat4.c in PostGIS source.
294293

294+
# The left/right lookup tests are known failures on PostGIS 2.0/2.0.1
295+
# http://trac.osgeo.org/postgis/ticket/2035
296+
if postgis_bug_version():
297+
self.skipTest("PostGIS 2.0/2.0.1 left and right lookups are known to be buggy.")
298+
295299
# Getting the borders for Colorado & Kansas
296300
co_border = State.objects.get(name='Colorado').poly
297301
ks_border = State.objects.get(name='Kansas').poly
@@ -325,11 +329,6 @@ def test_left_right_lookups(self):
325329
for c in qs:
326330
self.assertIn(c.name, cities)
327331

328-
# The left/right lookup tests are known failures on PostGIS 2.0/2.0.1
329-
# http://trac.osgeo.org/postgis/ticket/2035
330-
if postgis_bug_version():
331-
test_left_right_lookups = unittest.expectedFailure(test_left_right_lookups)
332-
333332
def test_equals_lookups(self):
334333
"Testing the 'same_as' and 'equals' lookup types."
335334
pnt = fromstr('POINT (-95.363151 29.763374)', srid=4326)

django/db/backends/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
FieldInfo = namedtuple('FieldInfo',
3131
'name type_code display_size internal_size precision scale null_ok')
3232

33+
NO_DB_ALIAS = '__no_db__'
34+
3335

3436
class BaseDatabaseWrapper(object):
3537
"""
@@ -477,6 +479,24 @@ def temporary_connection(self):
477479
if must_close:
478480
self.close()
479481

482+
@cached_property
483+
def _nodb_connection(self):
484+
"""
485+
Alternative connection to be used when there is no need to access
486+
the main database, specifically for test db creation/deletion.
487+
This also prevents the production database from being exposed to
488+
potential child threads while (or after) the test database is destroyed.
489+
Refs #10868, #17786, #16969.
490+
"""
491+
settings_dict = self.settings_dict.copy()
492+
settings_dict['NAME'] = None
493+
#backend = load_backend(settings_dict['ENGINE'])
494+
nodb_connection = self.__class__(
495+
settings_dict,
496+
alias=NO_DB_ALIAS,
497+
allow_thread_sharing=False)
498+
return nodb_connection
499+
480500
def _start_transaction_under_autocommit(self):
481501
"""
482502
Only required when autocommits_when_autocommit_is_off = True.

django/db/backends/creation.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import time
44

55
from django.conf import settings
6-
from django.db.utils import load_backend
76
from django.utils.encoding import force_bytes
8-
from django.utils.functional import cached_property
97
from django.utils.six.moves import input
108
from django.utils.six import StringIO
119
from django.db import router
@@ -17,7 +15,6 @@
1715
# The prefix to put on the default database name when creating
1816
# the test database.
1917
TEST_DATABASE_PREFIX = 'test_'
20-
NO_DB_ALIAS = '__no_db__'
2118

2219

2320
class BaseDatabaseCreation(object):
@@ -34,23 +31,12 @@ class BaseDatabaseCreation(object):
3431
def __init__(self, connection):
3532
self.connection = connection
3633

37-
@cached_property
34+
@property
3835
def _nodb_connection(self):
3936
"""
40-
Alternative connection to be used when there is no need to access
41-
the main database, specifically for test db creation/deletion.
42-
This also prevents the production database from being exposed to
43-
potential child threads while (or after) the test database is destroyed.
44-
Refs #10868, #17786, #16969.
45-
"""
46-
settings_dict = self.connection.settings_dict.copy()
47-
settings_dict['NAME'] = None
48-
backend = load_backend(settings_dict['ENGINE'])
49-
nodb_connection = backend.DatabaseWrapper(
50-
settings_dict,
51-
alias=NO_DB_ALIAS,
52-
allow_thread_sharing=False)
53-
return nodb_connection
37+
Used to be defined here, now moved to DatabaseWrapper.
38+
"""
39+
return self.connection._nodb_connection
5440

5541
@classmethod
5642
def _digest(cls, *args):

0 commit comments

Comments
 (0)