Is there a way to automatically have a "rake db:migrate RAILS_ENV=test" after "rake db:migrate" in development environment?

Viewed 15914

Is there a way to automatically do a rake db:migrate RAILS_ENV=test after each rake db:migrate when in development environment?

I have guard and guard-rspec running, and I am really annoyed about the failing tests, even if it works manually in the browser.

It costs me at least 15 minutes every time I had a pause from development, to figure out that I simply forgot to call rake db:migrate:test after the change of the database.

Since I am already using guard I thought about adding guard-rake to the project also, but I dont know which file I should watch. When watching development.sqlite3, rake db:migrate RAILS_ENV=test would be fired every time I do something with my records through the browser, so this is not really what I want.

Can someone help me with my problem?

7 Answers

If you feel like having this as a one-liner that just works, you can have it as:

echo "alias rake-migrate='rake db:migrate && rake db:migrate RAILS_ENV=test'" >> ~/.zshrc && source ~/.zshrc

and for the rollback there is:

echo "alias rake-rollback='rake db:rollback && rake db:rollback RAILS_ENV=test'" >> ~/.zshrc && source ~/.zshrc

then, just run in the terminal

rake-migrate

or

rake-rollback

note: this will add an alias to the ~/.zshrc file. note 2:: if you do this, say terminal1, and use another terminal tab, or vscode terminal, then just run source ~/.zshrc on the terminal you want to run it

Related