forked from mtusi2026/mtusi2026lab4
ЛР4: S3-вложения, приватный bucket и согласованность хранилищ (трек 2)
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
-- Lab2 schema. PostgreSQL is the source of truth for users, chats, chat
|
||||
-- membership and messages. Sessions intentionally live in Valkey with a TTL.
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL,
|
||||
display_name text NOT NULL,
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
CONSTRAINT users_username_format
|
||||
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT users_display_name_length
|
||||
CHECK (char_length(display_name) BETWEEN 1 AND 100),
|
||||
CONSTRAINT users_password_hash_present
|
||||
CHECK (length(password_hash) > 0)
|
||||
);
|
||||
|
||||
-- Uniqueness is enforced by the database, not by a read-then-insert check, so
|
||||
-- two concurrent registrations of the same username cannot both succeed.
|
||||
CREATE UNIQUE INDEX users_username_key ON users (username);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
CONSTRAINT chats_title_length
|
||||
CHECK (char_length(title) BETWEEN 1 AND 100)
|
||||
);
|
||||
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users (id),
|
||||
position integer NOT NULL,
|
||||
PRIMARY KEY (chat_id, user_id),
|
||||
CONSTRAINT chat_members_position_nonnegative CHECK (position >= 0)
|
||||
);
|
||||
|
||||
-- "Chats of the current user" walks chat_members by user_id.
|
||||
CREATE INDEX chat_members_user_chat_idx ON chat_members (user_id, chat_id);
|
||||
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL,
|
||||
sender_id uuid NOT NULL,
|
||||
text text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
CONSTRAINT messages_text_length
|
||||
CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
CONSTRAINT messages_chat_fk
|
||||
FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE,
|
||||
-- The sender must be a member of the chat at the database level too, not
|
||||
-- only in the HTTP handler.
|
||||
CONSTRAINT messages_sender_membership_fk
|
||||
FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members (chat_id, user_id)
|
||||
);
|
||||
@@ -0,0 +1,15 @@
|
||||
-- Indexes justified by the two keyset queries of the contract.
|
||||
--
|
||||
-- Message history:
|
||||
-- WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
|
||||
-- ORDER BY created_at, id LIMIT $4
|
||||
-- The three-column index lets PostgreSQL start at the cursor position inside
|
||||
-- one chat instead of scanning the whole history for every page.
|
||||
CREATE INDEX messages_chat_created_id_idx
|
||||
ON messages (chat_id, created_at, id);
|
||||
|
||||
-- Chat list:
|
||||
-- ORDER BY created_at, id LIMIT $1
|
||||
-- after joining chat_members for the current user.
|
||||
CREATE INDEX chats_created_id_idx
|
||||
ON chats (created_at, id);
|
||||
@@ -0,0 +1,28 @@
|
||||
-- Lab3: shared idempotency records for POST /api/v1/chats/{chat_id}/messages.
|
||||
--
|
||||
-- The primary key is exactly the contract scope: acting user + HTTP method +
|
||||
-- chat message path + Idempotency-Key. Different users or chats may reuse the
|
||||
-- same key independently. request_hash lets a replay distinguish the same
|
||||
-- semantic body (201 + original message) from a different body (409).
|
||||
--
|
||||
-- A row is written in the same transaction as the message it references, so a
|
||||
-- committed row always has a committed message and an aborted request leaves
|
||||
-- neither. created_at supports the 24-hour retention prune; contract requires
|
||||
-- at least 24 h, this stand prunes only records older than that bound.
|
||||
CREATE TABLE message_idempotency (
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
method text NOT NULL,
|
||||
path text NOT NULL,
|
||||
key text NOT NULL,
|
||||
request_hash bytea NOT NULL,
|
||||
message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (user_id, method, path, key),
|
||||
CONSTRAINT message_idempotency_method_post CHECK (method = 'POST'),
|
||||
CONSTRAINT message_idempotency_key_length CHECK (char_length(key) BETWEEN 1 AND 128),
|
||||
CONSTRAINT message_idempotency_path_length CHECK (char_length(path) BETWEEN 1 AND 256)
|
||||
);
|
||||
|
||||
-- Retention housekeeping walks old rows; new inserts stay cheap.
|
||||
CREATE INDEX message_idempotency_created_at_idx
|
||||
ON message_idempotency (created_at);
|
||||
@@ -0,0 +1,75 @@
|
||||
-- Lab4: attachment metadata lives in PostgreSQL; the original bytes live in
|
||||
-- the private S3 bucket. The only public state in the mandatory lab4 contract
|
||||
-- is ready. pending exists solely as an internal, server-side upload intent:
|
||||
-- it is written before the S3 PUT and promoted in the same request after the
|
||||
-- PUT. A crash or a database failure between the two steps therefore leaves an
|
||||
-- accounted row, never an anonymous object. The reconciler removes stale
|
||||
-- pending rows and their objects after a documented grace period.
|
||||
CREATE TABLE attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
owner_id uuid NOT NULL REFERENCES users (id),
|
||||
filename text NOT NULL,
|
||||
content_type text NOT NULL,
|
||||
size_bytes bigint NOT NULL,
|
||||
sha256 text NOT NULL,
|
||||
status text NOT NULL,
|
||||
error_code text,
|
||||
object_key text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
CONSTRAINT attachments_filename_length
|
||||
CHECK (char_length(filename) BETWEEN 1 AND 255),
|
||||
CONSTRAINT attachments_content_type
|
||||
CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
CONSTRAINT attachments_size
|
||||
CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
CONSTRAINT attachments_sha256
|
||||
CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
CONSTRAINT attachments_status
|
||||
CHECK (status IN ('pending', 'ready')),
|
||||
CONSTRAINT attachments_ready_error
|
||||
CHECK ((status = 'ready' AND error_code IS NULL) OR status = 'pending'),
|
||||
CONSTRAINT attachments_object_key
|
||||
CHECK (object_key LIKE 'attachments/%'),
|
||||
CONSTRAINT attachments_object_key_unique UNIQUE (object_key),
|
||||
-- Referenced by the composite foreign key of message_attachments so the
|
||||
-- database itself rejects a link to an attachment of another chat.
|
||||
CONSTRAINT attachments_id_chat_unique UNIQUE (id, chat_id)
|
||||
);
|
||||
|
||||
-- The only attachment query on the hot path is "attachment of this chat".
|
||||
CREATE INDEX attachments_chat_created_idx
|
||||
ON attachments (chat_id, created_at, id);
|
||||
|
||||
-- The reconciler scans all rows by state and age. It never runs on the HTTP
|
||||
-- hot path.
|
||||
CREATE INDEX attachments_status_updated_idx
|
||||
ON attachments (status, updated_at);
|
||||
|
||||
-- messages.id is already the primary key; this unique constraint is the
|
||||
-- target required by the composite foreign keys below, not a second identity.
|
||||
-- It must exist before the foreign key that references (id, chat_id).
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_id_chat_unique UNIQUE (id, chat_id);
|
||||
|
||||
-- A message and its attachment links share one transaction. The composite
|
||||
-- foreign keys make "file of another chat" impossible at the database level
|
||||
-- even if a future handler forgets the application check.
|
||||
CREATE TABLE message_attachments (
|
||||
message_id uuid NOT NULL,
|
||||
attachment_id uuid NOT NULL,
|
||||
chat_id uuid NOT NULL,
|
||||
position integer NOT NULL,
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
CONSTRAINT message_attachments_position_unique UNIQUE (message_id, position),
|
||||
CONSTRAINT message_attachments_position_nonnegative CHECK (position >= 0),
|
||||
CONSTRAINT message_attachments_message_chat_fk
|
||||
FOREIGN KEY (message_id, chat_id) REFERENCES messages (id, chat_id) ON DELETE CASCADE,
|
||||
CONSTRAINT message_attachments_attachment_chat_fk
|
||||
FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments (id, chat_id)
|
||||
);
|
||||
|
||||
-- Reconciler lookup: "is this object key still referenced by any row?".
|
||||
CREATE INDEX message_attachments_attachment_idx
|
||||
ON message_attachments (attachment_id);
|
||||
@@ -0,0 +1,10 @@
|
||||
-- Lab4 contract: a message may carry text = "" when it references at least one
|
||||
-- attachment of the chat (contracts/README.md, "Вложения"). The HTTP handler
|
||||
-- still rejects empty or blank text without attachments; the database
|
||||
-- constraint only widens to permit the documented attachment-only message.
|
||||
ALTER TABLE messages
|
||||
DROP CONSTRAINT messages_text_length;
|
||||
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_text_length
|
||||
CHECK (char_length(text) BETWEEN 0 AND 4000);
|
||||
Reference in New Issue
Block a user