A FastAPI backend for uploading, validating, and storing transaction data from CSV files. Designed for easy onboarding, robust testing, and rapid development.
-
main.py
Entry point. Initializes FastAPI, sets up CORS, creates DB tables, and includes API routers. -
app/core/
database.py: Handles SQLAlchemy DB engine, session, and base class. Loads DB config from.env.
-
app/uploads/
models.py: SQLAlchemy models (currently, theTransactiontable).routers.py: FastAPI endpoints for CSV upload and validation.schemas.py: Pydantic response schemas.services.py: Core logic for processing and merging CSV data into the DB.validators.py: CSV column validation logic.
-
app/tests/
conftest.py: Pytest fixtures for DB and FastAPI client.test_*.py: Unit and integration tests for uploads, performance, and duplicate handling.
-
alembic/
Database migrations (see below for usage). -
Dockerfile
Containerizes the app for production or local development. -
docker-compose.yml
Orchestrates the app (and optionally a Postgres DB) for local development. -
.env
Stores DB connection strings and secrets. You must create or update this before running the app!
-
Clone the repo
git clone <your-repo-url> cd csvup-server
-
Create
.envfile
Copy.env.example(if present) or create.envwith at least:DATABASE_URL=postgresql://<user>:<password>@<host>:<port>/<dbname> POSTGRES_DB=<dbname> POSTGRES_USER=<user> POSTGRES_PASSWORD=<password>Or, for SQLite (for quick testing):
DATABASE_URL=sqlite:///./uploads.db -
Install dependencies
python3 -m venv venv source venv/bin/activate pip install -r requirements.txt -
Run database migrations
alembic upgrade head
-
Run the app
uvicorn main:app --reload
- Docs available at http://localhost:8000/
-
Ensure your
.envis set up (see above). -
Start services
docker-compose up --build
The API will be available at http://localhost:8000/
Note:
The default
docker-compose.ymlexpects an external Postgres DB (yours sincerely uses one hosted by Render.com free tier).
To use a local Postgres container, uncomment thedb:service and related lines indocker-compose.yml.
Migrations:
The start.sh script automatically runs alembic upgrade head before starting the app in Docker.
- Create new migration
alembic revision --autogenerate -m "description" - Apply migrations
alembic upgrade head
- Rollback last migration
alembic downgrade -1
- Check current revision
alembic current
- Show migration history
alembic history
Render runs alembic upgrade head automatically before starting the app (see render.yaml), so your database is always up to date with the latest migrations.
- With local Python pytest:
pytest
Tests cover:
- Upload endpoint (valid, invalid, empty, malformed CSVs)
- Duplicate transaction handling
- Performance and edge cases
-
Folder meanings:
core/: Shared infra (DB, config)uploads/: All upload logic, models, and validationtests/: All test code and fixtures
-
Major files:
main.py: App entrypointdatabase.py: DB setupmodels.py: DB schemarouters.py: API endpointsservices.py: Business logicvalidators.py: Data validation
-
Before pushing:
- Run all tests (
pytest) - Check
.envis not committed (see.gitignore)
- Run all tests (
-
Where do I add new endpoints?
Inapp/uploads/routers.py. -
How do I add new DB fields?
Updateapp/uploads/models.pyand run migrations (if using Alembic). -
How do I test with a clean DB?
The test suite uses an in-memory SQLite DB for isolation.
Welcome aboard!
If you get stuck, check the code comments or ask a teammate.