This commit is contained in:
2026-11-19 19:45:26 +03:00
parent 276dbdad8a
commit ffa3d99557
34 changed files with 3277 additions and 41 deletions
+34
View File
@@ -0,0 +1,34 @@
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
username TEXT NOT NULL,
display_name TEXT NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS users_username_key ON users (username);
CREATE TABLE IF NOT EXISTS chats (
id UUID PRIMARY KEY,
title TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
);
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 CASCADE,
position INT NOT NULL,
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX IF NOT EXISTS chat_members_user_id_idx ON chat_members (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 CASCADE,
text TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX IF NOT EXISTS messages_chat_history_idx ON messages (chat_id, created_at ASC, id ASC);
+14
View File
@@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS message_idempotency (
user_id UUID NOT NULL REFERENCES users (id) ON DELETE CASCADE,
method TEXT NOT NULL CHECK (method = 'POST'),
resource_path TEXT NOT NULL,
idempotency_key VARCHAR(128) NOT NULL,
request_hash CHAR(64) NOT NULL,
response JSONB,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
expires_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY (user_id, method, resource_path, idempotency_key)
);
CREATE INDEX IF NOT EXISTS message_idempotency_expiry_idx
ON message_idempotency (user_id, expires_at);
+34
View File
@@ -0,0 +1,34 @@
CREATE TABLE IF NOT EXISTS 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,
object_key TEXT NOT NULL UNIQUE,
filename TEXT NOT NULL,
content_type TEXT NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')),
size_bytes BIGINT NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
sha256 CHAR(64) NOT NULL,
width INT NOT NULL CHECK (width > 0),
height INT NOT NULL CHECK (height > 0),
status TEXT NOT NULL CHECK (status IN ('processing', 'ready', 'failed')),
variants JSONB NOT NULL DEFAULT '[]'::jsonb,
error_code TEXT,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX IF NOT EXISTS attachments_chat_created_idx
ON attachments (chat_id, created_at, id);
CREATE INDEX IF NOT EXISTS attachments_processing_created_idx
ON attachments (created_at)
WHERE status = 'processing';
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 INT NOT NULL CHECK (position >= 0),
PRIMARY KEY (message_id, attachment_id),
UNIQUE (message_id, position)
);
CREATE INDEX IF NOT EXISTS message_attachments_attachment_idx
ON message_attachments (attachment_id);
+38
View File
@@ -0,0 +1,38 @@
ALTER TABLE attachments
ADD COLUMN IF NOT EXISTS staging_key TEXT,
ADD COLUMN IF NOT EXISTS pipeline_version INTEGER NOT NULL DEFAULT 1;
UPDATE attachments
SET staging_key = object_key
WHERE staging_key IS NULL;
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_check;
ALTER TABLE attachments ADD CONSTRAINT attachments_status_check
CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
CREATE TABLE IF NOT EXISTS attachment_jobs (
job_id UUID PRIMARY KEY,
attachment_id UUID NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
pipeline_version INTEGER NOT NULL,
attempts INTEGER NOT NULL DEFAULT 0,
status TEXT NOT NULL CHECK (status IN ('queued', 'processing', 'ready', 'failed')),
next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
lease_until TIMESTAMPTZ,
last_error TEXT,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
UNIQUE (attachment_id, pipeline_version)
);
CREATE INDEX IF NOT EXISTS attachment_jobs_ready_idx
ON attachment_jobs (status, next_attempt_at);
CREATE TABLE IF NOT EXISTS attachment_outbox (
id BIGSERIAL PRIMARY KEY,
job_id UUID NOT NULL REFERENCES attachment_jobs (job_id) ON DELETE CASCADE,
published_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS attachment_outbox_pending_idx
ON attachment_outbox (published_at, id);
+8
View File
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS attachment_replay_audit (
id BIGSERIAL PRIMARY KEY,
attachment_id UUID NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
old_pipeline_version INTEGER NOT NULL,
new_pipeline_version INTEGER NOT NULL,
reason TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
);