mike-backend / backend /migrations /002_comment_threads.sql
bardd's picture
Deploy Mike backend-only Space
06671aa verified
Raw History Blame Contribute Delete
4.09 kB
-- Comments & Issues feature
-- Root Word comments become document_comment_threads.
-- Imported Word replies and portal replies become document_comment_replies.
create table if not exists public.document_comment_threads (
id uuid primary key default gen_random_uuid(),
document_id uuid not null references public.documents(id) on delete cascade,
document_version_id uuid not null references public.document_versions(id) on delete cascade,
word_comment_id text,
parent_word_comment_id text,
source_type text not null default 'word_root'
check (source_type = any (array['word_root'::text, 'portal_thread'::text])),
author_raw text not null default '',
author_user_id text,
extracted_by_user_id text,
body text not null default '',
quoted_text text not null default '',
paragraph_id text,
native_para_id text,
stable_id text,
comment_para_id text,
section_label text,
clause_type text,
risk_level text,
status text not null default 'open'
check (status = any (array['open'::text, 'in_review'::text, 'resolved'::text, 'stale'::text])),
assignee_email text,
sort_index integer not null default 0,
word_done boolean,
commented_at timestamptz,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
create index if not exists document_comment_threads_document_idx
on public.document_comment_threads(document_id, sort_index);
create index if not exists document_comment_threads_version_idx
on public.document_comment_threads(document_version_id, sort_index);
create unique index if not exists document_comment_threads_version_word_comment_uidx
on public.document_comment_threads(document_version_id, word_comment_id)
where word_comment_id is not null;
create table if not exists public.document_comment_replies (
id uuid primary key default gen_random_uuid(),
thread_id uuid not null references public.document_comment_threads(id) on delete cascade,
document_id uuid not null references public.documents(id) on delete cascade,
document_version_id uuid not null references public.document_versions(id) on delete cascade,
word_comment_id text,
parent_word_comment_id text,
source_type text not null
check (source_type = any (array['word_reply'::text, 'portal_reply'::text])),
author_raw text not null default '',
author_user_id text,
body text not null default '',
comment_para_id text,
word_done boolean,
commented_at timestamptz,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
create index if not exists document_comment_replies_thread_idx
on public.document_comment_replies(thread_id, created_at);
create index if not exists document_comment_replies_document_idx
on public.document_comment_replies(document_id, created_at);
create index if not exists document_comment_replies_version_idx
on public.document_comment_replies(document_version_id, created_at);
create index if not exists document_comment_threads_commented_at_idx
on public.document_comment_threads(document_id, commented_at);
create index if not exists document_comment_replies_commented_at_idx
on public.document_comment_replies(thread_id, commented_at);
create unique index if not exists document_comment_replies_version_word_comment_uidx
on public.document_comment_replies(document_version_id, word_comment_id)
where word_comment_id is not null;
create or replace function public.set_updated_at()
returns trigger
language plpgsql
as $$
begin
new.updated_at = now();
return new;
end;
$$;
drop trigger if exists document_comment_threads_set_updated_at on public.document_comment_threads;
create trigger document_comment_threads_set_updated_at
before update on public.document_comment_threads
for each row execute function public.set_updated_at();
drop trigger if exists document_comment_replies_set_updated_at on public.document_comment_replies;
create trigger document_comment_replies_set_updated_at
before update on public.document_comment_replies
for each row execute function public.set_updated_at();