-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(apps/chat-with-pdf): apply migration to user role and userId field
- Loading branch information
Showing
2 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
apps/chat-with-pdf/supabase/migrations/20241102160258_authentication-and-roles.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
create extension if not exists "moddatetime" with schema "extensions"; | ||
|
||
|
||
create extension if not exists "vector" with schema "public" version '0.7.0'; | ||
|
||
drop policy "enabled for all" on "public"."DocumentSections"; | ||
|
||
alter table "public"."Chat" add column "suggestedQuestions" text[]; | ||
|
||
alter table "public"."Chat" add column "userId" uuid; | ||
|
||
update "public"."Chat" set "userId" = auth.uid(); | ||
|
||
alter table "public"."Chat" enable row level security; | ||
|
||
alter table "public"."Document" add column "userId" uuid; | ||
|
||
update "public"."Document" set "userId" = auth.uid(); | ||
|
||
alter table "public"."Document" enable row level security; | ||
|
||
alter table "public"."DocumentSections" add column "userId" uuid; | ||
|
||
update "public"."DocumentSections" set "userId" = auth.uid(); | ||
|
||
alter table "public"."DocumentSections" enable row level security; | ||
|
||
alter table "public"."Feedback" add column "userId" uuid; | ||
|
||
update "public"."Feedback" set "userId" = auth.uid(); | ||
|
||
alter table "public"."Feedback" enable row level security; | ||
|
||
do $$ | ||
begin | ||
if exists ( | ||
select 1 from pg_roles where rolname = 'supabase_functions_admin' | ||
) then | ||
grant delete on table "public"."DocumentSections" to "supabase_functions_admin"; | ||
grant insert on table "public"."DocumentSections" to "supabase_functions_admin"; | ||
grant references on table "public"."DocumentSections" to "supabase_functions_admin"; | ||
grant select on table "public"."DocumentSections" to "supabase_functions_admin"; | ||
grant trigger on table "public"."DocumentSections" to "supabase_functions_admin"; | ||
grant truncate on table "public"."DocumentSections" to "supabase_functions_admin"; | ||
grant update on table "public"."DocumentSections" to "supabase_functions_admin"; | ||
end if; | ||
end $$; | ||
|
||
create policy "Authenticated users can delete a chat." | ||
on "public"."Chat" | ||
as permissive | ||
for delete | ||
to authenticated | ||
using ((( SELECT auth.uid() AS uid) = "userId")); | ||
|
||
|
||
create policy "Authenticated users can insert a chat." | ||
on "public"."Chat" | ||
as permissive | ||
for insert | ||
to authenticated | ||
with check ((( SELECT auth.uid() AS uid) = "userId")); | ||
|
||
|
||
create policy "Authenticated users can update a chat." | ||
on "public"."Chat" | ||
as permissive | ||
for update | ||
to authenticated | ||
using ((( SELECT auth.uid() AS uid) = "userId")) | ||
with check ((( SELECT auth.uid() AS uid) = "userId")); | ||
|
||
|
||
create policy "Authenticated users can view their own chats." | ||
on "public"."Chat" | ||
as permissive | ||
for select | ||
to public | ||
using ((( SELECT auth.uid() AS uid) = "userId")); | ||
|
||
|
||
create policy "Authenticated users can delete a chat." | ||
on "public"."Document" | ||
as permissive | ||
for delete | ||
to authenticated | ||
using ((( SELECT auth.uid() AS uid) = "userId")); | ||
|
||
|
||
create policy "Authenticated users can insert a chat." | ||
on "public"."Document" | ||
as permissive | ||
for insert | ||
to authenticated | ||
with check ((( SELECT auth.uid() AS uid) = "userId")); | ||
|
||
|
||
create policy "Authenticated users can update a chat." | ||
on "public"."Document" | ||
as permissive | ||
for update | ||
to authenticated | ||
using ((( SELECT auth.uid() AS uid) = "userId")) | ||
with check ((( SELECT auth.uid() AS uid) = "userId")); | ||
|
||
|
||
create policy "Authenticated users can view their own chats." | ||
on "public"."Document" | ||
as permissive | ||
for select | ||
to public | ||
using ((( SELECT auth.uid() AS uid) = "userId")); | ||
|
||
|
||
create policy "Authenticated users can delete a chat." | ||
on "public"."DocumentSections" | ||
as permissive | ||
for delete | ||
to authenticated | ||
using ((( SELECT auth.uid() AS uid) = "userId")); | ||
|
||
|
||
create policy "Authenticated users can insert a chat." | ||
on "public"."DocumentSections" | ||
as permissive | ||
for insert | ||
to authenticated | ||
with check ((( SELECT auth.uid() AS uid) = "userId")); | ||
|
||
|
||
create policy "Authenticated users can update a chat." | ||
on "public"."DocumentSections" | ||
as permissive | ||
for update | ||
to authenticated | ||
using ((( SELECT auth.uid() AS uid) = "userId")) | ||
with check ((( SELECT auth.uid() AS uid) = "userId")); | ||
|
||
|
||
create policy "Authenticated users can view their own chats." | ||
on "public"."DocumentSections" | ||
as permissive | ||
for select | ||
to public | ||
using ((( SELECT auth.uid() AS uid) = "userId")); | ||
|
||
|
||
CREATE TRIGGER handle_updated_at BEFORE UPDATE ON public."Chat" FOR EACH ROW EXECUTE FUNCTION moddatetime('updatedAt'); | ||
|
||
CREATE TRIGGER handle_updated_at BEFORE UPDATE ON public."Document" FOR EACH ROW EXECUTE FUNCTION moddatetime('updatedAt'); | ||
|
||
CREATE TRIGGER handle_updated_at BEFORE UPDATE ON public."DocumentSections" FOR EACH ROW EXECUTE FUNCTION moddatetime('updatedAt'); | ||
|
||
|
22 changes: 22 additions & 0 deletions
22
apps/chat-with-pdf/supabase/migrations/20241102160420_set-userid-default-uuid.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
-- add the createdAt and updatedAt columns to the DocumentSections table | ||
alter table "public"."DocumentSections" add column "createdAt" timestamp with time zone not null default now(); | ||
alter table "public"."DocumentSections" add column "updatedAt" timestamp with time zone not null default now(); | ||
|
||
-- Update existing null records with a default UUID to replace it with the auth.uid() | ||
UPDATE "public"."Chat" SET "userId" = '00000000-0000-0000-0000-000000000000' WHERE "userId" IS NULL; | ||
UPDATE "public"."Document" SET "userId" = '00000000-0000-0000-0000-000000000000' WHERE "userId" IS NULL; | ||
UPDATE "public"."DocumentSections" SET "userId" = '00000000-0000-0000-0000-000000000000' WHERE "userId" IS NULL; | ||
UPDATE "public"."Feedback" SET "userId" = '00000000-0000-0000-0000-000000000000' WHERE "userId" IS NULL; | ||
|
||
-- Set the default value and make it not null | ||
alter table "public"."Chat" alter column "userId" set default auth.uid(); | ||
alter table "public"."Chat" alter column "userId" set not null; | ||
|
||
alter table "public"."Document" alter column "userId" set default auth.uid(); | ||
alter table "public"."Document" alter column "userId" set not null; | ||
|
||
alter table "public"."DocumentSections" alter column "userId" set default auth.uid(); | ||
alter table "public"."DocumentSections" alter column "userId" set not null; | ||
|
||
alter table "public"."Feedback" alter column "userId" set default auth.uid(); | ||
alter table "public"."Feedback" alter column "userId" set not null; |