Fresh 0.9.2 activerecord-jdbc-adapter Release
Posted by Nick Sieger Mon, 12 Oct 2009 18:30:45 GMT
As announced last week on the jruby-user list, 0.9.2, the latest
activerecord-jdbc-adapter release, has been pushed out. Please install the gem in the usual fashion and try it out on your applications.
One of the most contentious bugs broke
db:drop, ruining the quick-start workflow that Rails is known for. To fix this, a creative solution was needed that ended up bringing some nice benefits.
The problem stemmed from the fact that some database setup tasks in Rails 2.3 no longer load the environment; instead they just load the configuration data and work with that. This means that there is no easy way to hook into Rails and override those tasks, which is what activerecord-jdbc needs to do.
The solution I ended up with was to create a
jdbc Rails generator that inserts a couple files into your rails application that inject the JDBC logic into ActiveRecord and the database rake tasks. To wit:
$ jruby script/generate jdbc exists config/initializers create config/initializers/jdbc.rb exists lib/tasks create lib/tasks/jdbc.rake
The upside of this new technique is that now that we have a way to ensure the JDBC adapter is properly injected into Rails, and you no longer need to use adapter names like
jdbcsqlite3 and the like. The net result is that database.yml no longer needs to be modified for the default Rails databases (
postgresql). So while we introduced one additional step in the process to bootstrap a Rails application under JRuby, the removal the step where
database.yml needs to be modified results in a more predictable workflow.
The new JRuby-specific Rails workflow looks like the following, assuming you’ve installed the
activerecord-jdbc-adapter gem into JRuby, along with the appropriate database driver gem (e.g,
jdbc-mysql). (Of course, the gems only need to be installed once per JRuby installation.)
- Create your Rails application as usual.
- Run the
jdbcgenerator as shown above.
Additionally, if you maintain a Rails application template that you use to start a new application, you can simply add
generate(:jdbc) to that template’s script.
What’s more, the presence of the JDBC files in your application are guarded and only inject JDBC support when running under JRuby, so you can safely keep them around when running Rails under multiple Ruby implementations.