Last active
August 29, 2015 13:56
-
-
Save FranckyU/9024949 to your computer and use it in GitHub Desktop.
Revisions
-
FranckyU revised this gist
Feb 17, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,7 +30,7 @@ def with_connection(connexion_list, &block) # randomly pick a connection # acting as a load balancer # connections are picked in a uniform way connexion = connexion_list[rand(connexion_list.length)] establish_connection(connexion) unless connection_config == connexion yield end -
FranckyU revised this gist
Feb 15, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ # ----------------------------------- # Monkey patching ActiveRecord # TODO move these connections configs into the config/database.yaml instead and load them from module ActiveRecord class Base CONNECTIONS = { -
FranckyU revised this gist
Feb 15, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -66,13 +66,13 @@ def a_complex_operation_method # .... with_read_connection do # any db reaching code .... end # .... with_write_connection do # any db reaching code .... end # .... -
FranckyU revised this gist
Feb 15, 2014 . 1 changed file with 12 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,18 +25,6 @@ class Base {host: "any_host", adapter:"postgresql", encoding:"unicode", database:"db_name", pool: 5, username: "username", password:"xxxxxx"} ]} class << self def with_connection(connexion_list, &block) # randomly pick a connection @@ -51,6 +39,18 @@ def with_connection(connexion_list, &block) def with_connection(connexion_list, &block) self.class.with_connection(connexion_list, block) end CONNECTIONS.each do |mode, connexion_array| define_method "with_#{mode.to_s}_connection" do |&block| with_connection(connexion_array, block) end self.class.instance_eval do define_method "with_#{mode.to_s}_connection" do |&block| with_connection(connexion_array, block) end end end end end -
FranckyU revised this gist
Feb 15, 2014 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -61,7 +61,6 @@ def with_connection(connexion_list, &block) # In a model class Customer < ActiveRecord::Base def a_complex_operation_method # .... -
FranckyU created this gist
Feb 15, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,100 @@ # V2, please see https://gist.github.com/FranckyU/9024250 for V1 # UPDATES # - Monkey patching ActiveRecord::Base to to handle it all # - The 'with_..._connection' methods get native without having to include any module # ----------------------------------- # PUT THE FOLLOWING IN AN INITIALIZER # ----------------------------------- # Monkey patching ActiveRecord # TODO move these connections configs into the config/database.yaml instead and load them from instead module ActiveRecord class Base CONNECTIONS = { read: [ {host: "any_host", adapter:"postgresql", encoding:"unicode", database:"db_name", pool: 5, username: "username", password:"xxxxxx"}, {host: "any_host", adapter:"postgresql", encoding:"unicode", database:"db_name", pool: 5, username: "username", password:"xxxxxx"}, {host: "any_host", adapter:"postgresql", encoding:"unicode", database:"db_name", pool: 5, username: "username", password:"xxxxxx"} ], write: [ {host: "any_host", adapter:"postgresql", encoding:"unicode", database:"db_name", pool: 5, username: "username", password:"xxxxxx"}, {host: "any_host", adapter:"postgresql", encoding:"unicode", database:"db_name", pool: 5, username: "username", password:"xxxxxx"}, {host: "any_host", adapter:"postgresql", encoding:"unicode", database:"db_name", pool: 5, username: "username", password:"xxxxxx"} ]} CONNECTIONS.each do |mode, connexion_array| define_method "with_#{mode.to_s}_connection" do |&block| with_connection(connexion_array, block) end self.class.instance_eval do define_method "with_#{mode.to_s}_connection" do |&block| with_connection(connexion_array, block) end end end class << self def with_connection(connexion_list, &block) # randomly pick a connection # acting as a load balancer # connections are picked in a uniform way connexion = connexion_list[rand(connexion_array.length)] establish_connection(connexion) unless connection_config == connexion yield end end def with_connection(connexion_list, &block) self.class.with_connection(connexion_list, block) end end end # ----------------------------------- # USAGE # ----------------------------------- # In a model class Customer < ActiveRecord::Base include SwitchableConnection def a_complex_operation_method # .... with_read_connection do end # .... with_write_connection do end # .... end end # In a controller class AnyController < ApplicationController def any_action # .... Customer.with_read_connection do # any db reaching code .... end # .... Product.with_write_connection do # any db reaching code .... end # .... end end