Files
bike-app/.gitea/workflows/ci.yml
T
BBergleandClaude Opus 5 ddea750792
CI / Repo hygiene (pull_request) Successful in 3s
CI / Web (lint, typecheck, build) (pull_request) Successful in 2s
CI / Migrations reversible (pull_request) Successful in 12s
CI / API (lint, types, tests) (pull_request) Successful in 1m46s
feat(api): FastAPI skeleton with two-role RLS auth foundation
Phase 0's API half: a working FastAPI app with register/login/me/logout,
backed by a Postgres schema where row-level security is real and
independently proven, not just declared.

The core design decision, and the reason this lands as one PR instead of
several: request-scoped queries run as `velodrome_app` (NOBYPASSRLS), but
looking up identity in the first place — login by email, a session by its
token hash — has to happen *before* app.user_id can be set, so those specific
lookups run as a second role, `velodrome_auth` (BYPASSRLS), used nowhere else
in the codebase. See velodrome/db.py's module docstring and apps/api/README.md
for the full rationale. This is genuinely one reviewable unit: the migration,
the models, and the auth service only make sense evaluated together, since
they're three views of the same invariant.

tests/test_auth.py::test_rls_blocks_cross_user_session_reads is the test
worth reading first — it doesn't trust the RLS policy SQL because it reads
correctly, it proves isolation by registering two users and confirming a
scoped read of `sessions` for user A returns exactly one row, never two.

Bugs found and fixed while actually running this against real Postgres
(everything below was verified against a live postgis/postgis:16-3.4
container and a built Docker image, not just read for correctness):

- CREATE ROLE's PASSWORD clause is DDL, not DML — it doesn't accept bind
  parameters (`PASSWORD $1` is a syntax error). Fixed with dollar-quoting.
- Postgres roles are cluster-wide, not per-database — a second database in
  the same cluster hit "role already exists" on a plain CREATE ROLE. Fixed
  with a DO block catching duplicate_object.
- A bare `Mapped[datetime]` on the ORM models infers a naive timestamp,
  silently disagreeing with the migration's correct `DateTime(timezone=True)`
  — asyncpg rejected the mismatch at insert time. Fixed once, at the
  declarative Base level via type_annotation_map, rather than per-column.
- The session cookie's `secure` flag was gated on `!= "development"`, so
  anything else — including local testing and a real deploy running
  temporarily without TLS in front — got a Secure cookie no HTTP client
  will ever send back, breaking every authenticated request after login
  with no visible error. Gated on `== "production"` instead.
- `alembic check` initially flagged every PostGIS/TIGER-installed table
  (dozens of them) as drift, because they're not in our metadata. A
  schema-based denylist doesn't work — reflected foreign tables come back
  with schema=None regardless of their real schema. Fixed with an
  allowlist keyed on target_metadata.tables instead, which is also more
  robust against future PostGIS versions adding more tables.
- The migration itself was missing `nullable=False` on three timestamp
  columns that the ORM model assumed were never null — a genuine
  model/migration drift that alembic check caught once the PostGIS noise
  above was filtered out. Fixed in 0001 directly, since it's never shipped.
- Two indexes the migration creates explicitly weren't declared on the
  ORM models, causing the same kind of drift. Added index=True to match.

Deliberately deferred, not forgotten: per-IP/per-account login rate
limiting (docs/PLAN.md mentions it; Phase 0's bar is a working skeleton,
and this needs its own design pass) and the procrastinate job runner /
worker container (nothing to run yet — arrives with the ingestion
pipeline).

ci.yml updated to match: the api and migrations jobs now provision the
same two runtime roles this code actually needs, replacing the single
placeholder DATABASE_URL from before any code existed.

Verified: ruff check, ruff format --check, and mypy --strict all clean.
12/12 pytest passing against a real Postgres. Full alembic upgrade ->
downgrade -1 -> upgrade cycle run twice (once standalone, once inside a
two-database cluster to specifically catch the role-collision bug).
alembic check clean. Docker image builds and serves real traffic —
register and an authenticated GET /me both exercised against the actual
built container, not just the test suite.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 08:14:06 -04:00

214 lines
6.9 KiB
YAML

