-
Notifications
You must be signed in to change notification settings - Fork 109
/
20231006212813_documents.sql
120 lines (105 loc) · 3.02 KB
/
20231006212813_documents.sql
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
create extension if not exists pg_net with schema extensions;
create extension if not exists vector with schema extensions;
create table documents (
id bigint primary key generated always as identity,
name text not null,
storage_object_id uuid not null references storage.objects (id),
created_by uuid not null references auth.users (id) default auth.uid(),
created_at timestamp with time zone not null default now()
);
create view documents_with_storage_path
with (security_invoker=true)
as
select documents.*, storage.objects.name as storage_object_path
from documents
join storage.objects
on storage.objects.id = documents.storage_object_id;
create table document_sections (
id bigint primary key generated always as identity,
document_id bigint not null references documents (id),
content text not null,
embedding vector (384)
);
create index on document_sections using hnsw (embedding vector_ip_ops);
alter table documents enable row level security;
alter table document_sections enable row level security;
create policy "Users can insert documents"
on documents for insert to authenticated with check (
auth.uid() = created_by
);
create policy "Users can query their own documents"
on documents for select to authenticated using (
auth.uid() = created_by
);
create policy "Users can insert document sections"
on document_sections for insert to authenticated with check (
document_id in (
select id
from documents
where created_by = auth.uid()
)
);
create policy "Users can update their own document sections"
on document_sections for update to authenticated using (
document_id in (
select id
from documents
where created_by = auth.uid()
)
) with check (
document_id in (
select id
from documents
where created_by = auth.uid()
)
);
create policy "Users can query their own document sections"
on document_sections for select to authenticated using (
document_id in (
select id
from documents
where created_by = auth.uid()
)
);
create function supabase_url()
returns text
language plpgsql
security definer
as $$
declare
secret_value text;
begin
select decrypted_secret into secret_value from vault.decrypted_secrets where name = 'supabase_url';
return secret_value;
end;
$$;
create function private.handle_storage_update()
returns trigger
language plpgsql
as $$
declare
document_id bigint;
result int;
begin
insert into documents (name, storage_object_id, created_by)
values (new.path_tokens[2], new.id, new.owner)
returning id into document_id;
select
net.http_post(
url := supabase_url() || '/functions/v1/process',
headers := jsonb_build_object(
'Content-Type', 'application/json',
'Authorization', current_setting('request.headers')::json->>'authorization'
),
body := jsonb_build_object(
'document_id', document_id
)
)
into result;
return null;
end;
$$;
create trigger on_file_upload
after insert on storage.objects
for each row
execute procedure private.handle_storage_update();