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.
@@ -25,14 +25,12 @@ master database.
25
25
 
26
26
  === Caveats
27
27
 
28
- * works with activerecord 2.1 and 2.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 install schoefmax-multi_db --source http://gems.github.com
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:
@@ -1,4 +1,4 @@
1
- require 'multi_db/thread_local_accessors'
1
+ require 'tlattr_accessors'
2
2
  require 'multi_db/scheduler'
3
3
  require 'multi_db/active_record_extensions'
4
4
  require 'multi_db/observer_extensions'
@@ -13,8 +13,7 @@ module MultiDb
13
13
  DEFAULT_MASTER_MODELS = ['CGI::Session::ActiveRecordStore::Session']
14
14
 
15
15
  attr_accessor :master
16
- tlattr_accessor :master_depth
17
- tlattr_accessor :current
16
+ tlattr_accessor :master_depth, :current, true
18
17
 
19
18
  class << self
20
19
 
@@ -5,7 +5,7 @@ module MultiDb
5
5
 
6
6
  attr :items
7
7
  delegate :[], :[]=, :to => :items
8
- tlattr_accessor :current_index
8
+ tlattr_accessor :current_index, true
9
9
 
10
10
  def initialize(items, blacklist_timeout = 1.minute)
11
11
  @n = items.length
@@ -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.0"
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-08}
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/thread_local_accessors.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/thread_local_accessors_spec.rb", "spec/spec_helper.rb", "multi_db.gemspec"]
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
 
@@ -1,5 +1,4 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
- require MULTI_DB_SPEC_DIR + '/../lib/multi_db/thread_local_accessors'
3
2
  require MULTI_DB_SPEC_DIR + '/../lib/multi_db/scheduler'
4
3
 
5
4
  describe MultiDb::Scheduler do
@@ -1,6 +1,6 @@
1
1
  require 'rubygems'
2
2
  gem 'activerecord', '2.2.2'
3
- %w[active_record yaml erb spec].each {|lib| require lib}
3
+ %w[tlattr_accessors active_record yaml erb spec].each {|lib| require lib}
4
4
 
5
5
  RAILS_ENV = ENV['RAILS_ENV'] = 'test'
6
6
 
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.0
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-08 00:00:00 -08:00
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
-