Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Trigger and function not defined in right order using db diff #1113

Open
2 tasks done
rienheuver opened this issue May 17, 2023 · 5 comments
Open
2 tasks done

Trigger and function not defined in right order using db diff #1113

rienheuver opened this issue May 17, 2023 · 5 comments
Labels
bug Something isn't working migration

Comments

@rienheuver
Copy link

rienheuver commented May 17, 2023

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

When having a trigger on your database that calls a certain trigger and you run supabase db diff to generate a migration, it will first output the trigger and then the function. When running this code (migration), it will throw an error function (...) does not exist. The simple fix is to manually edit the output and put the trigger creation after the function creation.

To Reproduce

  1. Run the following SQL:
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();
  1. Run supabase db diff -f "add_automatic_profile_creation"
  2. Run supabase db reset
  3. See Error: ERROR: function handle_new_user() does not exist (SQLSTATE 42883)

Expected behavior

The migration should simply succeed

System information

  • OS: Ubuntu 20.04
  • Supabase CLI: 1.61.1
  • Docker version 23.0.6, build ef23cbc

Additional context

I created the function and trigger through the studio and later manipulated them through DBeaver. Can't imagine this has any effect though

@canardos
Copy link

canardos commented May 27, 2023

I can confirm this behavior, with 1.50.12 at least. It seems to be limited to triggers on tables in the auth schema.

My application has two triggers on auth.users (on on insert, one on update) and two triggers on tables in the public schema. The auth table triggers are outputted as the first two statements using supabase db diff (the functions they call are defined later), while the public table triggers are correctly outputted after their respective function are defined.

@CareTiger
Copy link

I had the same issue. I filed a ticket. this from the support engineering team -
I have made a product feedback to improve the diff tool and will present this case to engineering. Thanks for reporting this. I hope this information helps!

@sweatybridge
Copy link
Contributor

The way db diff works is by diffing schemas one by one in alphabetical order. I'm not sure if there's an easy way to fix this problem. Meanwhile you can work around it by specifying the order of schema explicitly. For eg.

supabase db diff -f public_changes -s public
supabase db diff -f auth_changes -s auth

If it helps, you can also combine them into a single migration afterwards by concatenating the generated migration files.

@muezz
Copy link

muezz commented Nov 16, 2023

I faced this with the latest version of 1.112 when I ran supabase db pull to pull down all changes from the remote instance. I am not sure if it is okay to manually modify the migration file. What is the recommended way to get around this?

@mqp
Copy link

mqp commented Dec 22, 2023

I also experienced this with db pull even though the functions in question were both in the public schema.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working migration
Projects
None yet
Development

No branches or pull requests

6 participants