master_slave_adapter 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +5 -1
 - data/lib/active_record/connection_adapters/master_slave_adapter.rb +8 -8
 - data/lib/active_record/connection_adapters/master_slave_adapter/version.rb +1 -1
 - data/spec/common/master_slave_adapter_spec.rb +13 -0
 - data/spec/integration/support/shared_mysql_examples.rb +28 -12
 - metadata +2 -2
 
    
        data/CHANGELOG.md
    CHANGED
    
    | 
         @@ -1,4 +1,8 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            # 1. 
     | 
| 
      
 1 
     | 
    
         
            +
            # 1.1.0 (November 15, 2012)
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            * [BUGFIX] Don't raise MasterUnavailable if a slave is unavailable
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            # 1.0.0 (July 24, 2012)
         
     | 
| 
       2 
6 
     | 
    
         | 
| 
       3 
7 
     | 
    
         
             
            * Add support for unavailable master connection
         
     | 
| 
       4 
8 
     | 
    
         
             
            * Restrict the public interface. Removed the following methods:
         
     | 
| 
         @@ -225,7 +225,7 @@ module ActiveRecord 
     | 
|
| 
       225 
225 
     | 
    
         
             
                          begin
         
     | 
| 
       226 
226 
     | 
    
         
             
                            #{to}.__send__(:#{method}, *args, &block)
         
     | 
| 
       227 
227 
     | 
    
         
             
                          rescue ActiveRecord::StatementInvalid => error
         
     | 
| 
       228 
     | 
    
         
            -
                             
     | 
| 
      
 228 
     | 
    
         
            +
                            handle_error(#{to}, error)
         
     | 
| 
       229 
229 
     | 
    
         
             
                          end
         
     | 
| 
       230 
230 
     | 
    
         
             
                        end
         
     | 
| 
       231 
231 
     | 
    
         
             
                      EOS
         
     | 
| 
         @@ -331,7 +331,7 @@ module ActiveRecord 
     | 
|
| 
       331 
331 
     | 
    
         
             
                      })
         
     | 
| 
       332 
332 
     | 
    
         
             
                    end
         
     | 
| 
       333 
333 
     | 
    
         
             
                  rescue ActiveRecord::StatementInvalid => exception
         
     | 
| 
       334 
     | 
    
         
            -
                     
     | 
| 
      
 334 
     | 
    
         
            +
                    handle_error(master_connection, exception)
         
     | 
| 
       335 
335 
     | 
    
         
             
                  end
         
     | 
| 
       336 
336 
     | 
    
         | 
| 
       337 
337 
     | 
    
         
             
                  # UTIL ==================================================================
         
     | 
| 
         @@ -442,11 +442,11 @@ module ActiveRecord 
     | 
|
| 
       442 
442 
     | 
    
         
             
                    end
         
     | 
| 
       443 
443 
     | 
    
         
             
                  end
         
     | 
| 
       444 
444 
     | 
    
         | 
