Выполнить лабораторную 4: S3-вложения и согласованность хранилищ

This commit is contained in:
wpyatzy
2026-11-06 19:53:58 +03:00
parent ec540a1a72
commit d228560474
77 changed files with 15769 additions and 63 deletions
+41
View File
@@ -0,0 +1,41 @@
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
display_name TEXT NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS chats (
id UUID PRIMARY KEY,
title TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
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,
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 CASCADE,
text TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS sessions (
id UUID PRIMARY KEY,
token TEXT NOT NULL UNIQUE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at TIMESTAMPTZ NOT NULL,
revoked_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_messages_chat_created ON messages(chat_id, created_at, id);
CREATE INDEX IF NOT EXISTS idx_chat_members_user ON chat_members(user_id, chat_id);
CREATE INDEX IF NOT EXISTS idx_chats_created ON chats(created_at, id);
CREATE INDEX IF NOT EXISTS idx_sessions_user ON sessions(user_id, expires_at);
+10
View File
@@ -0,0 +1,10 @@
CREATE TABLE message_requests (
sender_id UUID NOT NULL REFERENCES users(id),
chat_id UUID NOT NULL REFERENCES chats(id),
key VARCHAR(128) NOT NULL,
body TEXT NOT NULL,
response JSONB NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY(sender_id, chat_id, key)
);
CREATE INDEX message_requests_expiry ON message_requests(expires_at);
+27
View File
@@ -0,0 +1,27 @@
CREATE TABLE attachments (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id),
owner_id UUID NOT NULL REFERENCES users(id),
object_key TEXT NOT NULL UNIQUE,
filename TEXT NOT NULL,
content_type TEXT NOT NULL CHECK(content_type IN ('image/png','image/jpeg')),
size_bytes BIGINT NOT NULL CHECK(size_bytes BETWEEN 1 AND 10485760),
sha256 CHAR(64) NOT NULL,
state TEXT NOT NULL CHECK(state IN ('pending','ready','failed')),
error_code TEXT,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX attachments_state_age ON attachments(state, updated_at);
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 BETWEEN 0 AND 9),
PRIMARY KEY(message_id, attachment_id),
UNIQUE(message_id, position)
);
CREATE INDEX message_attachments_attachment ON message_attachments(attachment_id);
-- Preserve replay of lab3 text requests if an existing lab3 DB is upgraded.
ALTER TABLE message_requests ALTER COLUMN body TYPE JSONB
USING jsonb_build_object('text', body, 'attachment_ids', '[]'::jsonb);
UPDATE message_requests SET response = response || '{"attachment_ids":[]}'::jsonb;