Migrations & Unit Testing: Possible Bug in Rails?


#1

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?


#2

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


#3

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.


#4

Hi !

2005/12/27, Krishna K. 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 !


#5

Francois B. wrote:

Hope that helps !

Awesome. That solves my problem. Thanks!