name: CI
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
concurrency:
group: ci-${{ gitea.ref }}
cancel-in-progress: true
jobs:
meta:
name: Repo hygiene
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Branch name follows convention
if: gitea.event_name == 'pull_request'
run: |
BRANCH="${{ gitea.head_ref }}"
echo "Branch: $BRANCH"
if echo "$BRANCH" | grep -Eq '^(feat|fix|refactor|test|docs|chore|ci)/[a-z0-9._-]+$'; then
echo "OK"
else
echo "::error::Branch '$BRANCH' does not match <type>/<desc>."
echo "Valid types: feat fix refactor test docs chore ci"
exit 1
fi
- name: No secrets committed
run: |
# Deliberately narrow: high-signal patterns only, so this never cries wolf.
PATTERN='BEGIN (RSA|OPENSSH|EC|PGP) PRIVATE KEY|xoxb-[0-9A-Za-z-]{10,}|AKIA[0-9A-Z]{16}'
if git grep -nIE "$PATTERN" -- . ':!.gitea/workflows/*' ; then
echo "::error::Possible secret committed (see matches above)."
exit 1
fi
echo "No secret patterns found."
- name: No ride data committed
run: |
if git ls-files | grep -E '\.(fit|gpx|tcx)$' | grep -v '^apps/api/tests/fixtures/fit/'; then
echo "::error::Ride files belong in the blob store, not git."
echo "Only apps/api/tests/fixtures/fit/ may contain .fit files (golden fixtures)."
exit 1
fi
echo "Clean."
api:
name: API (lint, types, tests)
runs-on: ubuntu-latest
services:
postgres:
image: postgis/postgis:16-3.4
env:
# Superuser — this is the "owner" role migrations run as (see alembic/env.py); it's
# what CREATEs the two runtime roles below, which is why it isn't one of them.
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: velodrome_test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 10
env:
VELODROME_ENVIRONMENT: test
VELODROME_DATABASE_URL_MIGRATE: postgresql+asyncpg://postgres:postgres@postgres:5432/velodrome_test
VELODROME_DB_APP_PASSWORD: ci-only-app-password
VELODROME_DB_AUTH_PASSWORD: ci-only-auth-password
VELODROME_DATABASE_URL_APP: postgresql+asyncpg://velodrome_app:ci-only-app-password@postgres:5432/velodrome_test
VELODROME_DATABASE_URL_AUTH: postgresql+asyncpg://velodrome_auth:ci-only-auth-password@postgres:5432/velodrome_test
steps:
- uses: actions/checkout@v4
- name: Is there API code yet?
id: guard
run: |
if [ -n "$(find apps/api -name '*.py' -not -path '*/.*' 2>/dev/null | head -1)" ]; then
echo "present=true" >> "$GITHUB_OUTPUT"
else
echo "present=false" >> "$GITHUB_OUTPUT"
echo "No Python sources under apps/api yet — skipping."
fi
- uses: actions/setup-python@v5
if: steps.guard.outputs.present == 'true'
with:
python-version: '3.12'
- name: Cache uv
if: steps.guard.outputs.present == 'true'
uses: actions/cache@v4
with:
path: ~/.cache/uv
key: uv-${{ runner.os }}-${{ hashFiles('apps/api/pyproject.toml') }}
restore-keys: uv-${{ runner.os }}-
- name: Install
if: steps.guard.outputs.present == 'true'
working-directory: apps/api
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
export PATH="$HOME/.local/bin:$PATH"
uv sync --all-extras
- name: Lint (ruff)
if: steps.guard.outputs.present == 'true'
working-directory: apps/api
run: |
export PATH="$HOME/.local/bin:$PATH"
uv run ruff check .
uv run ruff format --check .
- name: Types (mypy --strict)
if: steps.guard.outputs.present == 'true'
working-directory: apps/api
run: |
export PATH="$HOME/.local/bin:$PATH"
uv run mypy --strict velodrome
- name: Tests
if: steps.guard.outputs.present == 'true'
working-directory: apps/api
run: |
export PATH="$HOME/.local/bin:$PATH"
uv run pytest -q
web:
name: Web (lint, typecheck, build)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Is there web code yet?
id: guard
run: |
if [ -f apps/web/package.json ]; then
echo "present=true" >> "$GITHUB_OUTPUT"
else
echo "present=false" >> "$GITHUB_OUTPUT"
echo "No apps/web/package.json yet — skipping."
fi
- uses: actions/setup-node@v4
if: steps.guard.outputs.present == 'true'
with:
node-version: '22'
- name: Install, lint, build
if: steps.guard.outputs.present == 'true'
working-directory: apps/web
run: |
corepack enable
pnpm install --frozen-lockfile
pnpm run lint
pnpm run check
pnpm run build
migrations:
name: Migrations reversible
runs-on: ubuntu-latest
services:
postgres:
image: postgis/postgis:16-3.4
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: velodrome_mig
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 10
env:
VELODROME_DATABASE_URL_MIGRATE: postgresql+asyncpg://postgres:postgres@postgres:5432/velodrome_mig
VELODROME_DB_APP_PASSWORD: ci-only-app-password
VELODROME_DB_AUTH_PASSWORD: ci-only-auth-password
steps:
- uses: actions/checkout@v4
- name: Are there migrations yet?
id: guard
run: |
if [ -d apps/api/alembic/versions ] && [ -n "$(ls -A apps/api/alembic/versions/*.py 2>/dev/null)" ]; then
echo "present=true" >> "$GITHUB_OUTPUT"
else
echo "present=false" >> "$GITHUB_OUTPUT"
echo "No migrations yet — skipping."
fi
- uses: actions/setup-python@v5
if: steps.guard.outputs.present == 'true'
with:
python-version: '3.12'
- name: upgrade -> downgrade -> upgrade, and check for model drift
if: steps.guard.outputs.present == 'true'
working-directory: apps/api
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
export PATH="$HOME/.local/bin:$PATH"
uv sync --all-extras
uv run alembic upgrade head
uv run alembic downgrade -1
uv run alembic upgrade head
# Fails if the models have drifted from the migrations.
uv run alembic check