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
- Orders now ship from three regional warehouses, and each order records the region it is routed to in a new `orders.region` column (`text`, never null): `us` for shipping countries `US` and `CA`, `apac` for `AU`, `NZ`, `JP` and `SG`, and `eu` for every other country. `create_order` sets it from the shipping country, and `Order` (so also `GET /orders/{id}`) includes it.
- Every order placed before this release shipped from the EU warehouse, so every existing order gets `eu`. So does any order that a pod still on the previous release inserts during the rollout (such orders carry no region and are routed the old way, to the EU warehouse).
- Migration `0042` applies to the existing staging and production databases as part of the normal deploy. Checkout writes to `orders` all day, so the migration may hold a lock on `orders` only briefly.
- The PR's tests run with the existing pytest setup.
Files touched
- migrations/0042_add_order_region.sql
- app/routing.py
- app/orders.py
- tests/test_order_region.py