Is there a way to call the CLI from Typescript? I'd like to reset the local database in tests
Is your feature request related to a problem? Please describe. I'm defining my database in migrations. I don't want to use pgTAP for testing it though, so I'm writing my tests in vitest in Typescript. I need to reset the database to the clean, seeded state after each test suite, because my tests are inserting, updating, and deleting all over the place. I'd like to simply reset the database after each suite, or even better, after every test.
Describe the solution you'd like
await supabase.db.reset() - and it does the same thing as running supabase db reset in the terminal.
Describe alternatives you've considered My alternative is to meticulously clean up manually after each test by reverting its updates and deleting its inserts after my assertions, or designing the tests to never use the seeded data, but the different test suites are conflicting with each other when running in parallel.
Currently the only way this is possible is to use the child_process module to call cli. I quite like the interface you suggested so we could consider adding a typescript wrapper for cli that you can import in tests.
Cc @soedirgo
@sweatybridge Would you happen to have an example of calling the cli from child_process? Trying it and it can't seem to connect to Docker:
Error: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running
Much appreciated!
For anyone coming across this later, here's an example of how to reset the database using child_process, in this case, in Playwright's globalSetup.
import util from 'util';
import { exec } from 'child_process';
const execAsync = util.promisify(exec);
async function globalSetup() {
const command = '/usr/local/bin/supabase db reset';
try {
const { stdout, stderr } = await execAsync(command, { cwd: '[ ABSOLUTE PATH TO YOUR PROJECT ]' });
console.log('stdout:', stdout);
console.error('stderr:', stderr);
} catch (error) {
console.error('exec error:', error);
}
}
export default globalSetup;
For groups of tests that don't require a full reset, I use a database function to reset certain tables, and call it via rpc.