-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-setup.sql
More file actions
46 lines (38 loc) · 1.47 KB
/
Copy pathsupabase-setup.sql
File metadata and controls
46 lines (38 loc) · 1.47 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
-- Create users table
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create an index on the email column for faster lookups
CREATE INDEX IF NOT EXISTS users_email_idx ON users (email);
-- Enable Row Level Security (RLS)
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
-- Create a policy that allows users to read their own data
CREATE POLICY "Users can read their own data" ON users
FOR SELECT USING (auth.uid() = id);
-- Create a policy that allows users to update their own data
CREATE POLICY "Users can update their own data" ON users
FOR UPDATE USING (auth.uid() = id);
-- Create a policy that allows users to delete their own data
CREATE POLICY "Users can delete their own data" ON users
FOR DELETE USING (auth.uid() = id);
-- Create a policy that allows anyone to insert data (for registration)
CREATE POLICY "Anyone can insert data" ON users
FOR INSERT WITH CHECK (true);
-- Create a function to update the updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Create a trigger to automatically update the updated_at column
CREATE TRIGGER update_users_updated_at
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();