Enforce unique project slugs in the database

Database · PostgreSQL · beginner · modification

Today `create_project` checks for a live project with the same slug and then inserts, so two requests at the same moment can both pass the check and create two projects with one slug (seen twice last month, which broke the project URL). This PR lets the database enforce it instead. Migration `0032_projects_slug_unique.sql` adds a unique constraint on `(workspace_id, slug)`. `app/projects.py` drops the pre-check, inserts directly and turns a violation of that constraint into the same 409 `slug_taken` the API already returns; any other database error still propagates. `tests/test_project_slugs.py` (new) adds API tests for slug uniqueness.

Project tracker 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. `projects` (migration `0001`, unchanged): `id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY`, `workspace_id bigint NOT NULL`, `name text NOT NULL`, `slug text NOT NULL`, `created_at timestamptz NOT NULL DEFAULT now()`, `deleted_at timestamptz` (NULL while the project is live), and an index on `workspace_id`; its only unique index so far is the primary key. Projects are deleted only by `delete_project`, which sets `deleted_at`; deleted rows stay for the activity history and are never restored (there is no restore). Slugs are lowercase ASCII, enforced by the `NewProject` pattern, and appear in project URLs. Production: about 51,000 live and 9,000 deleted projects, and new projects are created a few dozen times a day; a query run for this PR found no two live projects of a workspace with the same slug, and about 400 live projects use the slug of a project their workspace deleted earlier. `projects` is small enough that, by team convention (tables under 100,000 rows), index and constraint changes on it run as plain statements in the runner's transaction. 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. `app.auth.require_member` (existing) is a FastAPI dependency on the projects router. The test suite (`tests/conftest.py`, existing) builds a fresh, empty test database from all migrations, empties `projects` before each test and provides `client_for(workspace_id)` (a test client signed in as a member of that workspace only).

Requirements

Files touched

Review this PR

Database practice