forked from mtusi2026/mtusi2026lab4
ЛР4: S3-вложения (SeaweedFS), приватный bucket, компенсация/reconciler, тесты, отчёт
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
-- Lab2 initial schema. Idempotent and safe to re-run via the migration runner.
|
||||
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 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) 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
|
||||
);
|
||||
|
||||
-- Composite index prepared for keyset pagination:
|
||||
-- where chat_id = $1 and (created_at, id) > ($2, $3) order by created_at, id
|
||||
create index if not exists messages_chat_created_id_idx
|
||||
on messages (chat_id, created_at, id);
|
||||
|
||||
create table if not exists sessions (
|
||||
token_hash text primary key,
|
||||
user_id uuid not null references users (id) on delete cascade,
|
||||
created_at timestamptz not null,
|
||||
expires_at timestamptz not null
|
||||
);
|
||||
|
||||
create index if not exists sessions_expires_at_idx on sessions (expires_at);
|
||||
@@ -0,0 +1,19 @@
|
||||
-- Lab3 shared idempotency ledger for POST message retries.
|
||||
--
|
||||
-- Scope is (user_id, HTTP method + chat path, key); the stored row carries the
|
||||
-- canonical request hash and the exact 201 body so any replica can replay the
|
||||
-- same Message. Rows expire after IDEMPOTENCY_TTL_SECONDS (default 24h).
|
||||
create table if not exists idempotency_keys (
|
||||
user_id uuid not null references users (id) on delete cascade,
|
||||
scope text not null,
|
||||
key text not null,
|
||||
request_hash text not null,
|
||||
status_code integer not null,
|
||||
response_body jsonb not null,
|
||||
created_at timestamptz not null default now(),
|
||||
expires_at timestamptz not null,
|
||||
primary key (user_id, scope, key)
|
||||
);
|
||||
|
||||
create index if not exists idempotency_keys_expires_at_idx
|
||||
on idempotency_keys (expires_at);
|
||||
@@ -0,0 +1,15 @@
|
||||
-- Lab3 durable cache invalidation queue.
|
||||
--
|
||||
-- Logout deletes the authoritative session row in PostgreSQL and enqueues the
|
||||
-- token hash here in the SAME transaction. A lightweight background drainer in
|
||||
-- each API replica removes the corresponding Valkey cache key and then the row.
|
||||
-- If Valkey was unreachable at logout time, the row survives and the stale
|
||||
-- cache entry is removed as soon as the cache comes back, instead of being
|
||||
-- trusted until its TTL.
|
||||
create table if not exists cache_invalidations (
|
||||
token_hash text primary key,
|
||||
created_at timestamptz not null default now()
|
||||
);
|
||||
|
||||
create index if not exists cache_invalidations_created_at_idx
|
||||
on cache_invalidations (created_at);
|
||||
@@ -0,0 +1,31 @@
|
||||
-- Lab4 attachments: metadata in PostgreSQL, bytes in S3.
|
||||
--
|
||||
-- ``s3_key`` is server-generated and never exposed through the API. Linkage to
|
||||
-- a message is a many-to-many table so a message can carry ordered attachments
|
||||
-- and the same attachment can be referenced again without duplicating bytes.
|
||||
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),
|
||||
filename text not null,
|
||||
content_type text not null,
|
||||
size_bytes bigint not null,
|
||||
sha256 text not null,
|
||||
status text not null,
|
||||
s3_key text not null,
|
||||
error_code text,
|
||||
created_at timestamptz not null,
|
||||
updated_at timestamptz not null
|
||||
);
|
||||
|
||||
create index if not exists attachments_chat_id_idx on attachments (chat_id);
|
||||
|
||||
create table if not exists message_attachments (
|
||||
message_id uuid not null references messages (id) on delete cascade,
|
||||
attachment_id uuid not null references attachments (id) on delete cascade,
|
||||
position integer not null,
|
||||
primary key (message_id, attachment_id)
|
||||
);
|
||||
|
||||
create index if not exists message_attachments_attachment_idx
|
||||
on message_attachments (attachment_id);
|
||||
Reference in New Issue
Block a user