README.md in slavery-1.4.3 vs README.md in slavery-2.0.0

- old
+ new

@@ -1,14 +1,16 @@ # Slavery - Simple, conservative slave reads for ActiveRecord -Slavery is a simple, easy to use plugin for ActiveRecord that enables conservative slave reads, which means it doesn't automatically redirect all SELECTs to slaves. Instead, it lets you specify `Slavery.on_slave` to send a particular query to a slave. +Slavery is a simple, easy to use gem for ActiveRecord that enables conservative slave reads, which means it doesn't automatically redirect all SELECTs to slaves. -Probably you just start off with one single database. As your app grows, you would move to master-slave replication for redundancy. At this point, all queries still go to the master and slaves are just backups. With that configuration, it's tempting to run some long-running queries on the slave. And that's exactly what Slavery does. +Instead, you can do `Slavery.on_slave { User.count }` to send a particular query to a slave. +Background: Probably your app started off with one single database. As it grows, you would upgrade to a master-slave replication for redundancy. At this point, all queries still go to the master and slaves are just backups. With that configuration, it's tempting to run some long-running queries on the slave. And that's exactly what Slavery does. + * Conservative - Safe by default. Installing Slavery won't change your app's current behavior. -* Future proof - No dirty hacks, simply works as a proxy for `ActiveRecord::Base.connection`. -* Simple - Only 100+ LOC, you can read the entire source and completely stay in control. +* Future proof - No dirty hacks. Simply works as a proxy for `ActiveRecord::Base.connection`. +* Simple code - Intentionally small. You can read the entire source and completely stay in control. Slavery works with ActiveRecord 3 or later. ## Install @@ -91,39 +93,37 @@ ```SQL GRANT SELECT ON *.* TO 'readonly'@'localhost'; ``` -With this user, writes on slave should raises an exception. +With this user, writes on slave should raise an exception. ```ruby Slavery.on_slave { User.create } # => ActiveRecord::StatementInvalid: Mysql2::Error: INSERT command denied... ``` It is a good idea to confirm this behavior in your test code as well. -## Database failure +## Disable temporarily -When one of the master or the slave goes down, you would rewrite `database.yml` to make all queries go to the surviving database, until you restore or rebuild the failed one. +You can quickly disable slave reads by dropping the following line in `config/initializers/slavery.rb`. -In such an event, you don't want to manually remove `Slavery.on_slave` from your code. Instead, just put the following line in `config/initializers/slavery.rb`. - ```ruby Slavely.disabled = true ``` -With this line, Slavery stops connection switching and all queries go to the new master. +With this line, Slavery stops connection switching and all queries go to the master. +This may be useful when one of the master or the slave goes down. You would rewrite `database.yml` to make all queries go to the surviving database, until you restore or rebuild the failed one. + ## Support for non-Rails apps -If you're using ActiveRecord in a non-Rails app (e.g. Sinatra), be sure to set `Slavery.env` in the boot sequence. +If you're using ActiveRecord in a non-Rails app (e.g. Sinatra), be sure to set `RACK_ENV` environment variable in the boot sequence, then: ```ruby -Slavery.env = 'development' +require 'slavery' -ActiveRecord::Base.send(:include, Slavery) - ActiveRecord::Base.configurations = { 'development' => { adapter: 'mysql2', ... }, 'development_slave' => { adapter: 'mysql2', ... } } ActiveRecord::Base.establish_connection(:development) @@ -135,10 +135,8 @@ ```ruby Slavery.spec_key = "slave" #instead of production_slave ``` -Alternatively you can pass it a lambda for dynamically setting this. +## Changelog -```ruby -Slavery.spec_key = lambda{ "#{Slavery.env}_slave" } -``` +* v2.0.0: Rails 5 support