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
- Within one workspace, no two projects that are not deleted share a slug. Creating a project whose slug a live project of the same workspace already has answers 409 `{"detail": "slug_taken"}`, also when two such requests run at the same time. The rule moves from the check in `create_project` into the database.
- Slugs are per workspace: different workspaces can use the same slug.
- Deleting a project (`DELETE /api/workspaces/{workspace_id}/projects/{slug}`, existing) sets its `deleted_at` and keeps the row, and frees its slug: creating a project in that workspace with the slug of a deleted project succeeds with 201, as it does today.
- Migration `0032` applies on production as it is today, with the data described in the context.
- Unchanged: `GET` and `DELETE` on `/projects/{slug}` see live projects only and answer 404 otherwise; a successful create answers 201 with `{"id", "name", "slug", "created_at"}`; `require_member` answers 401 or 403 unless the caller is a signed-in member of the workspace in the path; a slug that fails the `NewProject` pattern gets 422.
Files touched
- migrations/0032_projects_slug_unique.sql
- app/projects.py
- tests/test_project_slugs.py