Repository navigation
Expand file tree
/
Copy pathsupabase-schema.sql
More file actions
67 lines (56 loc) · 2.01 KB
/
Copy pathsupabase-schema.sql
File metadata and controls
67 lines (56 loc) · 2.01 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
-- Constellar Database Schema
-- Run this in Supabase SQL Editor: https://supabase.com/dashboard/project/_/sql
-- Create projects table
CREATE TABLE IF NOT EXISTS projects (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
owner_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
description TEXT,
canvas_data JSONB NOT NULL DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Create index for faster queries by owner
CREATE INDEX IF NOT EXISTS idx_projects_owner_id ON projects(owner_id);
-- Create index for faster queries by created_at
CREATE INDEX IF NOT EXISTS idx_projects_created_at ON projects(created_at DESC);
-- Enable Row Level Security (RLS)
ALTER TABLE projects ENABLE ROW LEVEL SECURITY;
-- RLS Policy: Users can only see their own projects
CREATE POLICY "Users can view their own projects"
ON projects
FOR SELECT
USING (auth.uid() = owner_id);
-- RLS Policy: Users can create their own projects
CREATE POLICY "Users can create their own projects"
ON projects
FOR INSERT
WITH CHECK (auth.uid() = owner_id);
-- RLS Policy: Users can update their own projects
CREATE POLICY "Users can update their own projects"
ON projects
FOR UPDATE
USING (auth.uid() = owner_id)
WITH CHECK (auth.uid() = owner_id);
-- RLS Policy: Users can delete their own projects
CREATE POLICY "Users can delete their own projects"
ON projects
FOR DELETE
USING (auth.uid() = owner_id);
-- Function to auto-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;
-- Trigger to auto-update updated_at
CREATE TRIGGER update_projects_updated_at
BEFORE UPDATE ON projects
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- Done! Your database is ready.
-- Test by inserting a sample project:
-- INSERT INTO projects (owner_id, title, canvas_data)
-- VALUES (auth.uid(), 'Test Project', '{"elements": []}');