-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_runs_study_notes.sql
More file actions
60 lines (51 loc) · 1.95 KB
/
sql_runs_study_notes.sql
File metadata and controls
60 lines (51 loc) · 1.95 KB
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
-- Study Notes Table
-- Enable pgcrypto for UUIDs if not already enabled
create extension if not exists "pgcrypto";
-- Study Notes table to store project-specific research notes
create table if not exists public.study_notes (
id uuid primary key default gen_random_uuid(),
project_id uuid not null,
title text not null,
content text not null,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now(),
constraint fk_project foreign key (project_id) references public.projects(id) on delete cascade
);
-- Updated-at trigger
drop trigger if exists trg_study_notes_set_updated_at on public.study_notes;
create trigger trg_study_notes_set_updated_at
before update on public.study_notes
for each row
execute function public.set_updated_at();
-- Helpful indexes
create index if not exists idx_study_notes_project_id on public.study_notes(project_id);
create index if not exists idx_study_notes_created_at on public.study_notes(created_at desc);
create index if not exists idx_study_notes_title on public.study_notes using gin(to_tsvector('english', title));
-- Row Level Security (RLS)
alter table public.study_notes enable row level security;
-- Policies (allow anon reads/inserts/updates for now)
drop policy if exists "Allow read to anon" on public.study_notes;
create policy "Allow read to anon"
on public.study_notes
for select
to anon, authenticated
using (true);
drop policy if exists "Allow insert to anon" on public.study_notes;
create policy "Allow insert to anon"
on public.study_notes
for insert
to anon, authenticated
with check (true);
drop policy if exists "Allow update to authenticated" on public.study_notes;
create policy "Allow update to authenticated"
on public.study_notes
for update
to authenticated
using (true)
with check (true);
drop policy if exists "Allow delete to authenticated" on public.study_notes;
create policy "Allow delete to authenticated"
on public.study_notes
for delete
to authenticated
using (true);