Complete Lab 5 grade 4 async attachment pipeline

This commit is contained in:
2026-11-16 16:46:44 +03:00
parent 276dbdad8a
commit f9246b8844
52 changed files with 4023 additions and 50 deletions
+36
View File
@@ -0,0 +1,36 @@
CREATE TABLE users (
id UUID PRIMARY KEY,
username VARCHAR(32) NOT NULL UNIQUE
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
display_name VARCHAR(100) NOT NULL
CHECK (char_length(display_name) BETWEEN 1 AND 100 AND btrim(display_name) <> ''),
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE chats (
id UUID PRIMARY KEY,
title VARCHAR(100) NOT NULL
CHECK (char_length(title) BETWEEN 1 AND 100 AND btrim(title) <> ''),
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE chat_members (
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX chat_members_user_id_idx ON chat_members(user_id, chat_id);
CREATE TABLE messages (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL,
sender_id UUID NOT NULL,
text VARCHAR(4000) NOT NULL
CHECK (char_length(text) BETWEEN 1 AND 4000 AND btrim(text) <> ''),
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT messages_sender_is_chat_member_fk
FOREIGN KEY (chat_id, sender_id)
REFERENCES chat_members(chat_id, user_id) ON DELETE CASCADE
);
CREATE INDEX messages_history_idx ON messages(chat_id, created_at, id);
+16
View File
@@ -0,0 +1,16 @@
CREATE TABLE idempotency_records (
id UUID PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
method VARCHAR(8) NOT NULL,
path VARCHAR(200) NOT NULL,
idempotency_key VARCHAR(128) NOT NULL,
request_hash CHAR(64) NOT NULL,
response_body JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMPTZ NOT NULL,
CONSTRAINT idempotency_records_scope_key_uq
UNIQUE (user_id, method, path, idempotency_key),
CHECK (method = 'POST'),
CHECK (char_length(idempotency_key) BETWEEN 1 AND 128)
);
CREATE INDEX idempotency_records_expires_at_idx ON idempotency_records(expires_at);
+48
View File
@@ -0,0 +1,48 @@
-- Lab 4: S3 stores file bytes, while PostgreSQL owns metadata and chat relations.
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_check;
ALTER TABLE messages
ADD CONSTRAINT messages_text_length_ck CHECK (char_length(text) BETWEEN 0 AND 4000);
ALTER TABLE messages
ADD CONSTRAINT messages_id_chat_id_uq UNIQUE (id, chat_id);
CREATE TABLE attachments (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL,
owner_id UUID NOT NULL,
filename VARCHAR(255) NOT NULL,
content_type VARCHAR(32) NOT NULL,
size_bytes INTEGER NOT NULL,
sha256 CHAR(64) NOT NULL,
status VARCHAR(16) NOT NULL DEFAULT 'ready',
variants JSONB NOT NULL DEFAULT '[]'::jsonb,
error_code VARCHAR(64),
object_key TEXT NOT NULL UNIQUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT attachments_owner_is_chat_member_fk
FOREIGN KEY (chat_id, owner_id)
REFERENCES chat_members(chat_id, user_id) ON DELETE CASCADE,
CONSTRAINT attachments_id_chat_id_uq UNIQUE (id, chat_id),
CONSTRAINT attachments_filename_length_ck CHECK (char_length(filename) BETWEEN 1 AND 255),
CONSTRAINT attachments_content_type_ck CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT attachments_size_bytes_ck CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachments_sha256_ck CHECK (sha256 ~ '^[a-f0-9]{64}$'),
CONSTRAINT attachments_status_ck CHECK (status = 'ready')
);
CREATE INDEX attachments_chat_created_idx ON attachments(chat_id, created_at, id);
CREATE TABLE message_attachments (
message_id UUID NOT NULL,
attachment_id UUID NOT NULL,
chat_id UUID NOT NULL,
position SMALLINT NOT NULL CHECK (position BETWEEN 0 AND 9),
PRIMARY KEY (message_id, attachment_id),
CONSTRAINT message_attachments_message_chat_fk
FOREIGN KEY (message_id, chat_id)
REFERENCES messages(id, chat_id) ON DELETE CASCADE,
CONSTRAINT message_attachments_attachment_chat_fk
FOREIGN KEY (attachment_id, chat_id)
REFERENCES attachments(id, chat_id) ON DELETE CASCADE,
CONSTRAINT message_attachments_position_uq UNIQUE (message_id, position)
);
CREATE INDEX message_attachments_attachment_idx ON message_attachments(attachment_id);
@@ -0,0 +1,32 @@
-- Bring databases that already recorded an earlier 003_attachments layout
-- forward without dropping attachment metadata or stored object references.
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS variants JSONB NOT NULL DEFAULT '[]'::jsonb;
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS error_code VARCHAR(64);
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS width INTEGER;
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS height INTEGER;
ALTER TABLE attachments ALTER COLUMN width DROP NOT NULL;
ALTER TABLE attachments ALTER COLUMN height DROP NOT NULL;
ALTER TABLE message_attachments ADD COLUMN IF NOT EXISTS chat_id UUID;
UPDATE message_attachments AS link
SET chat_id = message.chat_id
FROM messages AS message
WHERE message.id = link.message_id
AND (link.chat_id IS NULL OR link.chat_id <> message.chat_id);
ALTER TABLE message_attachments ALTER COLUMN chat_id SET NOT NULL;
-- Drop referencing foreign keys before replacing their referenced unique keys.
ALTER TABLE message_attachments DROP CONSTRAINT IF EXISTS message_attachments_message_chat_fk;
ALTER TABLE message_attachments DROP CONSTRAINT IF EXISTS message_attachments_attachment_chat_fk;
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_id_chat_id_uq;
ALTER TABLE messages ADD CONSTRAINT messages_id_chat_id_uq UNIQUE (id, chat_id);
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_id_chat_id_uq;
ALTER TABLE attachments ADD CONSTRAINT attachments_id_chat_id_uq UNIQUE (id, chat_id);
ALTER TABLE message_attachments
ADD CONSTRAINT message_attachments_message_chat_fk
FOREIGN KEY (message_id, chat_id) REFERENCES messages(id, chat_id) ON DELETE CASCADE;
ALTER TABLE message_attachments
ADD CONSTRAINT message_attachments_attachment_chat_fk
FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments(id, chat_id) ON DELETE CASCADE;
+45
View File
@@ -0,0 +1,45 @@
-- Lab 5: durable staging, transactional outbox and leased image jobs.
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_ck;
ALTER TABLE attachments ADD CONSTRAINT attachments_status_ck CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
ALTER TABLE attachments ALTER COLUMN status SET DEFAULT 'queued';
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS staging_key TEXT;
CREATE UNIQUE INDEX IF NOT EXISTS attachments_staging_key_uq ON attachments(staging_key) WHERE staging_key IS NOT NULL;
CREATE TABLE attachment_jobs (
id UUID PRIMARY KEY,
attachment_id UUID NOT NULL UNIQUE REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version INTEGER NOT NULL DEFAULT 1 CHECK (pipeline_version >= 1),
attempt INTEGER NOT NULL DEFAULT 1 CHECK (attempt >= 1),
attempts_in_cycle INTEGER NOT NULL DEFAULT 1,
max_attempts INTEGER NOT NULL CHECK (max_attempts BETWEEN 1 AND 20),
status VARCHAR(16) NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'processing', 'succeeded', 'failed')),
available_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
lease_until TIMESTAMPTZ,
last_error_code VARCHAR(64),
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
completed_at TIMESTAMPTZ,
CONSTRAINT attachment_jobs_cycle_attempt_ck CHECK (attempts_in_cycle BETWEEN 1 AND max_attempts)
);
CREATE INDEX attachment_jobs_due_idx ON attachment_jobs(status, available_at);
CREATE INDEX attachment_jobs_lease_idx ON attachment_jobs(status, lease_until);
CREATE TABLE job_outbox (
id UUID PRIMARY KEY,
job_id UUID NOT NULL REFERENCES attachment_jobs(id) ON DELETE CASCADE,
attempt INTEGER NOT NULL CHECK (attempt >= 1),
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
published_at TIMESTAMPTZ,
CONSTRAINT job_outbox_job_attempt_uq UNIQUE (job_id, attempt)
);
CREATE INDEX job_outbox_pending_idx ON job_outbox(published_at, created_at);
CREATE TABLE job_replay_audit (
id UUID PRIMARY KEY,
job_id UUID NOT NULL REFERENCES attachment_jobs(id) ON DELETE CASCADE,
previous_attempt INTEGER NOT NULL,
new_attempt INTEGER NOT NULL,
operator VARCHAR(100) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CHECK (new_attempt > previous_attempt)
);