feat: port lab4 implementation

This commit is contained in:
kk10
2026-11-13 15:52:39 +03:00
parent 276dbdad8a
commit 36d9b0c8f6
101 changed files with 8426 additions and 6 deletions
+51
View File
@@ -0,0 +1,51 @@
-- +goose Up
CREATE TABLE users (
id uuid PRIMARY KEY,
username text NOT NULL UNIQUE
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
display_name text NOT NULL
CONSTRAINT users_display_name_not_blank CHECK (display_name ~ '\S'),
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE sessions (
token_hash text 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
);
CREATE INDEX idx_sessions_expires_at ON sessions (expires_at);
CREATE TABLE chats (
id uuid PRIMARY KEY,
title text NOT NULL
CONSTRAINT chats_title_not_blank CHECK (title ~ '\S'),
created_at timestamptz NOT NULL DEFAULT now()
);
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 CASCADE,
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX idx_chat_members_user_id ON chat_members (user_id);
CREATE TABLE 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 now()
);
CREATE INDEX idx_messages_chat_history ON messages (chat_id, created_at, id);
-- +goose Down
DROP TABLE messages;
DROP TABLE chat_members;
DROP TABLE chats;
DROP TABLE sessions;
DROP TABLE users;
@@ -0,0 +1,17 @@
-- +goose Up
CREATE TABLE 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 NOT NULL REFERENCES messages (id) ON DELETE CASCADE
DEFERRABLE INITIALLY DEFERRED,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, method, path, key)
);
CREATE INDEX idx_idempotency_keys_created_at ON idempotency_keys (created_at);
-- +goose Down
DROP TABLE idempotency_keys;
+38
View File
@@ -0,0 +1,38 @@
-- +goose Up
CREATE TYPE attachment_content_type AS ENUM ('image/png', 'image/jpeg');
CREATE TYPE attachment_status AS ENUM ('queued', 'processing', 'ready', 'failed');
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) ON DELETE RESTRICT,
s3_key text NOT NULL UNIQUE,
filename text NOT NULL,
content_type attachment_content_type NOT NULL,
size_bytes int NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
status attachment_status NOT NULL DEFAULT 'queued',
error_code text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX idx_attachments_chat_id ON attachments (chat_id, created_at);
CREATE INDEX idx_attachments_owner_id ON attachments (owner_id);
CREATE INDEX idx_attachments_queued_created_at ON attachments (created_at) WHERE status = 'queued';
CREATE TABLE 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 int NOT NULL CHECK (position >= 0 AND position < 10),
PRIMARY KEY (message_id, attachment_id),
UNIQUE (message_id, position)
);
CREATE INDEX idx_message_attachments_attachment_id ON message_attachments (attachment_id);
-- +goose Down
DROP TABLE message_attachments;
DROP TABLE attachments;
DROP TYPE attachment_status;
DROP TYPE attachment_content_type;