-- Lab2 initial schema. Idempotent and safe to re-run via the migration runner. create table if not exists users ( id uuid primary key, username text not null unique, display_name text not null, password_hash text not null, created_at timestamptz not null ); create table if not exists chats ( id uuid primary key, title text not null, created_at timestamptz not null ); create table if not exists chat_members ( chat_id uuid not null references chats (id) on delete cascade, user_id uuid not null references users (id) on delete cascade, primary key (chat_id, user_id) ); create table if not exists messages ( id uuid primary key, chat_id uuid not null references chats (id) on delete cascade, sender_id uuid not null references users (id), text text not null, created_at timestamptz not null ); -- Composite index prepared for keyset pagination: -- where chat_id = $1 and (created_at, id) > ($2, $3) order by created_at, id create index if not exists messages_chat_created_id_idx on messages (chat_id, created_at, id); create table if not exists sessions ( token_hash text primary key, user_id uuid not null references users (id) on delete cascade, created_at timestamptz not null, expires_at timestamptz not null ); create index if not exists sessions_expires_at_idx on sessions (expires_at);