This commit is contained in:
2026-10-30 14:32:18 +03:00
parent f39147689a
commit 057143dd91
27 changed files with 1178 additions and 58 deletions
+7 -11
View File
@@ -1,6 +1,5 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- ---------- users ----------
CREATE TABLE users (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
username VARCHAR(32) NOT NULL,
@@ -14,7 +13,6 @@ CREATE TABLE users (
CHECK (length(btrim(display_name)) > 0)
);
-- ---------- chats ----------
CREATE TABLE chats (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
title VARCHAR(100) NOT NULL,
@@ -22,14 +20,12 @@ CREATE TABLE chats (
CONSTRAINT ck_chats_title_not_blank CHECK (length(btrim(title)) > 0)
);
-- ---------- chat_members ----------
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)
);
-- ---------- messages ----------
CREATE TABLE messages (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
@@ -39,7 +35,6 @@ CREATE TABLE messages (
CONSTRAINT ck_messages_text_not_blank CHECK (length(btrim(text)) > 0)
);
-- ---------- sessions ----------
CREATE TABLE sessions (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
@@ -52,7 +47,6 @@ CREATE TABLE sessions (
CONSTRAINT ck_sessions_token_len CHECK (octet_length(token_hash) = 32)
);
-- ---------- idempotency_keys (lab3) ----------
CREATE TABLE idempotency_keys (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
@@ -84,24 +78,26 @@ CREATE TABLE attachments (
CONSTRAINT uq_attachments_object_key UNIQUE (object_key)
);
-- Keyset pagination: ORDER BY created_at ASC, id ASC (JPQL uses ASC)
CREATE TABLE shedlock (
name VARCHAR(64) NOT NULL PRIMARY KEY,
lock_until TIMESTAMP NOT NULL,
locked_at TIMESTAMP NOT NULL,
locked_by VARCHAR(255) NOT NULL
);
CREATE INDEX idx_messages_chat_created_asc_id
ON messages (chat_id, created_at ASC, id ASC);
-- "My chats" list (listChats)
CREATE INDEX idx_chat_members_user
ON chat_members (user_id);
-- For chats list pagination: ORDER BY created_at ASC, id ASC
CREATE INDEX idx_chats_created_asc_id
ON chats (created_at ASC, id ASC);
-- Active sessions per user (logout-all / audit)
CREATE INDEX idx_sessions_user_active
ON sessions (user_id)
WHERE revoked_at IS NULL;
-- Cleanup of expired sessions
CREATE INDEX idx_sessions_expires_active
ON sessions (expires_at)
WHERE revoked_at IS NULL;