-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase_schema.sql
More file actions
73 lines (48 loc) · 6.54 KB
/
Copy pathsupabase_schema.sql
File metadata and controls
73 lines (48 loc) · 6.54 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
-- ================================ IMPORTANT! =========================================
-- First,you MUST SET UP CLERK WITH SUPABASE
-- 1) Go to clerk dashboard then integrations then enable integration with supabase
-- 2) Click Manage Integration and fill all fields needed then copy the domain url.
-- 2) Go to supabase dashboard provider third party and select Clerk and paste the domain url there.
-- =====================================================================================
-- TABLE CREATION
CREATE TABLE IF NOT EXISTS "public"."comments" (
"id" "uuid" DEFAULT "gen_random_uuid"() NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"comment" "text" NOT NULL,
"user_id" "text" NOT NULL,
"recipe_id" "uuid" NOT NULL
);
CREATE TABLE IF NOT EXISTS "public"."recipes" (
"id" "uuid" DEFAULT "gen_random_uuid"() NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"name" character varying NOT NULL,
"ingredients" "text"[],
"instructions" "text" NOT NULL,
"user_id" "text" NOT NULL
);
CREATE TABLE IF NOT EXISTS "public"."recipes_unlocked" (
"id" "uuid" DEFAULT "gen_random_uuid"() NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"recipe_id" "uuid" NOT NULL,
"user_id" "text" NOT NULL
);
ALTER TABLE ONLY "public"."comments"
ADD CONSTRAINT "comments_pkey" PRIMARY KEY ("id");
ALTER TABLE ONLY "public"."recipes"
ADD CONSTRAINT "recipes_pkey" PRIMARY KEY ("id");
ALTER TABLE ONLY "public"."recipes_unlocked"
ADD CONSTRAINT "recipes_unlocked_pkey" PRIMARY KEY ("id");
ALTER TABLE ONLY "public"."comments"
ADD CONSTRAINT "comments_recipe_id_fkey" FOREIGN KEY ("recipe_id") REFERENCES "public"."recipes"("id") ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE ONLY "public"."recipes_unlocked"
ADD CONSTRAINT "recipes_unlocked_recipe_id_fkey" FOREIGN KEY ("recipe_id") REFERENCES "public"."recipes"("id") ON UPDATE CASCADE ON DELETE CASCADE;
-- POLICIES
CREATE POLICY "All users can see the recipes" ON "public"."recipes" FOR SELECT USING (true);
CREATE POLICY "Authenticated users can create/modify only own comments" ON "public"."comments" TO "authenticated" USING ((( SELECT ("auth"."jwt"() ->> 'sub'::"text")) = "user_id")) WITH CHECK ((( SELECT ("auth"."jwt"() ->> 'sub'::"text")) = "user_id"));
CREATE POLICY "Authenticated users can create/modify only own recipes" ON "public"."recipes" TO "authenticated" USING ((( SELECT ("auth"."jwt"() ->> 'sub'::"text")) = "user_id")) WITH CHECK ((( SELECT ("auth"."jwt"() ->> 'sub'::"text")) = "user_id"));
CREATE POLICY "Authenticated users can see all comments" ON "public"."comments" FOR SELECT TO "authenticated" USING ((( SELECT "auth"."jwt"() AS "jwt") IS NOT NULL));
CREATE POLICY "Users can view/modify own unlocked recipes" ON "public"."recipes_unlocked" TO "authenticated" USING ((( SELECT ("auth"."jwt"() ->> 'sub'::"text")) = "user_id")) WITH CHECK ((( SELECT ("auth"."jwt"() ->> 'sub'::"text")) = "user_id"));
-- ENABLE ROW LEVEL SECURITY (RLS)
ALTER TABLE "public"."comments" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "public"."recipes" ENABLE ROW LEVEL SECURITY;
ALTER TABLE "public"."recipes_unlocked" ENABLE ROW LEVEL SECURITY;