Skip to content

Commit d8c5227

Browse files
committed
feat: Support for postgis database
This adds support for PostGis database which is also supported officially by django-- geodjango. References: #41 #90 #140 Closes #64
1 parent c6acf59 commit d8c5227

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ services:
99
- redis-server
1010
- mysql
1111
- postgresql
12+
addons:
13+
postgresql: 9.5
14+
apt:
15+
packages:
16+
- postgresql-9.5-postgis-2.4
17+
- gdal-bin
1218

1319
install:
1420
- pip install tox-travis coverage coveralls codacy-coverage
1521
- mysql -e 'create database django_prometheus_1;'
22+
- psql -U postgres -c 'CREATE DATABASE postgis'
23+
- psql -U postgres postgis -c 'CREATE EXTENSION IF NOT EXISTS postgis;'
1624
script:
1725
- tox
1826
after_success:

django_prometheus/db/backends/postgis/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import psycopg2.extensions
2+
from django.contrib.gis.db.backends.postgis import base
3+
from django_prometheus.db.common import DatabaseWrapperMixin, ExportingCursorWrapper
4+
5+
6+
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
7+
def get_connection_params(self):
8+
conn_params = super(DatabaseWrapper, self).get_connection_params()
9+
conn_params["cursor_factory"] = ExportingCursorWrapper(
10+
psycopg2.extensions.cursor, "postgis", self.vendor,
11+
)
12+
return conn_params
13+
14+
def create_cursor(self, name=None):
15+
# cursor_factory is a kwarg to connect() so restore create_cursor()'s
16+
# default behavior
17+
return base.DatabaseWrapper.create_cursor(self, name=name)

django_prometheus/tests/end2end/testapp/settings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@
6262
"HOST": "localhost",
6363
"PORT": "5432",
6464
},
65+
# Comment this to not test django_prometheus.db.backends.postgis.
66+
"postgis": {
67+
"ENGINE": "django_prometheus.db.backends.postgis",
68+
"NAME": "postgis",
69+
"USER": "postgres",
70+
"PASSWORD": "",
71+
"HOST": "localhost",
72+
"PORT": "5432",
73+
},
6574
# Comment this to not test django_prometheus.db.backends.mysql.
6675
"mysql": {
6776
"ENGINE": "django_prometheus.db.backends.mysql",

django_prometheus/tests/end2end/testapp/test_db.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,30 @@ def testCounters(self):
155155
alias="mysql",
156156
vendor="mysql",
157157
)
158+
159+
160+
@skipUnless("postgis" in connections, "Skipped unless postgis database is enabled")
161+
class TestPostgisDbMetrics(BaseDbMetricTest):
162+
"""Test django_prometheus.db metrics for postgis backend.
163+
164+
Note regarding the values of metrics: many tests interact with the
165+
database, and the test runner itself does. As such, tests that
166+
require that a metric has a specific value are at best very
167+
fragile. Consider asserting that the value exceeds a certain
168+
threshold, or check by how much it increased during the test.
169+
"""
170+
171+
def testCounters(self):
172+
r = self.saveRegistry()
173+
cursor = connections["postgis"].cursor()
174+
175+
for _ in range(20):
176+
cursor.execute("SELECT 1")
177+
178+
self.assertMetricCompare(
179+
r,
180+
lambda a, b: a + 20 <= b < a + 25,
181+
"django_db_execute_total",
182+
alias="postgis",
183+
vendor="postgresql",
184+
)

0 commit comments

Comments
 (0)