diff --git a/tradein-mvp/backend/app/services/scrapers/cian_detail.py b/tradein-mvp/backend/app/services/scrapers/cian_detail.py index fb7f824f..d931e9be 100644 --- a/tradein-mvp/backend/app/services/scrapers/cian_detail.py +++ b/tradein-mvp/backend/app/services/scrapers/cian_detail.py @@ -260,16 +260,19 @@ def save_detail_enrichment(db: Session, listing_id: int, enrichment: DetailEnric """), {"lid": listing_id}, ).fetchone() + _snap_written = False if _snap_row is not None and _snap_row.price_rub is not None: try: - upsert_listing_snapshot( - db, - listing_id=listing_id, - price_rub=int(_snap_row.price_rub), - price_per_m2=int(_snap_row.price_per_m2) if _snap_row.price_per_m2 else None, - run_id=None, - status="active", - ) + with db.begin_nested(): + upsert_listing_snapshot( + db, + listing_id=listing_id, + price_rub=int(_snap_row.price_rub), + price_per_m2=int(_snap_row.price_per_m2) if _snap_row.price_per_m2 else None, + run_id=None, + status="active", + ) + _snap_written = True except Exception as exc: logger.warning( "save_detail_enrichment: snapshot write failed listing_id=%s: %s", @@ -351,5 +354,5 @@ def save_detail_enrichment(db: Session, listing_id: int, enrichment: DetailEnric listing_id, len(enrichment.price_changes), bool(enrichment.agent_profile), - _snap_row is not None and _snap_row.price_rub is not None, + _snap_written, )