migrating legacy data (ruby midwest)

60
Migrating Legacy Data

Upload: patrick-crowley

Post on 16-May-2015

1.912 views

Category:

Technology


0 download

DESCRIPTION

Slides from talk on legacy data migration. Includes introduction of Trucker gem and covers common migration issues. This talk was given by Patrick Crowley and Rob Kaufman at RubyMidwest 2010 in Kansas City, MO.

TRANSCRIPT

Page 1: Migrating Legacy Data (Ruby Midwest)

Migrating Legacy Data

Page 2: Migrating Legacy Data (Ruby Midwest)

Patrick CrowleyRob Kaufman

Page 3: Migrating Legacy Data (Ruby Midwest)

(We’re from SD Ruby)

Page 4: Migrating Legacy Data (Ruby Midwest)

Let’s talk about migrating

Page 5: Migrating Legacy Data (Ruby Midwest)

Suck => Less Suck

Page 6: Migrating Legacy Data (Ruby Midwest)

PHP => Rails

Page 7: Migrating Legacy Data (Ruby Midwest)

Perl => Rails

Page 8: Migrating Legacy Data (Ruby Midwest)

Java => Rails

Page 9: Migrating Legacy Data (Ruby Midwest)

Rails => Rails

Page 10: Migrating Legacy Data (Ruby Midwest)

Migrating = pain

Page 11: Migrating Legacy Data (Ruby Midwest)

• Develop new site

• Migrate data

• Deploy new site

Page 12: Migrating Legacy Data (Ruby Midwest)

Migrate data

Page 13: Migrating Legacy Data (Ruby Midwest)

• Dump database

• Drop some tables

• Rename attributes

• Tweak data by hand

• Import data into new app

• Pray things are okay

Page 14: Migrating Legacy Data (Ruby Midwest)

Sucks, right?

Page 15: Migrating Legacy Data (Ruby Midwest)

• Labor intensive

• Error prone

• One way trip

Page 16: Migrating Legacy Data (Ruby Midwest)

Trucker

Page 17: Migrating Legacy Data (Ruby Midwest)

Migrate legacy data(with less suck)

Page 18: Migrating Legacy Data (Ruby Midwest)

• Migrate data from day one

• Improve data over time

• Launch with confidence

Page 19: Migrating Legacy Data (Ruby Midwest)

rake +legacy classes +migration helper

Page 20: Migrating Legacy Data (Ruby Midwest)

Let’s get started

Page 21: Migrating Legacy Data (Ruby Midwest)

sudo gem install trucker

Page 22: Migrating Legacy Data (Ruby Midwest)

config.gem “trucker”

Page 23: Migrating Legacy Data (Ruby Midwest)

script/generate truck

Page 24: Migrating Legacy Data (Ruby Midwest)

$ script/generate truck create app/models/legacy create app/models/legacy/legacy_base.rb exists lib/tasks create lib/tasks/legacy.rake insert added legacy adapter to end of database.yml insert added new load path to environment.rb

Page 25: Migrating Legacy Data (Ruby Midwest)

