forked from mtusi2026/mtusi2026lab3
лабораторная работа 3
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);
|
||||
Reference in New Issue
Block a user