Перенесите реализацию ЛР 4 на стенд ЛР 5

This commit is contained in:
2026-11-18 15:33:31 +03:00
parent 276dbdad8a
commit 911d97a77a
153 changed files with 17451 additions and 1 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;
+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