Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rake in Rails: should I be using db:reset?

I'm a little confused by the intended use of the default Rails Rake tasks, and would like advice on whether I should be using db:reset or writing a custom Rake task. Nothing clever, just daily housekeeping, and I may well be missing an obvious doc as I'm new to Rails.

My problem: I want to throw away my database and run from a completely clean setup, in order that I can be sure the database contains known data only. This is useful for demo prep, for debugging, and for making sure Jenkins is comparing like-with-like in tests.

Currently, I'm writing this:

bin/rake db:drop:all db:create:all db:migrate db:seed db:test:prepare

This is a lot to type, but leaves seed data only in both dev and test databases. I am unsure how this differs from db:reset, which would be more convenient to type.

Should I use db:reset or write a custom db:from_scratch task?

like image 802
David Kennedy Avatar asked Dec 20 '11 10:12

David Kennedy


People also ask

What does rake db Reset do?

rake db:migrate - Runs the migrations which haven't been run yet. rake db:reset - Clears the database (presumably does a rake db:drop + rake db:create + rake db:migrate ) and runs migration on a fresh database.

What does rake db migrate do?

A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined.

How do I get rid of rake db migrate?

rake db:migrate RAILS_ENV=test - Run migrations in the given environment. rake db:migrate:redo - Roll back one migration and run it again. rake db:migrate:redo STEP=n - Roll back the last n migrations and run them again.


2 Answers

You should be using:

rake db:reset

This will drop the database, recreate it and load the current schema into it.

For a full list of rake db tasks:

rake --describe db

If your requirements change then it would be better to write a custom rake task, where you can apply your own customisation.

like image 69
Phil Bottomley Avatar answered Sep 22 '22 09:09

Phil Bottomley


If you're not sure what a rake task does, then I would suggest not using it. In this case, you're probably ok, however db:reset is not the equivalent to what you are doing above. db:reset recreates the database from scheme.rb, this may be different as you could have written migrations that have not yet been run.

I would suggest that you use a custom rake task, you can then modify it to fit your exact purposes, especially if you want to perform other tasks as well (for example tagging in git)

like image 32
Yule Avatar answered Sep 19 '22 09:09

Yule