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
- `POST /api/accounts` with `{"email", "name", "password"}` creates a customer account and answers 201 with `{"id", "email", "name"}`. The email is trimmed of surrounding whitespace and must match the `NewAccount` pattern (plain ASCII addresses, up to 254 characters); the name is 1 to 100 characters after trimming, with no control characters; the password is 12 to 128 characters. Anything else answers 422.
- One account per email address, regardless of letter case: `[email protected]` and `[email protected]` are the same address. Signing up with an address that already has an account, in any letter case, answers 409 `{"detail": "email_taken"}` and creates nothing, also when two such requests run at the same time.
- The address is stored and returned the way the customer typed it (after trimming), because receipts and emails greet them with it.
- The password is stored only as the output of `hash_password`.
Files touched
- migrations/0002_accounts.sql
- app/accounts.py
- app/main.py
- tests/test_accounts.py