Redeem coupons through one database function

Database · PostgreSQL · advanced · modification

Moves coupon redemption behind a single database function so the API role no longer touches the `coupons` table. `migrations/0047_coupon_redeem_function.sql` (new) revokes the API role's access to `coupons`, creates `redeem_coupon(code, order_id)` owned by the schema owner to validate and mark a coupon used in one statement, and grants only EXECUTE on it to the API role. `app/checkout.py` switches the endpoint from its own `UPDATE coupons` to a `SELECT public.redeem_coupon(...)` call. `tests/test_coupons.py` (new) covers a successful redemption, the single-use, expired, unknown and other-customer cases, and that the API role can no longer read or write `coupons` directly.

Checkout service for a web shop: PostgreSQL 16, Python 3.12, FastAPI 0.115, psycopg 3.2 with dict rows and psycopg_pool 3.2 (plain SQL, no ORM). Existing and unchanged schema (earlier migrations in the `public` schema): `orders` (`id`, `customer_id`, `status` in `pending`/`paid`/`cancelled`, `total_cents`, nullable `percent_off`), `coupons` (`id`, `code` unique, `percent_off` 1..100, `expires_at timestamptz NOT NULL`, nullable `redeemed_at`, nullable `redeemed_order_id`) and `partner_payouts` (`id`, `partner_name`, `iban`, `amount_cents`), which holds partner bank details and is read only by the finance service. Roles: `app_owner` owns the database, the `public` schema and every table in it and is used only by the migration runner; the API's pool connects as `app_api` (`DATABASE_URL`; LOGIN, not a superuser, no BYPASSRLS, owns nothing in `public`). `app_api` also owns a separate schema `app_api` for its own scratch and cache tables (declared, pre-existing, unchanged), and may create objects there. The cluster default `search_path` is `"$user", public` and is not overridden per role or per database, so for `app_api` its own schema is searched before `public`. The same `app_api` credentials are also held by other callers: a nightly reporting job and a small number of contractors, so what `app_api` can do to the database matters beyond the web endpoint. Migrations are plain SQL files in `migrations/`, applied in order as `app_owner` by the existing runner, each file in its own transaction. A deploy stops the API, runs the pending migrations and then starts the new release. `pool.connection()` (psycopg_pool) hands out a connection for the block and commits (or rolls back on an exception) when the block ends. `app.auth.current_customer` answers 401 itself unless the request has a signed-in user and returns that user's customer id, a positive `int`. Tests run with pytest against a fresh test database built from `migrations/` as `app_owner`, with the API's pool connected as `app_api` like production; `tests/conftest.py` provides `db` (an `app_owner` connection, with the tables emptied before each test) and `client_for(customer_id)` (a FastAPI `TestClient` signed in as that customer).

Requirements

Files touched

Review this PR

Database practice