Forum: Ruby on Rails Migrations & Unit Testing: Possible Bug in Rails?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
753440b19b08d0609448f91d53738b20?d=identicon&s=25 Krishna Kotecha (krishna)
on 2005-12-27 19:23
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?
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2005-12-27 19:57
(Received via mailing list)
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
753440b19b08d0609448f91d53738b20?d=identicon&s=25 Krishna Kotecha (krishna)
on 2005-12-27 20:05
Pat Maddox 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.
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 Francois Beausoleil (Guest)
on 2005-12-27 20:21
(Received via mailing list)
Hi !

2005/12/27, Krishna Kotecha <krishna.kotecha@gmail.com>:
> 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 !
753440b19b08d0609448f91d53738b20?d=identicon&s=25 Krishna Kotecha (krishna)
on 2005-12-27 20:55
Francois Beausoleil wrote:

> Hope that helps !

Awesome. That solves my problem. Thanks!
This topic is locked and can not be replied to.