schoefmax-multi_db 0.2.0 → 0.2.1
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/README.rdoc +25 -5
- data/lib/multi_db.rb +1 -1
- data/lib/multi_db/connection_proxy.rb +1 -2
- data/lib/multi_db/scheduler.rb +1 -1
- data/multi_db.gemspec +6 -3
- data/spec/connection_proxy_spec.rb +0 -1
- data/spec/scheduler_spec.rb +0 -1
- data/spec/spec_helper.rb +1 -1
- metadata +12 -4
- data/lib/multi_db/thread_local_accessors.rb +0 -32
- data/spec/thread_local_accessors_spec.rb +0 -40
    
        data/README.rdoc
    CHANGED
    
    | @@ -25,14 +25,12 @@ master database. | |
| 25 25 |  | 
| 26 26 | 
             
            === Caveats
         | 
| 27 27 |  | 
| 28 | 
            -
            * works with activerecord 2.1 and 2. | 
| 29 | 
            -
            * when using Passenger or lightspeed you will probably need to introduce a
         | 
| 30 | 
            -
              before_filter which checks if the proxy is setup (see the discussion in the
         | 
| 31 | 
            -
              masochism readme: http://github.com/technoweenie/masochism)
         | 
| 28 | 
            +
            * works only with activerecord 2.1, 2.2 and 2.3
         | 
| 32 29 |  | 
| 33 30 | 
             
            === Install
         | 
| 34 31 |  | 
| 35 | 
            -
              gem  | 
| 32 | 
            +
              gem sources --add http://gems.github.com # only if you haven't already added github
         | 
| 33 | 
            +
              gem install schoefmax-multi_db
         | 
| 36 34 |  | 
| 37 35 | 
             
            When using Rails, add this to your environment.rb:
         | 
| 38 36 |  | 
| @@ -86,6 +84,28 @@ for master and slave connections. This can help you finding (some of the) issues | |
| 86 84 | 
             
            your application might have with a replicated database setup without actually having
         | 
| 87 85 | 
             
            one on your development machine.
         | 
| 88 86 |  | 
| 87 | 
            +
            === Using with Phusion Passenger
         | 
| 88 | 
            +
             | 
| 89 | 
            +
            With Passengers smart spawning method, child processes forked by the ApplicationSpawner
         | 
| 90 | 
            +
            won't have the connection proxy set up properly.
         | 
| 91 | 
            +
             | 
| 92 | 
            +
            To make it work, add this to your <tt>environment.rb</tt> or an initializer script
         | 
| 93 | 
            +
            (e.g. <tt>config/initializers/connection_proxy.rb</tt>):
         | 
| 94 | 
            +
             | 
| 95 | 
            +
              if defined?(PhusionPassenger)
         | 
| 96 | 
            +
                PhusionPassenger.on_event(:starting_worker_process) do |forked|
         | 
| 97 | 
            +
                  if forked
         | 
| 98 | 
            +
                    # ... set MultiDb configuration options, if any ...
         | 
| 99 | 
            +
                    MultiDb::ConnectionProxy.setup!
         | 
| 100 | 
            +
                  end
         | 
| 101 | 
            +
                end
         | 
| 102 | 
            +
              else # not using passenger (e.g. development/testing)
         | 
| 103 | 
            +
                # ... set MultiDb configuration options, if any ...
         | 
| 104 | 
            +
                MultiDb::ConnectionProxy.setup!
         | 
| 105 | 
            +
              end
         | 
| 106 | 
            +
             | 
| 107 | 
            +
            Thanks to Nathan Esquenazi for testing this.
         | 
| 108 | 
            +
             | 
| 89 109 | 
             
            === Forcing the master for certain actions
         | 
| 90 110 |  | 
| 91 111 | 
             
            Just add this to your controller:
         | 
    
        data/lib/multi_db.rb
    CHANGED
    
    
    
        data/lib/multi_db/scheduler.rb
    CHANGED
    
    
    
        data/multi_db.gemspec
    CHANGED
    
    | @@ -2,15 +2,15 @@ | |
| 2 2 |  | 
| 3 3 | 
             
            Gem::Specification.new do |s|
         | 
| 4 4 | 
             
              s.name = %q{multi_db}
         | 
| 5 | 
            -
              s.version = "0.2. | 
| 5 | 
            +
              s.version = "0.2.1"
         | 
| 6 6 |  | 
| 7 7 | 
             
              s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
         | 
| 8 8 | 
             
              s.authors = ["Maximilian Sch\303\266fmann"]
         | 
| 9 | 
            -
              s.date = %q{2009-03- | 
| 9 | 
            +
              s.date = %q{2009-03-11}
         | 
| 10 10 | 
             
              s.description = "Connection proxy for ActiveRecord for single master / multiple slave database deployments"
         | 
| 11 11 | 
             
              s.email = "max@pragmatic-it.de"
         | 
| 12 12 | 
             
              s.extra_rdoc_files = ["LICENSE", "README.rdoc"]
         | 
| 13 | 
            -
              s.files = ["lib/multi_db.rb", "lib/multi_db/ | 
| 13 | 
            +
              s.files = ["lib/multi_db.rb", "lib/multi_db/active_record_extensions.rb", "lib/multi_db/connection_proxy.rb", "lib/multi_db/observer_extensions.rb", "lib/multi_db/query_cache_compat.rb", "lib/multi_db/scheduler.rb", "LICENSE", "README.rdoc", "spec/config/database.yml", "spec/connection_proxy_spec.rb", "spec/scheduler_spec.rb", "spec/spec_helper.rb", "multi_db.gemspec"]
         | 
| 14 14 | 
             
              s.has_rdoc = true
         | 
| 15 15 | 
             
              s.homepage = "http://github.com/schoefmax/multi_db"
         | 
| 16 16 | 
             
              s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "multi_db", "--main", "README.rdoc"]
         | 
| @@ -24,10 +24,13 @@ Gem::Specification.new do |s| | |
| 24 24 |  | 
| 25 25 | 
             
                if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
         | 
| 26 26 | 
             
                  s.add_runtime_dependency('activerecord', [">= 2.1.0"])
         | 
| 27 | 
            +
                  s.add_runtime_dependency('schoefmax-tlattr_accessors', [">= 0.0.3"])
         | 
| 27 28 | 
             
                else
         | 
| 28 29 | 
             
                  s.add_dependency('activerecord', [">= 2.1.0"])
         | 
| 30 | 
            +
                  s.add_dependency('schoefmax-tlattr_accessors', [">= 0.0.3"])
         | 
| 29 31 | 
             
                end
         | 
| 30 32 | 
             
              else
         | 
| 31 33 | 
             
                s.add_dependency('activerecord', [">= 2.1.0"])
         | 
| 34 | 
            +
                s.add_dependency('schoefmax-tlattr_accessors', [">= 0.0.3"])
         | 
| 32 35 | 
             
              end
         | 
| 33 36 | 
             
            end
         | 
| @@ -2,7 +2,6 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') | |
| 2 2 | 
             
            require MULTI_DB_SPEC_DIR + '/../lib/multi_db/query_cache_compat'
         | 
| 3 3 | 
             
            require MULTI_DB_SPEC_DIR + '/../lib/multi_db/active_record_extensions'
         | 
| 4 4 | 
             
            require MULTI_DB_SPEC_DIR + '/../lib/multi_db/observer_extensions'
         | 
| 5 | 
            -
            require MULTI_DB_SPEC_DIR + '/../lib/multi_db/thread_local_accessors'
         | 
| 6 5 | 
             
            require MULTI_DB_SPEC_DIR + '/../lib/multi_db/scheduler'
         | 
| 7 6 | 
             
            require MULTI_DB_SPEC_DIR + '/../lib/multi_db/connection_proxy'
         | 
| 8 7 |  | 
    
        data/spec/scheduler_spec.rb
    CHANGED
    
    
    
        data/spec/spec_helper.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: schoefmax-multi_db
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              version: 0.2. | 
| 4 | 
            +
              version: 0.2.1
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors: 
         | 
| 7 7 | 
             
            - "Maximilian Sch\xC3\xB6fmann"
         | 
| @@ -9,7 +9,7 @@ autorequire: | |
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 11 |  | 
| 12 | 
            -
            date: 2009-03- | 
| 12 | 
            +
            date: 2009-03-11 00:00:00 -07:00
         | 
| 13 13 | 
             
            default_executable: 
         | 
| 14 14 | 
             
            dependencies: 
         | 
| 15 15 | 
             
            - !ruby/object:Gem::Dependency 
         | 
| @@ -22,6 +22,16 @@ dependencies: | |
| 22 22 | 
             
                  - !ruby/object:Gem::Version 
         | 
| 23 23 | 
             
                    version: 2.1.0
         | 
| 24 24 | 
             
                version: 
         | 
| 25 | 
            +
            - !ruby/object:Gem::Dependency 
         | 
| 26 | 
            +
              name: schoefmax-tlattr_accessors
         | 
| 27 | 
            +
              type: :runtime
         | 
| 28 | 
            +
              version_requirement: 
         | 
| 29 | 
            +
              version_requirements: !ruby/object:Gem::Requirement 
         | 
| 30 | 
            +
                requirements: 
         | 
| 31 | 
            +
                - - ">="
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version 
         | 
| 33 | 
            +
                    version: 0.0.3
         | 
| 34 | 
            +
                version: 
         | 
| 25 35 | 
             
            description: Connection proxy for ActiveRecord for single master / multiple slave database deployments
         | 
| 26 36 | 
             
            email: max@pragmatic-it.de
         | 
| 27 37 | 
             
            executables: []
         | 
| @@ -33,7 +43,6 @@ extra_rdoc_files: | |
| 33 43 | 
             
            - README.rdoc
         | 
| 34 44 | 
             
            files: 
         | 
| 35 45 | 
             
            - lib/multi_db.rb
         | 
| 36 | 
            -
            - lib/multi_db/thread_local_accessors.rb
         | 
| 37 46 | 
             
            - lib/multi_db/active_record_extensions.rb
         | 
| 38 47 | 
             
            - lib/multi_db/connection_proxy.rb
         | 
| 39 48 | 
             
            - lib/multi_db/observer_extensions.rb
         | 
| @@ -44,7 +53,6 @@ files: | |
| 44 53 | 
             
            - spec/config/database.yml
         | 
| 45 54 | 
             
            - spec/connection_proxy_spec.rb
         | 
| 46 55 | 
             
            - spec/scheduler_spec.rb
         | 
| 47 | 
            -
            - spec/thread_local_accessors_spec.rb
         | 
| 48 56 | 
             
            - spec/spec_helper.rb
         | 
| 49 57 | 
             
            - multi_db.gemspec
         | 
| 50 58 | 
             
            has_rdoc: true
         | 
| @@ -1,32 +0,0 @@ | |
| 1 | 
            -
            module MultiDb
         | 
| 2 | 
            -
              module ThreadLocalAccessors
         | 
| 3 | 
            -
                # Creates thread-local accessors for the given attribute name.
         | 
| 4 | 
            -
                #
         | 
| 5 | 
            -
                # The first invokation of the setter will make this value the default
         | 
| 6 | 
            -
                # for any subsequent threads.
         | 
| 7 | 
            -
                def tlattr_accessor(name)
         | 
| 8 | 
            -
                  ivar = "@_tlattr_#{name}"
         | 
| 9 | 
            -
                  class_eval %Q{
         | 
| 10 | 
            -
                    def #{name}
         | 
| 11 | 
            -
                      if #{ivar}
         | 
| 12 | 
            -
                        #{ivar}[Thread.current.object_id]
         | 
| 13 | 
            -
                      else
         | 
| 14 | 
            -
                        nil
         | 
| 15 | 
            -
                      end
         | 
| 16 | 
            -
                    end
         | 
| 17 | 
            -
             | 
| 18 | 
            -
                    def #{name}=(val)
         | 
| 19 | 
            -
                      if #{ivar}
         | 
| 20 | 
            -
                        unless #{ivar}.has_key?(Thread.current.object_id)
         | 
| 21 | 
            -
                          ObjectSpace.define_finalizer(Thread.current, lambda {|id| #{ivar}.delete(id) })
         | 
| 22 | 
            -
                        end
         | 
| 23 | 
            -
                        #{ivar}[Thread.current.object_id] = val
         | 
| 24 | 
            -
                      else
         | 
| 25 | 
            -
                        #{ivar} = Hash.new {|h, k| h[k] = val}
         | 
| 26 | 
            -
                        val
         | 
| 27 | 
            -
                      end
         | 
| 28 | 
            -
                    end
         | 
| 29 | 
            -
                  }, __FILE__, __LINE__
         | 
| 30 | 
            -
                end
         | 
| 31 | 
            -
              end
         | 
| 32 | 
            -
            end
         | 
| @@ -1,40 +0,0 @@ | |
| 1 | 
            -
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         | 
| 2 | 
            -
            require MULTI_DB_SPEC_DIR + '/../lib/multi_db/thread_local_accessors.rb'
         | 
| 3 | 
            -
             | 
| 4 | 
            -
            class Foo
         | 
| 5 | 
            -
              extend MultiDb::ThreadLocalAccessors
         | 
| 6 | 
            -
              tlattr_accessor :bar
         | 
| 7 | 
            -
            end
         | 
| 8 | 
            -
             | 
| 9 | 
            -
            describe MultiDb::ThreadLocalAccessors do
         | 
| 10 | 
            -
             | 
| 11 | 
            -
              it "should store values local to the thread" do
         | 
| 12 | 
            -
                x = Foo.new
         | 
| 13 | 
            -
                x.bar = 2
         | 
| 14 | 
            -
                Thread.new do
         | 
| 15 | 
            -
                  x.bar.should == 2
         | 
| 16 | 
            -
                  x.bar = 3
         | 
| 17 | 
            -
                  Thread.new do
         | 
| 18 | 
            -
                    x.bar.should == 2
         | 
| 19 | 
            -
                    x.bar = 5
         | 
| 20 | 
            -
                  end.join
         | 
| 21 | 
            -
                  x.bar.should == 3
         | 
| 22 | 
            -
                end.join
         | 
| 23 | 
            -
                x.bar.should == 2
         | 
| 24 | 
            -
              end
         | 
| 25 | 
            -
             | 
| 26 | 
            -
              it 'should not leak memory' do
         | 
| 27 | 
            -
                x = Foo.new
         | 
| 28 | 
            -
                n = 6000
         | 
| 29 | 
            -
                # create many thread-local values to make sure GC is invoked
         | 
| 30 | 
            -
                n.times do
         | 
| 31 | 
            -
                  Thread.new do
         | 
| 32 | 
            -
                    x.bar = rand
         | 
| 33 | 
            -
                  end.join
         | 
| 34 | 
            -
                end
         | 
| 35 | 
            -
                hash = x.send :instance_variable_get, '@_tlattr_bar'
         | 
| 36 | 
            -
                hash.size.should < (n / 2) # it should be a lot lower than n!
         | 
| 37 | 
            -
              end
         | 
| 38 | 
            -
             | 
| 39 | 
            -
            end
         | 
| 40 | 
            -
             |