# Migrations & Unit Testing: Possible Bug in Rails?

**URL:** <https://www.ruby-forum.com/t/migrations-unit-testing-possible-bug-in-rails/51178>\
**Category:** Rails\
**Created:** [December 27, 2005, 7:23pm UTC](https://www.ruby-forum.com/t/migrations-unit-testing-possible-bug-in-rails/51178 "2005-12-27T19:23:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krishna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/71c47a/32.png) [@krishna](https://www.ruby-forum.com/u/krishna)\
**Post date:** [December 27, 2005, 7:23pm UTC](https://www.ruby-forum.com/t/migrations-unit-testing-possible-bug-in-rails/51178/1 "2005-12-27T19:23:00Z")

</div>

I recently started using migrations to create the schema of a Rails  
project I am working on. The database I am using for this project is  
MySQL. The initial migration creates the table and also includes a  
number of execute statements (to do some things like setting primary  
keys, autoincrement on some columns and other messy voodoo).

Running “rake migrate” creates the database with no problems and  
everything is happy in my world!

However things go wrong when I run “rake test\_units”. Basically the test  
database is not recreated properly. I delved into the testing.rake and  
databases.rake files to see what is going on, and this is my  
understanding:

1. “rake test\_units” recreates the test database by running the  
db/schema.rb file.
2. However this file will NOT include any “execute” actions, thus  
resulting in a different database structure in development and test  
environments.

In my case the above behaviour causes some of my unit tests to fail. But  
if I explicitly do a “rake clone\_structure\_to\_test” and then explicitly  
run individual unit tests, the tests all pass with flying colours.

I think the test database also needs to be generated by applying all the  
migration steps, and that the current behaviour is buggy. Of course  
there might be a really good reason for the behaviour which I am not  
getting at all. Thoughts, comments, suggestions?

---

<div class="post-metadata">

**Author:** ![krishna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/71c47a/32.png) [@krishna](https://www.ruby-forum.com/u/krishna)\
**Post date:** [December 27, 2005, 7:57pm UTC](https://www.ruby-forum.com/t/migrations-unit-testing-possible-bug-in-rails/51178/2 "2005-12-27T19:57:30Z")

</div>

The database is only cloned when you run “rake” or “rake  
clone\_structure\_to\_test”. That’s a gotcha I ran into a while back as  
well…just remember that when you run “rake test\_units” you’ll need  
to manually clone the db first, or just run “rake” to run all your  
unit and functional tests.

Pat

---

<div class="post-metadata">

**Author:** ![krishna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/71c47a/32.png) [@krishna](https://www.ruby-forum.com/u/krishna)\
**Post date:** [December 27, 2005, 8:05pm UTC](https://www.ruby-forum.com/t/migrations-unit-testing-possible-bug-in-rails/51178/3 "2005-12-27T20:05:21Z")

</div>

Pat M. wrote:

> when you run “rake test\_units” you’ll need to manually clone the db first

The problem with this is that the “rake test\_units” task will nuke the  
existing structure of the test database, by executing the db/schema.rb  
file (because my initial migration creates the database structure from  
scratch). So running “rake clone\_structure\_to\_test” before hand wont  
fix the problem.

---

<div class="post-metadata">

**Author:** ![krishna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/71c47a/32.png) [@krishna](https://www.ruby-forum.com/u/krishna)\
**Post date:** [December 27, 2005, 8:21pm UTC](https://www.ruby-forum.com/t/migrations-unit-testing-possible-bug-in-rails/51178/4 "2005-12-27T20:21:36Z")

</div>

Hi !

2005/12/27, Krishna K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 1. “rake test\_units” recreates the test database by running the  
> db/schema.rb file.
> 2. However this file will NOT include any “execute” actions, thus  
> resulting in a different database structure in development and test  
> environments.

Please investigate the config.active\_record.schema\_format option.  
Comment this line and you should be fine.

# Use Active Record’s schema dumper instead of SQL when creating the

test database

# (enables use of different database adapters for development and test

environments)  
config.active\_record.schema\_format = :ruby

If you set :ruby, ActiveRecord will generate the schema.rb file. If  
instead you leave it off, ActiveRecord will do a structure dump (show  
create table \*), dumping to development.sql. Rails will then load  
that schema back as your test database. Going that route will ensure  
that any primary keys, indexes and constraints will stay active.

Hope that helps !

---

<div class="post-metadata">

**Author:** ![krishna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/71c47a/32.png) [@krishna](https://www.ruby-forum.com/u/krishna)\
**Post date:** [December 27, 2005, 8:55pm UTC](https://www.ruby-forum.com/t/migrations-unit-testing-possible-bug-in-rails/51178/5 "2005-12-27T20:55:34Z")

</div>

Francois B. wrote:

> Hope that helps !

Awesome. That solves my problem. Thanks!
