Выполнение ЛР5 на 5 (трек 2)

This commit is contained in:
Ilya
2026-11-11 19:09:08 +03:00
parent 276dbdad8a
commit 9de64e36b2
58 changed files with 5132 additions and 30 deletions
+49
View File
@@ -0,0 +1,49 @@
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 clock_timestamp(),
CONSTRAINT users_username_key UNIQUE (username),
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 TABLE chats (
id uuid PRIMARY KEY,
title text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT chats_title_len CHECK (char_length(title) BETWEEN 1 AND 100)
);
CREATE TABLE chat_members (
chat_id uuid NOT NULL REFERENCES chats (id),
user_id uuid NOT NULL REFERENCES users (id),
position smallint NOT NULL,
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX chat_members_user_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 DEFAULT clock_timestamp(),
CONSTRAINT messages_sender_member_fk FOREIGN KEY (chat_id, sender_id)
REFERENCES chat_members (chat_id, user_id),
CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000)
);
CREATE INDEX messages_chat_history_idx ON messages (chat_id, created_at, id);
CREATE TABLE sessions (
token_hash bytea PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users (id),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
expires_at timestamptz NOT NULL,
revoked_at timestamptz
);
CREATE INDEX sessions_expires_idx ON sessions (expires_at);
+12
View File
@@ -0,0 +1,12 @@
CREATE TABLE idempotency_keys (
user_id uuid NOT NULL REFERENCES users (id),
chat_id uuid NOT NULL REFERENCES chats (id),
key text NOT NULL,
request_hash bytea NOT NULL,
message_id uuid NOT NULL REFERENCES messages (id) DEFERRABLE INITIALLY DEFERRED,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (user_id, chat_id, key),
CONSTRAINT idempotency_key_format CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$')
);
CREATE INDEX idempotency_keys_created_idx ON idempotency_keys (created_at);
+49
View File
@@ -0,0 +1,49 @@
-- lab4: message text may be empty when the message carries attachments (checked in the API).
ALTER TABLE messages DROP CONSTRAINT messages_text_len;
ALTER TABLE messages ADD CONSTRAINT messages_text_len CHECK (char_length(text) <= 4000);
ALTER TABLE messages ADD CONSTRAINT messages_id_chat_key UNIQUE (id, chat_id);
-- Metadata only; bytes live in the private S3 bucket under object_key (assigned by the server).
-- 'pending' = intent recorded before S3 PUT, invisible to the API; the sweeper removes stale ones.
CREATE TABLE attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL,
owner_id uuid NOT NULL,
filename text NOT NULL,
content_type text NOT NULL,
size_bytes bigint NOT NULL,
sha256 text NOT NULL,
width integer NOT NULL,
height integer NOT NULL,
object_key text NOT NULL,
status text NOT NULL DEFAULT 'pending',
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT attachments_object_key_key UNIQUE (object_key),
CONSTRAINT attachments_id_chat_key UNIQUE (id, chat_id),
CONSTRAINT attachments_owner_member_fk FOREIGN KEY (chat_id, owner_id)
REFERENCES chat_members (chat_id, user_id),
CONSTRAINT attachments_status CHECK (status IN ('pending', 'ready')),
CONSTRAINT attachments_filename_len 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_pixels CHECK (width > 0 AND height > 0 AND width::bigint * height <= 40000000)
);
CREATE INDEX attachments_pending_idx ON attachments (created_at) WHERE status = 'pending';
-- chat_id is repeated so that both composite FKs guarantee: an attachment is linked only within its own chat.
CREATE TABLE message_attachments (
message_id uuid NOT NULL,
attachment_id uuid NOT NULL,
chat_id uuid NOT NULL,
position smallint NOT NULL,
PRIMARY KEY (message_id, position),
CONSTRAINT message_attachments_unique UNIQUE (message_id, attachment_id),
CONSTRAINT message_attachments_message_fk FOREIGN KEY (message_id, chat_id) REFERENCES messages (id, chat_id),
CONSTRAINT message_attachments_attachment_fk FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments (id, chat_id),
CONSTRAINT message_attachments_position CHECK (position BETWEEN 0 AND 9)
);
CREATE INDEX message_attachments_attachment_idx ON message_attachments (attachment_id);
+94
View File
@@ -0,0 +1,94 @@
-- lab5: asynchronous processing. Upload = staging object + attachment(queued) + job + outbox row in ONE transaction.
-- Lab4 'pending' rows were invisible intents of an unfinished upload; they have no lab5 meaning.
DELETE FROM attachments WHERE status = 'pending';
ALTER TABLE attachments DROP CONSTRAINT attachments_status;
ALTER TABLE attachments ALTER COLUMN status SET DEFAULT 'queued';
ALTER TABLE attachments ADD CONSTRAINT attachments_status CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
-- staging_key: private S3 prefix staging/ where the API puts the full original before 202.
-- object_key: final key of the published original (written by the worker, served only when status = ready).
-- pipeline_version: version whose variants are currently published (NULL until the first ready).
ALTER TABLE attachments ADD COLUMN staging_key text;
ALTER TABLE attachments ADD COLUMN staging_deleted_at timestamptz;
ALTER TABLE attachments ADD COLUMN error_code text;
ALTER TABLE attachments ADD COLUMN pipeline_version integer;
ALTER TABLE attachments ADD CONSTRAINT attachments_error_code CHECK (error_code IS NULL OR error_code ~ '^[a-z_]{1,64}$');
DROP INDEX IF EXISTS attachments_pending_idx;
CREATE INDEX attachments_staging_idx ON attachments (status, updated_at) WHERE staging_deleted_at IS NULL;
-- One logical unit of work = (attachment, pipeline_version). job_id never changes on redelivery or retry.
CREATE TABLE jobs (
job_id uuid PRIMARY KEY,
attachment_id uuid NOT NULL REFERENCES attachments (id),
pipeline_version integer NOT NULL CHECK (pipeline_version >= 1),
status text NOT NULL DEFAULT 'queued',
attempt integer NOT NULL DEFAULT 0, -- attempts already started (fencing token together with job_id)
max_attempts integer NOT NULL, -- raised by an explicit operator replay
next_attempt_at timestamptz NOT NULL DEFAULT clock_timestamp(),
lease_owner text,
lease_until timestamptz,
last_error text,
error_code text,
replays integer NOT NULL DEFAULT 0,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT jobs_effect_key UNIQUE (attachment_id, pipeline_version),
CONSTRAINT jobs_status CHECK (status IN ('queued', 'processing', 'done', 'failed', 'superseded')),
CONSTRAINT jobs_lease CHECK ((status = 'processing') = (lease_owner IS NOT NULL AND lease_until IS NOT NULL)),
CONSTRAINT jobs_attempts CHECK (attempt >= 0 AND max_attempts >= 1)
);
CREATE INDEX jobs_processing_lease_idx ON jobs (lease_until) WHERE status = 'processing';
CREATE INDEX jobs_queued_idx ON jobs (next_attempt_at) WHERE status = 'queued';
CREATE INDEX jobs_failed_idx ON jobs (updated_at) WHERE status = 'failed';
-- Transactional outbox: written in the same transaction as the state change, published by the relay.
CREATE TABLE outbox (
id bigserial PRIMARY KEY,
job_id uuid NOT NULL REFERENCES jobs (job_id),
payload jsonb NOT NULL, -- exactly contracts/jobs.schema.json, no bytes/URLs/credentials
available_at timestamptz NOT NULL DEFAULT clock_timestamp(), -- delayed retry (backoff + jitter)
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
published_at timestamptz,
publish_count integer NOT NULL DEFAULT 0
);
CREATE INDEX outbox_unpublished_idx ON outbox (available_at, id) WHERE published_at IS NULL;
-- Effect key (attachment_id, pipeline_version, name): one row per variant, never a second set.
CREATE TABLE attachment_variants (
attachment_id uuid NOT NULL REFERENCES attachments (id),
pipeline_version integer NOT NULL,
name text NOT NULL CHECK (name IN ('thumbnail', 'crop', 'optimized')),
object_key text NOT NULL UNIQUE,
content_type text NOT NULL CHECK (content_type IN ('image/jpeg', 'image/png', 'image/webp')),
size_bytes bigint 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}$'),
job_id uuid NOT NULL REFERENCES jobs (job_id),
attempt integer NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (attachment_id, pipeline_version, name)
);
-- Audit/observability log: claims, retries, duplicates, replays. Never contains secrets or bytes.
CREATE TABLE job_events (
id bigserial PRIMARY KEY,
job_id uuid,
attachment_id uuid,
event text NOT NULL,
attempt integer,
actor text NOT NULL,
detail text,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE INDEX job_events_attachment_idx ON job_events (attachment_id, id);
-- Fault injection for the track-5 tests; consulted only when FAULT_INJECTION=1 in the worker/relay environment.
CREATE TABLE fault_injections (
id bigserial PRIMARY KEY,
point text NOT NULL,
action text NOT NULL,
attachment_id uuid,
remaining integer NOT NULL DEFAULT 1,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);