# Understanding how DB migrations are applied to test databases

**URL:** <https://www.ruby-forum.com/t/understanding-how-db-migrations-are-applied-to-test-databases/146674>\
**Category:** Rails\
**Created:** [August 23, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/understanding-how-db-migrations-are-applied-to-test-databases/146674 "2008-08-23T22:40:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![User2084](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/5e9695/32.png) [@User2084](https://www.ruby-forum.com/u/User2084)\
**Post date:** [August 23, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/understanding-how-db-migrations-are-applied-to-test-databases/146674/1 "2008-08-23T22:40:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![User2084](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/5e9695/32.png) [@User2084](https://www.ruby-forum.com/u/User2084)\
**Post date:** [August 23, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/understanding-how-db-migrations-are-applied-to-test-databases/146674/2 "2008-08-23T22:44:19Z")

</div>

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
