forked from mtusi2026/mtusi2026lab5
ЛР 5 — БВТ2303 — Брезгунова — на 5
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id uuid PRIMARY KEY, username text UNIQUE NOT NULL, display_name text NOT NULL,
|
||||
password_hash text NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
token_hash text PRIMARY KEY, user_id uuid NOT NULL REFERENCES users(id), expires_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
id uuid PRIMARY KEY, title text NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS members (
|
||||
chat_id uuid REFERENCES chats(id), user_id uuid REFERENCES users(id), PRIMARY KEY(chat_id,user_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS members_user_idx ON members(user_id,chat_id);
|
||||
CREATE TABLE IF NOT EXISTS attachments (
|
||||
id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats(id), owner_id uuid NOT NULL REFERENCES users(id),
|
||||
filename text NOT NULL, object_key text UNIQUE NOT NULL, content_type text NOT NULL,
|
||||
size_bytes bigint NOT NULL CHECK(size_bytes BETWEEN 1 AND 10485760), sha256 text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(), updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
availability text NOT NULL DEFAULT 'present' CHECK(availability IN ('present','missing'))
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats(id), sender_id uuid NOT NULL REFERENCES users(id),
|
||||
text text NOT NULL, created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS messages_order_idx ON messages(chat_id,created_at,id);
|
||||
CREATE INDEX IF NOT EXISTS chats_order_idx ON chats(created_at,id);
|
||||
CREATE TABLE IF NOT EXISTS message_attachments (
|
||||
message_id uuid REFERENCES messages(id), attachment_id uuid REFERENCES attachments(id),
|
||||
position integer NOT NULL, PRIMARY KEY(message_id,attachment_id), UNIQUE(message_id,position)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS idempotency (
|
||||
user_id uuid REFERENCES users(id), chat_id uuid REFERENCES chats(id), key text NOT NULL,
|
||||
body_hash text NOT NULL, message_id uuid NOT NULL REFERENCES messages(id),
|
||||
expires_at timestamptz NOT NULL, PRIMARY KEY(user_id,chat_id,key)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS missing_observations (
|
||||
attachment_id uuid PRIMARY KEY REFERENCES attachments(id), first_seen timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
@@ -0,0 +1,28 @@
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS status text NOT NULL DEFAULT 'queued' CHECK(status IN ('queued','processing','ready','failed'));
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS staging_key text;
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS pipeline_version integer NOT NULL DEFAULT 1 CHECK(pipeline_version > 0);
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS variants jsonb NOT NULL DEFAULT '[]';
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS error_code text;
|
||||
CREATE TABLE IF NOT EXISTS jobs (
|
||||
id uuid PRIMARY KEY, attachment_id uuid NOT NULL REFERENCES attachments(id),
|
||||
pipeline_version integer NOT NULL CHECK(pipeline_version > 0),
|
||||
attempts integer NOT NULL DEFAULT 0, status text NOT NULL DEFAULT 'queued'
|
||||
CHECK(status IN ('queued','processing','ready','failed','superseded')),
|
||||
next_attempt_at timestamptz NOT NULL DEFAULT clock_timestamp(), lease_until timestamptz,
|
||||
error_code text, created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
UNIQUE(attachment_id,pipeline_version)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS jobs_due ON jobs(status,next_attempt_at);
|
||||
CREATE TABLE IF NOT EXISTS outbox (
|
||||
job_id uuid PRIMARY KEY REFERENCES jobs(id),
|
||||
published_at timestamptz, publish_count integer NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS job_audit (
|
||||
id bigserial PRIMARY KEY, job_id uuid NOT NULL REFERENCES jobs(id),
|
||||
event text NOT NULL, detail text NOT NULL DEFAULT '',
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS fault_rules (
|
||||
job_id uuid PRIMARY KEY REFERENCES jobs(id), point text NOT NULL,
|
||||
remaining integer NOT NULL DEFAULT 1
|
||||
);
|
||||
Reference in New Issue
Block a user