Record the shipping region on every order

Database · PostgreSQL · intermediate · modification

Checkout now routes every order to one of three regional warehouses. `app/routing.py` (new) maps the shipping country to a region. `app/orders.py` stores the region when it inserts an order and returns it with the order. Migration `migrations/0042_add_order_region.sql` adds the `region` column to `orders`. `tests/test_order_region.py` (new) covers the country mapping and an order round trip through the database.

Checkout service: Python 3.12, psycopg 3.2 (plain SQL, no ORM), PostgreSQL 16, pytest. Migrations are plain SQL files in `migrations/`, applied in order by the existing runner `python -m app.migrate` (unchanged): each pending file runs in its own transaction with `SET LOCAL lock_timeout = '5s'` (retried up to 3 times on a lock timeout), and its version is recorded in `schema_migrations` in the same transaction, so a file that fails leaves nothing behind and the runner exits non-zero. The deploy pipeline runs the runner against the environment's database before it rolls out any pod; if the runner fails, the deploy stops and the previous release keeps serving. Pods are then replaced one at a time over a few minutes. `orders` (migration `0001`, unchanged: `id`, `customer_id`, `shipping_country char(2)`, `total_cents`, `created_at`) holds about 3 million rows in production and about 20,000 on staging. Orders are inserted only by `create_order`, and nothing updates or deletes them. The checkout endpoint (existing, unchanged) calls `create_order` inside its request transaction with a `shipping_country` it has validated as an uppercase ISO 3166-1 alpha-2 code. `GET /orders/{id}` (existing, unchanged) returns `dataclasses.asdict` of `get_order(...)`, or 404 for `None`. Value sets like the region are enforced by the application's types, not by CHECK constraints (team convention). `tests/conftest.py` (existing, unchanged) creates a fresh, empty database for each test session, runs the migration runner on it, and provides the `conn` fixture (rolled back after each test) and the `customer_id` fixture; CI runs `pytest` this way.

Requirements

Files touched

Review this PR

Database practice