README.md in ebisu_connection-0.3.1 vs README.md in ebisu_connection-1.0.0
- old
+ new
@@ -8,132 +8,156 @@
## Installation
Add this line to your application's Gemfile:
- gem 'ebisu_connection'
+```ruby
+gem 'ebisu_connection'
+```
And then execute:
- $ bundle
+```
+$ bundle
+```
Or install it yourself as:
- $ gem install ebisu_connection
+```
+$ gem install ebisu_connection
+```
## Config
config/database.yml
- production:
- adapter: mysql2
- encoding: utf8
- reconnect: true
- database: kaeru
- pool: 5
- username: master
- password: master
- host: localhost
- socket: /var/run/mysqld/mysqld.sock
+```yaml
+production:
+ adapter: mysql2
+ encoding: utf8
+ reconnect: true
+ database: kaeru
+ pool: 5
+ username: master
+ password: master
+ host: localhost
+ socket: /var/run/mysqld/mysqld.sock
- slave:
- username: slave
- password: slave
- host: slave
+ slave:
+ username: slave
+ password: slave
+ host: slave
+```
```slave``` is base config to connect to slave servers.
Others will use the master setting. If you want to change, write in the slave.
Config of each slave server fill out config/slave.yaml
- production:
- - "slave1, 10"
- - "slave2, 20"
- -
- host: "slave3"
- weight: 30
+```yaml
+production:
+ - "slave1, 10"
+ - "slave2, 20"
+ -
+ host: "slave3"
+ weight: 30
+```
config/slave.yaml is checked by end of action. If config changed, it's reflected dynamic. Application doesn't need restart.
- "hostname, weight"
+```yaml
+"hostname, weight"
+```
String format is it. You can write config with hash.
### use multiple slave servers group
If you may want to user multiple slave group, write multiple slave group to config/database.yml.
- production:
- adapter: mysql2
- encoding: utf8
- reconnect: true
- database: kaeru
- pool: 5
- username: master
- password: master
- host: localhost
- socket: /var/run/mysqld/mysqld.sock
+```yaml
+production:
+ adapter: mysql2
+ encoding: utf8
+ reconnect: true
+ database: kaeru
+ pool: 5
+ username: master
+ password: master
+ host: localhost
+ socket: /var/run/mysqld/mysqld.sock
- slave:
- username: slave
- password: slave
- host: slave
+ slave:
+ username: slave
+ password: slave
+ host: slave
- admin_slave:
- username: slave
- password: slave
- host: admin_slaves
+ admin_slave:
+ username: slave
+ password: slave
+ host: admin_slaves
+```
Config of each slave server fill out config/slave.yaml
- production:
- slave:
- - "slave1, 10"
- - "slave2, 20"
- -
- host: "slave3"
- weight: 30
- admin_slave:
- - "slave3, 10"
- - "slave4, 20"
+```yaml
+production:
+ slave:
+ - "slave1, 10"
+ - "slave2, 20"
+ -
+ host: "slave3"
+ weight: 30
+ admin_slave:
+ - "slave3, 10"
+ - "slave4, 20"
+```
And call establish_fresh_connection method in model that access to ```admin_slave``` slave group.
- class AdminUser < ActiveRecord::Base
- establish_fresh_connection :admin_slave
- end
+```ruby
+class AdminUser < ActiveRecord::Base
+ establish_fresh_connection :admin_slave
+end
+```
The children is access to same slave group of parent.
- class Parent < ActiveRecord::Base
- establish_fresh_connection :admin_slave
- end
+```ruby
+class Parent < ActiveRecord::Base
+ establish_fresh_connection :admin_slave
+end
- class AdminUser < Parent
- end
+class AdminUser < Parent
+end
- class Benefit < Parent
- end
+class Benefit < Parent
+end
+```
AdminUser and Benefit access to ```admin_slave``` slave group.
### Declare model that doesn't use slave db
- class SomethingModel < ActiveRecord::Base
- master_db_only!
- end
+```ruby
+class SomethingModel < ActiveRecord::Base
+ master_db_only!
+end
+```
If model that always access to master servers is exist, You may want to write ```master_db_only!``` in model.
The model that master_db_only model's child is always access to master db.
## Usage
Read query will be access to slave server.
- Article.where(:id => 1)
- Article.count
+```ruby
+Article.where(:id => 1)
+Article.count
+```
If you want to access to the master server, use read_master.
```ruby
Article.where(:id => 1).read_master
@@ -141,13 +165,15 @@
It is possible to use readonly(false) instead of read_master, but it will be depricated at future version.
In transaction, Always will be access to master server.
- Article.transaction do
- Article.where(:id => 1)
- end
+```ruby
+Article.transaction do
+ Article.where(:id => 1)
+end
+```
### for Unicorn
```ruby
@@ -178,15 +204,14 @@
To run the test suite, you need mysql installed.
How to setup your test environment.
```bash
bundle install --path .bundle
-GEM_HOME=.bundle/ruby/(your ruby version) gem install bundler --pre
bundle exec appraisal install
```
This command run the spec suite for all rails versions supported.
```base
-bundle exec appraisal rake spec
+bundle exec appraisal rake test
```