Реализовать лабораторную 4 на оценку 5
Добавить приватное S3-хранилище, вложения, presigned upload, reconciler и доказательства.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
-- Laboratory 2 initial schema.
|
||||
-- PostgreSQL is the source of truth for users, chats, membership and messages.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS 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_length CHECK (char_length(display_name) BETWEEN 1 AND 100)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT chats_title_length CHECK (char_length(title) BETWEEN 1 AND 100)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
joined_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
sender_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
text text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT messages_text_length CHECK (char_length(text) BETWEEN 1 AND 4000)
|
||||
);
|
||||
|
||||
-- Reverse membership lookup: "my chats, newest last".
|
||||
CREATE INDEX IF NOT EXISTS chat_members_user_chat_idx
|
||||
ON chat_members (user_id, chat_id);
|
||||
|
||||
-- Chat page ordering and keyset pagination.
|
||||
CREATE INDEX IF NOT EXISTS chats_created_at_id_idx
|
||||
ON chats (created_at, id);
|
||||
|
||||
-- The only history access path:
|
||||
-- WHERE chat_id = ? AND (created_at, id) > (?, ?)
|
||||
-- ORDER BY created_at, id LIMIT ?
|
||||
-- It supports the range scan without reading the whole chat history.
|
||||
CREATE INDEX IF NOT EXISTS messages_chat_created_at_id_idx
|
||||
ON messages (chat_id, created_at, id);
|
||||
@@ -0,0 +1,26 @@
|
||||
-- Laboratory 3: shared idempotency for message creation.
|
||||
-- Scope is (user_id, method, path, key); the stored result is the message id.
|
||||
-- The row and the message are written in one transaction by the API.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS idempotency_keys (
|
||||
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 text NOT NULL,
|
||||
message_id uuid REFERENCES messages(id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
expires_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (user_id, method, path, key),
|
||||
CONSTRAINT idempotency_key_format CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
CONSTRAINT idempotency_method_post CHECK (method = 'POST')
|
||||
);
|
||||
|
||||
-- Expired rows are removed lazily; this index keeps that cleanup bounded.
|
||||
CREATE INDEX IF NOT EXISTS idempotency_expires_at_idx
|
||||
ON idempotency_keys (expires_at);
|
||||
|
||||
-- The message id is unique per stored key, but the same message is never
|
||||
-- shared across keys; a plain lookup by primary key is enough for replay.
|
||||
CREATE INDEX IF NOT EXISTS idempotency_message_id_idx
|
||||
ON idempotency_keys (message_id);
|
||||
@@ -0,0 +1,62 @@
|
||||
-- Laboratory 4: attachment metadata in PostgreSQL, bytes in a private S3 bucket.
|
||||
--
|
||||
-- The API assigns the object key. The original filename is display metadata
|
||||
-- only and is never used to build a key. A row is inserted only after the S3
|
||||
-- PUT succeeds; the proxy upload compensates a failed INSERT by deleting the
|
||||
-- object, and the reconciler cleans up objects left by a process crash.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
owner_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
filename text NOT NULL,
|
||||
content_type text NOT NULL,
|
||||
size_bytes bigint NOT NULL,
|
||||
sha256 text NOT NULL,
|
||||
object_key text NOT NULL,
|
||||
status text NOT NULL DEFAULT 'ready',
|
||||
error_code text,
|
||||
presign_expires_at timestamptz,
|
||||
missing_since timestamptz,
|
||||
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_content_type_check
|
||||
CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
CONSTRAINT attachments_size_check CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
CONSTRAINT attachments_sha256_check CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
CONSTRAINT attachments_status_check
|
||||
CHECK (status IN ('pending', 'ready', 'failed')),
|
||||
CONSTRAINT attachments_filename_check
|
||||
CHECK (char_length(filename) BETWEEN 1 AND 255),
|
||||
CONSTRAINT attachments_error_code_check
|
||||
CHECK (error_code IS NULL OR error_code IN
|
||||
('invalid_image', 'processing_failed', 'retry_exhausted', 'missing_object'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS attachments_chat_created_idx
|
||||
ON attachments (chat_id, created_at, id);
|
||||
|
||||
-- Reconciler access paths.
|
||||
CREATE INDEX IF NOT EXISTS attachments_status_created_idx
|
||||
ON attachments (status, created_at);
|
||||
CREATE INDEX IF NOT EXISTS attachments_object_key_idx
|
||||
ON attachments (object_key);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS message_attachments (
|
||||
message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE RESTRICT,
|
||||
position integer NOT NULL,
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
CONSTRAINT message_attachments_position_key UNIQUE (message_id, position),
|
||||
CONSTRAINT message_attachments_position_check CHECK (position >= 0 AND position < 10)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS message_attachments_attachment_idx
|
||||
ON message_attachments (attachment_id);
|
||||
|
||||
-- A message may carry an empty text only when it has at least one attachment.
|
||||
-- The API enforces the semantic part; this constraint is the storage backstop.
|
||||
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_length;
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_text_length CHECK (char_length(text) BETWEEN 0 AND 4000);
|
||||
@@ -0,0 +1,10 @@
|
||||
-- The idempotency reservation is inserted before the message row inside one
|
||||
-- transaction. A deferred foreign key lets both statements commit together
|
||||
-- while still guaranteeing that a committed key always points at a message.
|
||||
ALTER TABLE idempotency_keys
|
||||
DROP CONSTRAINT IF EXISTS idempotency_keys_message_id_fkey;
|
||||
|
||||
ALTER TABLE idempotency_keys
|
||||
ADD CONSTRAINT idempotency_keys_message_id_fkey
|
||||
FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE
|
||||
DEFERRABLE INITIALLY DEFERRED;
|
||||
Reference in New Issue
Block a user