cli icon indicating copy to clipboard operation
cli copied to clipboard

Add hooks to cli commands

Open osaxma opened this issue 2 years ago • 12 comments

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 avatar Oct 08 '22 15:10 osaxma

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

sweatybridge avatar Oct 14 '22 05:10 sweatybridge

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

NixBiks avatar Jan 11 '23 22:01 NixBiks

@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

ccssmnn avatar Jan 24 '23 20:01 ccssmnn

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

mosnicholas avatar Mar 07 '23 16:03 mosnicholas

@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

isaacharrisholt avatar Mar 16 '23 16:03 isaacharrisholt

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

mosnicholas avatar Mar 16 '23 16:03 mosnicholas

@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');

isaacharrisholt avatar Mar 16 '23 18:03 isaacharrisholt

Any updates here? :)

ydennisy avatar Jun 27 '23 15:06 ydennisy

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

psteinroe avatar Dec 13 '23 15:12 psteinroe

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.

logemann avatar Jan 15 '24 09:01 logemann

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.

lauri865 avatar Feb 06 '24 11:02 lauri865