Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thiagopradi/octopus_sharding_example
Sample Rails app using Octopus Library to Enable database Sharding on ActiveRecord
https://github.com/thiagopradi/octopus_sharding_example
Last synced: 29 days ago
JSON representation
Sample Rails app using Octopus Library to Enable database Sharding on ActiveRecord
- Host: GitHub
- URL: https://github.com/thiagopradi/octopus_sharding_example
- Owner: thiagopradi
- Created: 2010-06-14T04:35:41.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2011-07-03T17:01:11.000Z (over 13 years ago)
- Last Synced: 2024-11-29T12:08:14.313Z (about 1 month ago)
- Language: Ruby
- Homepage: http://github.com/tchandy/octopus
- Size: 579 KB
- Stars: 12
- Watchers: 4
- Forks: 7
- Open Issues: 4
-
Metadata Files:
- Readme: README.mkdn
Awesome Lists containing this project
README
Octopus Sharding Example
This is a sample rails application using sharding with Octopus. This application have the User information on master database, and all informations after login are stored in the shard, based on `country` attribute of the current_user. Available shards are: brazil, mexico and canada.
How-to
Octopus is simple to use, just follow the steps:
- Add the gem to your Gemfile:gem 'ar-octopus', '0.0.12', :require => "octopus"- Create a shards.yml file inside config directory, that looks like this:
development:
shards:
canada:
host: localhost
adapter: mysql
database: canada_shard
brazil:
host: localhost
adapter: mysql
database: brazil_shard
mexico:
host: localhost
adapter: mysql
database: mexico_shardAfter this, you need to select what shard to use. This could be done with around_filter in controller, or setting the shard manually. This code in application_controller.rb that selects the shard:
class ApplicationController < ActionController::Base
around_filter :select_sharddef select_shard()
if user_signed_in?
using(current_user.country.to_sym) { yield }
else
yield
end
end
endIf the user isn't logged in, the shard will be the master. after this, it will pick the country attribute, passes to Octopus, and all queries will be sent to the selected shard. If you want to specify manually what shard to use, this is the syntax:
User.using(:brazil).allImportant!
Each ActiveRecord object knows where is the source shard. if you want to move objects between shards, you need to specify what shard to sent, like this:
# This will save the user in the brazil shard
@user = User.using(:brazil).first
@user.name = "New Name"
@user.save()# This will move the data to another shard:
new_user = User.new
new_user.attributes = @user.attributes
new_user.save()More info could be found at Octopus Wiki
Thanks
This project is sponsored by the Ruby Summer of Code,
and my mentors Mike Perham and Amit Agarwal.Copyright
Copyright (c) 2010 Thiago Pradi, released under the MIT license.