Rails::Initializer.run do |config| config.load_paths += %W( #{RAILS_ROOT}/app/models/legacy )end

config/environment.rb

Page 26: Migrating Legacy Data (Ruby Midwest)

legacy: adapter: mysql encoding: utf8 database: trucker_legacy username: root password:

config/database.yml

Page 27: Migrating Legacy Data (Ruby Midwest)

class LegacyBase < ActiveRecord::Base self.abstract_class = true establish_connection "legacy" def migrate new_record = self.class.to_s.gsub(/Legacy/,'::').constantize.new(map) new_record[:id] = self.id new_record.save end

app/models/legacy/legacy_base.rb

Page 28: Migrating Legacy Data (Ruby Midwest)

class LegacyPost < LegacyBase set_table_name "blog_posts"

def map { :name => self.headline.squish, :body => self.body.squish } end

end

app/models/legacy/legacy_post.rb

Page 29: Migrating Legacy Data (Ruby Midwest)

class LegacyPost < LegacyBase set_table_name "blog_posts"

def map { :name => tweak(self.headline.squish), :body => self.body.squish } end def tweak(name) name.capitalize.gsub(/teh/, "the") end

app/models/legacy/legacy_post.rb

Page 30: Migrating Legacy Data (Ruby Midwest)

namespace :db do namespace :migrate do

desc 'Migrates posts' task :posts => :environment do Trucker.migrate :posts end

endend

lib/tasks/legacy.rake

Page 31: Migrating Legacy Data (Ruby Midwest)

Let’s do some migrating.

Page 32: Migrating Legacy Data (Ruby Midwest)

Don’t forget to import your legacy database!

Page 33: Migrating Legacy Data (Ruby Midwest)

$ rake db:migrate:posts

Migrating all posts (1/10)Migrating all posts (2/10)Migrating all posts (3/10)Migrating all posts (4/10)Migrating all posts (5/10)Migrating all posts (6/10)Migrating all posts (7/10)Migrating all posts (8/10)Migrating all posts (9/10)Migrating all posts (10/10)

Page 34: Migrating Legacy Data (Ruby Midwest)

$ rake db:migrate:posts limit=5

Migrating 5 posts (1/10)Migrating 5 posts (2/10)Migrating 5 posts (3/10)Migrating 5 posts (4/10)Migrating 5 posts (5/10)

Page 35: Migrating Legacy Data (Ruby Midwest)

$ rake db:migrate:posts limit=5 offset=5

Migrating 5 posts after 5 (6/10)Migrating 5 posts after 5 (7/10)Migrating 5 posts after 5 (8/10)Migrating 5 posts after 5 (9/10)Migrating 5 posts after 5 (10/10)

Page 36: Migrating Legacy Data (Ruby Midwest)

DEMO

Page 37: Migrating Legacy Data (Ruby Midwest)

Use helper method for custom migrations

Page 38: Migrating Legacy Data (Ruby Midwest)

namespace :db do namespace :migrate do

desc 'Migrate pain_in_the_ass model' task :pain_in_the_ass => :environment do Trucker.migrate :pain_in_the_ass, :helper => pain_in_the_ass_migration end

endend

def pain_in_the_ass_migration # Custom code goes hereend

Page 39: Migrating Legacy Data (Ruby Midwest)

What about?!?

Page 40: Migrating Legacy Data (Ruby Midwest)

Trucker helps you move.

Page 41: Migrating Legacy Data (Ruby Midwest)

But you still need to pack your stuff up.

Page 42: Migrating Legacy Data (Ruby Midwest)

Some other things to think about

Page 43: Migrating Legacy Data (Ruby Midwest)

Encoding issues

Page 44: Migrating Legacy Data (Ruby Midwest)

• MySQL issues:“ALTER TABLE mytable CONVERT TO CHARACTER SET utf8;

• iconv

Page 45: Migrating Legacy Data (Ruby Midwest)

Java to the rescue?!?

Page 46: Migrating Legacy Data (Ruby Midwest)

JDBC Adapters vs ActiveRecord

Page 47: Migrating Legacy Data (Ruby Midwest)

• There are about 12 adapters in the wild for pure ActiveRecord

• The ActiveRecord adapter for sqlserver runs on Win/Unix, but Unix setup is complicated

• Text adapter does CSV, Tab, and other plain text formats

Page 48: Migrating Legacy Data (Ruby Midwest)
Page 49: Migrating Legacy Data (Ruby Midwest)
Page 50: Migrating Legacy Data (Ruby Midwest)

JDBC Adapters

Page 51: Migrating Legacy Data (Ruby Midwest)

MySQLPostgreSQLOracleMicrosoft SQL Server

DB2FireBird

DerbyHSQLDBH2SQLite3Informix

ActiveRecord Tested:

Page 52: Migrating Legacy Data (Ruby Midwest)

MSSQL – An Example

Page 53: Migrating Legacy Data (Ruby Midwest)

• Download MSSQL driver (Google for it, they move it around a lot)

• Copy sqljdbc4.jar into RAILS_ROOT/lib

• Add require 'lib/sqljdbc4.jar' at the top of your environment.rb or application.rb

Page 54: Migrating Legacy Data (Ruby Midwest)

legacy: adapter: jdbc username: USERNAME password: PASSWORD driver: com.microsoft.sqlserver.jdbc.SQLServerDriverjdbc: sqlserver://63.134.199.59:1433

config/database.yml

Page 55: Migrating Legacy Data (Ruby Midwest)

Take it live

Page 56: Migrating Legacy Data (Ruby Midwest)

• Run in prod when you can

• Run locally when you can't (Heroku, VPNs, etc)

Page 57: Migrating Legacy Data (Ruby Midwest)

Resources

Page 58: Migrating Legacy Data (Ruby Midwest)

• http://github.com/mokolabs/trucker

• http://github.com/mokolabs/trucker_sample_app

• http://spkr8.com/t/3749

Page 59: Migrating Legacy Data (Ruby Midwest)

Special thanksto Dave Thomas

Page 60: Migrating Legacy Data (Ruby Midwest)

The End