Search code examples
node.jsnpmdbmigrate

how to drop and re-run migrations with db-migrate


I am trying to seed the db before I run all my tests. So I have created a new file bootstrapTest.js and included it in my test source file like this gulp.src(['test/bootstrapTest.js', 'test/**/*.js']

bootstrapTest.js now needs to reset the database and re-run the migrations and seed the data. So I create a child process and let it run a shell script (seed.sh).

`const exec = require('child_process').exec;
exec('./seed.sh', 
    {env: {database__host: 'localhost', database__user: 'username', 
    database__port: 3306, database__password: 'password'}}, function 
    (error, stdout, stderr) { ...
}`

and this is how my seed.sh looks like

#!/bin/bash ./node_modules/.bin/db-migrate reset --env test echo '## Database truncated' ./node_modules/.bin/db-migrate up --env test echo '## Database migrated'

In the output on the console, I only see the echo output and not the commands that are run. I have validated that indeed db-migrate is not running.

Any idea why db-migrate is not running? When I run this manually from my zsh, it works fine.


Solution

  • Figured out a better way to achieve the same result. Instead of running these via shell script, I added them in my package.json script and run that script before starting the test. So my package.json looks like this "scripts": { "testdb": "db-migrate reset --env test && db-migrate up --env test", "test": "npm run testdb && ./node_modules/.bin/gulp" }