tisdag, juni 05, 2007

Testing with JRuby on Rails and ActiveRecord-JDBC

This will be a highly uninflammatory blog post, as contrast to the last one. Specifically, there is a slight problem when running the command
jruby -S rake
in a a JRuby on Rails-application. This problem is caused by some hard coded values in the database Rake definitions for Rails. But don't despair, there is a simple solution to this. It's not as simple as it should be (invisible) but it's easy enough. Provided you have JRUBY_HOME set and your version of AR-JDBC is 0.3.1, execute this command from your Rails application root
cp $JRUBY_HOME/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/tasks/jdbc_databases.rake lib/tasks
Since the hard coded values are hard to override, the jdbc_databases.rake file just hacks Rake to be able to redefine tasks and then redefines the core tasks. This shouldn't affect a bi-Ruby installation, since the overriding only happens on JRuby, not on MRI. If someone has a better way to do this, please tell me. =)

3 kommentarer:

Ryan H sa...

i couldn't verify this. i went to copy the file and the tasks directory was non-existent. here's what the inside of the gem i got after an update looks like:

$ jruby -S gem contents activerecord-jdbc
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/History.txt
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/Manifest.txt
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/README.txt
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/Rakefile
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/LICENSE
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/active_record/connection_adapters/jdbc_adapter.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/active_record/connection_adapters/jdbc_adapter_spec.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/jdbc_adapter/jdbc_db2.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/jdbc_adapter/jdbc_derby.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/jdbc_adapter/jdbc_firebird.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/jdbc_adapter/jdbc_hsqldb.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/jdbc_adapter/jdbc_mimer.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/jdbc_adapter/jdbc_mssql.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/jdbc_adapter/jdbc_mysql.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/jdbc_adapter/jdbc_oracle.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/jdbc_adapter/jdbc_postgre.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/jdbc_adapter/missing_functionality_helper.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/jdbc_adapter.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/lib/jdbc_adapter_internal.jar
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/activerecord/connection_adapters/type_conversion_test.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/activerecord/connections/native_jdbc_mysql/connection.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/db/derby.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/db/h2.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/db/hsqldb.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/db/jndi_config.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/db/logger.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/db/mysql.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/db/postgres.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/derby_simple_test.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/h2_simple_test.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/hsqldb_simple_test.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/jdbc_common.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/jndi_test.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/manualTestDatabase.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/minirunit/testConnect.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/minirunit/testH2.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/minirunit/testHsqldb.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/minirunit/testLoadActiveRecord.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/minirunit/testMysql.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/minirunit/testRawSelect.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/minirunit.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/models/auto_id.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/models/data_types.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/models/entry.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/mysql_simple_test.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/postgres_simple_test.rb
/usr/local/jruby-1.0.0RC3/lib/ruby/gems/1.8/gems/ActiveRecord-JDBC-0.3.1/test/simple.rb

Unknown sa...

Well, a better way would be not to have to do this at all :-) .... I assume that you have submitted a patch for the rails project so that this hack will be unnecessary in the next version of rails?

Matt Stine sa...

Ola,

Matt Stine again from book review land....I hit this snag in Chapter 4 (page 51). Thanks for posting this entry to correct the problem.