feat: implement lab4 persistent attachments and replicated API

This commit is contained in:
Fedoroff
2026-11-11 21:45:09 +03:00
parent ec540a1a72
commit 827d9c577d
44 changed files with 3507 additions and 31 deletions
+5
View File
@@ -0,0 +1,5 @@
DROP TABLE IF EXISTS sessions;
DROP TABLE IF EXISTS messages;
DROP TABLE IF EXISTS chat_members;
DROP TABLE IF EXISTS chats;
DROP TABLE IF EXISTS users;
+73
View File
@@ -0,0 +1,73 @@
-- 001_init.up.sql
-- Первоначальная схема PostgreSQL для лабораторной №2.
CREATE TABLE users (
id UUID PRIMARY KEY,
username VARCHAR(32) NOT NULL UNIQUE,
display_name VARCHAR(100) NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT users_username_format
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_display_name_not_blank
CHECK (length(btrim(display_name)) > 0)
);
CREATE TABLE chats (
id UUID PRIMARY KEY,
title VARCHAR(100) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT chats_title_not_blank
CHECK (length(btrim(title)) > 0)
);
CREATE TABLE chat_members (
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX idx_chat_members_user_id
ON chat_members (user_id, chat_id);
CREATE INDEX idx_chats_created_at_id
ON chats (created_at, id);
CREATE TABLE messages (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL,
sender_id UUID NOT NULL,
text VARCHAR(4000) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT messages_sender_is_member
FOREIGN KEY (chat_id, sender_id)
REFERENCES chat_members (chat_id, user_id)
ON DELETE RESTRICT,
CONSTRAINT messages_text_not_blank
CHECK (length(btrim(text)) > 0)
);
CREATE INDEX idx_messages_chat_created_id
ON messages (chat_id, created_at, id);
CREATE TABLE sessions (
token_hash CHAR(64) PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
expires_at TIMESTAMPTZ NOT NULL,
CONSTRAINT sessions_expiry_valid
CHECK (expires_at > created_at)
);
CREATE INDEX idx_sessions_user_id
ON sessions (user_id);
CREATE INDEX idx_sessions_expires_at
ON sessions (expires_at);
+1
View File
@@ -0,0 +1 @@
DROP TABLE IF EXISTS message_idempotency;
+9
View File
@@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS message_idempotency (
sender_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
idem_key VARCHAR(128) NOT NULL,
text TEXT NOT NULL,
message_id UUID NOT NULL UNIQUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY (sender_id, chat_id, idem_key)
);
+3
View File
@@ -0,0 +1,3 @@
ALTER TABLE messages ADD CONSTRAINT messages_text_not_blank CHECK (length(btrim(text)) > 0);
DROP TABLE message_attachments;
DROP TABLE attachments;
+19
View File
@@ -0,0 +1,19 @@
CREATE TABLE attachments (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
owner_id UUID NOT NULL REFERENCES users(id),
filename TEXT NOT NULL,
content_type TEXT NOT NULL,
size_bytes BIGINT NOT NULL,
sha256 CHAR(64) NOT NULL,
object_key TEXT NOT NULL UNIQUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX attachments_chat_idx ON attachments(chat_id);
CREATE TABLE message_attachments (
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
attachment_id UUID NOT NULL REFERENCES attachments(id),
PRIMARY KEY(message_id,attachment_id)
);
ALTER TABLE messages DROP CONSTRAINT messages_text_not_blank;
+8
View File
@@ -0,0 +1,8 @@
package migrations
import "embed"
// Files contains SQL migrations embedded into the API binary.
//
//go:embed *.up.sql
var Files embed.FS