diff --git a/.envrc b/.envrc index 94a6067..160c112 100644 --- a/.envrc +++ b/.envrc @@ -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 diff --git a/Makefile b/Makefile index 67db7ed..d06cbfe 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/backend/seed.py b/backend/seed.py new file mode 100644 index 0000000..2bc2d6a --- /dev/null +++ b/backend/seed.py @@ -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()) +