Files
mtusi2026lab4/migrations/README.md
T

3.3 KiB

PostgreSQL schema and transactions

Tables and invariants

  • users: UUID primary key, unique constrained username, display name, Argon2 password hash, server timestamp.
  • chats: UUID primary key and title.
  • chat_members: composite (chat_id, user_id) primary key, foreign keys to both parent tables, unique stable member position per chat. The (user_id, chat_id) index supports listing a user's chats.
  • messages: UUID primary key and foreign keys to chat and sender. (chat_id, created_at, id) supports the API's ascending keyset history query.
  • sessions: SHA-256 digest of a random opaque token as primary key, user foreign key, creation and expiry timestamps. Raw tokens are returned only once and are never stored.
  • idempotency_records: unique per user, method, request path, and key; stores the request hash and committed response for 24 hours. Expired records are removed when a message is submitted.
  • attachments: PostgreSQL metadata for a chat-scoped image; object_key points to the object in the private MinIO bucket and is never exposed by the API.
  • message_attachments: ordered message-to-attachment links with composite foreign keys that prevent linking a file from another chat.
  • schema_migrations: migration filename and application timestamp; created by the migration runner.

All timestamps are TIMESTAMPTZ. PostgreSQL owns generated UUIDs and creation timestamps. Named migrations run once in lexicographic order; an advisory transaction lock serializes startup migration attempts.

Transaction boundaries

Each HTTP request uses one pooled PostgreSQL connection and one transaction. A successful response is written only after that transaction commits. Creating a chat and inserting every member is one transaction, so a failed member insert rolls back both the chat and any earlier member rows. Registration relies on the unique username constraint as the final arbiter of concurrent registrations; the losing request returns 409.

Creating a message, linking its attachments, and recording its idempotency result share one transaction. PostgreSQL's composite primary key serializes concurrent requests across API replicas. Matching bodies replay the original status and message; a different body returns 409. Authentication, chat membership, and attachment scope are checked before replay. Upload writes bytes to S3 first, then inserts metadata in PostgreSQL; a failed DB transaction triggers a best-effort object delete compensation.

The original file is authoritative in MinIO; PostgreSQL stores its chat/owner ACL, object key, checksum, type, and size. Bucket bootstrap is idempotent and creates a private bucket. The API currently uses the local MinIO root credentials, acceptable only for this grade-3 isolated lab topology; use a scoped service account for a stronger deployment.

Message history uses WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT $4, matching messages_chat_history_idx. Cursors include their resource scope and the last row's timestamp and UUID.

Durability

Compose mounts PostgreSQL and MinIO data directories on named postgres_data and minio_data volumes. The persistence hook restarts the API, database, and MinIO without removing those volumes. Backups and destructive volume resets are deliberately separate operator actions.