Skip to content

Epictetus/acts_as_multi_connection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ActsAsMultiConnection
=====================
this plugin enables to hash partitioning table 

Example
=======

== database.yml ==
production:
  adapter: sqlite3
  database: db/production.sqlite3
  
  sub:
    sub_0:
      adapter: sqlite3
      database: db/production/sub0.sqlite3
    sub_1:
      adapter: sqlite3
      database: db/production/sub1.sqlite3
====


if you want to part 'cities' table by country_id, 

== model ==
class City << ActiveRecord::MultiConnection
   def connection_hash
       if country_id < 100
          :sub_0
       else
          :sub_1
       end
   end
   
   def self.connection_hash_by_sql(sql)
       match = sql.match /country_id\s*=\s*(\d+)/
       if match
          if match[1].to_i < 100
             :sub_0
          else
             :sub_1
          end
       else
          connection
       end
   end
end
City.find(:all, :conditions => ["country_id = ?", 120]) wil connect to 'db/production/sub1.sqlte3'
City.new(:country_id => 120).save will write on 'db/production/sub1.sqlite3'

== Transaction ==
you can use transaction only in same connection with using instance.
 
city = City.find(:first, :conditions => ['country_id = ?', 120])  
city.transaction do
  city.name = 'other name'
  city.save!
  foo.name = 'xxx'
  foo.save!
  raise
end

if city and foo is same connection, each changes should rollback.
but if foo is other connection, foo's change will be commited.


NOT YET IMPLEMENTED
===================
* (instance method) reload method is not implemented 
* ONLY rake db:migrate can migrate all databases
* OTHER commands are not implemented

Copyright (c) 2010 masarakki <[email protected]>, released under the MIT license

About

rails plugin to use hash_partitioning table

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published