Store a due date on every invoice

Database · PostgreSQL · intermediate · modification

Adds a stored due date to invoices, in three migrations. `0071` adds the nullable `invoices.due_on` column. `0072` is a Python migration that fills it for the existing invoices in batches. `0073` makes the column NOT NULL by way of a validated CHECK and builds a partial index on the due dates of open invoices. `app/invoices.py` writes `due_on` when it creates an invoice, and the overdue query now reads it. `tests/test_invoice_due_dates.py` (new) covers the module.

Billing service: Python 3.12, psycopg 3.2 (plain SQL, no ORM), PostgreSQL 16 with one primary. `invoices` (migration `0001`, unchanged until this PR): `id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`, `customer_id bigint NOT NULL`, `issued_on date NOT NULL`, `terms_days integer NOT NULL CHECK (terms_days BETWEEN 0 AND 120)`, `total_cents bigint NOT NULL`, `status text NOT NULL DEFAULT 'open'` (`open`, `paid` or `void`), `paid_at timestamptz` (nullable), `last_reminded_on date` (nullable), `created_at timestamptz NOT NULL DEFAULT now()`; indexes: the primary key and `invoices_customer_idx` on `customer_id`. It holds about 6 million invoices, about 2% of them open; `terms_days` is fixed when an invoice is issued, and `issued_on` is always between 2015-01-01 and today: the invoice form and the API reject any other date, and every stored invoice is in that range. Writers: the API pods mark invoices paid all day (`UPDATE invoices SET status = 'paid', paid_at = now() WHERE id = %s AND status = 'open'`, one invoice per transaction, up to about 20 a second); the billing worker, a single process, is the only code that inserts invoices (through `create_invoice`, inside its own transaction) and also runs the nightly reminders (`overdue_invoices`, then `last_reminded_on` per invoice). Finance runs read-only reports on `invoices` that can keep a transaction open for several minutes. Deploys: the pipeline stops the billing worker, runs the migration runner, starts the new worker and then replaces the API pods one at a time; a failed migration fails the deploy, which the pipeline retries up to 3 times. A failed deploy leaves the billing worker stopped (no rollback brings the previous worker back); it starts again only after a deploy's migrations have all succeeded. The runner (`app/migrate.py`, existing, unchanged) applies the files in `migrations/` in name order, each on its own new connection (so a session setting ends with its file), and records each file in `schema_migrations` once it has succeeded; a failed file is not recorded and is run again next time. A `.sql` file runs as one transaction, and the runner inserts its `schema_migrations` row inside that same transaction, so the file and its record commit together. If its first line is `-- migrate: no-transaction`, each statement runs on its own in autocommit (a statement ends at a line ending in `;`), and the file is recorded only after its last statement has succeeded, so a file that stops part-way runs again from the top. A `.py` file's `run(conn)` is called with an autocommit connection and the file is recorded after `run` returns, in a separate statement, so it too can run again after a failure at any point. No `lock_timeout` or `statement_timeout` is set unless a file sets one. The test suite (`tests/conftest.py`, existing) builds a fresh, empty test database from all migrations once per session and provides `db`, an autocommit connection with `invoices` truncated before each test.

Requirements

Files touched

Review this PR

Database practice