Skip to content

Instantly share code, notes, and snippets.

@FranckyU
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save FranckyU/9024949 to your computer and use it in GitHub Desktop.

Select an option

Save FranckyU/9024949 to your computer and use it in GitHub Desktop.

Revisions

  1. FranckyU revised this gist Feb 17, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails-response-to-cqrs-2.rb
    Original 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_array.length)]
    connexion = connexion_list[rand(connexion_list.length)]
    establish_connection(connexion) unless connection_config == connexion
    yield
    end
  2. FranckyU revised this gist Feb 15, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rails-response-to-cqrs-2.rb
    Original 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 instead
    # TODO move these connections configs into the config/database.yaml instead and load them from
    module ActiveRecord
    class Base
    CONNECTIONS = {
  3. FranckyU revised this gist Feb 15, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions rails-response-to-cqrs-2.rb
    Original 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

    # ....
  4. FranckyU revised this gist Feb 15, 2014. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions rails-response-to-cqrs-2.rb
    Original 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"}
    ]}

    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
    @@ -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

  5. FranckyU revised this gist Feb 15, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion rails-response-to-cqrs-2.rb
    Original 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
    include SwitchableConnection

    def a_complex_operation_method
    # ....
  6. FranckyU created this gist Feb 15, 2014.
    100 changes: 100 additions & 0 deletions rails-response-to-cqrs-2.rb
    Original 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