From a22883991d2a51870ad407f4723d7e20b2b178fc Mon Sep 17 00:00:00 2001 From: OllyNicholass Date: Tue, 4 Jun 2024 19:22:27 +0100 Subject: [PATCH] chore(#15): add migration to create profiles table --- .../20240515185751_create_profiles_table.sql | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 supabase/migrations/20240515185751_create_profiles_table.sql diff --git a/supabase/migrations/20240515185751_create_profiles_table.sql b/supabase/migrations/20240515185751_create_profiles_table.sql new file mode 100644 index 0000000..fb04c73 --- /dev/null +++ b/supabase/migrations/20240515185751_create_profiles_table.sql @@ -0,0 +1,109 @@ +create table "public"."profiles" ( + "id" uuid not null, + "updated_at" timestamp with time zone, + "created_at" timestamp with time zone default now(), + "username" text, + "full_name" text +); + + +alter table "public"."profiles" enable row level security; + +CREATE UNIQUE INDEX profiles_pkey ON public.profiles USING btree (id); + +CREATE UNIQUE INDEX profiles_username_key ON public.profiles USING btree (username); + +alter table "public"."profiles" add constraint "profiles_pkey" PRIMARY KEY using index "profiles_pkey"; + +alter table "public"."profiles" add constraint "profiles_id_fkey" FOREIGN KEY (id) REFERENCES auth.users(id) ON DELETE CASCADE not valid; + +alter table "public"."profiles" validate constraint "profiles_id_fkey"; + +alter table "public"."profiles" add constraint "profiles_username_key" UNIQUE using index "profiles_username_key"; + +alter table "public"."profiles" add constraint "username_length" CHECK ((char_length(username) >= 3)) not valid; + +alter table "public"."profiles" validate constraint "username_length"; + +set check_function_bodies = off; + +CREATE OR REPLACE FUNCTION public.handle_new_user() + RETURNS trigger + LANGUAGE plpgsql + SECURITY DEFINER +AS $function$ +begin + insert into public.profiles (id, full_name) + values (new.id, new.raw_user_meta_data->>'full_name'); + return new; +end; +$function$ +; + +grant delete on table "public"."profiles" to "anon"; + +grant insert on table "public"."profiles" to "anon"; + +grant references on table "public"."profiles" to "anon"; + +grant select on table "public"."profiles" to "anon"; + +grant trigger on table "public"."profiles" to "anon"; + +grant truncate on table "public"."profiles" to "anon"; + +grant update on table "public"."profiles" to "anon"; + +grant delete on table "public"."profiles" to "authenticated"; + +grant insert on table "public"."profiles" to "authenticated"; + +grant references on table "public"."profiles" to "authenticated"; + +grant select on table "public"."profiles" to "authenticated"; + +grant trigger on table "public"."profiles" to "authenticated"; + +grant truncate on table "public"."profiles" to "authenticated"; + +grant update on table "public"."profiles" to "authenticated"; + +grant delete on table "public"."profiles" to "service_role"; + +grant insert on table "public"."profiles" to "service_role"; + +grant references on table "public"."profiles" to "service_role"; + +grant select on table "public"."profiles" to "service_role"; + +grant trigger on table "public"."profiles" to "service_role"; + +grant truncate on table "public"."profiles" to "service_role"; + +grant update on table "public"."profiles" to "service_role"; + +create policy "Public profiles are viewable by everyone." +on "public"."profiles" +as permissive +for select +to public +using (true); + + +create policy "Users can insert their own profile." +on "public"."profiles" +as permissive +for insert +to public +with check ((auth.uid() = id)); + + +create policy "Users can update own profile." +on "public"."profiles" +as permissive +for update +to public +using ((auth.uid() = id)); + + +