69 lines
2.9 KiB
SQL
69 lines
2.9 KiB
SQL
-- Lab4: attachments. PostgreSQL keeps metadata, the bytes live in a private
|
|
-- S3 bucket under a server-generated object_key. Expand-only migration: the
|
|
-- lab3 API keeps working on this schema.
|
|
|
|
-- Text may be empty when the message has attachments; "not blank without
|
|
-- attachments" is checked by the API (one message + its links are inserted
|
|
-- in one transaction).
|
|
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_nonblank_check;
|
|
|
|
CREATE TABLE IF NOT EXISTS attachments (
|
|
id UUID PRIMARY KEY,
|
|
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE RESTRICT,
|
|
owner_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
|
-- Display name only; never used as a path or an S3 key.
|
|
filename VARCHAR(255) NOT NULL,
|
|
content_type VARCHAR(32) NOT NULL,
|
|
size_bytes BIGINT NOT NULL,
|
|
sha256 CHAR(64) NOT NULL,
|
|
width INTEGER NOT NULL,
|
|
height INTEGER NOT NULL,
|
|
-- Lab4 has only "ready": the row is inserted after the object is stored.
|
|
status VARCHAR(16) NOT NULL DEFAULT 'ready',
|
|
error_code VARCHAR(64),
|
|
object_key TEXT NOT NULL UNIQUE,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
|
|
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 ('ready'))
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS attachments_chat_idx ON attachments (chat_id);
|
|
|
|
-- A message references 0-10 attachments of its own chat; "position" keeps
|
|
-- the order of attachment_ids. One attachment may be referenced by several
|
|
-- messages of the chat.
|
|
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 SMALLINT NOT NULL,
|
|
PRIMARY KEY (message_id, attachment_id),
|
|
CONSTRAINT message_attachments_position_check CHECK (position BETWEEN 0 AND 9),
|
|
CONSTRAINT message_attachments_position_unique UNIQUE (message_id, position)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS message_attachments_attachment_idx
|
|
ON message_attachments (attachment_id);
|
|
|
|
-- Upload intent: committed BEFORE the S3 PUT and deleted in the same
|
|
-- transaction that inserts the attachment row. An intent older than the
|
|
-- grace period therefore means "the object may exist in S3, but the request
|
|
-- never committed" (crash, lost DB, client gone) -> the janitor deletes the
|
|
-- object and then the intent.
|
|
CREATE TABLE IF NOT EXISTS upload_intents (
|
|
object_key TEXT PRIMARY KEY,
|
|
attachment_id UUID NOT NULL,
|
|
chat_id UUID NOT NULL,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS upload_intents_created_idx
|
|
ON upload_intents (created_at);
|