Реализовать лабораторную 4 на оценку 5
Добавить приватное S3-хранилище, вложения, presigned upload, reconciler и доказательства.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
-- Laboratory 4: attachment metadata in PostgreSQL, bytes in a private S3 bucket.
|
||||
--
|
||||
-- The API assigns the object key. The original filename is display metadata
|
||||
-- only and is never used to build a key. A row is inserted only after the S3
|
||||
-- PUT succeeds; the proxy upload compensates a failed INSERT by deleting the
|
||||
-- object, and the reconciler cleans up objects left by a process crash.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS 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,
|
||||
filename text NOT NULL,
|
||||
content_type text NOT NULL,
|
||||
size_bytes bigint NOT NULL,
|
||||
sha256 text NOT NULL,
|
||||
object_key text NOT NULL,
|
||||
status text NOT NULL DEFAULT 'ready',
|
||||
error_code text,
|
||||
presign_expires_at timestamptz,
|
||||
missing_since timestamptz,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT attachments_object_key_key UNIQUE (object_key),
|
||||
CONSTRAINT attachments_content_type_check
|
||||
CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
CONSTRAINT attachments_size_check CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
CONSTRAINT attachments_sha256_check CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
CONSTRAINT attachments_status_check
|
||||
CHECK (status IN ('pending', 'ready', 'failed')),
|
||||
CONSTRAINT attachments_filename_check
|
||||
CHECK (char_length(filename) BETWEEN 1 AND 255),
|
||||
CONSTRAINT attachments_error_code_check
|
||||
CHECK (error_code IS NULL OR error_code IN
|
||||
('invalid_image', 'processing_failed', 'retry_exhausted', 'missing_object'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS attachments_chat_created_idx
|
||||
ON attachments (chat_id, created_at, id);
|
||||
|
||||
-- Reconciler access paths.
|
||||
CREATE INDEX IF NOT EXISTS attachments_status_created_idx
|
||||
ON attachments (status, created_at);
|
||||
CREATE INDEX IF NOT EXISTS attachments_object_key_idx
|
||||
ON attachments (object_key);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS 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 integer NOT NULL,
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
CONSTRAINT message_attachments_position_key UNIQUE (message_id, position),
|
||||
CONSTRAINT message_attachments_position_check CHECK (position >= 0 AND position < 10)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS message_attachments_attachment_idx
|
||||
ON message_attachments (attachment_id);
|
||||
|
||||
-- A message may carry an empty text only when it has at least one attachment.
|
||||
-- The API enforces the semantic part; this constraint is the storage backstop.
|
||||
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_length;
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_text_length CHECK (char_length(text) BETWEEN 0 AND 4000);
|
||||
Reference in New Issue
Block a user