Implement durable image processing pipeline with outbox and recovery

This commit is contained in:
Polusikkkk
2026-11-17 18:48:50 +03:00
parent 276dbdad8a
commit be223efc8b
84 changed files with 8982 additions and 18 deletions
+71
View File
@@ -0,0 +1,71 @@
CREATE TABLE users (
id UUID PRIMARY KEY,
username TEXT NOT NULL,
display_name TEXT NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT users_username_unique UNIQUE (username),
CONSTRAINT users_username_format
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_display_name_length
CHECK (char_length(display_name) BETWEEN 1 AND 100)
);
CREATE TABLE chats (
id UUID PRIMARY KEY,
title TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT chats_title_length
CHECK (char_length(title) BETWEEN 1 AND 100)
);
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_chat_idx
ON chat_members (user_id, chat_id);
CREATE INDEX chats_created_id_idx
ON chats (created_at, id);
CREATE TABLE messages (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL,
sender_id UUID NOT NULL,
text TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT messages_sender_membership_fk
FOREIGN KEY (chat_id, sender_id)
REFERENCES chat_members (chat_id, user_id),
CONSTRAINT messages_text_length
CHECK (char_length(text) BETWEEN 1 AND 4000)
);
CREATE INDEX messages_chat_created_id_idx
ON messages (chat_id, created_at, id);
CREATE TABLE sessions (
token_hash TEXT PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
expires_at TIMESTAMPTZ NOT NULL,
CONSTRAINT sessions_token_hash_format
CHECK (token_hash ~ '^[0-9a-f]{64}$'),
CONSTRAINT sessions_expiry_order
CHECK (expires_at > created_at)
);
CREATE INDEX sessions_expires_at_idx
ON sessions (expires_at);
CREATE INDEX sessions_user_id_idx
ON sessions (user_id);
+22
View File
@@ -0,0 +1,22 @@
CREATE TABLE message_idempotency (
user_id UUID NOT NULL REFERENCES users(id),
chat_id UUID NOT NULL REFERENCES chats(id),
http_method TEXT NOT NULL DEFAULT 'POST',
idempotency_key TEXT NOT NULL,
request_text TEXT NOT NULL,
message_id UUID NOT NULL REFERENCES messages(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (
user_id, chat_id, http_method, idempotency_key
),
CONSTRAINT message_idempotency_method
CHECK (http_method = 'POST'),
CONSTRAINT message_idempotency_key_format
CHECK (
char_length(idempotency_key) BETWEEN 1 AND 128
AND idempotency_key ~ '^[A-Za-z0-9._:-]+$'
)
);
+67
View File
@@ -0,0 +1,67 @@
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,
status TEXT NOT NULL DEFAULT 'ready',
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT attachments_filename_length
CHECK (char_length(filename) BETWEEN 1 AND 255),
CONSTRAINT attachments_content_type
CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT attachments_size_bytes
CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachments_sha256_format
CHECK (sha256 ~ '^[a-f0-9]{64}$'),
CONSTRAINT attachments_status
CHECK (status = 'ready'),
CONSTRAINT attachments_owner_membership_fk
FOREIGN KEY (chat_id, owner_id)
REFERENCES chat_members (chat_id, user_id)
);
CREATE INDEX attachments_chat_created_id_idx
ON attachments (chat_id, created_at, id);
CREATE TABLE message_attachments (
message_id UUID NOT NULL REFERENCES messages(id),
attachment_id UUID NOT NULL REFERENCES attachments(id),
position SMALLINT NOT NULL,
PRIMARY KEY (message_id, attachment_id),
CONSTRAINT message_attachments_position_unique
UNIQUE (message_id, position),
CONSTRAINT message_attachments_position_range
CHECK (position BETWEEN 0 AND 9)
);
CREATE INDEX message_attachments_attachment_idx
ON message_attachments (attachment_id);
ALTER TABLE messages
DROP CONSTRAINT messages_text_length;
ALTER TABLE messages
ADD CONSTRAINT messages_text_length
CHECK (char_length(text) BETWEEN 0 AND 4000);
ALTER TABLE message_idempotency
ADD COLUMN request_attachment_ids UUID[] NOT NULL
DEFAULT ARRAY[]::UUID[];
ALTER TABLE message_idempotency
ADD CONSTRAINT message_idempotency_attachment_count
CHECK (cardinality(request_attachment_ids) <= 10);
+101
View File
@@ -0,0 +1,101 @@
ALTER TABLE attachments
DROP CONSTRAINT attachments_status;
ALTER TABLE attachments
ADD CONSTRAINT attachments_status
CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
ADD COLUMN staging_key TEXT,
ADD COLUMN pipeline_version INTEGER NOT NULL DEFAULT 1
CHECK (pipeline_version >= 1),
ADD COLUMN error_code TEXT;
ALTER TABLE attachments
ADD CONSTRAINT attachments_error_state
CHECK (
(status = 'failed' AND error_code IS NOT NULL)
OR
(status <> 'failed' AND error_code IS NULL)
);
CREATE UNIQUE INDEX attachments_staging_key_unique
ON attachments (staging_key)
WHERE staging_key IS NOT NULL;
CREATE TABLE attachment_variants (
attachment_id UUID NOT NULL REFERENCES attachments(id),
pipeline_version INTEGER NOT NULL CHECK (pipeline_version >= 1),
name TEXT NOT NULL CHECK (name IN ('thumbnail', 'crop', 'optimized')),
object_key TEXT NOT NULL UNIQUE,
content_type TEXT NOT NULL
CHECK (content_type IN ('image/jpeg', 'image/png', 'image/webp')),
size_bytes BIGINT NOT NULL CHECK (size_bytes > 0),
width INTEGER NOT NULL CHECK (width > 0),
height INTEGER NOT NULL CHECK (height > 0),
sha256 TEXT NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (attachment_id, pipeline_version, name)
);
CREATE TABLE attachment_jobs (
id UUID PRIMARY KEY,
attachment_id UUID NOT NULL REFERENCES attachments(id),
pipeline_version INTEGER NOT NULL CHECK (pipeline_version >= 1),
status TEXT NOT NULL DEFAULT 'queued'
CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
attempt INTEGER NOT NULL DEFAULT 1 CHECK (attempt >= 1),
max_attempts INTEGER NOT NULL CHECK (max_attempts >= 1),
available_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
lease_token UUID,
lease_until TIMESTAMPTZ,
error_code TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
UNIQUE (attachment_id, pipeline_version),
CHECK (attempt <= max_attempts),
CHECK (
(
status = 'processing'
AND lease_token IS NOT NULL
AND lease_until IS NOT NULL
)
OR
(
status <> 'processing'
AND lease_token IS NULL
AND lease_until IS NULL
)
),
CHECK (
(status = 'failed' AND error_code IS NOT NULL)
OR
(status <> 'failed' AND error_code IS NULL)
)
);
CREATE INDEX attachment_jobs_recovery_idx
ON attachment_jobs (lease_until)
WHERE status = 'processing';
CREATE INDEX attachment_jobs_failed_idx
ON attachment_jobs (updated_at, id)
WHERE status = 'failed';
CREATE TABLE attachment_outbox (
id UUID PRIMARY KEY,
job_id UUID NOT NULL REFERENCES attachment_jobs(id),
attempt INTEGER NOT NULL CHECK (attempt >= 1),
available_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
published_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
UNIQUE (job_id, attempt)
);
CREATE INDEX attachment_outbox_pending_idx
ON attachment_outbox (available_at, id)
WHERE published_at IS NULL;
+8
View File
@@ -0,0 +1,8 @@
CREATE TABLE pipeline_fault_injections (
job_id UUID NOT NULL REFERENCES attachment_jobs(id),
stage TEXT NOT NULL CHECK (stage IN ('before_ack')),
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
consumed_at TIMESTAMPTZ,
PRIMARY KEY (job_id, stage)
);
+6
View File
@@ -0,0 +1,6 @@
ALTER TABLE pipeline_fault_injections
DROP CONSTRAINT pipeline_fault_injections_stage_check;
ALTER TABLE pipeline_fault_injections
ADD CONSTRAINT pipeline_fault_injections_stage_check
CHECK (stage IN ('before_ack', 'after_objects'));
+13
View File
@@ -0,0 +1,13 @@
CREATE TABLE attachment_job_replays (
id UUID PRIMARY KEY,
job_id UUID NOT NULL REFERENCES attachment_jobs(id),
actor TEXT NOT NULL CHECK (char_length(actor) BETWEEN 1 AND 100),
reason TEXT NOT NULL CHECK (char_length(reason) BETWEEN 1 AND 500),
previous_attempt INTEGER NOT NULL CHECK (previous_attempt >= 1),
new_attempt INTEGER NOT NULL,
previous_error_code TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
CHECK (new_attempt = previous_attempt + 1),
UNIQUE (job_id, new_attempt)
);