-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
75 lines (61 loc) · 2.5 KB
/
Copy pathschema.sql
File metadata and controls
75 lines (61 loc) · 2.5 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
-- pulse.city database schema
-- Run this in Supabase SQL Editor to set up tables
-- Profiles (extends Supabase auth.users)
create table if not exists public.profiles (
id uuid references auth.users on delete cascade primary key,
username text unique,
display_name text,
avatar_url text,
created_at timestamptz default now() not null
);
-- Enable RLS
alter table public.profiles enable row level security;
-- Profiles policies
create policy "Public profiles are viewable by everyone"
on public.profiles for select using (true);
create policy "Users can update own profile"
on public.profiles for update using (auth.uid() = id);
-- Auto-create profile on signup
create or replace function public.handle_new_user()
returns trigger as $$
begin
insert into public.profiles (id, display_name, avatar_url)
values (
new.id,
coalesce(new.raw_user_meta_data ->> 'full_name', new.raw_user_meta_data ->> 'name'),
new.raw_user_meta_data ->> 'avatar_url'
);
return new;
end;
$$ language plpgsql security definer;
create or replace trigger on_auth_user_created
after insert on auth.users
for each row execute function public.handle_new_user();
-- Patterns
create table if not exists public.patterns (
id uuid default gen_random_uuid() primary key,
user_id uuid references auth.users on delete cascade not null,
title text not null default 'Untitled',
code text not null,
mode text not null default 'autopilot' check (mode in ('autopilot', 'manual')),
is_public boolean not null default false,
created_at timestamptz default now() not null,
updated_at timestamptz default now() not null
);
-- Enable RLS
alter table public.patterns enable row level security;
-- Patterns policies
create policy "Users can read own patterns"
on public.patterns for select using (auth.uid() = user_id);
create policy "Public patterns are viewable by everyone"
on public.patterns for select using (is_public = true);
create policy "Users can insert own patterns"
on public.patterns for insert with check (auth.uid() = user_id);
create policy "Users can update own patterns"
on public.patterns for update using (auth.uid() = user_id);
create policy "Users can delete own patterns"
on public.patterns for delete using (auth.uid() = user_id);
-- Indexes
create index if not exists idx_patterns_user_id on public.patterns(user_id);
create index if not exists idx_patterns_public on public.patterns(is_public) where is_public = true;
create index if not exists idx_patterns_updated on public.patterns(updated_at desc);