This commit is contained in:
2026-11-12 17:28:28 +03:00
parent 276dbdad8a
commit 4fae7d7e87
103 changed files with 10742 additions and 54 deletions
+7
View File
@@ -0,0 +1,7 @@
-- 000001_init_schema.down.sql
DROP TABLE IF EXISTS sessions;
DROP TABLE IF EXISTS messages;
DROP TABLE IF EXISTS chat_members;
DROP TABLE IF EXISTS chats;
DROP TABLE IF EXISTS users;
+60
View File
@@ -0,0 +1,60 @@
-- 000001_init_schema.up.sql
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username TEXT NOT NULL UNIQUE,
display_name TEXT NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT display_name_length CHECK (char_length(trim(display_name)) BETWEEN 1 AND 100)
);
CREATE TABLE chats (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT title_length CHECK (char_length(title) BETWEEN 1 AND 100)
);
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 OR REPLACE FUNCTION bytea_utf8_char_length(value BYTEA)
RETURNS INTEGER
LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE
AS $$
SELECT COUNT(*)::INTEGER
FROM generate_series(0, octet_length(value) - 1) AS positions(i)
WHERE get_byte(value, i) < 128
OR get_byte(value, i) >= 192;
$$;
CREATE TABLE messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
sender_id UUID NOT NULL REFERENCES users(id),
text BYTEA NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CONSTRAINT text_length CHECK (
bytea_utf8_char_length(text) BETWEEN 1 AND 4000
)
);
CREATE INDEX idx_messages_chat_created_id ON messages (chat_id, created_at, id);
CREATE TABLE sessions (
token TEXT 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 idx_sessions_expires_at ON sessions (expires_at);
CREATE INDEX idx_sessions_user_id ON sessions (user_id);
+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,
request_hash TEXT NOT NULL,
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE
DEFERRABLE INITIALLY DEFERRED,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, chat_id, key)
);
CREATE INDEX idx_idempotency_created_at ON idempotency_keys (created_at);
+4
View File
@@ -0,0 +1,4 @@
DROP TABLE message_attachments;
DROP TABLE attachments;
ALTER TABLE messages DROP CONSTRAINT text_length;
ALTER TABLE messages ADD CONSTRAINT text_length CHECK (char_length(text) BETWEEN 1 AND 4000);
+27
View File
@@ -0,0 +1,27 @@
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),
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 TEXT NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
object_key TEXT NOT NULL UNIQUE,
status TEXT NOT NULL DEFAULT 'ready',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_attachments_chat ON attachments (chat_id);
CREATE TABLE message_attachments (
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
attachment_id UUID NOT NULL REFERENCES attachments(id),
position SMALLINT NOT NULL,
PRIMARY KEY (message_id, attachment_id)
);
CREATE INDEX idx_message_attachments_attachment ON message_attachments (attachment_id);
ALTER TABLE messages DROP CONSTRAINT text_length;
ALTER TABLE messages ADD CONSTRAINT text_length CHECK (
bytea_utf8_char_length(text) BETWEEN 0 AND 4000
);
+3
View File
@@ -0,0 +1,3 @@
DROP TABLE job_replays, attachment_outbox, attachment_jobs;
ALTER TABLE attachments DROP CONSTRAINT attachment_status;
ALTER TABLE attachments DROP COLUMN staging_key, DROP COLUMN variants, DROP COLUMN error_code;
+34
View File
@@ -0,0 +1,34 @@
ALTER TABLE attachments ADD COLUMN staging_key TEXT;
ALTER TABLE attachments ADD COLUMN variants JSONB NOT NULL DEFAULT '[]';
ALTER TABLE attachments ADD COLUMN error_code TEXT;
ALTER TABLE attachments ADD CONSTRAINT attachment_status CHECK (status IN ('queued','processing','ready','failed'));
CREATE TABLE attachment_jobs (
id UUID PRIMARY KEY,
attachment_id UUID NOT NULL UNIQUE REFERENCES attachments(id) ON DELETE CASCADE,
pipeline_version INTEGER NOT NULL DEFAULT 1 CHECK (pipeline_version = 1),
attempt INTEGER NOT NULL DEFAULT 0,
status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued','processing','ready','failed')),
lease_token UUID,
lease_until TIMESTAMPTZ,
next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT now(),
error_code TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX job_recovery ON attachment_jobs(lease_until) WHERE status = 'processing';
CREATE TABLE attachment_outbox (
id BIGSERIAL PRIMARY KEY,
job_id UUID NOT NULL REFERENCES attachment_jobs(id) ON DELETE CASCADE,
attempt INTEGER NOT NULL,
available_at TIMESTAMPTZ NOT NULL DEFAULT now(),
published_at TIMESTAMPTZ
);
CREATE INDEX outbox_pending ON attachment_outbox(available_at, id) WHERE published_at IS NULL;
CREATE TABLE job_replays (
id BIGSERIAL PRIMARY KEY,
job_id UUID NOT NULL REFERENCES attachment_jobs(id),
reason TEXT NOT NULL CHECK (length(reason) BETWEEN 1 AND 200),
previous_attempt INTEGER NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);