Перенеси реализацию мессенджера из ЛР 6
Application / e2e (pull_request) Blocked by required conditions
Application / unit (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions

Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
This commit is contained in:
2026-11-30 10:54:41 +03:00
parent 20afc2c790
commit 8054913ef9
204 changed files with 23709 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
-- +goose Up
CREATE TABLE users (
id uuid PRIMARY KEY,
username text NOT NULL CONSTRAINT users_username_key UNIQUE,
display_name text NOT NULL,
password_hash text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE TABLE chats (
id uuid PRIMARY KEY,
title text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE TABLE chat_members (
chat_id uuid NOT NULL REFERENCES chats (id),
user_id uuid NOT NULL REFERENCES users (id),
PRIMARY KEY (chat_id, user_id)
);
-- Lists the chats of one user in (created_at, id) order: chat ids are UUIDv7.
CREATE INDEX chat_members_user_chat_idx ON chat_members (user_id, chat_id);
-- The key is (chat_id, id) and serves the history query directly:
-- WHERE chat_id = $1 AND id > $2 ORDER BY id LIMIT $3. Nothing looks a
-- message up by id alone, and a separate index on id would only cost writes
-- and tempt the planner into scanning every chat's messages in id order.
CREATE TABLE messages (
chat_id uuid NOT NULL REFERENCES chats (id),
id uuid NOT NULL,
sender_id uuid NOT NULL REFERENCES users (id),
text text NOT NULL,
created_at timestamptz NOT NULL,
PRIMARY KEY (chat_id, id)
);
-- +goose Down
DROP TABLE messages;
DROP TABLE chat_members;
DROP TABLE chats;
DROP TABLE users;
@@ -0,0 +1,17 @@
-- +goose Up
-- Keys of POST /chats/{chat_id}/messages: the contract scope user + method +
-- chat path + key is (user_id, chat_id, key), as no other endpoint takes keys.
CREATE TABLE message_idempotency_keys (
user_id uuid NOT NULL,
chat_id uuid NOT NULL,
key text NOT NULL,
message_id uuid NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, chat_id, key),
FOREIGN KEY (chat_id, message_id) REFERENCES messages (chat_id, id)
);
CREATE INDEX message_idempotency_keys_created_at_idx ON message_idempotency_keys (created_at);
-- +goose Down
DROP TABLE message_idempotency_keys;
+35
View File
@@ -0,0 +1,35 @@
-- +goose Up
CREATE TABLE files (
hash bytea PRIMARY KEY CHECK (octet_length(hash) = 32),
size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
mime_type text NOT NULL CHECK (mime_type IN ('image/png', 'image/jpeg')),
status text NOT NULL CHECK (status IN ('pending', 'ready', 'failed')),
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
owner_id uuid NOT NULL REFERENCES users(id),
filename text NOT NULL,
file_hash bytea NOT NULL REFERENCES files(hash),
created_at timestamptz NOT NULL
);
-- The reconciler must distinguish a shared file from an unused file.
CREATE INDEX attachments_file_hash_idx ON attachments(file_hash);
CREATE TABLE message_attachments (
chat_id uuid NOT NULL,
message_id uuid NOT NULL,
attachment_id uuid NOT NULL REFERENCES attachments(id),
ordinal smallint NOT NULL CHECK (ordinal BETWEEN 0 AND 9),
PRIMARY KEY(chat_id, message_id, attachment_id),
UNIQUE(chat_id, message_id, ordinal),
FOREIGN KEY(chat_id, message_id) REFERENCES messages(chat_id, id)
);
-- +goose Down
DROP TABLE message_attachments;
DROP TABLE attachments;
DROP TABLE files;
+79
View File
@@ -0,0 +1,79 @@
-- +goose Up
ALTER TABLE attachments
ADD COLUMN target_version integer NOT NULL DEFAULT 1 CHECK (target_version >= 1),
ADD COLUMN published_version integer CHECK (published_version BETWEEN 1 AND target_version);
-- A queued row with no publish timestamp is an outbox entry for this attempt.
-- attempt never decreases. The separate budget starts again only on operator replay.
CREATE TABLE attachment_jobs (
job_id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
attachment_id uuid NOT NULL REFERENCES attachments(id),
pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version >= 1),
state text NOT NULL DEFAULT 'queued' CHECK (state IN ('queued','processing','ready','failed','superseded')),
source_key text NOT NULL,
attempt integer NOT NULL DEFAULT 1 CHECK (attempt >= 1),
budget_attempt integer NOT NULL DEFAULT 1 CHECK (budget_attempt BETWEEN 1 AND attempt),
available_at timestamptz NOT NULL DEFAULT now(),
published_at timestamptz,
lease_token uuid,
lease_until timestamptz,
error_code text CHECK (error_code ~ '^[a-z][a-z0-9_]{0,63}$'),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (attachment_id, pipeline_version),
CHECK ((state = 'processing') = (lease_token IS NOT NULL)),
CHECK ((state = 'processing') = (lease_until IS NOT NULL)),
CHECK (state <> 'failed' OR error_code IS NOT NULL)
);
CREATE INDEX attachment_jobs_outbox_idx ON attachment_jobs (available_at, job_id)
WHERE state='queued' AND published_at IS NULL;
CREATE INDEX attachment_jobs_recover_idx ON attachment_jobs (lease_until, job_id) WHERE state='processing';
CREATE INDEX attachment_jobs_failed_idx ON attachment_jobs (updated_at, job_id) WHERE state='failed';
CREATE TABLE attachment_variants (
attachment_id uuid NOT NULL,
pipeline_version integer NOT NULL,
variant text NOT NULL CHECK (variant IN ('thumbnail', 'crop', 'optimized')),
object_key text NOT NULL,
mime_type text NOT NULL CHECK (mime_type = CASE WHEN variant='optimized' THEN 'image/webp' ELSE 'image/jpeg' END),
width integer NOT NULL CHECK (width > 0),
height integer NOT NULL CHECK (height > 0),
size_bytes bigint NOT NULL CHECK (size_bytes > 0),
sha256 bytea NOT NULL CHECK (octet_length(sha256)=32),
PRIMARY KEY (attachment_id,pipeline_version,variant),
FOREIGN KEY (attachment_id,pipeline_version) REFERENCES attachment_jobs(attachment_id,pipeline_version)
);
CREATE TABLE attachment_replays (
job_id uuid NOT NULL REFERENCES attachment_jobs(job_id),
attempt integer NOT NULL,
actor text NOT NULL CHECK (length(actor) BETWEEN 1 AND 128),
previous_error text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (job_id, attempt)
);
-- Published Lab 4 objects are valid inputs. A recorded missing original
-- stays diagnosable even before a worker is started.
INSERT INTO attachment_jobs(attachment_id,source_key,state,error_code)
SELECT a.id,'blobs/'||encode(f.hash,'hex'),
CASE WHEN f.status='failed' THEN 'failed' ELSE 'queued' END,
CASE WHEN f.status='failed' THEN 'source_missing' END
FROM attachments a JOIN files f ON f.hash=a.file_hash;
CREATE VIEW attachment_states AS
SELECT a.id,
CASE WHEN a.published_version IS NULL THEN j.state
WHEN f.status='failed' THEN 'failed' ELSE 'ready' END AS status,
CASE WHEN a.published_version IS NOT NULL AND f.status='failed' THEN 'source_missing'
WHEN a.published_version IS NULL AND j.state='failed' THEN j.error_code END AS error_code,
j.updated_at
FROM attachments a JOIN files f ON f.hash=a.file_hash
JOIN attachment_jobs j ON j.attachment_id=a.id AND j.pipeline_version=a.target_version;
-- +goose Down
DROP VIEW attachment_states;
DROP TABLE attachment_replays;
DROP TABLE attachment_variants;
DROP TABLE attachment_jobs;
ALTER TABLE attachments DROP COLUMN published_version, DROP COLUMN target_version;
+12
View File
@@ -0,0 +1,12 @@
-- +goose Up
ALTER TABLE attachment_jobs ADD COLUMN traceparent text, ADD COLUMN tracestate text;
-- Pre-existing uploads lack a reliable acceptance/publication clock. Exclude them from SLI cohorts.
ALTER TABLE attachments ADD COLUMN accepted_at timestamptz, ADD COLUMN first_ready_at timestamptz;
CREATE INDEX attachments_accepted_idx ON attachments (accepted_at) WHERE accepted_at IS NOT NULL;
CREATE INDEX attachment_jobs_queued_age_idx ON attachment_jobs (available_at) WHERE state='queued';
-- +goose Down
DROP INDEX attachment_jobs_queued_age_idx;
DROP INDEX attachments_accepted_idx;
ALTER TABLE attachments DROP COLUMN first_ready_at, DROP COLUMN accepted_at;
ALTER TABLE attachment_jobs DROP COLUMN tracestate, DROP COLUMN traceparent;
+8
View File
@@ -0,0 +1,8 @@
// Package migrations embeds the versioned SQL schema, so the server binary
// always carries the schema it was built against.
package migrations
import "embed"
//go:embed *.sql
var FS embed.FS