Skip to content

Instantly share code, notes, and snippets.

@po8rewq
Created March 4, 2023 21:51
Show Gist options
  • Save po8rewq/b33b89693edc9b630b2315ae79f556d2 to your computer and use it in GitHub Desktop.
Save po8rewq/b33b89693edc9b630b2315ae79f556d2 to your computer and use it in GitHub Desktop.
Supabase initial migration
create table "public"."profiles" (
"id" uuid not null,
"email" character varying not null,
"firstname" character varying,
"lastname" character varying,
"created_at" timestamp with time zone not null default now()
);
alter table "public"."profiles" enable row level security;
CREATE UNIQUE INDEX profiles_pkey ON public.profiles USING btree (id);
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) not valid;
alter table "public"."profiles" validate constraint "profiles_id_fkey";
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, email)
values (new.id, new.email);
return new;
end;
$function$
;
CREATE TRIGGER on_auth_user_created AFTER INSERT ON auth.users FOR EACH ROW EXECUTE FUNCTION handle_new_user();
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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment