JRuby and ActiveRecord: Schema Dump

11 10 2006

Want to dump a legacy database to a schema.rb file from a JDBC connection? Yeah, me neither, but I have to. Since ActiveRecord is working, I can just use the SchemaDumper, right? That’s basically true, but, as of JRuby 0.9.0, I need a few additional tweaks.

For starters, the JDBC adapter doesn’t implement an indexes method. Since I’m not worried about capturing the index information right now, I just added this hack:

module ActiveRecord
  module ConnectionAdapters
    class JdbcAdapter
      def indexes(table);[];end

This is just for unit testing for now, so the missing index information isn’t a problem. If you need this information, you’ll probably have to write your own indexes implementation. I think it can probably be added to the JdbcSpec for the specific database you’re supporting, but I haven’t actually tried it myself. I was thinking about trying that this weekend, so I may have some code for that next week.

The other problem I have is that JRuby 0.9.0 has a bug in the StringIO#puts method. Calling puts without an argument does not add a new line character to the stream. The result is a schema.rb file that doesn’t parse. My schema is over 1800 lines long, so I’m not about to try adding all the new lines myself. I filed a bug report and uploaded a patch here.

With a my indexes hack and a patched JRuby, my schema seems to be generating. Now to see if it will load…




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: