-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path001_initial_schema.sql
More file actions
160 lines (139 loc) · 5.17 KB
/
Copy path001_initial_schema.sql
File metadata and controls
160 lines (139 loc) · 5.17 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
-- closedNote Database Schema
-- This migration sets up the complete database schema for closedNote
-- with Row Level Security (RLS) to ensure users can only access their own data
-- Enable UUID extension
create extension if not exists "uuid-ossp";
-- Create users table (synced with Supabase Auth)
create table if not exists public.users (
id uuid primary key references auth.users on delete cascade,
email text unique not null,
display_name text not null,
created_at timestamptz default now() not null,
updated_at timestamptz default now() not null
);
-- Create prompts table
create table if not exists public.prompts (
id uuid primary key default uuid_generate_v4(),
user_id uuid references public.users(id) on delete cascade not null,
title text not null,
content text not null,
model text not null,
collection text not null default 'uncategorized',
created_at timestamptz default now() not null,
updated_at timestamptz default now() not null
);
-- Create tags table (many-to-many relationship with prompts)
create table if not exists public.tags (
id uuid primary key default uuid_generate_v4(),
prompt_id uuid references public.prompts(id) on delete cascade not null,
tag text not null,
created_at timestamptz default now() not null,
unique(prompt_id, tag)
);
-- Create indexes for better query performance
create index if not exists prompts_user_id_idx on public.prompts(user_id);
create index if not exists prompts_collection_idx on public.prompts(collection);
create index if not exists prompts_model_idx on public.prompts(model);
create index if not exists prompts_created_at_idx on public.prompts(created_at desc);
create index if not exists tags_prompt_id_idx on public.tags(prompt_id);
create index if not exists tags_tag_idx on public.tags(tag);
-- Enable Row Level Security (RLS)
alter table public.users enable row level security;
alter table public.prompts enable row level security;
alter table public.tags enable row level security;
-- RLS Policies for users table
-- Users can only read their own profile
create policy "Users can view their own profile"
on public.users for select
using (auth.uid() = id);
-- Users can update their own profile
create policy "Users can update their own profile"
on public.users for update
using (auth.uid() = id);
-- Users can insert their own profile (triggered on signup)
create policy "Users can insert their own profile"
on public.users for insert
with check (auth.uid() = id);
-- RLS Policies for prompts table
-- Users can only view their own prompts
create policy "Users can view their own prompts"
on public.prompts for select
using (auth.uid() = user_id);
-- Users can create their own prompts
create policy "Users can create their own prompts"
on public.prompts for insert
with check (auth.uid() = user_id);
-- Users can update their own prompts
create policy "Users can update their own prompts"
on public.prompts for update
using (auth.uid() = user_id);
-- Users can delete their own prompts
create policy "Users can delete their own prompts"
on public.prompts for delete
using (auth.uid() = user_id);
-- RLS Policies for tags table
-- Users can only view tags for their own prompts
create policy "Users can view tags for their own prompts"
on public.tags for select
using (
exists (
select 1 from public.prompts
where prompts.id = tags.prompt_id
and prompts.user_id = auth.uid()
)
);
-- Users can create tags for their own prompts
create policy "Users can create tags for their own prompts"
on public.tags for insert
with check (
exists (
select 1 from public.prompts
where prompts.id = tags.prompt_id
and prompts.user_id = auth.uid()
)
);
-- Users can delete tags for their own prompts
create policy "Users can delete tags for their own prompts"
on public.tags for delete
using (
exists (
select 1 from public.prompts
where prompts.id = tags.prompt_id
and prompts.user_id = auth.uid()
)
);
-- Function to handle user creation (trigger on auth.users)
create or replace function public.handle_new_user()
returns trigger as $$
begin
insert into public.users (id, email, display_name)
values (
new.id,
new.email,
coalesce(new.raw_user_meta_data->>'display_name', split_part(new.email, '@', 1))
);
return new;
end;
$$ language plpgsql security definer;
-- Trigger to automatically create user profile on signup
drop trigger if exists on_auth_user_created on auth.users;
create trigger on_auth_user_created
after insert on auth.users
for each row execute function public.handle_new_user();
-- Function to update updated_at timestamp
create or replace function public.handle_updated_at()
returns trigger as $$
begin
new.updated_at = now();
return new;
end;
$$ language plpgsql;
-- Triggers to automatically update updated_at
drop trigger if exists handle_users_updated_at on public.users;
create trigger handle_users_updated_at
before update on public.users
for each row execute function public.handle_updated_at();
drop trigger if exists handle_prompts_updated_at on public.prompts;
create trigger handle_prompts_updated_at
before update on public.prompts
for each row execute function public.handle_updated_at();