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

Add hooks to cli commands #501

Open
osaxma opened this issue Oct 8, 2022 · 12 comments
Open

Add hooks to cli commands #501

osaxma opened this issue Oct 8, 2022 · 12 comments
Labels
enhancement New feature or request

Comments

@osaxma
Copy link
Contributor

osaxma commented Oct 8, 2022

Feature request

Is your feature request related to a problem? Please describe.

Currently, when I would like to run a command before or after certain supabase cli commands, I've to run that manually or write a script to run both commands.

Describe the solution you'd like

It would be useful if we could hook commands to supabase cli commands. One idea comes to mind is to define them within the config.toml such as:

[hooks]

post_start='psql postgresql://postgres:postgres@localhost:54322/postgres < custom_seed.sql'
post_start='dart run generate_types.dart'

post_db_reset='dart run generate_types.dart'

pre_stop='pg_dump postgresql://postgres:postgres@localhost:54322/postgres -n public > dumps/schema_dump.sql' 

excuse my poor toml -- I'm sure this could be structured in a better way for sub-commands (e.g. supabase db reset).

In the above example, the idea is that each lifecycle command (e.g. stop, start, reset) can have pre- or post-hook(s).

@osaxma osaxma added the enhancement New feature or request label Oct 8, 2022
@sweatybridge
Copy link
Contributor

sweatybridge commented Oct 14, 2022

Also related is #160 which asks for pre/post-migration sql files.

@NixBiks
Copy link
Contributor

NixBiks commented Jan 11, 2023

Would be great with an example where a seed.ts file is executed after resetting the db. I always run into TS config issues with imports etc. I imagine I'm not the only one

@ccssmnn
Copy link

ccssmnn commented Jan 24, 2023

@NixBiks as you might have Deno set up in your workspace for functions anyway, using Deno for the seed script is quite comfortable.

supabase start && deno run --allow-read=. --allow-net scripts/seed.ts

@mosnicholas
Copy link
Contributor

would love to know if there have been more thoughts on this topic? Particularly for seed files. My seed.sql is getting really large, would be very helpful to be able to split it up a bit and simplify

@isaacharrisholt
Copy link

@mosnicholas we ran into this problem too, so we just wrote a little script that'll read CSVs from a supabase/seeds directory and create a load of insert statements in the seeds.sql file.

You can also have pre-seed.sql and post-seed.sql for running any other SQL you need (extensions etc.).

If you name files 1-foo.csv, 2-bar.csv, the numbering will be respected (and 10 will come after 9, not 1... thanks ascii...).

Code is here: https://gist.github.com/isaacharrisholt/8b92d21429e2981827795c5e29763a5f

@mosnicholas
Copy link
Contributor

oh wow nice idea :) when / how are you running it @isaacharrisholt ?

@isaacharrisholt
Copy link

isaacharrisholt commented Mar 16, 2023

@mosnicholas we run it using a pre-commit hook with the following:

bash -c 'cd scripts/csv-seeder; go run main.go ../../supabase'

But you could also do it in CI and push the changes to the branch, like you might do with supabase gen types.

One thing you have to be careful with is that it'll create insert statements exactly how you've written the CSVs - it won't add quotes or anything. This is handy though, because you can call postgres functions. If the following file is user.csv:

id,uuid,date,name
1,gen_random_uuid(),now(),'Isaac'

Then it'll generate:

INSERT INTO user (id, uuid, date, name)
VALUES (1, gen_random_uuid(), now(), 'Isaac');

@ydennisy
Copy link

Any updates here? :)

@psteinroe
Copy link

would love a pre-migrations hook to run externally managed migrations e.g. for graphile/worker.

@logemann
Copy link

This feature req would also be the best way to seed files into storage right? I really would like to have some files already present on starting local dev.

@lauri865
Copy link

lauri865 commented Feb 6, 2024

This would be great. We already have custom scripts for e.g. seeding storage, and now looking to run idempotent migrations for some stuff like webhooks, and so fort. Would be great to pipe them into the CLI workflows, as custom scripts can be easy to forgotten / accidentially bypassed in the team.

@logemann
Copy link

This would be great. We already have custom scripts for e.g. seeding storage

how do you approached seeding storage? I really would like to see a feature lfrom supabase ike seed-storage.yml where we just define the files together with the fileURL and map them to the buckets created in seed.sql. This way we could db reset everything and have a full running DB with storage again...

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

10 participants