Перенести реализацию мессенджера из lab6 в skeleton lab7.

Контракт, публичные тесты и README задания оставлены от lab7.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-26 19:23:53 +03:00
co-authored by Cursor
parent 20afc2c790
commit 54b4b9e509
61 changed files with 5879 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
-- Lab2 schema (written before application queries).
-- Source of truth: PostgreSQL for users, chats, membership, messages.
-- Transaction boundary: INSERT chat + INSERT members in one transaction (rollback together).
-- Sessions are stored in Redis with AOF, not in this database.
CREATE TABLE IF NOT EXISTS schema_migrations (
filename TEXT PRIMARY KEY,
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
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 DEFAULT now(),
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_display_name_len CHECK (char_length(display_name) BETWEEN 1 AND 100)
);
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 DEFAULT now(),
CONSTRAINT chats_title_len 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),
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX chat_members_user_id_idx ON chat_members (user_id);
CREATE INDEX chats_created_at_id_idx ON chats (created_at, id);
CREATE TABLE messages (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
sender_id UUID NOT NULL REFERENCES users (id),
text TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000)
);
-- Keyset history: WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT n
CREATE INDEX messages_chat_created_id_idx ON messages (chat_id, created_at, id);
+14
View File
@@ -0,0 +1,14 @@
-- Shared idempotency for POST /messages. Scope: user + method + path + key.
-- The message row and this result are committed in one transaction.
-- A rolled-back request leaves no row, so a later retry with the same key can insert.
CREATE TABLE IF NOT EXISTS idempotency_keys (
user_id UUID NOT NULL REFERENCES users (id),
http_method TEXT NOT NULL,
path TEXT NOT NULL,
idem_key TEXT NOT NULL,
request_hash TEXT NOT NULL,
response_json TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, http_method, path, idem_key)
);
+50
View File
@@ -0,0 +1,50 @@
-- Attachment metadata stays in Postgres. Bytes stay in the private bucket.
-- A blob_writes row is inserted before the S3 PUT and marked committed in the
-- same transaction as the attachment row. If that transaction rolls back, the
-- API deletes the object. Rows still uncommitted after ORPHAN_GRACE_SECONDS
-- are removed by the sweeper, so a crash between PUT and COMMIT cannot leave
-- a permanent untracked object. Fresh in-flight uploads are inside the grace
-- window and are not deleted.
ALTER TABLE messages DROP CONSTRAINT messages_text_len;
ALTER TABLE messages
ADD CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 0 AND 4000);
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 INTEGER NOT NULL,
sha256 TEXT NOT NULL,
object_key TEXT NOT NULL UNIQUE,
status TEXT NOT NULL,
error_code TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT attachments_filename_len CHECK (char_length(filename) BETWEEN 1 AND 255),
CONSTRAINT attachments_size CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachments_sha CHECK (sha256 ~ '^[a-f0-9]{64}$'),
CONSTRAINT attachments_type CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT attachments_status CHECK (status = 'ready'),
CONSTRAINT attachments_ready_error CHECK (status <> 'ready' OR error_code IS NULL)
);
CREATE INDEX attachments_chat_idx ON attachments (chat_id);
CREATE TABLE message_attachments (
message_id UUID NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
attachment_id UUID NOT NULL REFERENCES attachments (id),
position INTEGER NOT NULL CHECK (position >= 0 AND position < 10),
PRIMARY KEY (message_id, attachment_id),
UNIQUE (message_id, position)
);
CREATE TABLE blob_writes (
object_key TEXT PRIMARY KEY,
committed BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX blob_writes_pending_idx ON blob_writes (created_at) WHERE NOT committed;
+22
View File
@@ -0,0 +1,22 @@
-- Optional presigned upload. The signed URL dies quickly. The row stays pending
-- until complete checks the object that actually landed in the bucket.
-- Incomplete rows are aborted by the same grace sweeper as other uncommitted keys.
CREATE TABLE direct_uploads (
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 INTEGER NOT NULL,
object_key TEXT NOT NULL UNIQUE,
status TEXT NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT direct_uploads_filename_len CHECK (char_length(filename) BETWEEN 1 AND 255),
CONSTRAINT direct_uploads_size CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT direct_uploads_type CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT direct_uploads_status CHECK (status IN ('pending', 'completed', 'aborted'))
);
CREATE INDEX direct_uploads_pending_idx ON direct_uploads (expires_at) WHERE status = 'pending';
+67
View File
@@ -0,0 +1,67 @@
-- Async attachment processing. Bytes stay in S3. The broker message carries only ids.
-- 202 is returned after the staging object and this job row commit. Publish is a
-- later step: published_at stays null until the broker confirms, so a crash
-- between COMMIT and publish is recovered by the outbox loop.
ALTER TABLE attachments DROP CONSTRAINT attachments_status;
ALTER TABLE attachments
ADD CONSTRAINT attachments_status CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
ALTER TABLE attachments DROP CONSTRAINT attachments_ready_error;
ALTER TABLE attachments
ADD CONSTRAINT attachments_error CHECK (
(status = 'failed' AND error_code IS NOT NULL AND error_code ~ '^[a-z0-9_]{1,64}$')
OR (status <> 'failed' AND error_code IS NULL)
);
CREATE TABLE attachment_variants (
attachment_id UUID NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
pipeline_version INTEGER NOT NULL CHECK (pipeline_version >= 1),
name TEXT NOT NULL,
content_type TEXT NOT NULL,
size_bytes INTEGER NOT NULL CHECK (size_bytes >= 1),
width INTEGER NOT NULL CHECK (width >= 1),
height INTEGER NOT NULL CHECK (height >= 1),
sha256 TEXT NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
object_key TEXT NOT NULL UNIQUE,
PRIMARY KEY (attachment_id, pipeline_version, name),
CONSTRAINT attachment_variants_name CHECK (name IN ('thumbnail', 'crop', 'optimized'))
);
CREATE TABLE attachment_jobs (
job_id UUID PRIMARY KEY,
attachment_id UUID NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
pipeline_version INTEGER NOT NULL CHECK (pipeline_version >= 1),
attempt INTEGER NOT NULL CHECK (attempt >= 1),
status TEXT NOT NULL,
error_code TEXT,
lease_until TIMESTAMPTZ,
lease_token UUID,
next_attempt_at TIMESTAMPTZ,
published_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (attachment_id, pipeline_version),
CONSTRAINT attachment_jobs_status CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
CONSTRAINT attachment_jobs_error CHECK (
error_code IS NULL OR error_code ~ '^[a-z0-9_]{1,64}$'
),
CONSTRAINT attachment_jobs_failed_error CHECK (status <> 'failed' OR error_code IS NOT NULL)
);
CREATE INDEX attachment_jobs_outbox_idx
ON attachment_jobs (created_at)
WHERE published_at IS NULL AND status = 'queued';
CREATE INDEX attachment_jobs_lease_idx
ON attachment_jobs (lease_until)
WHERE status = 'processing';
CREATE TABLE attachment_job_events (
id BIGSERIAL PRIMARY KEY,
job_id UUID NOT NULL,
attachment_id UUID NOT NULL,
kind TEXT NOT NULL,
detail TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
+14
View File
@@ -0,0 +1,14 @@
-- The served result is one pipeline version. An older job must not replace it.
-- Variant rows stay addressable per version; the attachment points at the winner.
ALTER TABLE attachments
ADD COLUMN applied_pipeline_version INTEGER NOT NULL DEFAULT 1
CHECK (applied_pipeline_version >= 1);
CREATE TABLE pipeline_faults (
id INTEGER PRIMARY KEY,
mode TEXT NOT NULL DEFAULT '',
remaining INTEGER NOT NULL DEFAULT 0 CHECK (remaining >= 0)
);
INSERT INTO pipeline_faults (id, mode, remaining) VALUES (1, '', 0);
+3
View File
@@ -0,0 +1,3 @@
ALTER TABLE attachment_jobs
ADD COLUMN traceparent TEXT,
ADD COLUMN tracestate TEXT;