This commit is contained in:
Матвей
2026-11-07 13:15:01 +03:00
parent 20afc2c790
commit eafb825657
93 changed files with 7549 additions and 1 deletions
+37
View File
@@ -0,0 +1,37 @@
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(btrim(display_name)) > 0),
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE chats (
id uuid PRIMARY KEY,
title varchar(100) NOT NULL CHECK (char_length(btrim(title)) > 0),
created_at timestamptz NOT NULL DEFAULT now()
);
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_chat_idx ON chat_members (user_id, chat_id);
CREATE TABLE 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 CHECK (char_length(btrim(text)) > 0),
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX messages_chat_created_id_idx ON messages (chat_id, created_at, id);
CREATE TABLE sessions (
token_hash char(64) PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at timestamptz NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX sessions_expiry_idx ON sessions (expires_at);
+1
View File
@@ -0,0 +1 @@
ALTER TABLE chats ADD COLUMN history_version bigint NOT NULL DEFAULT 0;
+12
View File
@@ -0,0 +1,12 @@
CREATE TABLE idempotency_records (
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
method varchar(8) NOT NULL,
path text NOT NULL,
idempotency_key varchar(128) NOT NULL,
request_hash char(64) NOT NULL,
message_id uuid REFERENCES messages(id) ON DELETE RESTRICT,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, method, path, idempotency_key)
);
CREATE INDEX idempotency_records_created_at_idx ON idempotency_records (created_at);
+18
View File
@@ -0,0 +1,18 @@
CREATE TABLE attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
owner_id uuid NOT NULL REFERENCES users(id),
filename varchar(255) NOT NULL,
content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')),
size_bytes integer NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
sha256 char(64) NOT NULL,
object_key text NOT NULL UNIQUE,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (id, chat_id)
);
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
ALTER TABLE messages ADD COLUMN attachment_ids uuid[] NOT NULL DEFAULT '{}';
ALTER TABLE messages ADD CONSTRAINT messages_content_check
CHECK (char_length(btrim(text)) > 0 OR cardinality(attachment_ids) > 0);
CREATE INDEX attachments_chat_idx ON attachments(chat_id);
+34
View File
@@ -0,0 +1,34 @@
ALTER TABLE attachments
RENAME COLUMN object_key TO staging_key;
ALTER TABLE attachments
ADD COLUMN original_key text,
ADD COLUMN status text NOT NULL DEFAULT 'queued'
CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
ADD COLUMN error_code text,
ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1,
ADD COLUMN processing_started_at timestamptz;
CREATE TABLE attachment_variants (
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version integer NOT NULL,
name text NOT NULL CHECK (name IN ('thumbnail', 'crop', 'optimized')),
object_key text NOT NULL UNIQUE,
content_type text NOT NULL,
size_bytes integer NOT NULL CHECK (size_bytes > 0),
width integer NOT NULL CHECK (width > 0),
height integer NOT NULL CHECK (height > 0),
sha256 char(64) NOT NULL,
PRIMARY KEY (attachment_id, pipeline_version, name)
);
CREATE TABLE attachment_jobs (
job_id uuid PRIMARY KEY,
attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version integer NOT NULL DEFAULT 1,
attempt integer NOT NULL DEFAULT 1 CHECK (attempt >= 1),
status text NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
last_error text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX attachment_jobs_status_idx ON attachment_jobs(status, updated_at);
+17
View File
@@ -0,0 +1,17 @@
ALTER TABLE attachment_jobs
ADD COLUMN next_attempt_at timestamptz NOT NULL DEFAULT now(),
ADD COLUMN lease_until timestamptz,
ADD COLUMN replayed_at timestamptz;
CREATE INDEX attachment_jobs_due_idx ON attachment_jobs(status, next_attempt_at);
CREATE INDEX attachment_jobs_lease_idx ON attachment_jobs(status, lease_until);
CREATE TABLE attachment_outbox (
id uuid PRIMARY KEY,
job_id uuid NOT NULL REFERENCES attachment_jobs(job_id) ON DELETE CASCADE,
payload jsonb NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
published_at timestamptz,
attempts integer NOT NULL DEFAULT 0,
UNIQUE (job_id, published_at)
);
CREATE INDEX attachment_outbox_pending_idx ON attachment_outbox(created_at) WHERE published_at IS NULL;
+1
View File
@@ -0,0 +1 @@
ALTER TABLE attachment_jobs ADD COLUMN first_started_at timestamptz;
+2
View File
@@ -0,0 +1,2 @@
-- Context is technical metadata, never user content. Retained across retry/outbox recreation.
ALTER TABLE attachment_jobs ADD COLUMN trace_context jsonb NOT NULL DEFAULT '{}'::jsonb;
+11
View File
@@ -0,0 +1,11 @@
-- A separate immutable cohort: replay and job cleanup must not rewrite SLO history.
-- Existing jobs have no recoverable acceptance commit and are deliberately not backfilled.
CREATE TABLE attachment_slo (
attachment_id uuid PRIMARY KEY,
accepted_xid xid NOT NULL DEFAULT pg_current_xact_id()::xid,
ready_xid xid,
accepted_at timestamptz,
ready_at timestamptz,
bad boolean
);
CREATE INDEX attachment_slo_unresolved_idx ON attachment_slo(accepted_at) WHERE bad IS NULL;