fix(router): guard OperationalError si tables météo inexistantes
This commit is contained in:
@@ -4,6 +4,7 @@ from typing import Any, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.exc import OperationalError
|
||||
from sqlmodel import Session
|
||||
|
||||
from app.database import get_session
|
||||
@@ -13,6 +14,7 @@ router = APIRouter(tags=["météo"])
|
||||
|
||||
def _station_daily_summary(session: Session, iso_date: str) -> Optional[dict]:
|
||||
"""Agrège les mesures horaires d'une journée en résumé."""
|
||||
try:
|
||||
rows = session.exec(
|
||||
text(
|
||||
"SELECT temp_ext, t_min, t_max, pluie_mm, vent_kmh, humidite "
|
||||
@@ -20,6 +22,8 @@ def _station_daily_summary(session: Session, iso_date: str) -> Optional[dict]:
|
||||
),
|
||||
params={"d": iso_date},
|
||||
).fetchall()
|
||||
except OperationalError:
|
||||
return None
|
||||
|
||||
if not rows:
|
||||
return None
|
||||
@@ -46,12 +50,15 @@ def _station_daily_summary(session: Session, iso_date: str) -> Optional[dict]:
|
||||
|
||||
def _station_current_row(session: Session) -> Optional[dict]:
|
||||
"""Dernière mesure station (max 2h d'ancienneté)."""
|
||||
try:
|
||||
row = session.exec(
|
||||
text(
|
||||
"SELECT temp_ext, humidite, pression, pluie_mm, vent_kmh, vent_dir, uv, solaire, date_heure "
|
||||
"FROM meteostation WHERE type='current' ORDER BY date_heure DESC LIMIT 1"
|
||||
)
|
||||
).fetchone()
|
||||
except OperationalError:
|
||||
return None
|
||||
|
||||
if not row:
|
||||
return None
|
||||
@@ -64,6 +71,7 @@ def _station_current_row(session: Session) -> Optional[dict]:
|
||||
|
||||
|
||||
def _open_meteo_day(session: Session, iso_date: str) -> Optional[dict]:
|
||||
try:
|
||||
row = session.exec(
|
||||
text(
|
||||
"SELECT t_min, t_max, pluie_mm, vent_kmh, wmo, label, humidite_moy, sol_0cm, etp_mm "
|
||||
@@ -71,6 +79,8 @@ def _open_meteo_day(session: Session, iso_date: str) -> Optional[dict]:
|
||||
),
|
||||
params={"d": iso_date},
|
||||
).fetchone()
|
||||
except OperationalError:
|
||||
return None
|
||||
|
||||
if not row:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user