Rails 2.0: XML Data Type and DB2

[2008/04/30] Update: The ibm_db gem has been updated to support the new Rails 2.0 style migration. Now you can just use t.xml and it will work. Look here for more info. Thanks to Mario Briggs for pointing me to the update.



In my new job we work with XML data natively stored on DB2. I have done some test with Rails and DB2 a few weeks ago and it’s pretty interesting, specially the easiness that Rails deals with XML.

We decided to test some new features of DB2 9.5 and I decided to test them with Rails 2.0.

So I created a new Rails app, configured the database.yml to use the idm_db gem and fired:

$ rake db:create

The output is nice but, the database isn’t there! I suppose it’s something with the gem that needs to be updated. But really don’t know about it. It just doesn’t work. But this will not hold us down. I will just create the database by hand and keep going with my tests.

After that, I created a new model XmlDocument, which has a column named data, of type XML. According to the new migrations syntax, my model’s migration would look something similar with this:

class CreateXmlDocuments < ActiveRecord::Migration
def self.up
create_table :xml_documents do |t|
t.xml :data
t.timestamps
end
end


def self.down
drop_table :xml_documents
end
end

Right? Ok, I fired up a terminal and lauched:

$ rake db:migrate

The output? Here it is:

== 3 CreateXmlDocuments: migrating ============================================
-- create_table(:xml_documents)
rake aborted!
undefined method `xml' for #


Yes, a big nice exception! The new migration syntax doesn’t allow you to do t.xml! But please don’t ask me why! :p
The solution? Well, although it’s weird - imo - , it’s also easy. You can mix the new and the old syntax in the same file. So our model’s migration will now look like this:

class CreateXmlDocuments < ActiveRecord::Migration
def self.up
create_table :xml_documents do |t|
t.column :data, :xml
t.timestamps
end
end


def self.down
drop_table :xml_documents
end
end

And that’s it! We’re ready to go! I didn’t find anything else different so far, besides what I described here. Hope this helps!

Comments