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 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 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 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