| 
       445 
     | 
    
         
            -
                  def with( 
     | 
| 
       446 
     | 
    
         
            -
                    self.current_connection =  
     | 
| 
       447 
     | 
    
         
            -
                    yield( 
     | 
| 
      
 445 
     | 
    
         
            +
                  def with(connection)
         
     | 
| 
      
 446 
     | 
    
         
            +
                    self.current_connection = connection
         
     | 
| 
      
 447 
     | 
    
         
            +
                    yield(connection).tap { connection_stack.shift if connection_stack.size > 1 }
         
     | 
| 
       448 
448 
     | 
    
         
             
                  rescue ActiveRecord::StatementInvalid => exception
         
     | 
| 
       449 
     | 
    
         
            -
                     
     | 
| 
      
 449 
     | 
    
         
            +
                    handle_error(connection, exception)
         
     | 
| 
       450 
450 
     | 
    
         
             
                  end
         
     | 
| 
       451 
451 
     | 
    
         | 
| 
       452 
452 
     | 
    
         
             
                  def connect(cfg, name)
         
     | 
| 
         @@ -477,8 +477,8 @@ module ActiveRecord 
     | 
|
| 
       477 
477 
     | 
    
         
             
                    raise NotImplementedError
         
     | 
| 
       478 
478 
     | 
    
         
             
                  end
         
     | 
| 
       479 
479 
     | 
    
         | 
| 
       480 
     | 
    
         
            -
                  def  
     | 
| 
       481 
     | 
    
         
            -
                    if connection_error?(exception)
         
     | 
| 
      
 480 
     | 
    
         
            +
                  def handle_error(connection, exception)
         
     | 
| 
      
 481 
     | 
    
         
            +
                    if master_connection?(connection) && connection_error?(exception)
         
     | 
| 
       482 
482 
     | 
    
         
             
                      reset_master_connection
         
     | 
| 
       483 
483 
     | 
    
         
             
                      raise MasterUnavailable
         
     | 
| 
       484 
484 
     | 
    
         
             
                    else
         
     | 
| 
         @@ -90,6 +90,19 @@ describe ActiveRecord::ConnectionAdapters::MasterSlaveAdapter do 
     | 
|
| 
       90 
90 
     | 
    
         
             
                      end.to raise_error(ActiveRecord::MasterUnavailable)
         
     | 
| 
       91 
91 
     | 
    
         
             
                    end
         
     | 
| 
       92 
92 
     | 
    
         
             
                  end
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
      
 94 
     | 
    
         
            +
                  context 'given slave is not available' do
         
     | 
| 
      
 95 
     | 
    
         
            +
                    it 'raises statement invalid exception' do
         
     | 
| 
      
 96 
     | 
    
         
            +
                      adapter_connection.stub(:connection_error?).and_return(true)
         
     | 
| 
      
 97 
     | 
    
         
            +
                      slave_connection.should_receive(method).with('testing').and_raise(ActiveRecord::StatementInvalid)
         
     | 
| 
      
 98 
     | 
    
         
            +
             
     | 
| 
      
 99 
     | 
    
         
            +
                      expect do
         
     | 
| 
      
 100 
     | 
    
         
            +
                        ActiveRecord::Base.with_slave do
         
     | 
| 
      
 101 
     | 
    
         
            +
                          adapter_connection.send(method, 'testing')
         
     | 
| 
      
 102 
     | 
    
         
            +
                        end
         
     | 
| 
      
 103 
     | 
    
         
            +
                      end.to raise_error(ActiveRecord::StatementInvalid)
         
     | 
| 
      
 104 
     | 
    
         
            +
                    end
         
     | 
| 
      
 105 
     | 
    
         
            +
                  end
         
     | 
| 
       93 
106 
     | 
    
         
             
                end # /SelectMethods.each
         
     | 
| 
       94 
107 
     | 
    
         | 
| 
       95 
108 
     | 
    
         
             
                SchemaStatements.each do |method|
         
     | 
| 
         @@ -22,6 +22,8 @@ shared_examples_for "a MySQL MasterSlaveAdapter" do 
     | 
|
| 
       22 
22 
     | 
    
         | 
| 
       23 
23 
     | 
    
         
             
              let(:test_table) { MysqlSetupHelper::TEST_TABLE }
         
     | 
| 
       24 
24 
     | 
    
         | 
| 
      
 25 
     | 
    
         
            +
              let(:logger) { nil }
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
       25 
27 
     | 
    
         
             
              def connection
         
     | 
| 
       26 
28 
     | 
    
         
             
                ActiveRecord::Base.connection
         
     | 
| 
       27 
29 
     | 
    
         
             
              end
         
     | 
| 
         @@ -52,6 +54,8 @@ shared_examples_for "a MySQL MasterSlaveAdapter" do 
     | 
|
| 
       52 
54 
     | 
    
         | 
| 
       53 
55 
     | 
    
         
             
              before do
         
     | 
| 
       54 
56 
     | 
    
         
             
                ActiveRecord::Base.establish_connection(configuration)
         
     | 
| 
      
 57 
     | 
    
         
            +
                ActiveRecord::Base.logger = logger
         
     | 
| 
      
 58 
     | 
    
         
            +
                ActiveRecord::Base.connection.should be_active
         
     | 
| 
       55 
59 
     | 
    
         
             
              end
         
     | 
| 
       56 
60 
     | 
    
         | 
| 
       57 
61 
     | 
    
         
             
              it "connects to the database" do
         
     | 
| 
         @@ -59,7 +63,7 @@ shared_examples_for "a MySQL MasterSlaveAdapter" do 
     | 
|
| 
       59 
63 
     | 
    
         
             
              end
         
     | 
| 
       60 
64 
     | 
    
         | 
| 
       61 
65 
     | 
    
         
             
              context "given a debug logger" do
         
     | 
| 
       62 
     | 
    
         
            -
                let(: 
     | 
| 
      
 66 
     | 
    
         
            +
                let(:logger) do
         
     | 
| 
       63 
67 
     | 
    
         
             
                  logger = []
         
     | 
| 
       64 
68 
     | 
    
         
             
                  def logger.debug(*args)
         
     | 
| 
       65 
69 
     | 
    
         
             
                    push(args.join)
         
     | 
| 
         @@ -71,18 +75,10 @@ shared_examples_for "a MySQL MasterSlaveAdapter" do 
     | 
|
| 
       71 
75 
     | 
    
         
             
                  logger
         
     | 
| 
       72 
76 
     | 
    
         
             
                end
         
     | 
| 
       73 
77 
     | 
    
         | 
| 
       74 
     | 
    
         
            -
                before do
         
     | 
| 
       75 
     | 
    
         
            -
                  ActiveRecord::Base.logger = debug_logger
         
     | 
| 
       76 
     | 
    
         
            -
                end
         
     | 
| 
       77 
     | 
    
         
            -
             
     | 
| 
       78 
     | 
    
         
            -
                after do
         
     | 
| 
       79 
     | 
    
         
            -
                  ActiveRecord::Base.logger = nil
         
     | 
| 
       80 
     | 
    
         
            -
                end
         
     | 
| 
       81 
     | 
    
         
            -
             
     | 
| 
       82 
78 
     | 
    
         
             
                it "logs the connection info" do
         
     | 
| 
       83 
79 
     | 
    
         
             
                  ActiveRecord::Base.connection.select_value("SELECT 42")
         
     | 
| 
       84 
80 
     | 
    
         | 
| 
       85 
     | 
    
         
            -
                   
     | 
| 
      
 81 
     | 
    
         
            +
                  logger.last.should =~ /\[slave:127.0.0.1:3311\] SQL .*SELECT 42/
         
     | 
| 
       86 
82 
     | 
    
         
             
                end
         
     | 
| 
       87 
83 
     | 
    
         
             
              end
         
     | 
| 
       88 
84 
     | 
    
         | 
| 
         @@ -185,11 +181,11 @@ shared_examples_for "a MySQL MasterSlaveAdapter" do 
     | 
|
| 
       185 
181 
     | 
    
         
             
              end
         
     | 
| 
       186 
182 
     | 
    
         | 
| 
       187 
183 
     | 
    
         
             
              context "given master is not available" do
         
     | 
| 
       188 
     | 
    
         
            -
                before 
     | 
| 
      
 184 
     | 
    
         
            +
                before do
         
     | 
| 
       189 
185 
     | 
    
         
             
                  stop_master
         
     | 
| 
       190 
186 
     | 
    
         
             
                end
         
     | 
| 
       191 
187 
     | 
    
         | 
| 
       192 
     | 
    
         
            -
                after 
     | 
| 
      
 188 
     | 
    
         
            +
                after do
         
     | 
| 
       193 
189 
     | 
    
         
             
                  start_master
         
     | 
| 
       194 
190 
     | 
    
         
             
                end
         
     | 
| 
       195 
191 
     | 
    
         | 
| 
         @@ -209,4 +205,24 @@ shared_examples_for "a MySQL MasterSlaveAdapter" do 
     | 
|
| 
       209 
205 
     | 
    
         
             
                  end
         
     | 
| 
       210 
206 
     | 
    
         
             
                end
         
     | 
| 
       211 
207 
     | 
    
         
             
              end
         
     | 
| 
      
 208 
     | 
    
         
            +
             
     | 
| 
      
 209 
     | 
    
         
            +
              context "given slave is not available" do
         
     | 
| 
      
 210 
     | 
    
         
            +
                before do
         
     | 
| 
      
 211 
     | 
    
         
            +
                  stop_slave
         
     | 
| 
      
 212 
     | 
    
         
            +
                end
         
     | 
| 
      
 213 
     | 
    
         
            +
             
     | 
| 
      
 214 
     | 
    
         
            +
                after do
         
     | 
| 
      
 215 
     | 
    
         
            +
                  start_slave
         
     | 
| 
      
 216 
     | 
    
         
            +
                end
         
     | 
| 
      
 217 
     | 
    
         
            +
             
     | 
| 
      
 218 
     | 
    
         
            +
                context "when asked for slave" do
         
     | 
| 
      
 219 
     | 
    
         
            +
                  it "fails" do
         
     | 
| 
      
 220 
     | 
    
         
            +
                    expect do
         
     | 
| 
      
 221 
     | 
    
         
            +
                      ActiveRecord::Base.with_slave { should_read_from :slave }
         
     | 
| 
      
 222 
     | 
    
         
            +
                    end.to raise_error(ActiveRecord::StatementInvalid)
         
     | 
| 
      
 223 
     | 
    
         
            +
                  end
         
     | 
| 
      
 224 
     | 
    
         
            +
                end
         
     | 
| 
      
 225 
     | 
    
         
            +
             
     | 
| 
      
 226 
     | 
    
         
            +
              end
         
     | 
| 
      
 227 
     | 
    
         
            +
             
     | 
| 
       212 
228 
     | 
    
         
             
            end
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: master_slave_adapter
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 1.1.0
         
     | 
| 
       5 
5 
     | 
    
         
             
              prerelease: 
         
     | 
| 
       6 
6 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       7 
7 
     | 
    
         
             
            authors:
         
     | 
| 
         @@ -15,7 +15,7 @@ authors: 
     | 
|
| 
       15 
15 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       16 
16 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       17 
17 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       18 
     | 
    
         
            -
            date: 2012- 
     | 
| 
      
 18 
     | 
    
         
            +
            date: 2012-11-15 00:00:00.000000000 Z
         
     | 
| 
       19 
19 
     | 
    
         
             
            dependencies:
         
     | 
| 
       20 
20 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       21 
21 
     | 
    
         
             
              name: activerecord
         
     |