fix(site-finder): isolate zone-regulation cache write off the request session (#1850 item 4) #1902

Merged
bot-backend merged 1 commit from fix/zone-resolver-write-session into main 2026-06-25 11:16:27 +00:00
Collaborator

Follow-up к #1850 (item 4 — задокументированный SMELL).

Проблема

get_or_fetch_zone_regulation на cache-miss делал upsert_zone_regulation(db, reg) + db.commit() на SHARED request-Session → преждевременно коммитил всю in-flight /analyze-транзакцию (которая по дизайну коммитится РОВНО один раз в конце через persist_analysis_run; get_db на success не коммитит). SAVEPOINT'ы analyze рвались mid-request.

Фикс

Апсёрт изолирован в собственную короткую SessionLocal() (commit/rollback/close) — durable-запись кэша больше не трогает границы tx вызывающего. Read-back по-прежнему через SHARED-сессию (READ COMMITTED видит закоммиченную строку) → форма ответа для callers (parcels.py / ird_analyze.py) не меняется. Канонический паттерн — как в workers/tasks/zone_regulation_refresh.py.

Тесты

test_zone_regulation_write_session.py (новый, 2 кейса: SHARED db.commit НЕ вызывается + fresh-сессия коммитит один раз; table-missing → graceful rollback). Регрессия: memo + backfill + refresh suites — 21 passed. ruff+mypy clean. Backend-only, без schema → codegen не нужен.

Refs #1850

Follow-up к #1850 (item 4 — задокументированный SMELL). ## Проблема `get_or_fetch_zone_regulation` на cache-miss делал `upsert_zone_regulation(db, reg)` + `db.commit()` на **SHARED** request-Session → преждевременно коммитил всю in-flight `/analyze`-транзакцию (которая по дизайну коммитится РОВНО один раз в конце через `persist_analysis_run`; `get_db` на success не коммитит). SAVEPOINT'ы analyze рвались mid-request. ## Фикс Апсёрт изолирован в собственную короткую `SessionLocal()` (commit/rollback/close) — durable-запись кэша больше не трогает границы tx вызывающего. Read-back по-прежнему через SHARED-сессию (READ COMMITTED видит закоммиченную строку) → форма ответа для callers (parcels.py / ird_analyze.py) не меняется. Канонический паттерн — как в `workers/tasks/zone_regulation_refresh.py`. ## Тесты `test_zone_regulation_write_session.py` (новый, 2 кейса: SHARED db.commit НЕ вызывается + fresh-сессия коммитит один раз; table-missing → graceful rollback). Регрессия: memo + backfill + refresh suites — 21 passed. ruff+mypy clean. Backend-only, без schema → codegen не нужен. Refs #1850
bot-backend added 1 commit 2026-06-25 10:54:27 +00:00
fix(site-finder): isolate zone-regulation cache write off the request session
All checks were successful
CI / changes (pull_request) Successful in 6s
CI / backend-tests (pull_request) Successful in 11m7s
CI / frontend-tests (pull_request) Has been skipped
CI / openapi-codegen-check (pull_request) Successful in 1m44s
7eee75f657
get_or_fetch_zone_regulation no longer commits the shared /analyze transaction
mid-request on cache-miss. The upsert now runs in a fresh short-lived
SessionLocal() (commit/rollback/close), mirroring the zone_regulation_refresh
Celery task. Read-back still uses the shared session (READ COMMITTED sees the
committed row), so caller return shape is unchanged.

Resolves the #1850 item-4 SMELL (the outer analyze tx is designed to commit
exactly once via persist_analysis_run).

Refs #1850
bot-backend merged commit f249212695 into main 2026-06-25 11:16:27 +00:00
bot-backend deleted branch fix/zone-resolver-write-session 2026-06-25 11:16:27 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: lekss361/gendesign#1902
No description provided.