Nightly customer export for the email marketing sync

Database · PostgreSQL · intermediate · greenfield

Adds the nightly export the marketing team needs to sync our customer list into their email tool. `app/export_customers.py` (new) reads `customers` in pages of 1,000 in one read-only repeatable-read transaction, so every page sees the same snapshot even while people sign up, and writes `id,email,created_at` lines to the CSV path it is given, oldest signup first. Ops already has the cron entry waiting for this command. `tests/test_export_customers.py` (new) runs the export against the test database with a small page size.

Shop backend: Python 3.12 with psycopg 3 (plain SQL), PostgreSQL 16, one primary, the default READ COMMITTED isolation for the app's sessions. `app/db.py` (existing) defines `pool = ConnectionPool(os.environ["DATABASE_URL"], min_size=1, max_size=10, open=False)` from `psycopg_pool`; a process opens it before use, and `with pool.connection() as conn` runs everything in the block in one transaction, committed when the block exits (rolled back on an exception). The table, created by an earlier migration: `customers (id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, email text NOT NULL UNIQUE, created_at timestamptz NOT NULL DEFAULT now())`, about 120,000 rows; its only indexes are the primary key and the unique index on `email`, so each page query sorts the table; runtime is not a concern for a nightly job at this size. Customers arrive two ways: signups, one row per transaction, and the existing admin CSV import (`app/imports.py`, unchanged), which inserts a whole uploaded file in one transaction and leaves `created_at` to the default, so every customer from one import has the same `created_at` (`now()` is the transaction's start time). Imports of a few hundred to about 5,000 customers happen several times a month. The ops cron runs the command at 03:00 and starts the email tool's upload only after the command exits 0, so a failed run uploads nothing. Schema changes are plain SQL files in `migrations/` applied in order by the existing runner `app/migrate.py`; this PR has none. The test suite (`tests/conftest.py`, existing) builds a fresh, empty test database from all migrations, opens `pool` for the session, truncates `customers` before each test and provides `insert_customer(email, created_at)`, which returns the new id.

Requirements

Files touched

Review this PR

Database practice