Understanding how DB migrations are applied to test databases

Hi. I am a new Rails developer. So far my experiences with the
framework has been very positive, and I’m excited to become a part of
Rails dev community.

I’ve started working on my first Rails application, and ran into
something that confused me regarding how db migrations are applied to
test databases. What I am trying to do is create a static lookup table
for certain parameters in my database. To do this, I created two
migrations:

create_car_table.rb
populate_car_table.rb

where the populate migration simply adds an array of values to the
rows in the database. These migrations work and the database is
populated as I expect.

So my next development activity was to write a simple unit test for
this lookup table - one that verified things such as the number of
cars in the table, the number of cars of a certain color, etc. I went
and ran:

rake test:units

And my tests fail. When I examine the test database, I see the cars
table created, but not populated (i.e., it appears that the create
migration was executed, but not the populate).

Setting RAILS_ENV to test and running the migration populates the test
database, but doesn’t solve my test issue, as when I run rake
test:units, it appears to overwrite what I’ve done. I ran:

rake test:units --trace

And it I see something like this:

** Invoke test:units (first_time)
** Invoke db:test:prepare (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:clone (first_time)
** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:clone
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
** Execute test:units

I assume these commands are undoing the migrations I ran on the test
database (based on the first_time indicators as well as the clone/
purge).

So my questions are - what is the right recipe for creating and
testing lookup tables in Rails? Should I be using fixtures for this,
or is there something simple I am missing with how I should run and
test my migrations using the test database?

Thanks for any help.

On 23 Aug 2008, at 21:36, User2084 wrote:

Hi. I am a new Rails developer. So far my experiences with the
framework has been very positive, and I’m excited to become a part of
Rails dev community.

I’ve started working on my first Rails application, and ran into
something that confused me regarding how db migrations are applied to
test databases.

They’re not. The current schema (schema.rb or development.sql) is
loaded into an empty database.

So my questions are - what is the right recipe for creating and
testing lookup tables in Rails? Should I be using fixtures for this,
or is there something simple I am missing with how I should run and
test my migrations using the test database?

If you need data in your test database then fixtures are usually the
way to go.

Fred