forked from mtusi2026/mtusi2026lab7
Закрывает обязательный минимум и трек 1: ротация leaf и секрета, sealed OpenBao и сохранность queued-задачи.
109 lines
3.2 KiB
SQL
109 lines
3.2 KiB
SQL
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
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS sessions (
|
|
token_hash bytea PRIMARY KEY,
|
|
user_id uuid NOT NULL REFERENCES users (id),
|
|
expires_at timestamptz NOT NULL,
|
|
revoked_at timestamptz
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS sessions_user_idx ON sessions (user_id);
|
|
|
|
CREATE TABLE IF NOT EXISTS chats (
|
|
id uuid PRIMARY KEY,
|
|
title text NOT NULL,
|
|
created_at timestamptz NOT NULL
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS 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)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS chat_members_user_idx ON chat_members (user_id, chat_id);
|
|
|
|
CREATE TABLE IF NOT EXISTS 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
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS messages_order_idx ON messages (chat_id, created_at, id);
|
|
|
|
CREATE TABLE IF NOT EXISTS 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,
|
|
status text NOT NULL CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
|
|
error_code text,
|
|
staging_key text NOT NULL,
|
|
original_key text,
|
|
pipeline_version integer NOT NULL DEFAULT 1,
|
|
attempt integer NOT NULL DEFAULT 0,
|
|
lease_until timestamptz,
|
|
worker_id text,
|
|
created_at timestamptz NOT NULL,
|
|
updated_at timestamptz NOT NULL,
|
|
ready_at timestamptz
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS attachments_open_idx ON attachments (created_at)
|
|
WHERE status IN ('queued', 'processing');
|
|
|
|
CREATE TABLE IF NOT EXISTS attachment_variants (
|
|
attachment_id uuid NOT NULL REFERENCES attachments (id),
|
|
name text NOT NULL,
|
|
content_type text NOT NULL,
|
|
size_bytes integer NOT NULL,
|
|
width integer NOT NULL,
|
|
height integer NOT NULL,
|
|
sha256 text NOT NULL,
|
|
object_key text NOT NULL,
|
|
pipeline_version integer NOT NULL,
|
|
PRIMARY KEY (attachment_id, name, pipeline_version)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS 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)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS idempotency (
|
|
user_id uuid NOT NULL,
|
|
method text NOT NULL,
|
|
path text NOT NULL,
|
|
idem_key text NOT NULL,
|
|
body_hash bytea NOT NULL,
|
|
status_code integer NOT NULL,
|
|
response_json jsonb NOT NULL,
|
|
created_at timestamptz NOT NULL,
|
|
PRIMARY KEY (user_id, method, path, idem_key)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS outbox (
|
|
job_id uuid PRIMARY KEY,
|
|
attachment_id uuid NOT NULL REFERENCES attachments (id),
|
|
payload jsonb NOT NULL,
|
|
created_at timestamptz NOT NULL,
|
|
published_at timestamptz
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS outbox_unpublished_idx ON outbox (created_at)
|
|
WHERE published_at IS NULL;
|