Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Cascade request rollups in the ORM, not the DB

Juan Denis3040db46ad9c
diff --git a/backend/app/models/app_request_metric.py b/backend/app/models/app_request_metric.py
index e08ba57e..6a1ed3ab 100644
--- a/backend/app/models/app_request_metric.py
+++ b/backend/app/models/app_request_metric.py
@@ -58,7 +58,7 @@ class AppRequestMetric(JsonColumnMixin, db.Model):
application = db.relationship(
'Application',
backref=db.backref('request_metrics', cascade='all, delete-orphan',
- passive_deletes=True, lazy='select'),
+ lazy='select'),
)
def get_hist(self):
diff --git a/backend/tests/test_request_metrics.py b/backend/tests/test_request_metrics.py
index 32a557bd..9b611c02 100644
--- a/backend/tests/test_request_metrics.py
+++ b/backend/tests/test_request_metrics.py
@@ -272,3 +272,15 @@ def test_api(app, client, auth_headers):
missing = client.get('/api/v1/bandwidth/apps/999999/requests',
headers=auth_headers)
assert missing.status_code == 404
+
+
+def test_hard_deleting_an_app_takes_its_rollups(app, tmp_path):
+ """SQLite doesn't enforce ON DELETE CASCADE here; the ORM relationship must."""
+ from app.models.application import Application
+ site = _make_app()
+ _write(tmp_path / 'shop.access.log', FIXTURE, 'w')
+ RequestMetricsService.sample(log_dir=str(tmp_path), now=NOW)
+ assert AppRequestMetric.query.filter_by(app_id=site.id).count() > 0
+ db.session.delete(Application.query.get(site.id))
+ db.session.commit()
+ assert AppRequestMetric.query.filter_by(app_id=site.id).count() == 0