Перенести и адаптировать обязательный минимум лабораторной 6
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
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 (length(btrim(display_name)) > 0),
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title varchar(100) NOT NULL CHECK (length(btrim(title)) > 0),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_id);
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
sender_id uuid NOT NULL REFERENCES users(id),
|
||||
text varchar(4000) NOT NULL CHECK (length(btrim(text)) > 0),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id)
|
||||
);
|
||||
CREATE INDEX messages_history_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),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX sessions_expiry_idx ON sessions (expires_at);
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE message_requests (
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
key varchar(128) NOT NULL,
|
||||
body text NOT NULL,
|
||||
message_id uuid NOT NULL REFERENCES messages(id),
|
||||
expires_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (user_id, chat_id, key)
|
||||
);
|
||||
CREATE INDEX message_requests_expiry_idx ON message_requests(expires_at);
|
||||
@@ -0,0 +1,17 @@
|
||||
ALTER TABLE messages DROP CONSTRAINT messages_text_check;␍
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_limit CHECK (length(text)<=4000);␍
|
||||
CREATE TABLE attachments (␍
|
||||
id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats(id),␍
|
||||
owner_id uuid NOT NULL REFERENCES users(id), object_key text NOT NULL UNIQUE,␍
|
||||
filename text NOT NULL, content_type text NOT NULL, size_bytes integer NOT NULL,␍
|
||||
sha256 char(64) NOT NULL, state text NOT NULL CHECK(state IN ('ready','missing')),␍
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),␍
|
||||
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),␍
|
||||
FOREIGN KEY(chat_id,owner_id) REFERENCES chat_members(chat_id,user_id)␍
|
||||
);␍
|
||||
CREATE TABLE message_attachments (␍
|
||||
message_id uuid NOT NULL REFERENCES messages(id), attachment_id uuid NOT NULL REFERENCES attachments(id),␍
|
||||
position integer NOT NULL, PRIMARY KEY(message_id,attachment_id), UNIQUE(message_id,position)␍
|
||||
);␍
|
||||
-- Старые сохранённые ключи теперь сравнивают полное нормализованное тело.␍
|
||||
UPDATE message_requests SET body=json_build_object('text',body,'attachment_ids',json_build_array())::text;␍
|
||||
@@ -0,0 +1,49 @@
|
||||
ALTER TABLE attachments DROP CONSTRAINT attachments_state_check;
|
||||
UPDATE attachments SET state = 'failed' WHERE state = 'missing';
|
||||
ALTER TABLE attachments ADD COLUMN error_code text;
|
||||
UPDATE attachments SET error_code = 'original_unavailable' WHERE state = 'failed';
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_pipeline_state CHECK (
|
||||
state IN ('queued', 'processing', 'ready', 'failed')
|
||||
AND ((state = 'failed') = (error_code IS NOT NULL))
|
||||
);
|
||||
|
||||
CREATE TABLE attachment_jobs (
|
||||
id uuid PRIMARY KEY,
|
||||
attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id),
|
||||
staging_key text NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
attempt integer NOT NULL DEFAULT 1 CHECK (attempt >= 1),
|
||||
attempt_limit integer NOT NULL CHECK (attempt_limit >= 1),
|
||||
state text NOT NULL DEFAULT 'queued' CHECK (state IN ('queued', 'processing', 'ready', 'failed')),
|
||||
next_publish_at timestamptz,
|
||||
published_at timestamptz,
|
||||
publish_failures integer NOT NULL DEFAULT 0,
|
||||
lease_until timestamptz,
|
||||
lease_token uuid,
|
||||
last_error text,
|
||||
CHECK ((lease_until IS NULL) = (lease_token IS NULL))
|
||||
);
|
||||
CREATE INDEX attachment_jobs_outbox ON attachment_jobs(next_publish_at)
|
||||
WHERE next_publish_at IS NOT NULL;
|
||||
CREATE INDEX attachment_jobs_leases ON attachment_jobs(lease_until)
|
||||
WHERE lease_until IS NOT NULL;
|
||||
|
||||
CREATE TABLE attachment_variants (
|
||||
attachment_id uuid NOT NULL REFERENCES attachments(id),
|
||||
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),
|
||||
sha256 char(64) NOT NULL,
|
||||
width integer NOT NULL CHECK (width > 0),
|
||||
height integer NOT NULL CHECK (height > 0),
|
||||
PRIMARY KEY (attachment_id, name)
|
||||
);
|
||||
|
||||
CREATE TABLE attachment_replays (
|
||||
job_id uuid NOT NULL REFERENCES attachment_jobs(id),
|
||||
attempt integer NOT NULL,
|
||||
reason text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
PRIMARY KEY (job_id, attempt)
|
||||
);
|
||||
@@ -0,0 +1,19 @@
|
||||
ALTER TABLE attachments
|
||||
ADD COLUMN target_pipeline_version integer NOT NULL DEFAULT 1 CHECK (target_pipeline_version >= 1),
|
||||
ADD COLUMN published_pipeline_version integer CHECK (published_pipeline_version >= 1),
|
||||
ADD CHECK (published_pipeline_version <= target_pipeline_version);
|
||||
UPDATE attachments SET published_pipeline_version = 1 WHERE state = 'ready';
|
||||
|
||||
ALTER TABLE attachment_jobs
|
||||
DROP CONSTRAINT attachment_jobs_attachment_id_key,
|
||||
DROP CONSTRAINT attachment_jobs_staging_key_key,
|
||||
DROP CONSTRAINT attachment_jobs_state_check,
|
||||
ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version >= 1),
|
||||
ADD COLUMN settled_at timestamptz,
|
||||
ADD CHECK (state IN ('queued', 'processing', 'ready', 'failed', 'superseded')),
|
||||
ADD UNIQUE (attachment_id, pipeline_version);
|
||||
CREATE INDEX attachment_jobs_staging ON attachment_jobs(staging_key);
|
||||
|
||||
ALTER TABLE attachment_variants ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1;
|
||||
ALTER TABLE attachment_variants DROP CONSTRAINT attachment_variants_pkey;
|
||||
ALTER TABLE attachment_variants ADD PRIMARY KEY (attachment_id, pipeline_version, name);
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE attachment_jobs ADD COLUMN trace_context jsonb NOT NULL DEFAULT '{}'::jsonb;
|
||||
@@ -0,0 +1,5 @@
|
||||
CREATE TABLE login_limits (
|
||||
identity_hash char(64) PRIMARY KEY,
|
||||
window_start timestamptz NOT NULL,
|
||||
attempts integer NOT NULL
|
||||
);
|
||||
Reference in New Issue
Block a user