Skip to content

Commit 0cd21c6

Browse files
authored
Add support for spatialite database backend (#439)
1 parent d5852f5 commit 0cd21c6

File tree

6 files changed

+54
-5
lines changed

6 files changed

+54
-5
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ jobs:
1515
strategy:
1616
fail-fast: false
1717
matrix:
18-
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
19-
os: [ubuntu-22.04]
18+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
19+
os: [ubuntu-24.04]
2020
runs-on: ${{ matrix.os }}
2121
name: "${{ matrix.os }} Python: ${{ matrix.python-version }}"
2222
services:
@@ -44,7 +44,7 @@ jobs:
4444
- name: Install OS Packages
4545
run: |
4646
sudo apt-get update
47-
sudo apt-get install binutils libproj-dev gdal-bin libmemcached-dev
47+
sudo apt-get install binutils libproj-dev gdal-bin libmemcached-dev libsqlite3-mod-spatialite
4848
- uses: actions/checkout@v3
4949
with:
5050
fetch-depth: 0

django_prometheus/db/backends/spatialite/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.contrib.gis.db.backends.spatialite import base, features
2+
from django.db.backends.sqlite3 import base as sqlite_base
3+
4+
from django_prometheus.db.common import DatabaseWrapperMixin
5+
6+
7+
class DatabaseFeatures(features.DatabaseFeatures):
8+
"""Our database has the exact same features as the base one."""
9+
10+
pass
11+
12+
13+
class DatabaseWrapper(DatabaseWrapperMixin, base.DatabaseWrapper):
14+
CURSOR_CLASS = sqlite_base.SQLiteCursorWrapper

django_prometheus/tests/end2end/testapp/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@
8181
"HOST": "127.0.0.1",
8282
"PORT": "3306",
8383
},
84+
"spatialite": {
85+
"ENGINE": "django_prometheus.db.backends.spatialite",
86+
"NAME": "db_spatialite.sqlite3",
87+
},
8488
# The following databases are used by test_db.py only
8589
"test_db_1": {
8690
"ENGINE": "django_prometheus.db.backends.sqlite3",

django_prometheus/tests/end2end/testapp/test_db.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,35 @@ def test_counters(self):
178178
alias="postgis",
179179
vendor="postgresql",
180180
)
181+
182+
183+
@pytest.mark.skipif("spatialite" not in connections, reason="Skipped unless spatialite database is enabled")
184+
class TestSpatialiteDbMetrics(BaseDBTest):
185+
"""Test django_prometheus.db metrics for spatialite backend.
186+
187+
Note regarding the values of metrics: many tests interact with the
188+
database, and the test runner itself does. As such, tests that
189+
require that a metric has a specific value are at best very
190+
fragile. Consider asserting that the value exceeds a certain
191+
threshold, or check by how much it increased during the test.
192+
"""
193+
194+
def test_counters(self):
195+
r = save_registry()
196+
connection = connections["spatialite"]
197+
198+
# Make sure the extension is loaded and geospatial tables are created
199+
connection.prepare_database()
200+
201+
cursor = connection.cursor()
202+
203+
for _ in range(20):
204+
cursor.execute("SELECT 1")
205+
206+
assert_metric_compare(
207+
r,
208+
lambda a, b: a + 20 <= b < a + 25,
209+
"django_db_execute_total",
210+
alias="spatialite",
211+
vendor="sqlite",
212+
)

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ legacy_tox_ini = """
2323
[tox]
2424
min_version = 4.4
2525
envlist =
26-
{py37,py38,py39,py310,py311}-django{320}-{end2end,unittests}
26+
{py38,py39,py310,py311}-django{320}-{end2end,unittests}
2727
{py38,py39,py310,py311,py312}-django{400,410,420}-{end2end,unittests}
2828
{py310,py311,py312}-django{500,510}-{end2end,unittests}
2929
py39-lint
3030
3131
[gh-actions]
3232
python =
33-
3.7: py37
3433
3.8: py38
3534
3.9: py39, py39-lint
3635
3.10: py310

0 commit comments

Comments
 (0)