Files
home_hub/backend/app/data/seed.py
T

43 lines
1.2 KiB
Python

"""
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())