fix seed : fixtures dev idempotentes, indépendantes de l'early-return
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

- DEV_FIXTURES défini au niveau module (constante partagée)
- seed principal (commune + 363 foyers + votes) sous `if commune is None`
- fixtures toujours vérifiées/insérées après, quel que soit l'état de la DB
- résout le cas prod avec DB déjà seedée

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Yvv
2026-03-24 03:58:31 +01:00
parent 19ac64c856
commit 90b069cb88

View File

@@ -20,17 +20,23 @@ from app.engine.pricing import HouseholdData, compute_p0
XLS_PATH = os.path.join(os.path.dirname(__file__), "..", "Eau2018.xls") XLS_PATH = os.path.join(os.path.dirname(__file__), "..", "Eau2018.xls")
# Codes fixes — identiques dans le dev hint frontend
DEV_FIXTURES = [
{"identifier": "[DEV] Foyer RS 60m³", "status": "RS", "volume_m3": 60.0, "price_paid_eur": 140.0, "auth_code": "DEVTEST2"},
{"identifier": "[DEV] Foyer RP 120m³", "status": "RP", "volume_m3": 120.0, "price_paid_eur": 265.0, "auth_code": "DEVTEST3"},
{"identifier": "[DEV] Foyer PRO 350m³", "status": "PRO", "volume_m3": 350.0, "price_paid_eur": 680.0, "auth_code": "DEVTEST4"},
]
async def seed(): async def seed():
await init_db() await init_db()
async with async_session() as db: async with async_session() as db:
# Check if already seeded # ── Commune Saoû (idempotent) ──────────────────────────────────────────
result = await db.execute(select(Commune).where(Commune.slug == "saou")) result = await db.execute(select(Commune).where(Commune.slug == "saou"))
if result.scalar_one_or_none(): commune = result.scalar_one_or_none()
print("Saoû already seeded.")
return
# Create commune if commune is None:
commune = Commune( commune = Commune(
name="Saoû", name="Saoû",
slug="saou", slug="saou",
@@ -72,21 +78,12 @@ async def seed():
commune_admin.communes.append(commune) commune_admin.communes.append(commune)
db.add(commune_admin) db.add(commune_admin)
# ── Dev fixture households (codes fixes, affichés dans les dev hints) ──
DEV_FIXTURES = [
{"identifier": "[DEV] Foyer RS 60m³", "status": "RS", "volume_m3": 60.0, "price_paid_eur": 140.0, "auth_code": "DEVTEST2"},
{"identifier": "[DEV] Foyer RP 120m³", "status": "RP", "volume_m3": 120.0, "price_paid_eur": 265.0, "auth_code": "DEVTEST3"},
{"identifier": "[DEV] Foyer PRO 350m³","status": "PRO", "volume_m3": 350.0, "price_paid_eur": 680.0, "auth_code": "DEVTEST4"},
]
existing_codes = {f["auth_code"] for f in DEV_FIXTURES}
for fixture in DEV_FIXTURES:
db.add(Household(commune_id=commune.id, **fixture))
# Import households from Eau2018.xls # Import households from Eau2018.xls
book = xlrd.open_workbook(XLS_PATH) book = xlrd.open_workbook(XLS_PATH)
sheet = book.sheet_by_name("CALCULS") sheet = book.sheet_by_name("CALCULS")
nb_hab = 363 nb_hab = 363
existing_codes = {f["auth_code"] for f in DEV_FIXTURES}
for r in range(1, nb_hab + 1): for r in range(1, nb_hab + 1):
name = sheet.cell_value(r, 0) name = sheet.cell_value(r, 0)
status = sheet.cell_value(r, 3) status = sheet.cell_value(r, 3)
@@ -98,20 +95,18 @@ async def seed():
code = generate_auth_code() code = generate_auth_code()
existing_codes.add(code) existing_codes.add(code)
household = Household( db.add(Household(
commune_id=commune.id, commune_id=commune.id,
identifier=str(name).strip(), identifier=str(name).strip(),
status=str(status).strip().upper(), status=str(status).strip().upper(),
volume_m3=float(volume), volume_m3=float(volume),
price_paid_eur=float(price) if price else 0.0, price_paid_eur=float(price) if price else 0.0,
auth_code=code, auth_code=code,
) ))
db.add(household)
await db.flush() await db.flush()
# ── Publish a reference curve ── # ── Publish a reference curve ──
# Reference: vinf=400, all params=0.5
ref_vinf, ref_a, ref_b, ref_c, ref_d, ref_e = 400, 0.5, 0.5, 0.5, 0.5, 0.5 ref_vinf, ref_a, ref_b, ref_c, ref_d, ref_e = 400, 0.5, 0.5, 0.5, 0.5, 0.5
hh_result = await db.execute( hh_result = await db.execute(
@@ -159,7 +154,6 @@ async def seed():
used_households = set() used_households = set()
vote_count = 0 vote_count = 0
for prof in vote_profiles: for prof in vote_profiles:
# Pick a unique household
hh_pick = random.choice(all_households) hh_pick = random.choice(all_households)
while hh_pick.id in used_households: while hh_pick.id in used_households:
hh_pick = random.choice(all_households) hh_pick = random.choice(all_households)
@@ -171,23 +165,36 @@ async def seed():
vinf=prof["vinf"], vmax=params.vmax, pmax=params.pmax, vinf=prof["vinf"], vmax=params.vmax, pmax=params.pmax,
a=prof["a"], b=prof["b"], c=prof["c"], d=prof["d"], e=prof["e"], a=prof["a"], b=prof["b"], c=prof["c"], d=prof["d"], e=prof["e"],
) )
vote = Vote( db.add(Vote(
commune_id=commune.id, commune_id=commune.id,
household_id=hh_pick.id, household_id=hh_pick.id,
vinf=prof["vinf"], vinf=prof["vinf"],
a=prof["a"], b=prof["b"], c=prof["c"], d=prof["d"], e=prof["e"], a=prof["a"], b=prof["b"], c=prof["c"], d=prof["d"], e=prof["e"],
computed_p0=vp0, computed_p0=vp0,
) ))
db.add(vote)
hh_pick.has_voted = True hh_pick.has_voted = True
vote_count += 1 vote_count += 1
await db.commit() await db.commit()
print(f"Seeded: commune 'saou', {nb_hab} + 3 fixture households, {vote_count} votes") print(f"Seeded: commune 'saou', {nb_hab} households, {vote_count} votes")
print(f" Published curve: vinf={ref_vinf}, p0={ref_p0:.3f}") print(f" Published curve: vinf={ref_vinf}, p0={ref_p0:.3f}")
print(f" Super admin: superadmin@sejeteralo.fr / superadmin") print(f" Super admin: superadmin@sejeteralo.fr / superadmin")
print(f" Commune admin Saou: saou@sejeteralo.fr / saou2024") print(f" Commune admin Saou: saou@sejeteralo.fr / saou2024")
print(f" Dev fixtures: DEVTEST2 (RS 60m³) · DEVTEST3 (RP 120m³) · DEVTEST4 (PRO 350m³)") else:
print("Saoû already seeded.")
# ── Dev fixtures (idempotent — insérés à chaque run si absents) ────────
fixture_added = 0
for fixture in DEV_FIXTURES:
res = await db.execute(
select(Household).where(Household.auth_code == fixture["auth_code"])
)
if res.scalar_one_or_none() is None:
db.add(Household(commune_id=commune.id, **fixture))
fixture_added += 1
if fixture_added:
await db.commit()
print(f" Dev fixtures: {fixture_added} ajoutés — DEVTEST2 (RS 60m³) · DEVTEST3 (RP 120m³) · DEVTEST4 (PRO 350m³)")
if __name__ == "__main__": if __name__ == "__main__":