AbhinavD
AbhinavD

Reputation: 7292

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.

Upvotes: 0

Views: 15522

Answers (2)

RIYAJ KHAN
RIYAJ KHAN

Reputation: 15290

Assuming,you are running node with npm start.

One can do db migration using npm hooks.

"scripts" : {
    "prestart":"db-migrate reset --env test && db-migrate up --env test",
    "start" : "node server.js",
    "poststart" :"echo 'do after server run'"
  },

In short,

$. npm start //command fired on terminal

//output
  1. npm preinstall //first to run

  2. npm start //now this

When you do,npm start,before it prestart script will be executed and then start script.

Upvotes: 1

AbhinavD
AbhinavD

Reputation: 7292

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" }

Upvotes: 2

Related Questions