|
Red Squirrel Reflections
Dave Hoover explores the psychology of software development
|
|
Fri, 29 Sep 2006Rails Across 3 Oracle Databases Over the last couple weeks at my main client, a small revolution has begun. This medium-sized Java shop is slowly, but surely adopting Ruby and Rails. It has been exciting to be a witness to the productivity gains and the willingness of people to learn something new. (Thanks in no small part to the simplicity and power of Watir.) I'll have more stories to share about this little revolution, but first I need to tell the story of a 30-minute development episode that Tyler and I just paired on.Short version: Rails can integrate wonderfully across multiple legacy Oracle databases. Longer version: Over the course of my Rails development experience, I've (wrongly) assumed that trying to get Rails to do enterprise'y things is difficult. I'm not going to go into why I assumed this, but I would bet that there are others out there who have been scared into believing that they shouldn't try to swim against the Rails' convention of a single database with specific table-naming conventions. My assumption was blown out of the water this morning when my pair and I needed to grab data from 2 other databases for a little Rails app that we created yesterday. The app was the brain-child of our client manager who needed a way to track the status of features and bug-fixes as they progressed through QA. We use XPlanner for iteration planning and iTracker for bug tracking, so she wanted to be able to refer to an XPlanner story id and/or an iTracker issue id for each QA task. We were able to deliver this to production in about an hour (thank you Capistrano) yesterday afternoon (yes, T is approaching zero). This morning, our client said that she didn't want to see the issue and story ids, she wanted to see the name of the issue and story. For that, we needed to grab them from the XPlanner and iTracker databases. With much wringing of hands, and prepared to see our Rails productivity plummet, we Google'd around to see how to connect to multiple databases in Rails. First, we needed to configure our databases: config/database.yml production: adapter: oci username: foo password: host: FOO:5353/BAR planner: adapter: oci username: xplanner password: host: (DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=... tracker: adapter: oci username: itracker password: host: (DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=...Then we needed models to represent the issue and story. app/models/story.rb class Story < ActiveRecord::Base end app/models/issue.rb class Issue < ActiveRecord::Base endThen we needed to create the associations between a QA task and the Issue and Story. app/models/task.rb class Task < ActiveRecord::Base belongs_to :issue belongs_to :story ... endThen we needed to configure Rails to use the appropriate connection and table names for Issue and Story. config/environment.rb Story.establish_connection "planner" Story.set_table_name "story" Issue.establish_connection "tracker" Issue.set_table_name "issuebean"And this just works: task = Task.find(1) # from prod Oracle database puts task.story.name # from XPlanner Oracle database puts task.issue.description # from iTracker Oracle database |