-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-setup.sql
More file actions
51 lines (43 loc) · 1.79 KB
/
supabase-setup.sql
File metadata and controls
51 lines (43 loc) · 1.79 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
-- Create leads table
CREATE TABLE IF NOT EXISTS leads (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
company VARCHAR(255),
service VARCHAR(100) NOT NULL,
status VARCHAR(50) DEFAULT 'new',
message 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 index for better performance
CREATE INDEX IF NOT EXISTS idx_leads_status ON leads(status);
CREATE INDEX IF NOT EXISTS idx_leads_created_at ON leads(created_at);
-- Enable Row Level Security (RLS)
ALTER TABLE leads ENABLE ROW LEVEL SECURITY;
-- Create policy to allow authenticated users to read all leads
CREATE POLICY "Allow authenticated users to read leads" ON leads
FOR SELECT USING (auth.role() = 'authenticated');
-- Create policy to allow authenticated users to insert leads
CREATE POLICY "Allow authenticated users to insert leads" ON leads
FOR INSERT WITH CHECK (auth.role() = 'authenticated');
-- Create policy to allow authenticated users to update leads
CREATE POLICY "Allow authenticated users to update leads" ON leads
FOR UPDATE USING (auth.role() = 'authenticated');
-- Create policy to allow authenticated users to delete leads
CREATE POLICY "Allow authenticated users to delete leads" ON leads
FOR DELETE USING (auth.role() = 'authenticated');
-- Create 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';
-- Create trigger to automatically update updated_at
CREATE TRIGGER update_leads_updated_at
BEFORE UPDATE ON leads
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();