-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-schema.sql
More file actions
154 lines (132 loc) · 4.77 KB
/
Copy pathsupabase-schema.sql
File metadata and controls
154 lines (132 loc) · 4.77 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
-- WinLog Database Schema
-- Run this in your Supabase SQL Editor
-- Create users table to store additional user profile data
CREATE TABLE IF NOT EXISTS users (
id UUID REFERENCES auth.users(id) ON DELETE CASCADE PRIMARY KEY,
username TEXT UNIQUE,
email TEXT NOT NULL,
first_name TEXT,
last_name TEXT,
job_title TEXT,
company TEXT,
avatar_url TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create achievements table (main table for storing user achievements)
CREATE TABLE IF NOT EXISTS achievements (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
description TEXT NOT NULL,
impact TEXT,
proof_urls TEXT[], -- Array of URLs for proof/evidence
category TEXT,
date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create brag_entries table (alias for achievements to maintain API compatibility)
-- Note: You can use either table name. Consider standardizing on one.
CREATE TABLE IF NOT EXISTS brag_entries (
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
description TEXT NOT NULL,
impact TEXT,
proof_urls TEXT[],
category TEXT,
date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Enable Row Level Security
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
ALTER TABLE achievements ENABLE ROW LEVEL SECURITY;
ALTER TABLE brag_entries ENABLE ROW LEVEL SECURITY;
-- Create policies for users table
CREATE POLICY "Users can view own profile"
ON users FOR SELECT
USING (auth.uid() = id);
CREATE POLICY "Users can update own profile"
ON users FOR UPDATE
USING (auth.uid() = id);
CREATE POLICY "Users can insert own profile"
ON users FOR INSERT
WITH CHECK (auth.uid() = id);
-- Create policies for achievements table
-- Users can only see their own achievements
CREATE POLICY "Users can view own achievements"
ON achievements FOR SELECT
USING (auth.uid() = user_id);
-- Users can insert their own achievements
CREATE POLICY "Users can insert own achievements"
ON achievements FOR INSERT
WITH CHECK (auth.uid() = user_id);
-- Users can update their own achievements
CREATE POLICY "Users can update own achievements"
ON achievements FOR UPDATE
USING (auth.uid() = user_id);
-- Users can delete their own achievements
CREATE POLICY "Users can delete own achievements"
ON achievements FOR DELETE
USING (auth.uid() = user_id);
-- Create policies for brag_entries table (same as achievements)
CREATE POLICY "Users can view own brag_entries"
ON brag_entries FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own brag_entries"
ON brag_entries FOR INSERT
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own brag_entries"
ON brag_entries FOR UPDATE
USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own brag_entries"
ON brag_entries FOR DELETE
USING (auth.uid() = user_id);
-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_users_id ON users(id);
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
CREATE INDEX IF NOT EXISTS idx_achievements_user_id ON achievements(user_id);
CREATE INDEX IF NOT EXISTS idx_achievements_date ON achievements(date DESC);
CREATE INDEX IF NOT EXISTS idx_brag_entries_user_id ON brag_entries(user_id);
CREATE INDEX IF NOT EXISTS idx_brag_entries_date ON brag_entries(date DESC);
-- Function to automatically update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Triggers to auto-update updated_at
CREATE TRIGGER update_users_updated_at
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_achievements_updated_at
BEFORE UPDATE ON achievements
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
CREATE TRIGGER update_brag_entries_updated_at
BEFORE UPDATE ON brag_entries
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- Function to create user profile on signup
CREATE OR REPLACE FUNCTION handle_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO users (id, email, username)
VALUES (
NEW.id,
NEW.email,
COALESCE(NEW.raw_user_meta_data->>'username', split_part(NEW.email, '@', 1))
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
-- Trigger to create user profile on signup
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW
EXECUTE FUNCTION handle_new_user();