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,13 +14,16 @@ 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é."""
|
||||
rows = session.exec(
|
||||
text(
|
||||
"SELECT temp_ext, t_min, t_max, pluie_mm, vent_kmh, humidite "
|
||||
"FROM meteostation WHERE substr(date_heure, 1, 10) = :d"
|
||||
),
|
||||
params={"d": iso_date},
|
||||
).fetchall()
|
||||
try:
|
||||
rows = session.exec(
|
||||
text(
|
||||
"SELECT temp_ext, t_min, t_max, pluie_mm, vent_kmh, humidite "
|
||||
"FROM meteostation WHERE substr(date_heure, 1, 10) = :d"
|
||||
),
|
||||
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é)."""
|
||||
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()
|
||||
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,13 +71,16 @@ def _station_current_row(session: Session) -> Optional[dict]:
|
||||
|
||||
|
||||
def _open_meteo_day(session: Session, iso_date: str) -> Optional[dict]:
|
||||
row = session.exec(
|
||||
text(
|
||||
"SELECT t_min, t_max, pluie_mm, vent_kmh, wmo, label, humidite_moy, sol_0cm, etp_mm "
|
||||
"FROM meteoopenmeteo WHERE date = :d"
|
||||
),
|
||||
params={"d": iso_date},
|
||||
).fetchone()
|
||||
try:
|
||||
row = session.exec(
|
||||
text(
|
||||
"SELECT t_min, t_max, pluie_mm, vent_kmh, wmo, label, humidite_moy, sol_0cm, etp_mm "
|
||||
"FROM meteoopenmeteo WHERE date = :d"
|
||||
),
|
||||
params={"d": iso_date},
|
||||
).fetchone()
|
||||
except OperationalError:
|
||||
return None
|
||||
|
||||
if not row:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user