際際滷

際際滷Share a Scribd company logo
Patrick Crowley
  the.railsi.st
Migrating Legacy Data
鍖i界一姻.界看馨/沿鞄看岳看壊/界看霞看岳艶逮温界一/1975053395/
鍖i界一姻.界看馨/沿鞄看岳看壊/岳看霞喝界温姻壊/34168866/
How do you migrate
  your old data?
Migrating Legacy Data
 Dump old database
 Dump old database
 Massage text with TextMate,
 BBEdit, grep, etc.
 Dump old database
 Massage text with TextMate,
  BBEdit, grep, etc.

 Eventually give up
 Dump old database
 Massage text with TextMate,
 BBEdit, grep, etc.

 Eventually give up
 Import into new database
Heres a better way.
Step 1:
Add new db adapter
/con鍖g/database.yml
development:
  adapter: mysql
  encoding: utf8
  database: cinema_development
  username: root
  password:

...

legacy:
  adapter: mysql
  encoding: utf8
  database: cinema_legacy
  username: root
  password:
Step 2:
Add Legacy models
/app/models/legacy_base.rb
class LegacyBase < ActiveRecord::Base
  self.abstract_class = true
  establish_connection quot;legacyquot;

  def migrate
    new_record = eval(quot;#{self.class}quot;.gsub(/Legacy/,'')).new(map)
    new_record[:id] = self.id
    new_record.save
  end

end
/app/models/legacy_architect.rb
class LegacyArchitect < LegacyBase
  set_table_name quot;architectquot;

  def map
    {
      :first_name => self.name_first,
      :last_name => self.name_last,
      :description => self.desc_long
    }
  end

end
Step 3:
Add Migration helper
/lib/migration_helper.rb
def migrate(name, options={})
  # Grab custom entity label if present
  label = options.delete(:label) if options[:label]

  unless options[:helper]
    model = name.to_s.singularize.capitalize
    model.constantize.delete_all

    puts quot;Migrating #{number_of_records || quot;allquot;} #{label || name} #{quot;after #{offset_for_records}quot; if
offset_for_records}quot;
    quot;Legacy#{model}quot;.constantize.find(:all, with(options)).each do |record|
      record.migrate
    end
  else
    eval options[:helper].to_s
  end
end

def with(options={})
  {:limit => number_of_records, :offset => offset_for_records}.merge(options)
end

def number_of_records
  nil || ENV['limit'].to_i if ENV['limit'].to_i > 0
end

def offset_for_records
  nil || ENV['offset'].to_i if ENV['offset'].to_i > 0
end
Step 4:
Add Rake task
/lib/tasks/migrate.rake
require 'migration_helper'

namespace :db do

 namespace :migrate do

   desc 'Migrates legacy content'
   task :legacy => [quot;architectsquot;, quot;stylesquot;]

   desc 'Migrates architects'
   task :architects => :environment do
     migrate :architects
   end

   desc 'Migrates theaters'
   task :theaters => :environment do
     migrate :theaters
   end

  end
end
Step 5:
Start migrating
Demo
http://github.com/
mokolabs/legacy/
Special thanks
Questions?
The End
Migrating Legacy Data

More Related Content

Migrating Legacy Data