fix: auto-seed test users when release command fails on PR envs

The background seeder now creates users directly via DB if login fails,
instead of silently aborting. This handles Railway PR environments where
the releaseCommand may not execute properly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
chihlasm
2026-02-19 20:29:22 -05:00
parent 372d412fec
commit efb3ec13b4

View File

@@ -27,8 +27,20 @@ def _configure_seed_module(mod: object, api_url: str, email: str, password: str)
mod.ADMIN_PASSWORD = password # type: ignore[attr-defined]
async def _seed_users_directly() -> None:
"""Seed test users directly via DB if they don't exist yet."""
try:
from scripts.seed_test_users import main as seed_users
logger.info("[seed] Seeding test users directly via DB...")
await seed_users()
logger.info("[seed] Test users seeded!")
except Exception as e:
logger.warning(f"[seed] User seeding failed: {e}")
raise
async def _seed_trees_background() -> None:
"""Background task: seed all flows via HTTP API after server is ready."""
"""Background task: seed test users + all flows after server is ready."""
await asyncio.sleep(5) # Wait for server to be fully ready
port = os.environ.get("PORT", "8000")
api_url = f"http://127.0.0.1:{port}/api/v1"
@@ -37,12 +49,17 @@ async def _seed_trees_background() -> None:
try:
import httpx
# Login to verify admin user exists
# Try to login — if it fails, seed users first
async with httpx.AsyncClient(base_url=api_url, timeout=30) as client:
login_resp = await client.post("/auth/login/json", json={"email": email, "password": password})
if login_resp.status_code != 200:
logger.warning("[seed] Could not login as admin — skipping flow seeding")
return
logger.warning("[seed] Admin login failed — seeding users first")
await _seed_users_directly()
# Retry login after seeding users
login_resp = await client.post("/auth/login/json", json={"email": email, "password": password})
if login_resp.status_code != 200:
logger.error(f"[seed] Admin login still failing after user seed (status={login_resp.status_code}) — aborting")
return
token = login_resp.json()["access_token"]
# Check if trees already exist