feat: seed données initiales (114 produits + 9 magasins) au démarrage
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
"""
|
||||
Lance le seed uniquement si la table shopping.stores est vide.
|
||||
Usage : python -m app.data.seed
|
||||
"""
|
||||
import asyncio
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.core.database import AsyncSessionLocal
|
||||
from app.models.shopping import Product, Store
|
||||
|
||||
DATA_DIR = Path(__file__).parent
|
||||
|
||||
|
||||
async def run_seed() -> None:
|
||||
async with AsyncSessionLocal() as session:
|
||||
count = await session.scalar(select(Store).limit(1))
|
||||
if count is not None:
|
||||
print("Seed déjà chargé — rien à faire.")
|
||||
return
|
||||
|
||||
stores_raw = json.loads((DATA_DIR / "seed_stores.json").read_text())
|
||||
for s in stores_raw:
|
||||
session.add(Store(name=s["name"], location=s.get("location")))
|
||||
|
||||
products_raw = json.loads((DATA_DIR / "seed_products.json").read_text())
|
||||
for p in products_raw:
|
||||
session.add(Product(
|
||||
name=p["name"],
|
||||
category=p.get("category"),
|
||||
default_unit=p.get("default_unit"),
|
||||
frequency_score=p.get("frequency_score", 0),
|
||||
))
|
||||
|
||||
await session.commit()
|
||||
print(f"Seed : {len(stores_raw)} magasins, {len(products_raw)} produits chargés.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(run_seed())
|
||||
+10
-1
@@ -1,10 +1,19 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from app.api.health import router as health_router
|
||||
from app.api.media import router as media_router
|
||||
from app.core.config import settings
|
||||
from app.data.seed import run_seed
|
||||
|
||||
app = FastAPI(title="HomeHub API", version="0.1.0")
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
await run_seed()
|
||||
yield
|
||||
|
||||
|
||||
app = FastAPI(title="HomeHub API", version="0.1.0", lifespan=lifespan)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
|
||||
Reference in New Issue
Block a user