From 76f36f4b571436216ddb42df765b6c17f5d838b2 Mon Sep 17 00:00:00 2001 From: nulfrost Date: Tue, 12 Dec 2023 18:41:05 -0500 Subject: [PATCH 1/4] start initial draft of schema, add create_hackathons migration --- .../20231212232610_create_hackathons_table.sql | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 supabase/migrations/20231212232610_create_hackathons_table.sql diff --git a/supabase/migrations/20231212232610_create_hackathons_table.sql b/supabase/migrations/20231212232610_create_hackathons_table.sql new file mode 100644 index 0000000..9873a05 --- /dev/null +++ b/supabase/migrations/20231212232610_create_hackathons_table.sql @@ -0,0 +1,14 @@ +create table "public"."hackathons" ( + "id" bigint generated by default as identity not null, + "created_at" timestamp with time zone default now(), + "title" text, + "description" text +); + + +alter table "public"."hackathons" enable row level security; + +CREATE UNIQUE INDEX hackathons_pkey ON public.hackathons USING btree (id); + +alter table "public"."hackathons" add constraint "hackathons_pkey" PRIMARY KEY using index "hackathons_pkey"; +alter table "public"."hackathons" add column "updated_at" timestamp with time zone default now(); \ No newline at end of file From b87e22e4519467c9f1d96e7a0bda91c9a6092de2 Mon Sep 17 00:00:00 2001 From: nulfrost Date: Tue, 12 Dec 2023 19:06:04 -0500 Subject: [PATCH 2/4] create user roles table with migration file --- ...20231212235012_create_user_roles_table.sql | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 supabase/migrations/20231212235012_create_user_roles_table.sql diff --git a/supabase/migrations/20231212235012_create_user_roles_table.sql b/supabase/migrations/20231212235012_create_user_roles_table.sql new file mode 100644 index 0000000..39e4fbc --- /dev/null +++ b/supabase/migrations/20231212235012_create_user_roles_table.sql @@ -0,0 +1,26 @@ +CREATE TYPE user_role AS ENUM ('admin', 'event_coordinator', 'participant'); + +create table "public"."user_roles" ( + "id" bigint generated by default as identity not null +); + +alter table "public"."user_roles" add column "role" user_role default 'participant'::user_role; + +alter table "public"."user_roles" add column "user_id" uuid; + +alter table "public"."user_roles" enable row level security; + +CREATE UNIQUE INDEX user_roles_pkey ON public.user_roles USING btree (id); + +alter table "public"."user_roles" add constraint "user_roles_pkey" PRIMARY KEY using index "user_roles_pkey"; + +alter table "public"."user_roles" add constraint "user_roles_user_id_fkey" FOREIGN KEY (user_id) REFERENCES profiles(id) not valid; + +alter table "public"."user_roles" validate constraint "user_roles_user_id_fkey"; + +create policy "Enable read access for user owned roles" +on "public"."user_roles" +as permissive +for select +to public +using ((auth.uid() = user_id)); \ No newline at end of file From cb877d199e758848ace00cc54293a672a8c64851 Mon Sep 17 00:00:00 2001 From: nulfrost Date: Tue, 12 Dec 2023 19:35:23 -0500 Subject: [PATCH 3/4] create participants table and relations --- ...231213001948_create_participants_table.sql | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 supabase/migrations/20231213001948_create_participants_table.sql diff --git a/supabase/migrations/20231213001948_create_participants_table.sql b/supabase/migrations/20231213001948_create_participants_table.sql new file mode 100644 index 0000000..e413fde --- /dev/null +++ b/supabase/migrations/20231213001948_create_participants_table.sql @@ -0,0 +1,38 @@ +create table "public"."hackathons_participants" ( + "user_id" uuid not null, + "hackathon_id" bigint not null +); + + +alter table "public"."hackathons_participants" enable row level security; + +create table "public"."participants" ( + "id" bigint generated by default as identity not null, + "created_at" timestamp with time zone default now(), + "user_id" uuid +); + + +alter table "public"."participants" enable row level security; + +alter table "public"."profiles" add column "bio" text; + +CREATE UNIQUE INDEX hackathons_participants_pkey ON public.hackathons_participants USING btree (user_id, hackathon_id); + +CREATE UNIQUE INDEX participants_pkey ON public.participants USING btree (id); + +alter table "public"."hackathons_participants" add constraint "hackathons_participants_pkey" PRIMARY KEY using index "hackathons_participants_pkey"; + +alter table "public"."participants" add constraint "participants_pkey" PRIMARY KEY using index "participants_pkey"; + +alter table "public"."hackathons_participants" add constraint "hackathons_participants_hackathon_id_fkey" FOREIGN KEY (hackathon_id) REFERENCES hackathons(id) not valid; + +alter table "public"."hackathons_participants" validate constraint "hackathons_participants_hackathon_id_fkey"; + +alter table "public"."hackathons_participants" add constraint "hackathons_participants_user_id_fkey" FOREIGN KEY (user_id) REFERENCES profiles(id) not valid; + +alter table "public"."hackathons_participants" validate constraint "hackathons_participants_user_id_fkey"; + +alter table "public"."participants" add constraint "participants_user_id_fkey" FOREIGN KEY (user_id) REFERENCES profiles(id) not valid; + +alter table "public"."participants" validate constraint "participants_user_id_fkey"; \ No newline at end of file From 734542a72677a8fdc57c38a76eeb25a48bc5da69 Mon Sep 17 00:00:00 2001 From: nulfrost Date: Tue, 12 Dec 2023 19:45:29 -0500 Subject: [PATCH 4/4] update rls rules on the hackathons table to only allow admins and event_coordinators the right to create hackathons --- .../20231213004446_update_rls_hackathons.sql | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 supabase/migrations/20231213004446_update_rls_hackathons.sql diff --git a/supabase/migrations/20231213004446_update_rls_hackathons.sql b/supabase/migrations/20231213004446_update_rls_hackathons.sql new file mode 100644 index 0000000..a475cfa --- /dev/null +++ b/supabase/migrations/20231213004446_update_rls_hackathons.sql @@ -0,0 +1,27 @@ +create policy "Enable insert for authenticated users with admin/event_co role" +on "public"."hackathons" +as permissive +for insert +to authenticated +with check ((auth.uid() IN ( SELECT user_roles.user_id + FROM user_roles + WHERE ((user_roles.role = 'admin'::user_role) OR (user_roles.role = 'event_coordinator'::user_role))))); + + +create policy "Enable read access for all users" +on "public"."hackathons" +as permissive +for select +to public +using (true); + + +create policy "Enable update for authenticated users with admin/event_co role" +on "public"."hackathons" +as permissive +for update +to authenticated +using ((auth.uid() IN ( SELECT user_roles.user_id + FROM user_roles + WHERE ((user_roles.role = 'admin'::user_role) OR (user_roles.role = 'event_coordinator'::user_role))))) +with check (true); \ No newline at end of file