In part I I quickly explained what I had to do, my limitations and a potential solution to connect to a legacy database.
In this post I’ll try to go through setting up a plugin for migration and start using RSpec for developing the migration plugin.
What we want is to migrate sites using the legacy application to our new Rails application. That means that new users won’t be be migrated. It therefore makes sense not to add the migrating logic to the main application but to create a plugin. (if you are not familiar with Rails plugins check this blog post from Geoffrey Grosenbach about plugins)
Let’s create our plugin
./script/generate plugin legacy_migration
Rails should have generated something like that:
legacy_migration
|-- init.rb
|-- install.rb
|-- uninstall.rb
|-- Rakefile
|-- README
|-- lib/
| |-- legacy_migration.rb
|-- tasks/
| |-- legacy_migration_tasks.rake
|-- test
| |-- legacy_migration_test.rb
Since we are going to use RSpec, we can remove the test folder and create a spec folder. In our spec folder, let’s add some subfolders to organize our files. Let’s create a fixtures folder to hold, a helpers folder, a migrate folder (we’ll use that to migrate our legacy database) and finally, a models folder.
Our plugin folder should look more or less like that:
legacy_migration
|-- init.rb
|-- install.rb
|-- uninstall.rb
|-- Rakefile
|-- README
|-- lib/
| |-- legacy_migration.rb
|-- tasks/
| |-- legacy_migration_tasks.rake
|-- spec
| |-- fixtures
| |-- helpers
| |-- migrate
| |-- models
Great, let’s get started and let’s create our first spec. We should probably start by migrating users so I’ll create a new spec in the spec/models folder called legacy_user_spec.rb and add the following code:
require File.dirname(FILE) + '/../helpers/legacy_user_helper'
require File.dirname(FILE) + '/../helpers/spec_helper'
describe "a connection to the legacy application" do
setup do
@connection_status = LEGACY.connect
end
end
it "should be connected to the legacy database" do
@connection_status.current_database.should == ActiveRecord::Base.configurations'legacy'
end
Note that I’m using RSpec trunk/edge and I use “describe” instead of “context” and “it” instead of “specify”. For more information on how to run RSpec edge with TextMate read this previous post.
If we look at the code above, we start by requiring 2 helpers, a general helper called spec_helper and a helper just for this spec called legacy_user_helper (we will obviously need to create them otherwise our spec will failed).
Then we start our first spec by describing a connection to the legacy application and we specify that it should be connected to the legacy database.
Here is our setup code:
setup do
@connection_status = LEGACY.connect
end
What I want is to retrieve a connection status after I connect to our legacy application. To manage the connection to the legacy application we will create a LEGACY module. We will need to connect to many legacy applications/sites and our module should help us doing that.
Then we can read that our spec checks that we are connected to the legacy database.
it "should be connected to the legacy database" do
@connection_status.current_database.should == ActiveRecord::Base.configurations'legacy'
end
That means we want to compare the connection status to the ‘legacy’ environment defined in the database.yml file.
We now need to get this spec to pass.
Let’s get started by adding a legacy environment to our database.yml and creating our LEGACY module in our legacy_migration.rb file.
Add the following to your database.yml
legacy:
adapter: mysql
database: legacy
username: root
password:
host: localhost
Now, let’s create the LEGACY module in our legacy_migration.rb file
module LEGACY
# Connect to a Legacy database.
# Usage:
# Manual connection: LEGACY.connect(:database => 'legacy-database', :adapter => 'mysql', :username => 'root', :password => '', :host => 'localhost')
# Auto connection to the database.yml defined legacy DB: LEGACY.connect
# Connection to any database available in database.yml LEGACY.connect('legacy_test')
# Connect a class to a specific database: LEGACY.connect(LegacyInstaller, ActiveRecord::Base.configurations['legacy_installer'])
def self.connect(spec = nil, opt_env = nil)
case spec
# Automatically connect to the legacy environment database defined in the database.yml
when nil
raise 'Legacy Database not defined' unless defined? ActiveRecord::Base.configurations['legacy']
LegacyActiveRecord.establish_connection(ActiveRecord::Base.configurations['legacy'])
# Return the connections status
LegacyActiveRecord.connection
# A connection's name from the database.yml can be passed
when Symbol, String
if configuration = ActiveRecord::Base.configurations[spec.to_s]
LegacyActiveRecord.establish_connection(configuration)
else
raise "#{spec} database is not configured"
end
# Connect a Model to a specific Database
when Class
raise 'Environment connection not provided or nil' unless defined? opt_env || opt_env['database'] == nil
if spec.connection.current_database == opt_env['database']
spec.connection
else
spec.establish_connection(opt_env)
end
# An array can be passed to establish the connection
else
spec = spec.symbolize_keys
unless spec.key?(:adapter) then raise "database configuration does not specify adapter" end
adapter_method = "#{spec[:adapter]}_connection"
LegacyActiveRecord.establish_connection(spec)
end
end
end
There we go, we have a really cool connect function, we can easily connect to the default legacy environment defined in the database.yml file, we can specify the connection settings, connect to another environment database and even connect one specific class/model to a specific environment. (that will useful since we have many databases). If we wanted to follow the TDD rules, I shouldn’t have written so much code… as a matter of fact, when I worked on this project I did not, but since I don’t have much time, I won’t go through the re-factoring steps.
One thing you might have noticed is that we establish a connection between LegacyActiveRecord and the legacy database. (instead of connecting ActiveRecord to the legacy database).
LegacyActiveRecord.establish_connection(ActiveRecord::Base.configurations['legacy'])
The problem is that we didn’t create the LegacyActiveRecord model yet. Let’s do that right away. Let’s add a new folder called models in our lib folder. In the models folder, let’s create a legacy_active_record.rb file and add the following code:
class LegacyActiveRecord < ActiveRecord::Base
self.abstract_class = true
end
Cool, now let’s have fun with our new module, fire the console (./script/console) and try LEGACY.connect
Here is what we get back:
>> LEGACY.connect
=> #nil, :database=>”legacy”, :allow_concurrency=>false, :host=>”localhost”, :username=>”root”, :adapter=>”mysql”}, @connection_options=[“localhost”, “root”, ””, “legacy”, nil, nil], [...]
let’s try to get the database we connected to:
>>LEGACY.connect.current_database
=> “legacy”
Awesome, we can now get our LegacyActiveRecord model to connect to our legacy database and return the connections status. Let’s run our specs……….. they pass.
Sweet, we setup our migration plugin, got our first spec written, added the code needed to get the spec to pass, I think we are done with PART 2