lab4 minimum moved to lab5

This commit is contained in:
2026-11-22 15:47:13 +03:00
parent 276dbdad8a
commit c10bf08feb
29 changed files with 2772 additions and 3 deletions
+53
View File
@@ -0,0 +1,53 @@
-- Initial schema. The runner records this file in schema_migrations and skips it on the next start.
CREATE TABLE IF NOT EXISTS schema_migrations (
version TEXT PRIMARY KEY,
applied_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE users (
id UUID PRIMARY KEY,
username TEXT NOT NULL,
display_name TEXT NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
CONSTRAINT users_username_unique UNIQUE (username),
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_display_name_len CHECK (char_length(display_name) BETWEEN 1 AND 100)
);
CREATE TABLE sessions (
token_hash TEXT PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users (id),
expires_at TIMESTAMPTZ NOT NULL
);
CREATE TABLE chats (
id UUID PRIMARY KEY,
title TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
CONSTRAINT chats_title_len CHECK (char_length(title) BETWEEN 1 AND 100)
);
CREATE TABLE chat_members (
chat_id UUID NOT NULL REFERENCES chats (id),
user_id UUID NOT NULL REFERENCES users (id),
position INTEGER NOT NULL,
PRIMARY KEY (chat_id, user_id),
CONSTRAINT chat_members_position_nonnegative CHECK (position >= 0)
);
CREATE TABLE messages (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats (id),
sender_id UUID NOT NULL REFERENCES users (id),
text TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000)
);
-- Keyset history: WHERE chat_id = ? AND (created_at, id) > cursor ORDER BY created_at, id.
CREATE INDEX messages_history_idx ON messages (chat_id, created_at, id);
-- Chat list is filtered by the caller, then ordered by the chat's own (created_at, id).
CREATE INDEX chat_members_user_idx ON chat_members (user_id);
+16
View File
@@ -0,0 +1,16 @@
-- Result of POST /chats/{id}/messages per (user, method, path, Idempotency-Key).
-- The row and the message are written in one transaction, so a failed attempt leaves no key behind.
CREATE TABLE idempotency_keys (
user_id UUID NOT NULL REFERENCES users (id),
method TEXT NOT NULL,
path TEXT NOT NULL,
key TEXT NOT NULL,
request_hash TEXT NOT NULL,
status_code INTEGER NOT NULL,
response JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (user_id, method, path, key),
CONSTRAINT idempotency_keys_key_format CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$')
);
CREATE INDEX idempotency_keys_created_idx ON idempotency_keys (created_at);
+4
View File
@@ -0,0 +1,4 @@
-- Expand-only: new indexes, no column/table removal or rename, so replicas of the previous build keep working
-- while the rolling update is in progress. Destructive changes go to a later release, after old replicas are gone.
CREATE INDEX IF NOT EXISTS sessions_user_idx ON sessions (user_id);
CREATE INDEX IF NOT EXISTS sessions_expires_idx ON sessions (expires_at);
+40
View File
@@ -0,0 +1,40 @@
-- Lab4: message may be text, attachments, or both. Empty text is stored only when
-- an attachment is linked; the API rejects a blank message that has neither.
ALTER TABLE messages DROP CONSTRAINT messages_text_len;
ALTER TABLE messages ADD CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 0 AND 4000);
-- filename is the display name only. object_key is assigned by the server and is never taken from filename.
-- The original bytes live in the private S3 bucket under that key, not in this table.
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,
content_type TEXT NOT NULL,
size_bytes INTEGER NOT NULL,
sha256 TEXT NOT NULL,
object_key TEXT NOT NULL,
-- Durable location of the accepted original. This step stores the same key as object_key.
staging_key TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'queued',
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
CONSTRAINT attachments_filename_len CHECK (char_length(filename) BETWEEN 1 AND 255),
CONSTRAINT attachments_content_type CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT attachments_size CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachments_sha256 CHECK (sha256 ~ '^[a-f0-9]{64}$'),
CONSTRAINT attachments_object_key_unique UNIQUE (object_key),
CONSTRAINT attachments_status CHECK (status IN ('queued', 'processing', 'ready', 'failed'))
);
CREATE INDEX attachments_chat_idx ON attachments (chat_id);
CREATE TABLE message_attachments (
message_id UUID NOT NULL REFERENCES messages (id),
attachment_id UUID NOT NULL REFERENCES attachments (id),
position INTEGER NOT NULL,
PRIMARY KEY (message_id, attachment_id),
CONSTRAINT message_attachments_position CHECK (position >= 0 AND position < 10)
);
CREATE INDEX message_attachments_message_idx ON message_attachments (message_id, position);
@@ -0,0 +1,4 @@
-- Databases that already applied the earlier 004 kept the original in a BYTEA column.
-- Drop it. Fresh installs create the table without that column; both statements are then no-ops.
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_body_len;
ALTER TABLE attachments DROP COLUMN IF EXISTS body;