replacing activerecord with datamapper

32
Replacing ActiveRecord with DataMapper in Ruby on Rails Peter Degen-Portnoy November 25, 2008

Upload: peter-degen-portnoy

Post on 26-Aug-2014

15.514 views

Category:

Self Improvement


4 download

DESCRIPTION

Presentation on replacing ActiveRecord with DataMapper in a Ruby on Rails application. Blog posting with info: http://degenportnoy.blogspot.com/2008/12/replacing-activerecord-with-datamapper.html Source: http://github.com/PeterDP/ar2dm

TRANSCRIPT

Page 1: Replacing ActiveRecord With DataMapper

Replacing ActiveRecordwith DataMapperin Ruby on Rails

Peter Degen-PortnoyNovember 25, 2008

Page 2: Replacing ActiveRecord With DataMapper

DataMapper is an ORM

Page 3: Replacing ActiveRecord With DataMapper

A lot like ActiveRecord● DB Adapters● Migrations● Associations

● One to one● One to many● Many to many● Many to one

Page 4: Replacing ActiveRecord With DataMapper

Rails Merb

DataMapper

DO.rb YAML IMAP

Application

ORM

Adaptors

Storage

Page 5: Replacing ActiveRecord With DataMapper

Most often used with Merb

...but not exclusively

Page 6: Replacing ActiveRecord With DataMapper

● Identity Map● Mappings in Model● Multiple Repositories● Lazy Loads of Large Lumps● Strategic Eager Loading

DataMapper Goodness

Page 7: Replacing ActiveRecord With DataMapper

Identity Map● One object for one row● Reduces hits to database● Exists during Identity Map Session

Page 8: Replacing ActiveRecord With DataMapper

Mapping in Model● Persist any class● Independent data store evolution● Connect to multiple databases● DM issues updates or creates only for what

it knows

Page 9: Replacing ActiveRecord With DataMapper

Multiple Repositories● Nested YAML structure● Override target table names● Specify connection per class or even call

Page 10: Replacing ActiveRecord With DataMapper

Lazy Loading● Reduces load of large columns

● Text● Blobs● Graphics

● Specify which columns lazy load● Lazy loads happen together; only 1 more call● Can be grouped

Page 11: Replacing ActiveRecord With DataMapper

Strategic Eager Loading● One call per child● Eliminates n+1 calls for associations

Page 12: Replacing ActiveRecord With DataMapper

Way faster than ActiveRecord

Page 13: Replacing ActiveRecord With DataMapper

10,000 records fetched w/ Active RecordAverage: 1.3429 seconds

Page 14: Replacing ActiveRecord With DataMapper

10,000 records fetched w/ DataMapperAverage: 0.0002 seconds

Page 15: Replacing ActiveRecord With DataMapper

1.3429 seconds compared to 0.0002 seconds

Page 16: Replacing ActiveRecord With DataMapper

0.0150% as much time

Page 17: Replacing ActiveRecord With DataMapper

To Fetch 1,000 Records

ActiveRecord Over 204,000 calls

DataMapper Under 7,000 calls

Page 18: Replacing ActiveRecord With DataMapper

Replacing ActiveRecord with DataMapper

the part for which you have been waiting

Page 19: Replacing ActiveRecord With DataMapper

Demo Applications● One application to test ActiveRecord● One application to test DataMapper● Each create same data structure:

● script/generate model book amount:float date:date name:string description:text --skip-timestamps

● Create 1000 records● Profile & Benchmark

Page 20: Replacing ActiveRecord With DataMapper

Gems You Need● data_mapper● do_mysql (or do_postgres or do_sqlite3)● dm-core● fastthread● json● rspec

Page 21: Replacing ActiveRecord With DataMapper

Edit config/environment.rb● config.frameworks -= [ :active_record ]● config.gem “do_mysql”● config.gem “dm-core”● config.gem “dm-migrations”

Make sure your gems are up to date by running “sudo rake gems:install”

Page 22: Replacing ActiveRecord With DataMapper

Configure database.ymldevelopment: &defaults :adapter: mysql :database: dm2_dev :user: root :password: mysql :socket: /var/run/mysqld/mysqld.sock :host: localhost

test: <<: *default :database: dm2_test

production: <<: *default :database: dm2_prod

Page 23: Replacing ActiveRecord With DataMapper

Create config/initializers/datamapper.rb

require "dm-core"hash = YAML.load(File.new(Rails.root + "/config/database.yml"))DataMapper.setup(:default, 'mysql://root:mysql@localhost/dm2_dev' )

Page 24: Replacing ActiveRecord With DataMapper

Generate Model> script/generate dm_model book amount:float date:date name:string description:text --skip-migration

NOTE: --skip-migration is needed because the rails-datamapper integration gem doesn't pull out the ActiveRecord dependency on migration creation yet.

Page 25: Replacing ActiveRecord With DataMapper

Finish model definitionrequire 'lorem'

class Book include DataMapper::Resource property :id, Integer, :serial => true property :amount, Float property :date, Date property :name, String property :description, Text, :lazy => true def self.create_dummy_data(num) num.times do |number| t = Book.new t.attributes = {:amount => number, :date => Time.now(),

:name => Lorem.lorem(4, false), :description => Lorem.lorem(60, false)} t.save end endend

Page 26: Replacing ActiveRecord With DataMapper

Create Migrationmigration 1, :create_books do up do create_table :books do column :id, Integer, :serial => true, :nullable? => false, :key => true column :amount, Float column :date, Date column :name, String column :description, DataMapper::Types::Text end end down do drop_table :books endend

Page 27: Replacing ActiveRecord With DataMapper

Create Migration Rake Tasknamespace :dm do task :migrate => :environment do gem "dm-migrations" require "migration_runner" Dir[File.join(Rails.root, "db", "migrate", "*")].each {|f| require f} migrate_up! end end

src/dm2> mysqladmin create dm2_devsrc/dm2> rake dm:migrate

Page 28: Replacing ActiveRecord With DataMapper

Wire up application● Create Controller for book● Create “bulk” generation capability

Page 29: Replacing ActiveRecord With DataMapper

Summary Checklist● Add gems & generate application● Remove ActiveRecord & add DataMapper● Configure database.yml & datamapper.rb● Create your database● Generate your model, complete definition &

migrate● Continue defining application

Page 30: Replacing ActiveRecord With DataMapper

Additional Goodness● Validations● Conditions● Order● Aggregations● Associations● Named Scope (class methods)

Page 31: Replacing ActiveRecord With DataMapper

Assessment● Can't beat the speed● Outstanding flexibility● Durn if it won't be more complex to develop

with

Page 32: Replacing ActiveRecord With DataMapper

Thank you