seed user

This commit is contained in:
counterweight 2025-12-18 22:42:32 +01:00
parent ca55932a41
commit c5d3c7f4c9
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
3 changed files with 49 additions and 10 deletions

11
.envrc
View file

@ -1,11 +1,6 @@
# Local development environment variables
# To use: install direnv (https://direnv.net), then run `direnv allow`
# Backend
export SECRET_KEY="dev-secret-key-change-in-production"
export DATABASE_URL="postgresql+asyncpg://postgres:postgres@localhost:5432/arbret"
export TEST_DATABASE_URL="postgresql+asyncpg://postgres:postgres@localhost:5432/arbret_test"
# Frontend
export NEXT_PUBLIC_API_URL="http://localhost:8000"
set -a
source .env
set +a

View file

@ -1,4 +1,7 @@
.PHONY: install-backend install-frontend install backend frontend db db-stop db-ready dev test test-backend test-frontend test-e2e
.PHONY: install-backend install-frontend install backend frontend db db-stop db-ready db-seed dev test test-backend test-frontend test-e2e
-include .env
export
install-backend:
cd backend && uv sync --all-groups
@ -30,8 +33,11 @@ db-ready:
docker compose exec -T db psql -U postgres -c "CREATE DATABASE arbret_test"
@echo "PostgreSQL is ready"
db-seed: db-ready
cd backend && uv run python seed.py
dev:
$(MAKE) db
$(MAKE) db-seed
cd backend && uv run uvicorn main:app --reload & \
cd frontend && npm run dev & \
wait

38
backend/seed.py Normal file
View file

@ -0,0 +1,38 @@
"""Seed the database with a dev user."""
import asyncio
import os
from sqlalchemy import select
from database import engine, async_session, Base
from models import User
from auth import get_password_hash
DEV_USER_EMAIL = os.environ["DEV_USER_EMAIL"]
DEV_USER_PASSWORD = os.environ["DEV_USER_PASSWORD"]
async def seed():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async with async_session() as db:
result = await db.execute(select(User).where(User.email == DEV_USER_EMAIL))
user = result.scalar_one_or_none()
if user:
user.hashed_password = get_password_hash(DEV_USER_PASSWORD)
await db.commit()
print(f"Updated dev user: {DEV_USER_EMAIL} / {DEV_USER_PASSWORD}")
else:
user = User(
email=DEV_USER_EMAIL,
hashed_password=get_password_hash(DEV_USER_PASSWORD),
)
db.add(user)
await db.commit()
print(f"Created dev user: {DEV_USER_EMAIL} / {DEV_USER_PASSWORD}")
if __name__ == "__main__":
asyncio.run(seed())