Customer accounts: sign up with an email address

Database · PostgreSQL · intermediate · modification

First step towards customer accounts (the shop has had guest checkout only). Migration `0002_accounts.sql` creates the `accounts` table with a unique constraint on the email, so the database decides whether an address is taken, also for two signups at the same moment. `app/accounts.py` (new) adds `POST /api/accounts`: it validates the body, hashes the password, inserts the account and turns a violation of the email constraint into 409 `email_taken`; any other database error propagates. `app/main.py` mounts the new router. `tests/test_accounts.py` (new) adds API tests for signup. Sign-in comes in the next PR.

Shop API: FastAPI 0.115 on Python 3.12 with psycopg 3 (plain SQL, a `psycopg_pool.ConnectionPool` in `app/db.py`, at most 10 connections per pod; `pool.connection()` commits when its block exits normally and rolls back when it raises), PostgreSQL 16, one primary, default READ COMMITTED, database encoding UTF8 with collation and ctype `en_US.utf8`. Existing and unchanged: migration `0001` (the `orders` table of guest checkout, not touched here), the guest checkout router `app/checkout.py`, and `app.security.hash_password(password: str) -> str` (argon2id, returns an encoded hash with its own salt). Schema changes are plain SQL files in `migrations/`, applied by the existing runner `app/migrate.py` at the start of every deploy, before the new pods take traffic: files run in name order and are recorded in `schema_migrations`; each file runs as one transaction (unless its first line is `-- migrate: no-transaction`), so a file that fails leaves nothing behind and fails the deploy. `accounts` is a new, empty table, and no released code reads it yet. Sign-in (next PR) will look an account up by its address regardless of letter case, per requirement 2. The test suite (`tests/conftest.py`, existing) builds a fresh, empty test database from all migrations, empties every table before each test and provides `client` (an unauthenticated test client).

Requirements

Files touched

Review this PR

Database practice