feat: add create_user_onboarding_function (#1383)

https://github.com/StanGirard/quivr/issues/1375

![Screenshot 2023-10-10 at 16 28
04](https://github.com/StanGirard/quivr/assets/63923024/07b8838f-74bc-408a-86ce-481e1224a8e4)
This commit is contained in:
Mamadou DICKO 2023-10-10 17:21:05 +02:00 committed by GitHub
parent 58ea481910
commit 0477cce971
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 2 deletions

View File

@ -0,0 +1,30 @@
-- Create the function to add user_id to the onboardings table
CREATE OR REPLACE FUNCTION public.create_user_onboarding() RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.onboardings (user_id)
VALUES (NEW.id);
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY definer;
-- Revoke all on function handle_new_user_onboarding() from PUBLIC;
REVOKE ALL ON FUNCTION create_user_onboarding() FROM PUBLIC;
-- Drop the trigger if it exists
DROP TRIGGER IF EXISTS create_user_onboarding_trigger ON auth.users;
-- Create the trigger on the insert into the auth.users table
CREATE TRIGGER create_user_onboarding_trigger
AFTER INSERT ON auth.users
FOR EACH ROW
EXECUTE FUNCTION public.create_user_onboarding();
-- Update migrations table
INSERT INTO migrations (name)
SELECT '20231010120000_add_create_user_onboarding_function'
WHERE NOT EXISTS (
SELECT 1
FROM migrations
WHERE name = '20231010120000_add_create_user_onboarding_function'
);

View File

@ -255,6 +255,26 @@ CREATE TABLE IF NOT EXISTS knowledge_vectors (
PRIMARY KEY (knowledge_id, vector_id, embedding_model)
);
-- Create the function to add user_id to the onboardings table
CREATE OR REPLACE FUNCTION public.create_user_onboarding() RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.onboardings (user_id)
VALUES (NEW.id);
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY definer;
-- Revoke all on function handle_new_user_onboarding() from PUBLIC;
REVOKE ALL ON FUNCTION create_user_onboarding() FROM PUBLIC;
-- Drop the trigger if it exists
DROP TRIGGER IF EXISTS create_user_onboarding_trigger ON auth.users;
-- Create the trigger on the insert into the auth.users table
CREATE TRIGGER create_user_onboarding_trigger
AFTER INSERT ON auth.users
FOR EACH ROW
EXECUTE FUNCTION public.create_user_onboarding();
-- Create the onboarding table
CREATE TABLE IF NOT EXISTS onboardings (
@ -280,9 +300,9 @@ CREATE POLICY "Access Quivr Storage 1jccrwz_2" ON storage.objects FOR UPDATE TO
CREATE POLICY "Access Quivr Storage 1jccrwz_3" ON storage.objects FOR DELETE TO anon USING (bucket_id = 'quivr');
INSERT INTO migrations (name)
SELECT '20231005170000_add_onboarding_a_to_onboarding_table'
SELECT '20231010120000_add_create_user_onboarding_function'
WHERE NOT EXISTS (
SELECT 1 FROM migrations WHERE name = '20231005170000_add_onboarding_a_to_onboarding_table'
SELECT 1 FROM migrations WHERE name = '20231010120000_add_create_user_onboarding_function'
);