generate-supabase-db-types-github-action icon indicating copy to clipboard operation
generate-supabase-db-types-github-action copied to clipboard

Use `repository_dispatch` to trigger rebuilt on ALTER TABLE

Open thorwebdev opened this issue 3 years ago • 1 comments

Just saw that you can use an API request to trigger a GitHub action, example: https://dev.to/rikurouvila/how-to-trigger-a-github-action-with-an-htt-request-545

So rather than a cron job, you could use an EVENT TRIGGER to kick off the GitHub action anytime a table is altered:

CREATE FUNCTION public.dispatch_github_action()
  RETURNS event_trigger
  LANGUAGE plpgsql
  AS $$
BEGIN
  PERFORM
    net.http_post(
        url:='https://api.github.com/repos/<username>/<repo>/dispatches',
        body:='{"event_type": "world"}'::jsonb,
        headers:='{"Content-type":"application/json","authorization":"Bearer <TOKEN>"}'::jsonb
    );
END;
$$;

CREATE EVENT TRIGGER dispatch_github_action ON ddl_command_end 
      WHEN TAG IN ('ALTER TABLE') 
      EXECUTE FUNCTION public.dispatch_github_action();

Alternatively, you could also set up something like husky https://dev.to/devictoribero/how-to-use-husky-to-create-pre-commit-and-pre-push-hooks-4448 to run the type-gen locally before committing/pushing to GitHub.

thorwebdev avatar Apr 12 '22 08:04 thorwebdev

Added some docs on the branch update-docs-for-event-trigger based on the comment above, however, I wasn't able to get the script working as intended successfully despite enabling pg net in the extensions.

Encountered the following error image

If anyone else can get the DB trigger to work successfully, feel free to create a PR based off the branch above and add to the docs accordingly.

lyqht avatar Sep 30 '22 07:09 lyqht