forked from mtusi2026/mtusi2026lab4
40 lines
2.3 KiB
SQL
40 lines
2.3 KiB
SQL
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()
|
|
);
|