This commit is contained in:
Vladislav Kunetskiy
2026-10-29 23:36:12 +03:00
parent 276dbdad8a
commit 6344754111
383 changed files with 68663 additions and 194 deletions
@@ -0,0 +1,71 @@
-- +goose Up
CREATE TABLE users (
id uuid PRIMARY KEY DEFAULT uuidv7(),
username text NOT NULL,
password_hash text NOT NULL,
display_name text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT users_username_key UNIQUE (username),
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_display_name_not_blank CHECK (btrim(display_name) <> ''),
CONSTRAINT users_display_name_length CHECK (char_length(display_name) BETWEEN 1 AND 100)
);
CREATE TABLE chats (
id uuid PRIMARY KEY DEFAULT uuidv7(),
title text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT chats_title_not_blank CHECK (btrim(title) <> '')
);
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,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (chat_id, user_id)
);
CREATE TABLE messages (
id uuid PRIMARY KEY DEFAULT uuidv7(),
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
sender_id uuid NOT NULL REFERENCES users (id) ON DELETE RESTRICT,
text text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT messages_text_not_blank CHECK (btrim(text) <> ''),
CONSTRAINT messages_text_length CHECK (char_length(text) <= 4000)
);
CREATE TABLE sessions (
token_hash bytea 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 TABLE user_chat_versions (
user_id uuid PRIMARY KEY REFERENCES users (id) ON DELETE CASCADE,
version bigint NOT NULL DEFAULT 0
);
CREATE INDEX messages_chat_id_created_at_id_idx
ON messages (chat_id, created_at, id);
CREATE INDEX chat_members_user_id_chat_id_idx
ON chat_members (user_id, chat_id);
CREATE INDEX chats_created_at_id_idx
ON chats (created_at, id);
CREATE INDEX sessions_user_id_idx
ON sessions (user_id);
-- +goose Down
DROP TABLE sessions;
DROP TABLE messages;
DROP TABLE chat_members;
DROP TABLE chats;
DROP TABLE users;
DROP TABLE user_chat_versions;
@@ -0,0 +1,25 @@
-- +goose Up
-- Версия 3, а не 2: в базе, перенесённой из лабораторной 2, goose уже содержит
-- запись версии 2 (другая миграция того же репозитория), и одноимённый номер был
-- бы молча пропущен при обновлении существующего стенда до ЛР3.
CREATE TABLE message_idempotency (
actor_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 bytea NOT NULL,
message_id uuid REFERENCES messages (id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT now(),
expires_at timestamptz NOT NULL,
PRIMARY KEY (actor_id, chat_id, key),
CONSTRAINT message_idempotency_key_length CHECK (char_length(key) BETWEEN 1 AND 128)
);
-- Expired records are replaced lazily by a new claim, the index keeps that
-- lookup and a future sweep cheap.
CREATE INDEX message_idempotency_expires_at_idx ON message_idempotency (expires_at);
-- +goose Down
DROP TABLE message_idempotency;
@@ -0,0 +1,55 @@
-- +goose Up
-- Attachment metadata lives in PostgreSQL; the original bytes live only in S3.
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 RESTRICT,
object_key text NOT NULL,
filename text NOT NULL,
content_type text NOT NULL,
size_bytes bigint NOT NULL,
sha256 text NOT NULL,
status text NOT NULL,
error_code text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT attachments_object_key_key UNIQUE (object_key),
CONSTRAINT attachments_status_check CHECK (status IN ('pending', 'ready', 'failed')),
CONSTRAINT attachments_content_type_check CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT attachments_filename_length CHECK (char_length(filename) BETWEEN 1 AND 255),
CONSTRAINT attachments_size_check CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachments_sha256_check CHECK (sha256 ~ '^[0-9a-f]{64}$'),
CONSTRAINT attachments_error_consistency CHECK (
(status = 'ready' AND error_code IS NULL) OR
(status = 'failed' AND error_code IS NOT NULL) OR
status = 'pending'
)
);
CREATE INDEX attachments_chat_id_created_at_id_idx ON attachments (chat_id, created_at, id);
CREATE INDEX attachments_status_created_at_idx ON attachments (status, created_at);
CREATE TABLE 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_key UNIQUE (message_id, position)
);
CREATE INDEX message_attachments_attachment_id_idx ON message_attachments (attachment_id);
-- Lab1-3 messages must stay valid. Attachment-only messages use text = '';
-- the "text OR attachment" rule is enforced by the service and ogen.
ALTER TABLE messages DROP CONSTRAINT messages_text_not_blank;
-- +goose Down
-- WARNING: fails if attachment-only messages already exist. Roll back such
-- rows or the constraint cannot be restored.
ALTER TABLE messages ADD CONSTRAINT messages_text_not_blank CHECK (btrim(text) <> '');
DROP TABLE message_attachments;
DROP TABLE attachments;
@@ -0,0 +1,6 @@
package migrations
import "embed"
//go:embed *.sql
var FS embed.FS