fix(campaigns): null-safe test_id serialization, correct stale comments

serialize_modification_request crashed toward a string 'None' instead of
JSON null for requests whose test_id was nulled by the SET NULL cascade.
Also corrects two comments that still described the debunked ordering
theory instead of the actual SET NULL fix, and fixes a test that
asserted post-cascade state without committing first (the ORM only
picks up out-of-band DB-side SET NULL on already-loaded objects after
expire_on_commit forces a re-read, matching real router behavior).
This commit is contained in:
kitos
2026-07-03 11:04:20 +02:00
parent 12a5484003
commit fd47db7bea
2 changed files with 21 additions and 9 deletions
@@ -957,7 +957,7 @@ def serialize_modification_request(db: Session, request: CampaignModificationReq
"campaign_name": request.campaign.name if request.campaign else None,
"requested_by": str(request.requested_by) if request.requested_by else None,
"action": request.action,
"test_id": str(request.test_id),
"test_id": str(request.test_id) if request.test_id else None,
"test_name": test.name if test else None,
"order_index": request.order_index,
"phase": request.phase,
@@ -1049,10 +1049,10 @@ def approve_modification_request(
if request.status != "pending":
raise BusinessRuleViolation("Only pending modification requests can be approved")
# Mark the request approved *before* applying the underlying change: a
# "remove_test" approval deletes the Test row, which cascades (ON DELETE
# CASCADE on test_id) and would otherwise wipe out this very request row
# before we get a chance to persist its reviewed/approved fields.
# A "remove_test" approval deletes the underlying Test row below, which
# nulls this row's test_id (test_id uses ON DELETE SET NULL specifically
# so this audit record survives that delete instead of being cascaded
# away with it — see CampaignModificationRequest.test_id).
request.status = "approved"
request.reviewed_by = reviewer_id
request.reviewed_at = datetime.utcnow()