-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase-schema.sql
More file actions
111 lines (92 loc) · 3.5 KB
/
database-schema.sql
File metadata and controls
111 lines (92 loc) · 3.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
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
-- Birthday Celebration App Database Schema
-- This schema should be run in your Supabase SQL editor
-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Profiles table (extends auth.users)
CREATE TABLE profiles (
id UUID REFERENCES auth.users ON DELETE CASCADE PRIMARY KEY,
organization_name TEXT NOT NULL,
display_url_slug TEXT UNIQUE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL
);
-- Team members table
CREATE TABLE team_members (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
user_id UUID REFERENCES profiles(id) ON DELETE CASCADE NOT NULL,
name TEXT NOT NULL,
birthday_month INTEGER NOT NULL CHECK (birthday_month >= 1 AND birthday_month <= 12),
birthday_day INTEGER NOT NULL CHECK (birthday_day >= 1 AND birthday_day <= 31),
photo_url TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL
);
-- Function to automatically create a profile on user signup
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.profiles (id, organization_name, display_url_slug)
VALUES (
NEW.id,
COALESCE(NEW.raw_user_meta_data->>'organization_name', 'My Organization'),
NEW.id::text
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Trigger to create profile on signup
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
-- Row Level Security (RLS) Policies
-- Enable RLS
ALTER TABLE profiles ENABLE ROW LEVEL SECURITY;
ALTER TABLE team_members ENABLE ROW LEVEL SECURITY;
-- Profiles policies
CREATE POLICY "Users can view their own profile"
ON profiles FOR SELECT
USING (auth.uid() = id);
CREATE POLICY "Users can update their own profile"
ON profiles FOR UPDATE
USING (auth.uid() = id);
-- Profiles are readable by display_url_slug for public display
CREATE POLICY "Profiles are viewable by slug"
ON profiles FOR SELECT
USING (true);
-- Team members policies
CREATE POLICY "Users can view their own team members"
ON team_members FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can insert their own team members"
ON team_members FOR INSERT
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update their own team members"
ON team_members FOR UPDATE
USING (auth.uid() = user_id);
CREATE POLICY "Users can delete their own team members"
ON team_members FOR DELETE
USING (auth.uid() = user_id);
-- Public display: anyone can view team members by user_id (for display page)
CREATE POLICY "Team members viewable for display"
ON team_members FOR SELECT
USING (true);
-- Storage bucket for photos
-- Run this in the Supabase dashboard Storage section:
-- 1. Create a new bucket called "profile-photos"
-- 2. Set it as public
-- 3. Add a policy to allow authenticated users to upload
-- Function to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = TIMEZONE('utc'::text, NOW());
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Triggers for updated_at
CREATE TRIGGER update_profiles_updated_at
BEFORE UPDATE ON profiles
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_team_members_updated_at
BEFORE UPDATE ON team_members
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();