forked from mtusi2026/mtusi2026lab5
36 lines
1.2 KiB
SQL
36 lines
1.2 KiB
SQL
-- +goose Up
|
|
CREATE TABLE files (
|
|
hash bytea PRIMARY KEY CHECK (octet_length(hash) = 32),
|
|
size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
|
|
mime_type text NOT NULL CHECK (mime_type IN ('image/png', 'image/jpeg')),
|
|
status text NOT NULL CHECK (status IN ('pending', 'ready', 'failed')),
|
|
created_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TABLE 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,
|
|
file_hash bytea NOT NULL REFERENCES files(hash),
|
|
created_at timestamptz NOT NULL
|
|
);
|
|
|
|
-- The reconciler must distinguish a shared file from an unused file.
|
|
CREATE INDEX attachments_file_hash_idx ON attachments(file_hash);
|
|
|
|
CREATE TABLE message_attachments (
|
|
chat_id uuid NOT NULL,
|
|
message_id uuid NOT NULL,
|
|
attachment_id uuid NOT NULL REFERENCES attachments(id),
|
|
ordinal smallint NOT NULL CHECK (ordinal BETWEEN 0 AND 9),
|
|
PRIMARY KEY(chat_id, message_id, attachment_id),
|
|
UNIQUE(chat_id, message_id, ordinal),
|
|
FOREIGN KEY(chat_id, message_id) REFERENCES messages(chat_id, id)
|
|
);
|
|
|
|
-- +goose Down
|
|
DROP TABLE message_attachments;
|
|
DROP TABLE attachments;
|
|
DROP TABLE files;
|