-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommunity_interactions_supabase.sql
More file actions
113 lines (101 loc) · 3.48 KB
/
community_interactions_supabase.sql
File metadata and controls
113 lines (101 loc) · 3.48 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
-- Community interactions upgrade for Safety Guardian / WayGuard
-- Run this in Supabase SQL editor for existing projects.
CREATE TABLE IF NOT EXISTS public.community_post_likes (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
post_id BIGINT REFERENCES public.safety_feed(id) ON DELETE CASCADE NOT NULL,
user_id UUID REFERENCES public.profiles(id) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS community_post_likes_post_user_idx
ON public.community_post_likes (post_id, user_id);
CREATE INDEX IF NOT EXISTS community_post_likes_post_idx
ON public.community_post_likes (post_id);
CREATE TABLE IF NOT EXISTS public.community_post_comments (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
post_id BIGINT REFERENCES public.safety_feed(id) ON DELETE CASCADE NOT NULL,
user_id UUID REFERENCES public.profiles(id) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT TIMEZONE('utc'::text, NOW()) NOT NULL
);
CREATE INDEX IF NOT EXISTS community_post_comments_post_idx
ON public.community_post_comments (post_id, created_at);
ALTER TABLE public.community_post_likes ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.community_post_comments ENABLE ROW LEVEL SECURITY;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_policies
WHERE schemaname = 'public'
AND tablename = 'community_post_likes'
AND policyname = 'Community likes are viewable by everyone'
) THEN
EXECUTE 'CREATE POLICY "Community likes are viewable by everyone" ON public.community_post_likes FOR SELECT USING (true)';
END IF;
END
$$;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_policies
WHERE schemaname = 'public'
AND tablename = 'community_post_likes'
AND policyname = 'Users can insert own community likes'
) THEN
EXECUTE 'CREATE POLICY "Users can insert own community likes" ON public.community_post_likes FOR INSERT WITH CHECK (auth.uid() = user_id)';
END IF;
END
$$;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_policies
WHERE schemaname = 'public'
AND tablename = 'community_post_likes'
AND policyname = 'Users can delete own community likes'
) THEN
EXECUTE 'CREATE POLICY "Users can delete own community likes" ON public.community_post_likes FOR DELETE USING (auth.uid() = user_id)';
END IF;
END
$$;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_policies
WHERE schemaname = 'public'
AND tablename = 'community_post_comments'
AND policyname = 'Community comments are viewable by everyone'
) THEN
EXECUTE 'CREATE POLICY "Community comments are viewable by everyone" ON public.community_post_comments FOR SELECT USING (true)';
END IF;
END
$$;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_policies
WHERE schemaname = 'public'
AND tablename = 'community_post_comments'
AND policyname = 'Users can insert own community comments'
) THEN
EXECUTE 'CREATE POLICY "Users can insert own community comments" ON public.community_post_comments FOR INSERT WITH CHECK (auth.uid() = user_id)';
END IF;
END
$$;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_policies
WHERE schemaname = 'public'
AND tablename = 'community_post_comments'
AND policyname = 'Users can delete own community comments'
) THEN
EXECUTE 'CREATE POLICY "Users can delete own community comments" ON public.community_post_comments FOR DELETE USING (auth.uid() = user_id)';
END IF;
END
$$;