Move budget between cost centres

Database · PostgreSQL · intermediate · modification

Finance admins can now move budget from one cost centre to another, for example when marketing hands part of its quarter to sales. `POST /api/budget-transfers` validates the request (positive amount up to 10^10 cents, two different cost centres) and calls the new `budget.transfer`, which does the whole move in one transaction: it locks both cost centre rows of the admin's company in id order, debits the source with a conditional `UPDATE` that only applies when the budget covers the amount, credits the destination and inserts the `budget_transfers` row. A missing cost centre or a short budget raises before anything is kept, and the route maps them to 404 and 409. Migration `0058_budget_transfers.sql` adds the `budget_transfers` table. Includes tests in `tests/test_budget_transfers.py`.

Spend-management API: FastAPI 0.115 on Python 3.12 with psycopg 3 (plain SQL, no ORM). `app/db.py` holds a `psycopg_pool.ConnectionPool` (at most 10 connections per pod; `pool.connection()` commits on a clean exit and rolls back on an exception), and the API runs as 4 pods against one PostgreSQL 16 primary at the default isolation level, READ COMMITTED. Tables from earlier migrations: `companies` (`id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`, `name text NOT NULL`, `created_at timestamptz NOT NULL DEFAULT now()`); `users` (`id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`, `company_id bigint NOT NULL REFERENCES companies (id)`, `email text NOT NULL UNIQUE`, `created_at timestamptz NOT NULL DEFAULT now()`); users are never deleted and their ids never change (a leaver is deactivated in the sign-in provider instead); `cost_centres` (`id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`, `company_id bigint NOT NULL REFERENCES companies (id)` with an index, `name text NOT NULL`, `budget_cents bigint NOT NULL DEFAULT 0 CHECK (budget_cents >= 0)`, `created_at timestamptz NOT NULL DEFAULT now()`). There are a few thousand cost centres; they are never deleted, and every budget stays below 10^12 cents. Two existing writers change budgets, each in its own short autocommit statement: card spend runs `UPDATE cost_centres SET budget_cents = budget_cents - %s WHERE id = %s AND budget_cents >= %s` and declines the payment when no row comes back, and the monthly top-up runs `UPDATE cost_centres SET budget_cents = budget_cents + %s WHERE id = %s`. No transaction on these tables stays open for long. `app.auth.current_finance_admin` (existing) returns `Admin(user_id, company_id)` for a signed-in finance admin, or answers 401 when signed out and 403 for any other user. `app/main.py` already includes the `cost_centres` router and opens the pool at startup. Schema changes are plain SQL files in `migrations/`, applied in name order by the existing runner at the start of each deploy, each file in one transaction. The test suite (`tests/conftest.py`, existing) builds a fresh test database from the migrations, empties it between tests, and provides `create_company(name="Acme")` (a company and one finance admin, returns `(company_id, user_id)`), `create_cost_centre(company_id, name, budget_cents=0)` (returns the id), `fetch_one(sql, params)` and `client_for(user_id, company_id)` (a test client signed in as that admin; the app, and with it the pool, runs for the whole session).

Requirements

Files touched

Review this PR

Database practice