SlideShare una empresa de Scribd logo
1 de 27
Descargar para leer sin conexión
Patrick Crowley
  the.railsi.st
Migrating Legacy Data
flickr.com/photos/coyotejack/1975053395/
flickr.com/photos/toy_cars/34168866/
How do you migrate
  your old 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
Here’s a better way.
Step 1:
Add new db adapter
/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:
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

Más contenido relacionado

Destacado (8)

Paperclip
PaperclipPaperclip
Paperclip
 
Mobile web apps
Mobile web appsMobile web apps
Mobile web apps
 
Trucker
TruckerTrucker
Trucker
 
Compass
CompassCompass
Compass
 
CSS for iPhones
CSS for iPhonesCSS for iPhones
CSS for iPhones
 
Migrating Legacy Data (Ruby Midwest)
Migrating Legacy Data (Ruby Midwest)Migrating Legacy Data (Ruby Midwest)
Migrating Legacy Data (Ruby Midwest)
 
Compass & Bearings
Compass & BearingsCompass & Bearings
Compass & Bearings
 
Compass
CompassCompass
Compass
 

Similar a Migrating Legacy Data

Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
Yehuda Katz
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
Yehuda Katz
 
Metaprogramovanie #1
Metaprogramovanie #1Metaprogramovanie #1
Metaprogramovanie #1
Jano Suchal
 

Similar a Migrating Legacy Data (20)

DataMapper
DataMapperDataMapper
DataMapper
 
[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Where's My SQL? Designing Databases with ActiveRecord Migrations
Where's My SQL? Designing Databases with ActiveRecord MigrationsWhere's My SQL? Designing Databases with ActiveRecord Migrations
Where's My SQL? Designing Databases with ActiveRecord Migrations
 
Ruby is Awesome
Ruby is AwesomeRuby is Awesome
Ruby is Awesome
 
All I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web FrameworkAll I Need to Know I Learned by Writing My Own Web Framework
All I Need to Know I Learned by Writing My Own Web Framework
 
Sequel
SequelSequel
Sequel
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
SOLID Ruby, SOLID Rails
SOLID Ruby, SOLID RailsSOLID Ruby, SOLID Rails
SOLID Ruby, SOLID Rails
 
Blocks by Lachs Cox
Blocks by Lachs CoxBlocks by Lachs Cox
Blocks by Lachs Cox
 
Metaprogramovanie #1
Metaprogramovanie #1Metaprogramovanie #1
Metaprogramovanie #1
 
Euruko 2009 - DataObjects
Euruko 2009 - DataObjectsEuruko 2009 - DataObjects
Euruko 2009 - DataObjects
 
Dsl
DslDsl
Dsl
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introduction
 
Ruby on rails
Ruby on rails Ruby on rails
Ruby on rails
 
Say Goodbye to Procedural Programming - Nick Sutterer
Say Goodbye to Procedural Programming - Nick SuttererSay Goodbye to Procedural Programming - Nick Sutterer
Say Goodbye to Procedural Programming - Nick Sutterer
 
Kaggle Winning Solution Xgboost algorithm -- Let us learn from its author
Kaggle Winning Solution Xgboost algorithm -- Let us learn from its authorKaggle Winning Solution Xgboost algorithm -- Let us learn from its author
Kaggle Winning Solution Xgboost algorithm -- Let us learn from its author
 
NativeBoost
NativeBoostNativeBoost
NativeBoost
 
Migrating legacy data
Migrating legacy dataMigrating legacy data
Migrating legacy data
 
Asciidoctor New, Noteworthy and Beyond Devoxx-2017
Asciidoctor New, Noteworthy and Beyond Devoxx-2017Asciidoctor New, Noteworthy and Beyond Devoxx-2017
Asciidoctor New, Noteworthy and Beyond Devoxx-2017
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Migrating Legacy Data