Реализация на 5

This commit is contained in:
Grbisba
2026-11-17 13:08:13 +03:00
parent 470d9d6f1e
commit 60f48c0337
229 changed files with 74928 additions and 47 deletions
@@ -0,0 +1,18 @@
CREATE TABLE IF NOT EXISTS users
(
id UUID PRIMARY KEY,
name VARCHAR(100) NOT NULL,
username VARCHAR(32) NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT users_username_unique UNIQUE (username),
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_name_not_blank CHECK (name !~ '^[[:space:]]*$'),
CONSTRAINT users_password_hash_not_empty CHECK (password_hash <> '')
);
---- create above / drop below ----
DROP TABLE IF EXISTS users;
@@ -0,0 +1,19 @@
CREATE TABLE IF NOT EXISTS refresh_tokens
(
id UUID PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users (id),
token_hash TEXT NOT NULL,
user_agent TEXT,
ip_address INET,
role VARCHAR(16) NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
revoked_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_refresh_user_id ON refresh_tokens (user_id);
CREATE INDEX IF NOT EXISTS idx_refresh_expires ON refresh_tokens (expires_at);
---- create above / drop below ----
DROP TABLE IF EXISTS refresh_tokens;
@@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS chats
(
id UUID PRIMARY KEY,
title VARCHAR(100) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT chats_title_not_blank CHECK (title !~ '^[[:space:]]*$')
);
CREATE INDEX IF NOT EXISTS idx_chats_created_at_id ON chats (created_at, id);
---- create above / drop below ----
DROP TABLE IF EXISTS chats;
@@ -0,0 +1,15 @@
CREATE TABLE IF NOT EXISTS chat_members
(
chat_id UUID NOT NULL,
user_id UUID NOT NULL,
PRIMARY KEY (chat_id, user_id),
CONSTRAINT chat_members_chat_fk FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE NO ACTION,
CONSTRAINT chat_members_user_fk FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE NO ACTION
);
CREATE INDEX IF NOT EXISTS idx_chat_members_user_id_chat_id ON chat_members (user_id, chat_id);
---- create above / drop below ----
DROP TABLE IF EXISTS chat_members;
@@ -0,0 +1,18 @@
CREATE TABLE IF NOT EXISTS messages
(
id UUID PRIMARY KEY,
chat_id UUID NOT NULL,
sender_id UUID NOT NULL,
text VARCHAR(4000) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT messages_text_not_blank CHECK (text !~ '^[[:space:]]*$'),
CONSTRAINT messages_sender_member_fk FOREIGN KEY (chat_id, sender_id)
REFERENCES chat_members (chat_id, user_id) ON DELETE NO ACTION
);
CREATE INDEX IF NOT EXISTS idx_messages_chat_id_created_at_id ON messages (chat_id, created_at, id);
---- create above / drop below ----
DROP TABLE IF EXISTS messages;
@@ -0,0 +1,14 @@
CREATE TABLE message_idempotency (
user_id UUID NOT NULL REFERENCES users(id),
method TEXT NOT NULL CHECK (method = 'POST'),
chat_id UUID NOT NULL REFERENCES chats(id),
key TEXT NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
request_hash BYTEA NOT NULL,
message_id UUID NOT NULL REFERENCES messages(id),
expires_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY (user_id, method, chat_id, key)
);
CREATE INDEX message_idempotency_expiry ON message_idempotency(expires_at);
---- create above / drop below ----
DROP TABLE message_idempotency;
+44
View File
@@ -0,0 +1,44 @@
CREATE TABLE attachments
(
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats (id),
owner_id UUID NOT NULL,
filename TEXT NOT NULL,
object_key TEXT NOT NULL UNIQUE,
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}$'),
integrity_missing BOOLEAN NOT NULL DEFAULT false,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
UNIQUE (id, chat_id),
FOREIGN KEY (chat_id, owner_id) REFERENCES chat_members (chat_id, user_id)
);
CREATE INDEX attachments_scan ON attachments (id);
ALTER TABLE messages
DROP CONSTRAINT messages_text_not_blank;
ALTER TABLE messages
ADD CONSTRAINT messages_id_chat UNIQUE (id, chat_id);
CREATE TABLE message_attachments
(
message_id UUID NOT NULL,
chat_id UUID NOT NULL,
attachment_id UUID NOT NULL,
position SMALLINT NOT NULL CHECK (position BETWEEN 0 AND 9),
PRIMARY KEY (message_id, attachment_id),
UNIQUE (message_id, position),
FOREIGN KEY (message_id, chat_id) REFERENCES messages (id, chat_id),
FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments (id, chat_id)
);
ALTER TABLE message_idempotency
ADD COLUMN hash_version SMALLINT NOT NULL DEFAULT 1;
---- create above / drop below ----
ALTER TABLE message_idempotency
DROP COLUMN hash_version;
DROP TABLE message_attachments;
ALTER TABLE messages
DROP CONSTRAINT messages_id_chat;
ALTER TABLE messages
ADD CONSTRAINT messages_text_not_blank CHECK (text !~ '^[[:space:]]*$');
DROP TABLE attachments;
+41
View File
@@ -0,0 +1,41 @@
ALTER TABLE attachments
ADD COLUMN status text NOT NULL DEFAULT 'queued' CHECK(status IN ('queued','processing','ready','failed')),
ADD COLUMN error_code text,
ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1,
ADD COLUMN staging_key text NOT NULL DEFAULT '',
ADD COLUMN variants jsonb NOT NULL DEFAULT '[]',
ADD COLUMN variant_keys jsonb NOT NULL DEFAULT '{}';
UPDATE attachments SET staging_key=object_key;
CREATE TABLE attachment_jobs (
id uuid PRIMARY KEY, attachment_id uuid NOT NULL REFERENCES attachments(id),
pipeline_version integer NOT NULL, attempt integer NOT NULL CHECK(attempt>0),
attempt_limit integer NOT NULL, state text NOT NULL,
token uuid NOT NULL, lease_until timestamptz, next_attempt_at timestamptz NOT NULL,
error_code text, created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL,
UNIQUE(attachment_id,pipeline_version)
);
CREATE INDEX jobs_recovery ON attachment_jobs(state,lease_until);
CREATE TABLE outbox (
id uuid PRIMARY KEY, job_id uuid NOT NULL REFERENCES attachment_jobs(id),
payload jsonb NOT NULL, status text NOT NULL,
attempts integer NOT NULL DEFAULT 0, max_attempts integer NOT NULL,
token uuid NOT NULL, locked_until timestamptz, next_attempt_at timestamptz NOT NULL,
last_error text, created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL
);
CREATE INDEX outbox_delivery ON outbox(status,next_attempt_at);
CREATE TABLE job_audit (
id bigserial PRIMARY KEY, job_id uuid NOT NULL REFERENCES attachment_jobs(id),
action text NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
-- Existing lab4 originals become durable sources for the first asynchronous run.
INSERT INTO attachment_jobs
SELECT gen_random_uuid(),id,1,1,5,'queued',gen_random_uuid(),NULL,clock_timestamp(),NULL,created_at,clock_timestamp() FROM attachments;
INSERT INTO outbox
SELECT gen_random_uuid(),id,jsonb_build_object('schema_version',1,'job_id',id,'type','attachment.process.v1','attachment_id',attachment_id,'pipeline_version',1,'attempt',1,'created_at',created_at),
'pending',0,5,gen_random_uuid(),NULL,clock_timestamp(),NULL,clock_timestamp(),clock_timestamp() FROM attachment_jobs;
---- create above / drop below ----
DROP TABLE job_audit;
DROP TABLE outbox;
DROP TABLE attachment_jobs;
ALTER TABLE attachments DROP COLUMN status,DROP COLUMN error_code,DROP COLUMN pipeline_version,DROP COLUMN staging_key,DROP COLUMN variants,DROP COLUMN variant_keys;
@@ -0,0 +1,8 @@
CREATE TABLE attachment_variants (
attachment_id uuid NOT NULL REFERENCES attachments(id), pipeline_version integer NOT NULL,
variant text NOT NULL CHECK(variant IN ('original','thumbnail','crop','optimized')),
object_key text NOT NULL UNIQUE, sha256 text NOT NULL, size_bytes bigint NOT NULL,
PRIMARY KEY(attachment_id,pipeline_version,variant)
);
---- create above / drop below ----
DROP TABLE attachment_variants;
+10
View File
@@ -0,0 +1,10 @@
ALTER TABLE attachment_jobs ADD COLUMN traceparent text NOT NULL DEFAULT '', ADD COLUMN tracestate text NOT NULL DEFAULT '';
CREATE TABLE job_slo (
job_id uuid PRIMARY KEY REFERENCES attachment_jobs(id) DEFERRABLE INITIALLY DEFERRED,
accepted_at timestamptz NOT NULL, deadline_at timestamptz NOT NULL,
ready_at timestamptz, outcome text CHECK(outcome IN ('good','bad')), evaluated_at timestamptz
);
CREATE INDEX job_slo_pending ON job_slo(deadline_at) WHERE outcome IS NULL;
---- create above / drop below ----
DROP TABLE job_slo;
ALTER TABLE attachment_jobs DROP COLUMN traceparent, DROP COLUMN tracestate;
+3
View File
@@ -0,0 +1,3 @@
ALTER TABLE job_slo ADD COLUMN failed_at timestamptz;
---- create above / drop below ----
ALTER TABLE job_slo DROP COLUMN failed_at;
+8
View File
@@ -0,0 +1,8 @@
package migrations
import (
"embed"
)
//go:embed *.sql
var Migrations embed.FS