ar-multidb 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -119,6 +119,23 @@ Note that the symbol `:default` will (unless you override it) refer to the defau
119
119
  top-level ActiveRecord configuration.
120
120
 
121
121
 
122
+ Development mode
123
+ ================
124
+
125
+ In development you will typically want `Multidb.use(:slave)` to still work, but you
126
+ probably don't want to run multiple databases on your development box. To make `use`
127
+ silently fall back to using the default connection, simply set `fallback: true` in
128
+ `database.yml`:
129
+
130
+ production:
131
+ adapter: postgresql
132
+ database: myapp_production
133
+ username: ohoh
134
+ password: mymy
135
+ host: db1
136
+ multidb:
137
+ fallback: true
138
+
122
139
  Legal
123
140
  =====
124
141
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
data/lib/multidb.rb CHANGED
@@ -11,8 +11,8 @@ module Multidb
11
11
  ActiveRecord::Base.class_eval do
12
12
  include Multidb::ModelExtensions
13
13
  end
14
- @balancer = Balancer.new(@configuration)
15
14
  end
15
+ @balancer = Balancer.new(@configuration)
16
16
  end
17
17
 
18
18
  attr_reader :balancer
@@ -16,8 +16,12 @@ module Multidb
16
16
  end
17
17
  end
18
18
 
19
- def connection
20
- @connection_pool.connection
19
+ def connection(&block)
20
+ if block_given?
21
+ @connection_pool.with_connection(&block)
22
+ else
23
+ @connection_pool.connection
24
+ end
21
25
  end
22
26
  end
23
27
 
@@ -26,7 +30,7 @@ module Multidb
26
30
  def initialize(configuration)
27
31
  @candidates = {}.with_indifferent_access
28
32
  @configuration = configuration
29
- @configuration.raw_configuration[:databases].each_pair do |name, config|
33
+ (@configuration.raw_configuration[:databases] || {}).each_pair do |name, config|
30
34
  configs = config.is_a?(Array) ? config : [config]
31
35
  configs.each do |config|
32
36
  candidate = Candidate.new(@configuration.default_adapter.merge(config))
@@ -34,6 +38,13 @@ module Multidb
34
38
  @candidates[name].push(candidate)
35
39
  end
36
40
  end
41
+ if @configuration.raw_configuration.include?(:fallback)
42
+ @fallback = @configuration.raw_configuration[:fallback]
43
+ elsif defined?(Rails)
44
+ @fallback = %w(development test).include?(Rails.env)
45
+ else
46
+ @fallback = false
47
+ end
37
48
  @default_candidate = Candidate.new(@configuration.default_pool)
38
49
  unless @candidates.include?(:default)
39
50
  @candidates[:default] = [@default_candidate]
@@ -41,8 +52,9 @@ module Multidb
41
52
  end
42
53
 
43
54
  def get(name, &block)
44
- candidates = @candidates[name] || []
45
- raise ArgumentError, "No such database connection '#{name}'" if candidates.blank?
55
+ candidates = @candidates[name]
56
+ candidates ||= @fallback ? @candidates[:default] : []
57
+ raise ArgumentError, "No such database connection '#{name}'" if candidates.empty?
46
58
  candidate = candidates.respond_to?(:sample) ?
47
59
  candidates.sample : candidates[rand(candidates.length)]
48
60
  block_given? ? yield(candidate) : candidate
@@ -51,18 +63,19 @@ module Multidb
51
63
  def use(name, &block)
52
64
  result = nil
53
65
  get(name) do |candidate|
54
- connection = candidate.connection
55
66
  if block_given?
56
- previous_connection, Thread.current[:multidb_connection] =
57
- Thread.current[:multidb_connection], connection
58
- begin
59
- result = yield
60
- ensure
61
- Thread.current[:multidb_connection] = previous_connection
67
+ candidate.connection do |connection|
68
+ previous_connection, Thread.current[:multidb_connection] =
69
+ Thread.current[:multidb_connection], connection
70
+ begin
71
+ result = yield
72
+ ensure
73
+ Thread.current[:multidb_connection] = previous_connection
74
+ end
75
+ result
62
76
  end
63
- result
64
77
  else
65
- result = Thread.current[:multidb_connection] = connection
78
+ result = Thread.current[:multidb_connection] = candidate.connection
66
79
  end
67
80
  end
68
81
  result
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar-multidb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alexander Staubo
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-19 00:00:00 +02:00
18
+ date: 2011-05-20 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency