My orders page for signed-in customers

Database · PostgreSQL · intermediate · modification

Adds the My orders endpoint the account page has been waiting for. `GET /api/me/orders` returns the signed-in customer's 20 most recent orders, newest first, with the order id as the tie-breaker for orders placed at the same instant. `app/orders.py` adds the endpoint and moves the row-to-JSON mapping into a helper that the existing single-order endpoint now uses too. Migration `0057_orders_customer_index.sql` adds an index on `orders (customer_id, created_at, id)` so the query reads one customer's rows instead of the whole table. `tests/test_my_orders.py` (new) covers the ordering, the customer scoping, the limit of 20 and the tie-break.

Shop API: FastAPI 0.115 on Python 3.12 with psycopg 3 (plain SQL, a `psycopg_pool.ConnectionPool` in `app/db.py`, at most 10 connections per pod), PostgreSQL 16, one primary. `orders` (`id bigint` identity primary key, `customer_id bigint NOT NULL`, `status text`, `total_cents bigint`, `created_at timestamptz NOT NULL DEFAULT now()`) holds about 40 million rows and is written all day: checkout inserts up to about 30 orders a second at peak and the fulfilment workers update `status` continuously. Its existing indexes are the primary key and `orders_status_idx` on `status`. `app.auth.current_customer_id` (existing) is a FastAPI dependency that returns the signed-in customer's id or answers 401. Schema changes are plain SQL files in `migrations/`, applied by the existing runner `app/migrate.py` at the start of every deploy, before the new pods take traffic: files run in name order and are recorded in `schema_migrations`; each file runs as one transaction, unless its first line is `-- migrate: no-transaction`, in which case each of its statements runs on its own in autocommit (for statements that cannot run in a transaction block) and the file is recorded only after its last statement succeeds. A failed migration fails the deploy, which is then retried. No `lock_timeout` or `statement_timeout` is set for the runner or for the app's sessions (the Postgres defaults). The test suite (`tests/conftest.py`, existing) builds a fresh, empty test database from all migrations and provides `client_for(customer_id)` (a test client signed in as that customer) and `insert_order(customer_id, created_at, status="paid", total_cents=1000)`.

Requirements

Files touched

Review this PR

Database practice