Best way to (quickly) reset between tests/after all tests?

Are there alternatives to npx supabase db reset if I want to reset my database to the seeded state for local testing? I'd imagine transactions could perhaps solve this, but I can't use those with the supabase-js client. If I run supabase db reset, then I get the functionality I want, but the entire Docker stack is reset, which takes quite a long time (comparitively, for simple testing). I can manually track the changes I think I'm making, and then try and invert those changes and use the service_role key to delete/un-update etc, but this doubles the amount of code to write, and potentially requires specific code in tests that I'd rather avoid... What's the best practice here?
3 Replies
inder
inder2w ago
I use these commands First force shut down the containers. Docker will forcefully delete the containers and not wait for any cleanups inside containers.
docker ps --format={{.Names}} | grep supabase | xargs -L1 docker rm -f
docker ps --format={{.Names}} | grep supabase | xargs -L1 docker rm -f
Then delete the volumes
docker volume ls --format={{.Name}} | grep supabase | xargs -L1 docker volume rm
docker volume ls --format={{.Name}} | grep supabase | xargs -L1 docker volume rm
And now you can run supabase start again You can further speed up start process by only running the services you use
silentworks
silentworks2w ago
I never reset the db on tests, just make sure the data you are inserting is random enough per test. I was reseting the db after each test like you until I spoke to a QA person and they told me they don't reset databases per test as that would be too slow (even outside of Supabase), they just randomise the test data enough for each test. You can see an example here https://github.com/silentworks/supabase-by-example/blob/main/nextjs/tests/user-profile-flow.spec.ts, for internal database functions tests I use transactions in my pgTap tests https://github.com/silentworks/supabase-by-example/blob/main/nextjs/supabase/tests/database/profiles.test.sql
chrisb2244
chrisb2244OP2w ago
That's a great set of examples, thank you!

Did you find this page helpful?