Migrating Legacy Data

1,075 views
1,042 views

Published on

This talk describes a nice technique for migrating legacy data into a Rails app.

Code samples are here:
http://github.com/mokolabs/legacy/tree/master

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,075
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
61
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Migrating Legacy Data

  1. 1. Patrick Crowley the.railsi.st
  2. 2. Migrating Legacy Data
  3. 3. flickr.com/photos/coyotejack/1975053395/
  4. 4. flickr.com/photos/toy_cars/34168866/
  5. 5. How do you migrate your old data?
  6. 6. • Dump old database
  7. 7. • Dump old database • Massage text with TextMate, BBEdit, grep, etc.
  8. 8. • Dump old database • Massage text with TextMate, BBEdit, grep, etc. • Eventually give up
  9. 9. • Dump old database • Massage text with TextMate, BBEdit, grep, etc. • Eventually give up • Import into new database
  10. 10. Here’s a better way.
  11. 11. Step 1: Add new db adapter
  12. 12. /config/database.yml development: adapter: mysql encoding: utf8 database: cinema_development username: root password: ... legacy: adapter: mysql encoding: utf8 database: cinema_legacy username: root password:
  13. 13. Step 2: Add Legacy models
  14. 14. /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
  15. 15. /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
  16. 16. Step 3: Add Migration helper
  17. 17. /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
  18. 18. Step 4: Add Rake task
  19. 19. /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
  20. 20. Step 5: Start migrating
  21. 21. Demo
  22. 22. http://github.com/ mokolabs/legacy/
  23. 23. Special thanks
  24. 24. Questions?
  25. 25. The End

×