This commit is contained in:
2026-10-30 21:09:56 +03:00
parent ec540a1a72
commit 3223dd469a
87 changed files with 15475 additions and 33 deletions
+33
View File
@@ -0,0 +1,33 @@
CREATE TABLE users (
id uuid PRIMARY KEY,
username text NOT NULL UNIQUE
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
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) ON DELETE CASCADE,
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX chat_members_user_idx 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),
text text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE INDEX messages_chat_keyset_idx
ON messages (chat_id, created_at, id);
+6
View File
@@ -0,0 +1,6 @@
CREATE TABLE sessions (
token text PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at timestamptz NOT NULL
);
CREATE INDEX sessions_expires_at_idx ON sessions (expires_at);
+11
View File
@@ -0,0 +1,11 @@
CREATE TABLE idempotency_keys (
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
key text NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
request_hash text NOT NULL,
message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, chat_id, key)
);
CREATE INDEX idempotency_keys_created_at_idx ON idempotency_keys (created_at);
+22
View File
@@ -0,0 +1,22 @@
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 CASCADE,
filename text NOT NULL
CHECK (char_length(filename) BETWEEN 1 AND 255),
content_type text NOT NULL
CHECK (content_type IN ('image/png', 'image/jpeg')),
size_bytes integer NOT NULL
CHECK (size_bytes >= 1 AND size_bytes <= 10485760),
sha256 text NOT NULL
CHECK (sha256 ~ '^[a-f0-9]{64}$'),
status text NOT NULL
CHECK (status IN ('ready')),
s3_key text NOT NULL UNIQUE,
error_code text NULL,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL
);
CREATE INDEX attachments_chat_id_idx ON attachments (chat_id);
CREATE INDEX attachments_created_at_idx ON attachments (created_at);
+12
View File
@@ -0,0 +1,12 @@
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 >= 0 AND position < 10),
PRIMARY KEY (message_id, attachment_id)
);
CREATE INDEX message_attachments_attachment_idx
ON message_attachments (attachment_id);
CREATE UNIQUE INDEX message_attachments_order_idx
ON message_attachments (message_id, position);
+26
View File
@@ -0,0 +1,26 @@
-- Track 1 (grade 5): pending direct/presigned upload sessions.
-- Attachment rows stay status=ready only; incomplete uploads live here.
CREATE TABLE direct_uploads (
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 CASCADE,
filename text NOT NULL
CHECK (char_length(filename) BETWEEN 1 AND 255),
content_type text NOT NULL
CHECK (content_type IN ('image/png', 'image/jpeg')),
expected_size integer NOT NULL
CHECK (expected_size >= 1 AND expected_size <= 10485760),
expected_sha256 text NOT NULL
CHECK (expected_sha256 ~ '^[a-f0-9]{64}$'),
s3_key text NOT NULL UNIQUE,
status text NOT NULL
CHECK (status IN ('pending', 'completed', 'aborted', 'expired')),
attachment_id uuid NULL REFERENCES attachments(id),
expires_at timestamptz NOT NULL,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL
);
CREATE INDEX direct_uploads_status_expires_idx
ON direct_uploads (status, expires_at);
CREATE INDEX direct_uploads_owner_idx ON direct_uploads (owner_id);