Skip to content

declarative: sync command always generates a migration for a trigger that does not change in any way #5094

Closed

Description

@tolgap

Describe the bug
I'm using the pre-release version with the pgmq changes. The declarative generate now works correctly. The sync command also no longer crashes.

But when using sync without changing anything, always seems to try and generate a migration for a trigger that simply does not change at all.

To Reproduce
Steps to reproduce the behavior:

  1. Run supabase db schema declarative generate --local
  2. Run supabase db schema declarative sync
  3. Observe that it tries to generate a migration for a trigger that already exists in the DB 1to1

Expected behavior

Given a migration file with:

# migrations/01_initial.sql
create trigger "trg_update_priority_scores"
before insert or update of visitors_total_count, start_date, background_images, priority_override_score
on "my_schema"."tournaments"
for each row
execute function "my_schema"."update_priority_scores"();

And a schema file:

# database/schema/my_schema/tables/tournaments.sql
CREATE TRIGGER trg_update_priority_scores
  BEFORE INSERT OR UPDATE OF visitors_total_count, start_date, background_images, priority_override_score ON my_schema.tournaments
  FOR EACH ROW
  EXECUTE FUNCTION my_schema.update_priority_scores();

I would have expected nothing to be generated.

But instead
The declarative commands generates only this migration:

# migrations/20260416140754_declarative_sync.sql
CREATE OR REPLACE TRIGGER trg_update_priority_scores BEFORE INSERT OR UPDATE OF visitors_total_count, start_date, background_images, priority_override_score ON my_schema.tournaments FOR EACH ROW EXECUTE FUNCTION my_schema.update_priority_scores();

When I

  • apply this generated migration to my db
  • rerun the sync command again
  • it, again, generates a migration with the same exact trigger

Screenshots
If applicable, add screenshots to help explain your problem.

System information
Rerun the failing command with --create-ticket flag.

  • Version of OS: MacOS 26.4.1
  • Version of CLI: v2.91.2
  • Version of Docker: v4.69.0
  • Versions of services: [output from supabase services command]
   SERVICE IMAGE          | LOCAL                  | LINKED 
  ------------------------|------------------------|--------
   supabase/postgres      | 15.8.1.085             | -      
   supabase/gotrue        | v2.188.1               | -      
   postgrest/postgrest    | v14.8                  | -      
   supabase/realtime      | v2.82.0                | -      
   supabase/storage-api   | v1.48.28               | -      
   supabase/edge-runtime  | v1.73.3                | -      
   supabase/studio        | 2026.04.08-sha-205cbe7 | -      
   supabase/postgres-meta | v0.96.4                | -      
   supabase/logflare      | 1.37.1                 | -      
   supabase/supavisor     | 2.7.4                  | -      

