File size: 4,092 Bytes
06671aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
-- 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();