This commit is contained in:
2026-11-16 18:47:37 +03:00
parent 470d9d6f1e
commit ccb463aea1
109 changed files with 64105 additions and 32 deletions
+33
View File
@@ -0,0 +1,33 @@
CREATE TABLE users (
id uuid PRIMARY KEY,
username text NOT NULL UNIQUE
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
display_name text NOT NULL,
password_hash text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE TABLE chats (
id uuid PRIMARY KEY,
title text NOT NULL,
created_at timestamptz NOT NULL
);
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 CASCADE,
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX chat_members_user_idx ON chat_members(user_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),
text text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE INDEX messages_chat_keyset_idx
ON messages (chat_id, created_at, id);
+6
View File
@@ -0,0 +1,6 @@
CREATE TABLE sessions (
token text PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at timestamptz NOT NULL
);
CREATE INDEX sessions_expires_at_idx ON sessions (expires_at);
+11
View File
@@ -0,0 +1,11 @@
CREATE TABLE idempotency_keys (
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
key text NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
request_hash text NOT NULL,
message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, chat_id, key)
);
CREATE INDEX idempotency_keys_created_at_idx ON idempotency_keys (created_at);
+22
View File
@@ -0,0 +1,22 @@
CREATE TABLE attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
owner_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
filename text NOT NULL
CHECK (char_length(filename) BETWEEN 1 AND 255),
content_type text NOT NULL
CHECK (content_type IN ('image/png', 'image/jpeg')),
size_bytes integer NOT NULL
CHECK (size_bytes >= 1 AND size_bytes <= 10485760),
sha256 text NOT NULL
CHECK (sha256 ~ '^[a-f0-9]{64}$'),
status text NOT NULL
CHECK (status IN ('ready')),
s3_key text NOT NULL UNIQUE,
error_code text NULL,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL
);
CREATE INDEX attachments_chat_id_idx ON attachments (chat_id);
CREATE INDEX attachments_created_at_idx ON attachments (created_at);
+12
View File
@@ -0,0 +1,12 @@
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 >= 0 AND position < 10),
PRIMARY KEY (message_id, attachment_id)
);
CREATE INDEX message_attachments_attachment_idx
ON message_attachments (attachment_id);
CREATE UNIQUE INDEX message_attachments_order_idx
ON message_attachments (message_id, position);
+26
View File
@@ -0,0 +1,26 @@
-- Track 1 (grade 5): pending direct/presigned upload sessions.
-- Attachment rows stay status=ready only; incomplete uploads live here.
CREATE TABLE direct_uploads (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
owner_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
filename text NOT NULL
CHECK (char_length(filename) BETWEEN 1 AND 255),
content_type text NOT NULL
CHECK (content_type IN ('image/png', 'image/jpeg')),
expected_size integer NOT NULL
CHECK (expected_size >= 1 AND expected_size <= 10485760),
expected_sha256 text NOT NULL
CHECK (expected_sha256 ~ '^[a-f0-9]{64}$'),
s3_key text NOT NULL UNIQUE,
status text NOT NULL
CHECK (status IN ('pending', 'completed', 'aborted', 'expired')),
attachment_id uuid NULL REFERENCES attachments(id),
expires_at timestamptz NOT NULL,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL
);
CREATE INDEX direct_uploads_status_expires_idx
ON direct_uploads (status, expires_at);
CREATE INDEX direct_uploads_owner_idx ON direct_uploads (owner_id);
+12
View File
@@ -0,0 +1,12 @@
-- Track 1: presigned PUT lands in staging/; complete copies to attachments/.
-- Existing rows keep the old key as staging so in-flight sessions still resolve.
ALTER TABLE direct_uploads ADD COLUMN staging_key text;
UPDATE direct_uploads
SET staging_key = s3_key
WHERE staging_key IS NULL;
ALTER TABLE direct_uploads ALTER COLUMN staging_key SET NOT NULL;
ALTER TABLE direct_uploads
ADD CONSTRAINT direct_uploads_staging_key_key UNIQUE (staging_key);
+21
View File
@@ -0,0 +1,21 @@
-- Keep existing lab4 originals ready; new jobs set queued explicitly.
ALTER TABLE attachments DROP CONSTRAINT attachments_status_check;
ALTER TABLE attachments
ADD CONSTRAINT attachments_status_check
CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
ADD COLUMN variants jsonb NOT NULL DEFAULT '[]'::jsonb
CHECK (jsonb_typeof(variants) = 'array');
CREATE TABLE jobs (
id uuid PRIMARY KEY,
attachment_id uuid NOT NULL UNIQUE
REFERENCES attachments(id) ON DELETE CASCADE,
status text NOT NULL DEFAULT 'queued'
CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
retries integer NOT NULL DEFAULT 0 CHECK (retries >= 0),
error_code text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX jobs_status_created_at_idx ON jobs (status, created_at);
+20
View File
@@ -0,0 +1,20 @@
ALTER TABLE jobs ADD COLUMN next_attempt_at timestamptz NOT NULL DEFAULT now();
CREATE TABLE outbox (
id bigserial PRIMARY KEY,
payload jsonb NOT NULL CHECK (jsonb_typeof(payload) = 'object'),
published boolean NOT NULL DEFAULT false,
created_at timestamptz NOT NULL DEFAULT now(),
available_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX outbox_pending_idx ON outbox (available_at, id) WHERE NOT published;
-- Upgrade jobs accepted before outbox existed; duplicates are safe.
INSERT INTO outbox (payload, available_at)
SELECT jsonb_build_object(
'schema_version', 1, 'type', 'attachment.process.v1',
'job_id', id::text, 'attachment_id', attachment_id::text,
'pipeline_version', 1, 'attempt', retries + 1,
'created_at', to_char(created_at AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
), next_attempt_at FROM jobs WHERE status IN ('queued', 'processing');
+22
View File
@@ -0,0 +1,22 @@
ALTER TABLE attachments
ADD COLUMN staging_key text,
ADD COLUMN staging_cleaned_at timestamptz;
UPDATE attachments a SET staging_key='staging/' || a.chat_id::text || '/' || a.id::text
WHERE EXISTS (SELECT 1 FROM jobs j WHERE j.attachment_id=a.id);
CREATE INDEX attachments_staging_cleanup_idx ON attachments(status, updated_at)
WHERE staging_key IS NOT NULL AND staging_cleaned_at IS NULL;
ALTER TABLE jobs ADD COLUMN retry_base integer NOT NULL DEFAULT 0
CHECK (retry_base >= 0 AND retry_base <= retries);
CREATE TABLE job_replays (
id bigserial PRIMARY KEY,
job_id uuid NOT NULL REFERENCES 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_error_code text,
retries_before integer NOT NULL CHECK (retries_before >= 0),
created_at timestamptz NOT NULL DEFAULT now()
);
+41
View File
@@ -0,0 +1,41 @@
ALTER TABLE attachments
ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version > 0),
ADD COLUMN published_version integer CHECK (published_version > 0),
ADD CONSTRAINT attachments_version_order CHECK (published_version <= pipeline_version);
UPDATE attachments SET published_version=1 WHERE status='ready';
ALTER TABLE jobs
ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version > 0),
ADD COLUMN source_key text;
UPDATE jobs j SET source_key=a.s3_key FROM attachments a WHERE a.id=j.attachment_id;
ALTER TABLE jobs ALTER COLUMN source_key SET NOT NULL;
ALTER TABLE jobs DROP CONSTRAINT jobs_attachment_id_key;
ALTER TABLE jobs ADD CONSTRAINT jobs_attachment_version_key UNIQUE (attachment_id, pipeline_version);
ALTER TABLE jobs DROP CONSTRAINT jobs_status_check;
ALTER TABLE jobs ADD CONSTRAINT jobs_status_check
CHECK (status IN ('queued','processing','ready','failed','superseded'));
-- One immutable logical result per (attachment, pipeline version).
CREATE TABLE attachment_results (
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version integer NOT NULL CHECK (pipeline_version > 0),
job_id uuid UNIQUE REFERENCES jobs(id),
original_key text NOT NULL,
variants jsonb NOT NULL CHECK (jsonb_typeof(variants)='array'),
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (attachment_id, pipeline_version)
);
INSERT INTO attachment_results(attachment_id,pipeline_version,job_id,original_key,variants)
SELECT a.id,1,j.id,a.s3_key,a.variants FROM attachments a
LEFT JOIN jobs j ON j.attachment_id=a.id AND j.pipeline_version=1 WHERE a.status='ready';
CREATE TABLE pipeline_requests (
id bigserial PRIMARY KEY,
attachment_id uuid NOT NULL REFERENCES attachments(id),
pipeline_version integer NOT NULL CHECK (pipeline_version > 0),
job_id uuid NOT NULL UNIQUE REFERENCES 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),
created_at timestamptz NOT NULL DEFAULT now(),
UNIQUE(attachment_id,pipeline_version)
);