benc
benc

Reputation: 2018

What does rake db:test:prepare actually do?

I am following the rails tutorial videos and I can't figure out what the db:test:prepare command actually does. Can someone provide an explanation?

Upvotes: 133

Views: 87339

Answers (3)

Kevin Bedell
Kevin Bedell

Reputation: 13414

Specifically, rake db:test:prepare will do the following:

  • Check for pending migrations and,
  • load the test schema

That is, it will look your db/schema.rb file to determine if any migrations that exist in your project that have not been run. Assuming there are no outstanding migrations, it will then empty the database and reload it based on the contents of the db/schema.rb file.

Upvotes: 43

Albert Català
Albert Català

Reputation: 2044

rake db:test:prepare is a good solution for PG issues like this.

“PG::UndefinedTable: ERROR: relation does not exist” with a correct Rails naming and convention" where I couldn't just execute rake db:migrate RAILS_ENV=production

When, for example you can't create test database for a bug discussed here: "PG undefinedtable error relation users does not exist"

All arround this error "PG::UndefinedTable: ERROR: relation xxxxx does not exist”

Upvotes: 4

Richard Brown
Richard Brown

Reputation: 11444

The rake db:migrate above runs any pending migrations on the development environment and updates db/schema.rb. The rake db:test:load recreates the test database from the current db/schema.rb. On subsequent attempts, it is a good idea to first run db:test:prepare, as it first checks for pending migrations and warns you appropriately.

-- http://guides.rubyonrails.org/testing.html

Basically it handles cloning the database so you don't have to run the migrations against test to update the test database.

Upvotes: 125

Related Questions