feat:report

This commit is contained in:
Оля Мороз
2026-11-16 20:30:12 +03:00
parent 90f407b174
commit c91ec7790f
37 changed files with 1159 additions and 209 deletions
+58
View File
@@ -0,0 +1,58 @@
-- ============================================================
-- Core: users, sessions, chats, members, messages, idempotency
-- ============================================================
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
display_name TEXT NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS sessions (
token TEXT PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_sessions_user ON sessions (user_id);
CREATE INDEX IF NOT EXISTS idx_sessions_expires ON sessions (expires_at);
CREATE TABLE IF NOT EXISTS chats (
id UUID PRIMARY KEY,
title TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS 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 TABLE IF NOT EXISTS 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 DEFAULT now()
);
-- Индекс истории: ровно под ORDER BY (created_at, id) и keyset-курсор.
CREATE INDEX IF NOT EXISTS idx_messages_chat_created_id
ON messages (chat_id, created_at, id);
CREATE TABLE IF NOT EXISTS 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,
status_code INTEGER NOT NULL,
response JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, method, path, key)
);
CREATE INDEX IF NOT EXISTS idx_idempotency_created
ON idempotency_keys (created_at);
-31
View File
@@ -1,31 +0,0 @@
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
display_name TEXT NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS chats (
id UUID PRIMARY KEY,
title TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS 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 TABLE IF NOT EXISTS 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 DEFAULT now()
);
-- Индекс истории: ровно под ORDER BY (created_at, id) и keyset-курсор.
CREATE INDEX IF NOT EXISTS idx_messages_chat_created_id
ON messages (chat_id, created_at, id);
+88
View File
@@ -0,0 +1,88 @@
-- ============================================================
-- Attachments: metadata, variants, jobs/outbox, GC
-- ============================================================
CREATE TABLE IF NOT EXISTS 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,
object_key TEXT NOT NULL UNIQUE,
filename TEXT NOT NULL,
content_type TEXT NOT NULL,
size_bytes BIGINT NOT NULL CHECK (size_bytes >= 0 AND size_bytes <= 10485760),
sha256 TEXT NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
width INTEGER NOT NULL CHECK (width >= 0),
height INTEGER NOT NULL CHECK (height >= 0),
status TEXT NOT NULL DEFAULT 'queued'
CHECK (status IN ('queued','processing','ready','failed','pending')),
error_code TEXT,
-- поля из 0006
pipeline_version INTEGER NOT NULL DEFAULT 1,
staging_key TEXT,
ready_at TIMESTAMPTZ,
failed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_attachments_chat ON attachments (chat_id, created_at, id);
CREATE INDEX IF NOT EXISTS idx_attachments_owner ON attachments (owner_id);
-- Для reconciler/поиска «зависших» pending-записей:
CREATE INDEX IF NOT EXISTS idx_attachments_pending
ON attachments (created_at) WHERE status <> 'ready';
-- Связь сообщение ↔ вложения. FK на элементы массива Postgres не умеет,
-- целостность — на уровне приложения (или триггера, если нужно).
ALTER TABLE messages ADD COLUMN IF NOT EXISTS attachment_ids UUID[] NOT NULL DEFAULT '{}';
-- Журнал компенсации: объекты S3, которые могли остаться без метаданных.
CREATE TABLE IF NOT EXISTS attachment_gc (
object_key TEXT PRIMARY KEY,
reason TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- Idempotency эффекта: (attachment_id, pipeline_version, variant)
-- content_type без CHECK: варианты могут быть webp и т.п.
-- sha256 — text+CHECK, чтобы не ловить padding от char(64).
CREATE TABLE IF NOT EXISTS attachment_variants (
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version INTEGER NOT NULL,
variant TEXT NOT NULL,
object_key TEXT NOT NULL,
content_type TEXT NOT NULL,
size_bytes BIGINT NOT NULL,
width INTEGER NOT NULL,
height INTEGER NOT NULL,
sha256 TEXT NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (attachment_id, pipeline_version, variant)
);
-- Outbox + очередь job-ов
CREATE TABLE IF NOT EXISTS attachment_jobs (
job_id UUID PRIMARY KEY,
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version INTEGER NOT NULL,
attempt INTEGER NOT NULL DEFAULT 1,
status TEXT NOT NULL DEFAULT 'queued'
CHECK (status IN ('queued','processing','done','failed')),
-- outbox
published_at TIMESTAMPTZ,
publish_attempts INTEGER NOT NULL DEFAULT 0,
-- lease
lease_owner TEXT,
lease_expires_at TIMESTAMPTZ,
-- retry
next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT now(),
last_error_code TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (attachment_id, pipeline_version) -- одна логическая работа на версию
);
CREATE INDEX IF NOT EXISTS idx_attachment_jobs_unpublished
ON attachment_jobs (next_attempt_at)
WHERE published_at IS NULL AND status = 'queued';
CREATE INDEX IF NOT EXISTS idx_attachment_jobs_lease
ON attachment_jobs (lease_expires_at)
WHERE status = 'processing';
-9
View File
@@ -1,9 +0,0 @@
CREATE TABLE IF NOT EXISTS sessions (
token TEXT PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_sessions_user ON sessions (user_id);
CREATE INDEX IF NOT EXISTS idx_sessions_expires ON sessions (expires_at);
@@ -1,5 +1,5 @@
-- Индекс B для «стоимости записи»:
-- покрывающий индекс, включающий text — растёт быстрее, помогает только
-- покрывающим запросам.
-- покрывающим запросам. Накатывать отдельно, чтобы сравнить с индексом A.
CREATE INDEX IF NOT EXISTS idx_messages_chat_covering
ON messages (chat_id, created_at, id) INCLUDE (sender_id, text);
-14
View File
@@ -1,14 +0,0 @@
CREATE TABLE IF NOT EXISTS 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,
status_code INTEGER NOT NULL,
response JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, method, path, key)
);
CREATE INDEX IF NOT EXISTS idx_idempotency_created
ON idempotency_keys (created_at);
-30
View File
@@ -1,30 +0,0 @@
CREATE TABLE IF NOT EXISTS 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,
object_key TEXT NOT NULL UNIQUE,
filename TEXT NOT NULL,
content_type TEXT NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
size_bytes BIGINT NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
sha256 TEXT NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
width INTEGER NOT NULL CHECK (width > 0),
height INTEGER NOT NULL CHECK (height > 0),
status TEXT NOT NULL DEFAULT 'ready' CHECK (status IN ('pending','ready','failed')),
error_code TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_attachments_chat ON attachments (chat_id, created_at, id);
CREATE INDEX IF NOT EXISTS idx_attachments_owner ON attachments (owner_id);
-- Для reconciler/поиска «зависших» pending-записей:
CREATE INDEX IF NOT EXISTS idx_attachments_pending ON attachments (created_at) WHERE status <> 'ready';
ALTER TABLE messages ADD COLUMN IF NOT EXISTS attachment_ids UUID[] NOT NULL DEFAULT '{}';
-- Журнал компенсации: объекты S3, которые могли остаться без метаданных.
CREATE TABLE IF NOT EXISTS attachment_gc (
object_key TEXT PRIMARY KEY,
reason TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-49
View File
@@ -1,49 +0,0 @@
-- staging → final prefix; job-очередь и lease
ALTER TABLE attachments
ADD COLUMN IF NOT EXISTS pipeline_version integer NOT NULL DEFAULT 1,
ADD COLUMN IF NOT EXISTS staging_key text,
ADD COLUMN IF NOT EXISTS ready_at timestamptz,
ADD COLUMN IF NOT EXISTS failed_at timestamptz,
ADD COLUMN IF NOT EXISTS error_code text;
-- idempotency эффекта: (attachment_id, pipeline_version, variant)
CREATE TABLE IF NOT EXISTS attachment_variants (
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version integer NOT NULL,
variant text NOT NULL,
object_key text NOT NULL,
content_type text NOT NULL,
size_bytes bigint NOT NULL,
width int NOT NULL,
height int NOT NULL,
sha256 char(64) NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (attachment_id, pipeline_version, variant)
);
-- outbox + очередь job-ов
CREATE TABLE IF NOT EXISTS attachment_jobs (
job_id uuid PRIMARY KEY,
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version integer NOT NULL,
attempt int NOT NULL DEFAULT 1,
status text NOT NULL DEFAULT 'queued', -- queued|processing|done|failed
-- outbox
published_at timestamptz,
publish_attempts int NOT NULL DEFAULT 0,
-- lease
lease_owner text,
lease_expires_at timestamptz,
-- retry
next_attempt_at timestamptz NOT NULL DEFAULT now(),
last_error_code text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (attachment_id, pipeline_version) -- одна логическая работа на версию
);
CREATE INDEX IF NOT EXISTS idx_attachment_jobs_unpublished
ON attachment_jobs (next_attempt_at)
WHERE published_at IS NULL AND status = 'queued';
CREATE INDEX IF NOT EXISTS idx_attachment_jobs_lease
ON attachment_jobs (lease_expires_at)
WHERE status = 'processing';