Repository navigation
Expand file tree
/
Copy pathchecklists_setup.sql
More file actions
80 lines (68 loc) · 2.54 KB
/
Copy pathchecklists_setup.sql
File metadata and controls
80 lines (68 loc) · 2.54 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- Create checklists table
create table if not exists public.checklists (
id uuid primary key default uuid_generate_v4(),
user_id uuid not null references auth.users(id) on delete cascade,
name text not null,
progress integer default 0, -- Percentage complete (0-100)
created_at timestamptz default now(),
updated_at timestamptz default now()
);
-- Create checklist_items table
create table if not exists public.checklist_items (
id uuid primary key default uuid_generate_v4(),
checklist_id uuid not null references public.checklists(id) on delete cascade,
text text not null,
completed boolean default false,
order_index integer, -- To maintain the order of items
created_at timestamptz default now(),
updated_at timestamptz default now()
);
-- Enable Row Level Security for checklists
alter table public.checklists enable row level security;
-- Create policies for checklists
create policy "Users can view their own checklists"
on public.checklists for select
using (auth.uid() = user_id);
create policy "Users can insert their own checklists"
on public.checklists for insert
with check (auth.uid() = user_id);
create policy "Users can update their own checklists"
on public.checklists for update
using (auth.uid() = user_id);
create policy "Users can delete their own checklists"
on public.checklists for delete
using (auth.uid() = user_id);
-- Enable Row Level Security for checklist_items
alter table public.checklist_items enable row level security;
-- Create policies for checklist_items
create policy "Users can view items in their own checklists"
on public.checklist_items for select
using (
checklist_id in (
select id from public.checklists where user_id = auth.uid()
)
);
create policy "Users can insert items into their own checklists"
on public.checklist_items for insert
with check (
checklist_id in (
select id from public.checklists where user_id = auth.uid()
)
);
create policy "Users can update items in their own checklists"
on public.checklist_items for update
using (
checklist_id in (
select id from public.checklists where user_id = auth.uid()
)
);
create policy "Users can delete items from their own checklists"
on public.checklist_items for delete
using (
checklist_id in (
select id from public.checklists where user_id = auth.uid()
)
);
-- Create indexes
create index if not exists checklists_user_id_idx on public.checklists(user_id);
create index if not exists checklist_items_checklist_id_idx on public.checklist_items(checklist_id);