Upload
peter-degen-portnoy
View
15.510
Download
4
Embed Size (px)
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
Citation preview
Replacing ActiveRecordwith DataMapperin Ruby on Rails
Peter Degen-PortnoyNovember 25, 2008
DataMapper is an ORM
A lot like ActiveRecord● DB Adapters● Migrations● Associations
● One to one● One to many● Many to many● Many to one
Rails Merb
DataMapper
DO.rb YAML IMAP
Application
ORM
Adaptors
Storage
Most often used with Merb
...but not exclusively
● Identity Map● Mappings in Model● Multiple Repositories● Lazy Loads of Large Lumps● Strategic Eager Loading
DataMapper Goodness
Identity Map● One object for one row● Reduces hits to database● Exists during Identity Map Session
Mapping in Model● Persist any class● Independent data store evolution● Connect to multiple databases● DM issues updates or creates only for what
it knows
Multiple Repositories● Nested YAML structure● Override target table names● Specify connection per class or even call
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
Strategic Eager Loading● One call per child● Eliminates n+1 calls for associations
Way faster than ActiveRecord
10,000 records fetched w/ Active RecordAverage: 1.3429 seconds
10,000 records fetched w/ DataMapperAverage: 0.0002 seconds
1.3429 seconds compared to 0.0002 seconds
0.0150% as much time
To Fetch 1,000 Records
ActiveRecord Over 204,000 calls
DataMapper Under 7,000 calls
Replacing ActiveRecord with DataMapper
the part for which you have been waiting
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
Gems You Need● data_mapper● do_mysql (or do_postgres or do_sqlite3)● dm-core● fastthread● json● rspec
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”
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
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' )
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.
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
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
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
Wire up application● Create Controller for book● Create “bulk” generation capability
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
Additional Goodness● Validations● Conditions● Order● Aggregations● Associations● Named Scope (class methods)
Assessment● Can't beat the speed● Outstanding flexibility● Durn if it won't be more complex to develop
with
Thank you