Activity

  1. avallete commented on Apr 16, 2026

    @avallete
    Member

    Hey ! Thank's for reporting ! I will have a look.

    That might be due to a missing normalization in the definition of the trigger 🤔
    I'm curious, what happen if you replace your migrations/01_initial.sql definition with the one produced by the output (CREATE OR REPLACE TRIGGER trg_update_priority_scores BEFORE INSERT OR UPDATE OF visitors_total_count, start_date, background_images, priority_override_score ON my_schema.tournaments FOR EACH ROW EXECUTE FUNCTION my_schema.update_priority_scores();)

    Does it still generate diff's ?

  2. tolgap commented on Apr 16, 2026

    @tolgap
    Author

    Hey @avallete

    If I replace the trigger in 01_initial.sql, and then run declarative generate and then declarative sync, it still generates the same exact trigger in the diff in a <timestamp>_declarative_sync.sql.

  3. avallete commented on Apr 17, 2026

    @avallete
    Member

    Hey there !

    I've tried to reproduce with this MRE:

    create schema if not exists my_schema;
    
    create table if not exists my_schema.tournaments (
      id bigint generated always as identity primary key,
      visitors_total_count integer,
      start_date timestamptz,
      background_images jsonb,
      priority_override_score numeric
    );
    
    create or replace function my_schema.update_priority_scores()
    returns trigger
    language plpgsql
    as $$
    begin
      return new;
    end;
    $$;
    
    create trigger trg_update_priority_scores
    before insert or update of visitors_total_count, start_date, background_images, priority_override_score
    on my_schema.tournaments
    for each row
    execute function my_schema.update_priority_scores();
    

    But, doing so, I can't get the same issue. So I'm suspecting that something else causes this change to always appears.
    What happen if you do npx supabase@latest db schema declarative sync --no-cache ? I'm thinking the issue could be due to some catalog caching persisting instead of being replaced.

  4. tolgap commented on Apr 17, 2026

    @tolgap
    Author

    @avallete

    I downloaded the latest supabase CLI (v2.92.1)

    Here's the lines:

    $> supabase db schema declarative generate --experimental --local
    Creating shadow database...
    Applying declarative schemas via pg-delta...
    Applied 1625 statements in 1 round(s).
    Declarative schema written to supabase/database
    $> supabase db schema declarative sync --experimental --no-cache 
    Creating shadow database...
    Initialising schema...
    Seeding globals from roles.sql...
    
    <hundreds of lines of migrations applying>
    
    Creating shadow database...
    Applying declarative schemas via pg-delta...
    Applied 1625 statements in 1 round(s).
    Generated migration SQL:
    CREATE OR REPLACE TRIGGER trg_update_priority_scores BEFORE INSERT OR UPDATE OF visitors_total_count, start_date, background_images, priority_override_score ON my_schema.tournaments FOR EACH ROW EXECUTE FUNCTION my_schema.update_priority_scores();
                                                                                                                                                                                                                                                                                 
    Enter a name for this migration (press Enter to keep 'declarative_sync')

    So the --no-cache didn't help out.

  5. avallete commented on Apr 17, 2026

    @avallete
    Member

    Hey @tolgap!

    Thanks for the answer. Sadly, I still can’t reproduce the issue with the MRE you shared. Would you be able to put together one that reproduces the bug and share it somewhere I can access?

    Otherwise, would you mind sharing your full supabase/migrations and supabase/database folders so I can try to reproduce it on my end? If you’d prefer not to share it publicly, you can add me to a private repo and I’ll take a look.

  6. tolgap commented on Apr 20, 2026

    @tolgap
    Author

    @avallete I cannot add you to our codebase. And I do not want to make any of our migrations/schema public. If possible, I could zip our supabase/ folder and email it to you? If you could (cryptically) provide me your email address.

    Otherwise you can email me directly: <firstname> at <companyname without app suffix>.nl

    PS: I've spent quite some time to create an MRE, but there are a LOT of migrations and tables. At some point, removing the clutter around it, the problem just stops happening but its definitely unrelated clutter. It would still be a gigantic MRE.

  7. avallete commented on Apr 20, 2026

    @avallete
    Member

    @avallete I cannot add you to our codebase. And I do not want to make any of our migrations/schema public. If possible, I could zip our supabase/ folder and email it to you? If you could (cryptically) provide me your email address.

    Otherwise you can email me directly: <firstname> at <companyname without app suffix>.nl

    PS: I've spent quite some time to create an MRE, but there are a LOT of migrations and tables. At some point, removing the clutter around it, the problem just stops happening but its definitely unrelated clutter. It would still be a gigantic MRE.

    No worries about the MRE what you’re describing (the issue disappearing when removing seemingly unrelated parts) usually points to some subtle interaction, so having the full setup will actually help more.

    Thanks for taking the time to dig into it, I've emailed you on your GitHub contact email 👍

  8. avallete commented on Apr 21, 2026

    @avallete
    Member

    Hey there !

    Quick update, I've been able to reproduce and identify the root cause of the issue on my end. Working on a fix right now.

  9. tolgap commented on Apr 21, 2026

    @tolgap
    Author

    That is great to hear @avallete . Thank you so much!

  10. added a commit that references this issue on Apr 22, 2026
    270efa9
  11. tolgap commented on Apr 24, 2026

    @tolgap
    Author

    @avallete I have tried CLI version v2.93.1 and v2.95.0 but these are failing to generate any diff at all. I just get a generic error:

    $> supabase db schema declarative generate --experimental --local --debug
    
    Creating shadow database...
    Applying declarative schemas via pg-delta...
    pg-delta apply result:
    {
      "status": "error",
      "totalStatements": 1633,
      "totalRounds": 1,
      "totalApplied": 1633,
      "totalSkipped": 0,
      "errors": [],
      "stuckStatements": []
    }
    2026/04/24 11:00:48 HTTP POST: https://eu.i.posthog.com/batch/
    pg-delta declarative apply failed with status: error

    It does generate the files, but running sync errors in the same way without generating any diff. Even if I change a function body in the migrations I had sent you:

    (changed some random function body in schema/database/**/functions/my_function.sql:

    $>  supabase db schema declarative sync --experimental --debug
    
    Creating shadow database...
    Applying declarative schemas via pg-delta...
    pg-delta apply result:
    {
      "status": "error",
      "totalStatements": 1633,
      "totalRounds": 1,
      "totalApplied": 1633,
      "totalSkipped": 0,
      "errors": [],
      "stuckStatements": []
    }
    pg-delta declarative apply failed with status: error

    There's no additional info in the debug/error.txt

    You could try it out with the same exact migrations I had sent you. I'm not sure if it is a new issue or related to this.

  12. avallete commented on Apr 24, 2026

    @avallete
    Member

    @avallete I have tried CLI version v2.93.1 and v2.95.0 but these are failing to generate any diff at all. I just get a generic error:

    $> supabase db schema declarative generate --experimental --local --debug

    Creating shadow database...
    Applying declarative schemas via pg-delta...
    pg-delta apply result:
    {
    "status": "error",
    "totalStatements": 1633,
    "totalRounds": 1,
    "totalApplied": 1633,
    "totalSkipped": 0,
    "errors": [],
    "stuckStatements": []
    }
    2026/04/24 11:00:48 HTTP POST: https://eu.i.posthog.com/batch/
    pg-delta declarative apply failed with status: error
    It does generate the files, but running sync errors in the same way without generating any diff. Even if I change a function body in the migrations I had sent you:

    (changed some random function body in schema/database/**/functions/my_function.sql:

    $> supabase db schema declarative sync --experimental --debug

    Creating shadow database...
    Applying declarative schemas via pg-delta...
    pg-delta apply result:
    {
    "status": "error",
    "totalStatements": 1633,
    "totalRounds": 1,
    "totalApplied": 1633,
    "totalSkipped": 0,
    "errors": [],
    "stuckStatements": []
    }
    pg-delta declarative apply failed with status: error
    There's no additional info in the debug/error.txt

    You could try it out with the same exact migrations I had sent you. I'm not sure if it is a new issue or related to this.

    Hey @tolgap, thank's for closing the loop. I can't reproduce on my end with your use case. However, your schema only generate 1625 statements on my end while it does 1633 on yours.

    I wonder if the issue might be due to invalid sql in your declarative schema. It seems like the validation errors for function were swallowed from the output (got a PR here to fix it: #5120)

    That would explain why the statement get applied (a function body containing an invalid SQL will still be applied with set check_function_body=false it will only fail at runtime. And at the end of the apply we run one pass to check all the functions bodies (which would explain why it fail at this point).

    If you updated the function boddy with an additional valid statement (such as SELECT 1;) do you still face the issue ?
    Would you be willing to send me the actual function body you are changing so I can try and reproduce on my end ?

  13. added 2 commits that reference this issue on Apr 24, 2026
    8c9553c
    3596a05
  14. tolgap commented on Apr 28, 2026

    @tolgap
    Author

    Hey @avallete

    With your recent fix from #5120, I just updated my CLI to v2.95.4 and I see the issue. We've had recently done a ALTER TABLE ... RENAME TO ... but one of the function bodies still referenced the old table name.

    So yeah it was definitely an error on our end, but now we can actually see what the error is 🙂.

  15. added 3 commits that reference this issue on Apr 28, 2026
    3c14581
    85a93a6
    be971cd
  16. added a commit that references this issue on May 15, 2026
    6923f5d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions