Перенести реализацию лабораторной 5 в лабораторную 6
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id UUID PRIMARY KEY,
|
||||
username VARCHAR(32) NOT NULL UNIQUE,
|
||||
display_name VARCHAR(100) NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT users_username_format_check
|
||||
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
|
||||
CONSTRAINT users_display_name_nonblank_check
|
||||
CHECK (char_length(btrim(display_name)) >= 1)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
id UUID PRIMARY KEY,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT chats_title_nonblank_check
|
||||
CHECK (char_length(btrim(title)) >= 1)
|
||||
);
|
||||
|
||||
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 RESTRICT,
|
||||
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 RESTRICT,
|
||||
text VARCHAR(4000) NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT messages_text_nonblank_check
|
||||
CHECK (char_length(btrim(text)) >= 1)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
id UUID PRIMARY KEY,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
token_hash CHAR(64) NOT NULL UNIQUE,
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
revoked_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
@@ -0,0 +1,12 @@
|
||||
CREATE INDEX IF NOT EXISTS messages_keyset_idx
|
||||
ON messages (chat_id, created_at ASC, id ASC);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS chats_keyset_idx
|
||||
ON chats (created_at ASC, id ASC);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS chat_members_user_chat_idx
|
||||
ON chat_members (user_id, chat_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS sessions_active_token_idx
|
||||
ON sessions (token_hash, expires_at)
|
||||
WHERE revoked_at IS NULL;
|
||||
@@ -0,0 +1,18 @@
|
||||
-- Lab3: shared idempotency of POST /chats/{chat_id}/messages.
|
||||
-- Scope: user + method + chat path + key. The key row and the message are
|
||||
-- written in one transaction, so either both exist or neither does.
|
||||
-- Expand-only change: the running lab2/lab3 API keeps working with it.
|
||||
CREATE TABLE IF NOT EXISTS idempotency_keys (
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
method VARCHAR(8) NOT NULL,
|
||||
key VARCHAR(128) NOT NULL,
|
||||
request_hash CHAR(64) NOT NULL,
|
||||
message_id UUID REFERENCES messages(id) ON DELETE CASCADE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (user_id, chat_id, method, key)
|
||||
);
|
||||
|
||||
-- For a periodic cleanup of keys older than IDEMPOTENCY_TTL_SECONDS.
|
||||
CREATE INDEX IF NOT EXISTS idempotency_keys_created_idx
|
||||
ON idempotency_keys (created_at);
|
||||
@@ -0,0 +1,68 @@
|
||||
-- Lab4: attachments. PostgreSQL keeps metadata, the bytes live in a private
|
||||
-- S3 bucket under a server-generated object_key. Expand-only migration: the
|
||||
-- lab3 API keeps working on this schema.
|
||||
|
||||
-- Text may be empty when the message has attachments; "not blank without
|
||||
-- attachments" is checked by the API (one message + its links are inserted
|
||||
-- in one transaction).
|
||||
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_nonblank_check;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS attachments (
|
||||
id UUID PRIMARY KEY,
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE RESTRICT,
|
||||
owner_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
-- Display name only; never used as a path or an S3 key.
|
||||
filename VARCHAR(255) NOT NULL,
|
||||
content_type VARCHAR(32) NOT NULL,
|
||||
size_bytes BIGINT NOT NULL,
|
||||
sha256 CHAR(64) NOT NULL,
|
||||
width INTEGER NOT NULL,
|
||||
height INTEGER NOT NULL,
|
||||
-- Lab4 has only "ready": the row is inserted after the object is stored.
|
||||
status VARCHAR(16) NOT NULL DEFAULT 'ready',
|
||||
error_code VARCHAR(64),
|
||||
object_key TEXT NOT NULL UNIQUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT attachments_content_type_check
|
||||
CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
CONSTRAINT attachments_size_check
|
||||
CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
CONSTRAINT attachments_sha256_check
|
||||
CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
CONSTRAINT attachments_status_check
|
||||
CHECK (status IN ('ready'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS attachments_chat_idx ON attachments (chat_id);
|
||||
|
||||
-- A message references 0-10 attachments of its own chat; "position" keeps
|
||||
-- the order of attachment_ids. One attachment may be referenced by several
|
||||
-- messages of the chat.
|
||||
CREATE TABLE IF NOT EXISTS message_attachments (
|
||||
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE RESTRICT,
|
||||
position SMALLINT NOT NULL,
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
CONSTRAINT message_attachments_position_check CHECK (position BETWEEN 0 AND 9),
|
||||
CONSTRAINT message_attachments_position_unique UNIQUE (message_id, position)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS message_attachments_attachment_idx
|
||||
ON message_attachments (attachment_id);
|
||||
|
||||
-- Upload intent: committed BEFORE the S3 PUT and deleted in the same
|
||||
-- transaction that inserts the attachment row. An intent older than the
|
||||
-- grace period therefore means "the object may exist in S3, but the request
|
||||
-- never committed" (crash, lost DB, client gone) -> the janitor deletes the
|
||||
-- object and then the intent.
|
||||
CREATE TABLE IF NOT EXISTS upload_intents (
|
||||
object_key TEXT PRIMARY KEY,
|
||||
attachment_id UUID NOT NULL,
|
||||
chat_id UUID NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS upload_intents_created_idx
|
||||
ON upload_intents (created_at);
|
||||
@@ -0,0 +1,49 @@
|
||||
-- Lab5: the database owns the work intent; RabbitMQ carries only its IDs.
|
||||
ALTER TABLE attachments DROP CONSTRAINT attachments_status_check;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_status_check
|
||||
CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
|
||||
ALTER TABLE attachments
|
||||
ADD COLUMN staging_key TEXT,
|
||||
ADD COLUMN pipeline_version INTEGER NOT NULL DEFAULT 1 CHECK (pipeline_version = 1),
|
||||
ADD COLUMN job_id UUID UNIQUE,
|
||||
ADD COLUMN attempt INTEGER NOT NULL DEFAULT 1 CHECK (attempt >= 1),
|
||||
ADD COLUMN attempts_run INTEGER NOT NULL DEFAULT 0,
|
||||
ADD COLUMN lease_token UUID,
|
||||
ADD COLUMN lease_until TIMESTAMPTZ,
|
||||
ADD COLUMN next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
ADD COLUMN staging_deleted BOOLEAN NOT NULL DEFAULT false,
|
||||
ADD COLUMN variants JSONB NOT NULL DEFAULT '[]'::jsonb;
|
||||
|
||||
CREATE TABLE outbox (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
job_id UUID NOT NULL,
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id),
|
||||
attempt INTEGER NOT NULL DEFAULT 1,
|
||||
available_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
published_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX outbox_pending_idx ON outbox(available_at, id) WHERE published_at IS NULL;
|
||||
CREATE INDEX attachments_lease_idx ON attachments(lease_until) WHERE status = 'processing';
|
||||
CREATE INDEX attachments_staging_idx ON attachments(updated_at)
|
||||
WHERE staging_key IS NOT NULL AND NOT staging_deleted;
|
||||
|
||||
-- Failed registry = attachments WHERE status='failed'. This append-only
|
||||
-- journal explains retry, lease recovery and explicit operator replay.
|
||||
CREATE TABLE job_events (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id),
|
||||
event TEXT NOT NULL,
|
||||
attempt INTEGER NOT NULL,
|
||||
detail TEXT NOT NULL DEFAULT '',
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX job_events_attachment_idx ON job_events(attachment_id, id);
|
||||
|
||||
-- Internal one-shot crash injection, never exposed through public HTTP.
|
||||
CREATE TABLE fault_injections (
|
||||
point TEXT PRIMARY KEY,
|
||||
remaining INTEGER NOT NULL DEFAULT 0 CHECK (remaining >= 0),
|
||||
hits INTEGER NOT NULL DEFAULT 0,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
Reference in New Issue
Block a user