Выполнить лабораторную 5: асинхронный pipeline и исследование идемпотентности

This commit is contained in:
wpyatzy
2026-11-20 18:47:23 +03:00
parent 276dbdad8a
commit ec277e7e64
72 changed files with 6521 additions and 27 deletions
+41
View File
@@ -0,0 +1,41 @@
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) ON DELETE CASCADE,
text TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS sessions (
id UUID PRIMARY KEY,
token TEXT NOT NULL UNIQUE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at TIMESTAMPTZ NOT NULL,
revoked_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_messages_chat_created ON messages(chat_id, created_at, id);
CREATE INDEX IF NOT EXISTS idx_chat_members_user ON chat_members(user_id, chat_id);
CREATE INDEX IF NOT EXISTS idx_chats_created ON chats(created_at, id);
CREATE INDEX IF NOT EXISTS idx_sessions_user ON sessions(user_id, expires_at);
+10
View File
@@ -0,0 +1,10 @@
CREATE TABLE message_requests (
sender_id UUID NOT NULL REFERENCES users(id),
chat_id UUID NOT NULL REFERENCES chats(id),
key VARCHAR(128) NOT NULL,
body TEXT NOT NULL,
response JSONB NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY(sender_id, chat_id, key)
);
CREATE INDEX message_requests_expiry ON message_requests(expires_at);
+27
View File
@@ -0,0 +1,27 @@
CREATE TABLE attachments (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id),
owner_id UUID NOT NULL REFERENCES users(id),
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 BETWEEN 1 AND 10485760),
sha256 CHAR(64) NOT NULL,
state TEXT NOT NULL CHECK(state IN ('pending','ready','failed')),
error_code TEXT,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX attachments_state_age ON attachments(state, updated_at);
CREATE TABLE message_attachments (
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
attachment_id UUID NOT NULL REFERENCES attachments(id),
position INTEGER NOT NULL CHECK(position BETWEEN 0 AND 9),
PRIMARY KEY(message_id, attachment_id),
UNIQUE(message_id, position)
);
CREATE INDEX message_attachments_attachment ON message_attachments(attachment_id);
-- Preserve replay of lab3 text requests if an existing lab3 DB is upgraded.
ALTER TABLE message_requests ALTER COLUMN body TYPE JSONB
USING jsonb_build_object('text', body, 'attachment_ids', '[]'::jsonb);
UPDATE message_requests SET response = response || '{"attachment_ids":[]}'::jsonb;
+36
View File
@@ -0,0 +1,36 @@
ALTER TABLE attachments DROP CONSTRAINT attachments_state_check;
ALTER TABLE attachments ADD CONSTRAINT attachments_state_check CHECK(state IN ('pending','queued','processing','ready','failed'));
ALTER TABLE attachments ADD COLUMN pipeline_version INTEGER NOT NULL DEFAULT 1 CHECK(pipeline_version>0);
ALTER TABLE attachments ADD COLUMN staging_key TEXT;
ALTER TABLE attachments ADD COLUMN variants JSONB NOT NULL DEFAULT '[]';
CREATE TABLE jobs (
id UUID PRIMARY KEY,
attachment_id UUID NOT NULL REFERENCES attachments(id),
pipeline_version INTEGER NOT NULL CHECK(pipeline_version>0),
state TEXT NOT NULL CHECK(state IN ('queued','processing','ready','failed','superseded')),
attempt INTEGER NOT NULL DEFAULT 0,
lease_until TIMESTAMPTZ,
next_at TIMESTAMPTZ NOT NULL DEFAULT now(),
error_code TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE(attachment_id,pipeline_version)
);
CREATE TABLE outbox (
job_id UUID PRIMARY KEY REFERENCES jobs(id),
published_at TIMESTAMPTZ,
deliveries INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE job_audit (
id BIGSERIAL PRIMARY KEY,
job_id UUID REFERENCES jobs(id),
event TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX jobs_due ON jobs(state,next_at);
-- Upgrade existing lab4 ready originals through the asynchronous pipeline.
UPDATE attachments SET staging_key=object_key,state='queued',updated_at=now() WHERE state IN ('ready','pending');
INSERT INTO jobs(id,attachment_id,pipeline_version,state)
SELECT gen_random_uuid(),id,pipeline_version,'queued' FROM attachments WHERE state='queued';
INSERT INTO outbox(job_id) SELECT id FROM jobs;