Перенёс приложение из lab4, добавил очередь заданий, outbox и worker

This commit is contained in:
gera
2026-11-06 01:59:07 +03:00
parent 276dbdad8a
commit dfa8784afa
19 changed files with 3371 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
create table 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 chats (
id uuid primary key,
title text not null,
created_at timestamptz not null
);
create table chat_members (
chat_id uuid not null references chats(id),
user_id uuid not null references users(id),
primary key (chat_id, user_id)
);
create index chat_members_user_idx on chat_members (user_id);
create table 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 messages_history_idx on messages (chat_id, created_at, id);
create table sessions (
token_hash bytea primary key,
user_id uuid not null references users(id) on delete cascade,
created_at timestamptz not null,
expires_at timestamptz not null
);
+10
View File
@@ -0,0 +1,10 @@
create table 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,
body_hash text not null,
response jsonb not null,
created_at timestamptz not null,
primary key (user_id, method, path, key)
);
+22
View File
@@ -0,0 +1,22 @@
create table 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 bigint not null,
sha256 text not null,
object_key text not null unique,
created_at timestamptz not null
);
create index attachments_chat_idx on attachments (chat_id);
create table message_attachments (
message_id uuid not null references messages(id),
attachment_id uuid not null references attachments(id),
position int not null,
primary key (message_id, attachment_id)
);
create index message_attachments_attachment_idx on message_attachments (attachment_id);
+60
View File
@@ -0,0 +1,60 @@
alter table attachments
add column status text not null default 'ready',
add column error_code text,
add column staging_key text,
add column updated_at timestamptz;
update attachments set updated_at = created_at;
alter table attachments
alter column status drop default,
alter column updated_at set not null,
alter column object_key drop not null;
create table attachment_variants (
attachment_id uuid not null references attachments(id),
pipeline_version int not null,
name text not null,
object_key text not null unique,
content_type text not null,
size_bytes bigint not null,
width int not null,
height int not null,
sha256 text not null,
primary key (attachment_id, pipeline_version, name)
);
create table jobs (
id uuid primary key,
attachment_id uuid not null unique references attachments(id),
pipeline_version int not null,
status text not null,
attempt int not null,
max_attempts int not null,
next_attempt_at timestamptz not null,
lease_owner text,
lease_until timestamptz,
last_error text,
created_at timestamptz not null,
updated_at timestamptz not null
);
create index jobs_lease_idx on jobs (lease_until) where status = 'processing';
create table outbox (
id bigserial primary key,
job_id uuid not null references jobs(id),
attempt int not null,
available_at timestamptz not null,
published_at timestamptz,
created_at timestamptz not null
);
create index outbox_pending_idx on outbox (available_at) where published_at is null;
create table job_replays (
id bigserial primary key,
job_id uuid not null references jobs(id),
previous_error text,
replayed_at timestamptz not null
);