Development Workflow
Overview
This document outlines the recommended development workflow for working with the Rhesis backend. Following these practices will help maintain code quality and consistency across the project.
Local Development Setup
Initial Setup
- Clone the repository
- Set up your Python environment (Python 3.10+)
- Install dependencies with UV:
uv pip install -e .
- Set up your local PostgreSQL database
- Configure your
.env
file with local settings
Running the Application
Start the FastAPI development server:
uvicorn rhesis.backend.app.main:app --reload
The --reload
flag enables auto-reloading when code changes are detected.
Code Style and Quality
Formatting
The project uses ruff
for code formatting. Format your code using:
uv run --all-groups ruff format .
This ensures consistent code style across the project.
Linting
Run the linter to check for code quality issues:
uv run --all-groups ruff check .
Fix automatically fixable issues:
uv run --all-groups ruff check --fix .
Type Checking
Although not strictly enforced, adding type hints to your code is encouraged for better IDE support and code quality.
Database Changes
Creating Models
When creating new database models:
- Define your model in
app/models/
- Import it in
app/models/__init__.py
- Create corresponding Pydantic schemas in
app/schemas/
- Add CRUD operations in
app/crud.py
Database Migrations
For schema changes:
- Make changes to your SQLAlchemy models
- Generate a migration:
alembic revision --autogenerate -m "Description of changes"
- Review the generated migration file in
alembic/versions/
- Apply the migration:
alembic upgrade head
API Development
Creating New Endpoints
When adding new API endpoints:
- Create or modify a router file in
app/routers/
- Define your endpoint functions with appropriate decorators
- Import and register your router in
app/routers/__init__.py
- Add necessary schemas, models, and CRUD operations
Testing Endpoints
Test your endpoints using:
- FastAPI’s interactive documentation (Swagger UI) at
/docs
- API client tools like Postman or Insomnia
- Automated tests (see Testing section)
Background Tasks
Creating Celery Tasks
When creating new background tasks:
- Define your task in an appropriate file in
tasks/
- Import and register your task in
tasks/__init__.py
- Add any necessary database models and schemas
Testing Tasks
Test your tasks using:
- Direct invocation in development mode
- Running a local Celery worker:
celery -A rhesis.backend.worker worker --loglevel=info
Testing
Writing Tests
Tests are located in the tests/backend/
directory. When writing tests:
- Create test files that mirror the structure of the code being tested
- Use pytest fixtures for common setup and teardown
- Use a test database (configured in
conftest.py
)
Running Tests
Run the test suite:
pytest
Run specific tests:
pytest tests/backend/test_specific_module.py
Debugging
Logging
Use the application’s logging system for debugging:
from rhesis.backend.logging import logger
logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")
Interactive Debugging
For interactive debugging:
- Add breakpoints using
breakpoint()
or your IDE’s debugging tools - Run the application in debug mode through your IDE
- Use
print()
statements judiciously for quick debugging
Git Workflow
Branching Strategy
main
: Production-ready codedevelop
: Integration branch for feature development- Feature branches: Created from
develop
for new features - Hotfix branches: Created from
main
for urgent fixes
Commit Guidelines
Follow these guidelines for commit messages:
- Use present tense (“Add feature” not “Added feature”)
- First line is a summary (max 50 characters)
- Optionally followed by a blank line and detailed description
- Reference issue numbers where appropriate
Example:
Add user authentication endpoint
- Implement JWT token generation
- Add user validation
- Set up Auth0 integration
Fixes #123
Pull Requests
When creating pull requests:
- Provide a clear description of the changes
- Reference any related issues
- Ensure all tests pass
- Request review from appropriate team members
Deployment
Preparing for Deployment
Before deploying:
- Ensure all tests pass
- Check for any security vulnerabilities in dependencies
- Update documentation if necessary
- Verify environment variables are correctly set
Deployment Process
The deployment process varies by environment but typically involves:
- Building a Docker image
- Running database migrations
- Deploying the new container
- Verifying the deployment
Troubleshooting
Common Issues
- Database connection errors: Check your database connection string and ensure PostgreSQL is running
- Missing environment variables: Verify your
.env
file contains all required variables - Import errors: Check your Python path and virtual environment
- Authentication issues: Verify Auth0 configuration and JWT settings
Getting Help
If you encounter issues:
- Check the project documentation
- Review relevant code and comments
- Ask for help in the project’s communication channels
- Create an issue on the project’s issue tracker