Loading the schema for Active Record tests

116 views
Skip to first unread message

Jon Leighton

unread,
Sep 7, 2008, 8:58:32 AM9/7/08
to Ruby on Rails: Core
Apologies if this is blatantly obvious and I am wasting your time -
I'm quite an infrequent Rails contributor...

Basically I am looking for an easy way to load the schema definitions
from activerecord/test/schema into my databases. In the
RUNNING_UNIT_TESTS file it says "When you have the database online,
you can import the fixture tables with the test/schema/*.sql files."
This seems to be outdated as all the files in test/schema are now .rb
schema files.

I can't see a rake task to perform this - maybe there should be one?

I'd be happy to create a patch to make this a bit easier for others in
the future.

Cheers,
Jon

Michael Koziarski

unread,
Sep 7, 2008, 4:37:26 PM9/7/08
to rubyonra...@googlegroups.com
On Sun, Sep 7, 2008 at 2:58 PM, Jon Leighton <j...@jonathanleighton.com> wrote:
>
> Apologies if this is blatantly obvious and I am wasting your time -
> I'm quite an infrequent Rails contributor...
>
> Basically I am looking for an easy way to load the schema definitions
> from activerecord/test/schema into my databases. In the
> RUNNING_UNIT_TESTS file it says "When you have the database online,
> you can import the fixture tables with the test/schema/*.sql files."
> This seems to be outdated as all the files in test/schema are now .rb
> schema files.
>
> I can't see a rake task to perform this - maybe there should be one?

There are a few:

rake build_frontbase_databases # Build the FrontBase test databases
rake build_mysql_databases # Build the MySQL test databases
rake build_postgresql_databases # Build the PostgreSQL test databases
rake drop_mysql_databases # Drop the MySQL test databases
rake drop_postgresql_databases # Drop the PostgreSQL test databases
rake rebuild_frontbase_databases # Rebuild the FrontBase test databases
rake rebuild_mysql_databases # Rebuild the MySQL test databases
rake rebuild_postgresql_databases # Rebuild the PostgreSQL test databases

I guess the advice in that file should be updated to reflect the rake
tasks we have now.

> I'd be happy to create a patch to make this a bit easier for others in
> the future.
>
> Cheers,
> Jon
>
> >
>

--
Cheers

Koz

Josh Susser

unread,
Sep 7, 2008, 5:53:05 PM9/7/08
to rubyonra...@googlegroups.com


If you run the whole test suite via rake test_mysql, it happens
automatically using a dirty trick. If you want to do it manually, you
need to run the file aaa_create_tables_test.rb - the 'aaa' forces the
file to run before all the other tests, ensuring the schema is loaded.


In directory rails/activerecord:

$ ruby -Ilib:test:test/connections/native_mysql test/cases/
aaa_create_tables_test.rb
Using native MySQL
Loaded suite test/cases/aaa_create_tables_test
Started
.-- adapter_name()
-> 0.0000s
-- create_table(:accounts, {:force=>true})
-> 0.0076s
# lots of lines omitted...
-- create_table(:binary_fields, {:options=>"CHARACTER SET
latin1", :force=>true})
-> 0.0074s
.
Finished in 1.067615 seconds.

2 tests, 2 assertions, 0 failures, 0 errors

If you use some other db than mysql, just change the -I option to use
the correct connection file.

RUNNING_UNIT_TESTS needs updating. I was going to do that at one
point but got distracted. If you have time to do a doc patch, I'll +1
it :-)


--
Josh Susser
http://blog.hasmanythrough.com


Jon Leighton

unread,
Sep 8, 2008, 3:55:43 AM9/8/08
to Ruby on Rails: Core
On Sep 7, 10:53 pm, Josh Susser <j...@hasmanythrough.com> wrote:
> On Sep 7, 2008, at 5:58 AM, Jon Leighton wrote:
> If you run the whole test suite via rake test_mysql, it happens  
> automatically using a dirty trick.  If you want to do it manually, you  
> need to run the file aaa_create_tables_test.rb - the 'aaa' forces the  
> file to run before all the other tests, ensuring the schema is loaded.

Thanks.

I will certainly create a doc patch for RUNNING_UNIT_TESTS, but what
do people thing about doing away with the aaa_create_table_test.rb
file? Instead we could check for the existence of one of the table we
need in helper.rb, and if it's not there we could load the schema.
This would enable the schema to be autoloaded even when running single
test files.

Jon

Michael Koziarski

unread,
Sep 8, 2008, 5:08:20 PM9/8/08
to rubyonra...@googlegroups.com

If you can figure out a nice solution that supports this that would be
fantastic. The only catch I can see to the test helper approach is that
you could be repeatedly testing for the existence or otherwise of those
tables. It's hardly likely to be a performance issue, but it's a little
messy all the same. If you need any help tidying this up, just let us know.


--
Cheers,

Koz

Reply all
Reply to author
Forward
0 new messages