Migrating Legacy Data

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    3 Favorites

    Migrating Legacy Data - Presentation Transcript

    1. Patrick Crowley the.railsi.st
    2. Migrating Legacy Data
    3. flickr.com/photos/coyotejack/1975053395/
    4. flickr.com/photos/toy_cars/34168866/
    5. How do you migrate your old data?
    6. • Dump old database
    7. • Dump old database • Massage text with TextMate, BBEdit, grep, etc.
    8. • Dump old database • Massage text with TextMate, BBEdit, grep, etc. • Eventually give up
    9. • Dump old database • Massage text with TextMate, BBEdit, grep, etc. • Eventually give up • Import into new database
    10. Here’s a better way.
    11. Step 1: Add new db adapter
    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. Step 2: Add Legacy models
    14. /app/models/legacy_base.rb class LegacyBase < ActiveRecord::Base self.abstract_class = true establish_connection \"legacy\" def migrate new_record = eval(\"#{self.class}\".gsub(/Legacy/,'')).new(map) new_record[:id] = self.id new_record.save end end
    15. /app/models/legacy_architect.rb class LegacyArchitect < LegacyBase set_table_name \"architect\" def map { :first_name => self.name_first, :last_name => self.name_last, :description => self.desc_long } end end
    16. Step 3: Add Migration helper
    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 \"Migrating #{number_of_records || \"all\"} #{label || name} #{\"after #{offset_for_records}\" if offset_for_records}\" \"Legacy#{model}\".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. Step 4: Add Rake task
    19. /lib/tasks/migrate.rake require 'migration_helper' namespace :db do namespace :migrate do desc 'Migrates legacy content' task :legacy => [\"architects\", \"styles\"] desc 'Migrates architects' task :architects => :environment do migrate :architects end desc 'Migrates theaters' task :theaters => :environment do migrate :theaters end end end
    20. Step 5: Start migrating
    21. Demo
    22. http://github.com/ mokolabs/legacy/
    23. Special thanks
    24. Questions?
    25. The End

    + Patrick CrowleyPatrick Crowley, 9 months ago

    custom

    822 views, 3 favs, 0 embeds more stats

    This talk describes a nice technique for migrating more

    More info about this document

    CC Attribution License

    Go to text version

    • Total Views 822
      • 822 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 3
    • Downloads 15
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories