dbhijacker 0.8.3 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Gemfile.lock +7 -7
  2. data/dbhijacker.gemspec +1 -1
  3. data/lib/hijacker.rb +30 -31
  4. metadata +36 -20
  5. checksums.yaml +0 -7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dbhijacker (0.7.3)
4
+ dbhijacker (0.9.0)
5
5
  rails (>= 2.3.14)
6
6
 
7
7
  GEM
@@ -43,10 +43,10 @@ PLATFORMS
43
43
  ruby
44
44
 
45
45
  DEPENDENCIES
46
- bundler (~> 1.3)
46
+ bundler (>= 1.3)
47
47
  dbhijacker!
48
- rack (~> 1.1.0)
49
- rack-test (~> 0.6.1)
50
- rake (~> 0.9.2)
51
- rspec (~> 2.8)
52
- sqlite3 (~> 1.3.5)
48
+ rack (>= 1.1.0)
49
+ rack-test (>= 0.6.1)
50
+ rake (>= 0.9.2)
51
+ rspec (>= 2.8)
52
+ sqlite3 (>= 1.3.5)
data/dbhijacker.gemspec CHANGED
@@ -3,7 +3,7 @@
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{dbhijacker}
5
5
  s.homepage = "https://github.com/crystalcommerce/hijacker"
6
- s.version = "0.8.3"
6
+ s.version = "0.9.0"
7
7
 
8
8
  s.license = "MIT"
9
9
 
data/lib/hijacker.rb CHANGED
@@ -24,7 +24,7 @@ module Hijacker
24
24
  #
25
25
  # Background: every time rails gets information
26
26
  # from the database, it uses the last established connection. So,
27
- # although we've already established a connection to a "dummy" db
27
+ # although we've already established a connection to a root db
28
28
  # ("crystal", in this case), if we establish a new connection, all
29
29
  # subsequent database calls will use these settings instead (well,
30
30
  # until it's called again when it gets another request).
@@ -44,9 +44,7 @@ module Hijacker
44
44
  return "Already connected to #{target_name}"
45
45
  end
46
46
 
47
- verify = options.fetch(:verify, Hijacker.do_hijacking?)
48
-
49
- database = determine_database(target_name, sister_name, verify)
47
+ database = determine_database(target_name, sister_name)
50
48
 
51
49
  establish_connection_to_database(database)
52
50
 
@@ -63,7 +61,8 @@ module Hijacker
63
61
  # don't cache sister site
64
62
  cache_database_route(target_name, database) unless sister_name
65
63
 
66
- connect_sister_site_models(target_name)
64
+ # Do this even on a site without a master so we reconnect these models
65
+ connect_sister_site_models(database.master || database)
67
66
 
68
67
  reenable_query_caching
69
68
 
@@ -80,29 +79,33 @@ module Hijacker
80
79
 
81
80
  # very small chance this will raise, but if it does, we will still handle it the
82
81
  # same as +Hijacker.connect+ so we don't lock up the app.
83
- #
82
+ #
84
83
  # Also note that sister site models share a connection via minor management of
85
84
  # AR's connection_pool stuff, and will use ActiveRecord::Base.connection_pool if
86
85
  # we're not in a sister-site situation
87
- def self.connect_sister_site_models(db)
88
- return if db.nil?
89
- database = Hijacker::Database.find_by_database(db)
86
+ def self.connect_sister_site_models(master_database)
87
+ master_db_connection_pool = if processing_sister_site?
88
+ nil
89
+ else
90
+ ActiveRecord::Base.connection_pool
91
+ end
92
+ master_config = connection_config(master_database)
93
+
94
+ config[:sister_site_models].each do |model_name|
95
+ klass = model_name.constantize
90
96
 
91
- sister_db_connection_pool = self.processing_sister_site? ? nil : ActiveRecord::Base.connection_pool
92
- self.config[:sister_site_models].each do |model_name|
93
- ar_model = model_name.constantize
97
+ klass.establish_connection(master_config)
94
98
 
95
- if !sister_db_connection_pool
96
- ar_model.establish_connection(connection_config(database))
99
+ if !master_db_connection_pool
97
100
  begin
98
- ar_model.connection
101
+ klass.connection
99
102
  rescue
100
- ar_model.establish_connection(root_config)
101
- raise Hijacker::InvalidDatabase, db
103
+ klass.establish_connection(root_config)
104
+ raise Hijacker::InvalidDatabase, database.name
102
105
  end
103
- sister_db_connection_pool = ar_model.connection_pool
106
+ master_db_connection_pool = klass.connection_pool
104
107
  else
105
- ActiveRecord::Base.connection_handler.connection_pools[model_name] = sister_db_connection_pool
108
+ ActiveRecord::Base.connection_handler.connection_pools[model_name] = master_db_connection_pool
106
109
  end
107
110
  end
108
111
  end
@@ -110,20 +113,21 @@ module Hijacker
110
113
  # connects the sister_site_models to +db+ while calling the block
111
114
  # if +db+ and self.master differ
112
115
  def self.temporary_sister_connect(db, &block)
113
- processing_sister_site = (db != self.master && db != self.sister)
116
+ processing_sister_site = (db != master && db != sister)
114
117
  self.sister = db if processing_sister_site
115
118
  self.connect_sister_site_models(db) if processing_sister_site
116
119
  result = block.call
117
120
  self.connect_sister_site_models(self.master) if processing_sister_site
118
121
  self.sister = nil if processing_sister_site
119
- return result
122
+
123
+ result
120
124
  end
121
125
 
122
- # maintains and returns a connection to the "dummy" database.
126
+ # maintains and returns a connection to the root database.
123
127
  #
124
128
  # The advantage of using this over just calling
125
129
  # ActiveRecord::Base.establish_connection (without arguments) to reconnect
126
- # to the dummy database is that reusing the same connection greatly reduces
130
+ # to the root database is that reusing the same connection greatly reduces
127
131
  # context switching overhead etc involved with establishing a connection to
128
132
  # the database. It may seem trivial, but it actually seems to speed things
129
133
  # up by ~ 1/3 for already fast requests (probably less noticeable on slower
@@ -139,7 +143,7 @@ module Hijacker
139
143
  ActiveRecord::Base.establish_connection(current_config) # reconnect, we don't intend to hijack
140
144
  end
141
145
 
142
- return $hijacker_root_connection
146
+ $hijacker_root_connection
143
147
  end
144
148
 
145
149
  def self.root_config
@@ -161,18 +165,13 @@ module Hijacker
161
165
  end
162
166
 
163
167
  def self.master
164
- @master || database_configurations.fetch(ENV['RAILS_ENV'] || RAILS_ENV)['database']
168
+ @master || database_configurations.fetch(ENV['RAILS_ENV'] || Rails.env)['database']
165
169
  end
166
170
 
167
171
  def self.current_client
168
172
  sister || master
169
173
  end
170
174
 
171
- def self.do_hijacking?
172
- (Hijacker.config[:hosted_environments] || %w[staging production]).
173
- include?(ENV['RAILS_ENV'])
174
- end
175
-
176
175
  # just calling establish_connection doesn't actually check to see if
177
176
  # we've established a VALID connection. a call to connection will check
178
177
  # this, and throw an error if the connection's invalid. It is important
@@ -192,7 +191,7 @@ private
192
191
  current_client == new_master && sister == new_sister
193
192
  end
194
193
 
195
- def self.determine_database(target_name, sister_name, verify)
194
+ def self.determine_database(target_name, sister_name)
196
195
  if sister_name
197
196
  database = Hijacker::Database.find_by_name(sister_name)
198
197
  raise(Hijacker::InvalidDatabase, sister_name) if database.nil?
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dbhijacker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.9.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Michael Xavier
@@ -15,99 +16,113 @@ dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: rails
17
18
  requirement: !ruby/object:Gem::Requirement
19
+ none: false
18
20
  requirements:
19
- - - '>='
21
+ - - ! '>='
20
22
  - !ruby/object:Gem::Version
21
23
  version: 2.3.14
22
24
  type: :runtime
23
25
  prerelease: false
24
26
  version_requirements: !ruby/object:Gem::Requirement
27
+ none: false
25
28
  requirements:
26
- - - '>='
29
+ - - ! '>='
27
30
  - !ruby/object:Gem::Version
28
31
  version: 2.3.14
29
32
  - !ruby/object:Gem::Dependency
30
33
  name: bundler
31
34
  requirement: !ruby/object:Gem::Requirement
35
+ none: false
32
36
  requirements:
33
- - - '>='
37
+ - - ! '>='
34
38
  - !ruby/object:Gem::Version
35
39
  version: '1.3'
36
40
  type: :development
37
41
  prerelease: false
38
42
  version_requirements: !ruby/object:Gem::Requirement
43
+ none: false
39
44
  requirements:
40
- - - '>='
45
+ - - ! '>='
41
46
  - !ruby/object:Gem::Version
42
47
  version: '1.3'
43
48
  - !ruby/object:Gem::Dependency
44
49
  name: rake
45
50
  requirement: !ruby/object:Gem::Requirement
51
+ none: false
46
52
  requirements:
47
- - - '>='
53
+ - - ! '>='
48
54
  - !ruby/object:Gem::Version
49
55
  version: 0.9.2
50
56
  type: :development
51
57
  prerelease: false
52
58
  version_requirements: !ruby/object:Gem::Requirement
59
+ none: false
53
60
  requirements:
54
- - - '>='
61
+ - - ! '>='
55
62
  - !ruby/object:Gem::Version
56
63
  version: 0.9.2
57
64
  - !ruby/object:Gem::Dependency
58
65
  name: rack-test
59
66
  requirement: !ruby/object:Gem::Requirement
67
+ none: false
60
68
  requirements:
61
- - - '>='
69
+ - - ! '>='
62
70
  - !ruby/object:Gem::Version
63
71
  version: 0.6.1
64
72
  type: :development
65
73
  prerelease: false
66
74
  version_requirements: !ruby/object:Gem::Requirement
75
+ none: false
67
76
  requirements:
68
- - - '>='
77
+ - - ! '>='
69
78
  - !ruby/object:Gem::Version
70
79
  version: 0.6.1
71
80
  - !ruby/object:Gem::Dependency
72
81
  name: rack
73
82
  requirement: !ruby/object:Gem::Requirement
83
+ none: false
74
84
  requirements:
75
- - - '>='
85
+ - - ! '>='
76
86
  - !ruby/object:Gem::Version
77
87
  version: 1.1.0
78
88
  type: :development
79
89
  prerelease: false
80
90
  version_requirements: !ruby/object:Gem::Requirement
91
+ none: false
81
92
  requirements:
82
- - - '>='
93
+ - - ! '>='
83
94
  - !ruby/object:Gem::Version
84
95
  version: 1.1.0
85
96
  - !ruby/object:Gem::Dependency
86
97
  name: rspec
87
98
  requirement: !ruby/object:Gem::Requirement
99
+ none: false
88
100
  requirements:
89
- - - '>='
101
+ - - ! '>='
90
102
  - !ruby/object:Gem::Version
91
103
  version: '2.8'
92
104
  type: :development
93
105
  prerelease: false
94
106
  version_requirements: !ruby/object:Gem::Requirement
107
+ none: false
95
108
  requirements:
96
- - - '>='
109
+ - - ! '>='
97
110
  - !ruby/object:Gem::Version
98
111
  version: '2.8'
99
112
  - !ruby/object:Gem::Dependency
100
113
  name: sqlite3
101
114
  requirement: !ruby/object:Gem::Requirement
115
+ none: false
102
116
  requirements:
103
- - - '>='
117
+ - - ! '>='
104
118
  - !ruby/object:Gem::Version
105
119
  version: 1.3.5
106
120
  type: :development
107
121
  prerelease: false
108
122
  version_requirements: !ruby/object:Gem::Requirement
123
+ none: false
109
124
  requirements:
110
- - - '>='
125
+ - - ! '>='
111
126
  - !ruby/object:Gem::Version
112
127
  version: 1.3.5
113
128
  description: Allows a single Rails appliation to access many different databases
@@ -142,27 +157,28 @@ files:
142
157
  homepage: https://github.com/crystalcommerce/hijacker
143
158
  licenses:
144
159
  - MIT
145
- metadata: {}
146
160
  post_install_message:
147
161
  rdoc_options:
148
162
  - --charset=UTF-8
149
163
  require_paths:
150
164
  - lib
151
165
  required_ruby_version: !ruby/object:Gem::Requirement
166
+ none: false
152
167
  requirements:
153
- - - '>='
168
+ - - ! '>='
154
169
  - !ruby/object:Gem::Version
155
170
  version: '0'
156
171
  required_rubygems_version: !ruby/object:Gem::Requirement
172
+ none: false
157
173
  requirements:
158
- - - '>='
174
+ - - ! '>='
159
175
  - !ruby/object:Gem::Version
160
176
  version: '0'
161
177
  requirements: []
162
178
  rubyforge_project:
163
- rubygems_version: 2.0.3
179
+ rubygems_version: 1.8.25
164
180
  signing_key:
165
- specification_version: 4
181
+ specification_version: 3
166
182
  summary: One application, multiple client databases
167
183
  test_files:
168
184
  - spec/hijacker/alias_spec.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 5e23b73c48be348d0c277a5f2753847a227e3a3e
4
- data.tar.gz: 3685236e56248cb7d1a06069d890d8dcc8d0a9cd
5
- SHA512:
6
- metadata.gz: 6c3624189638fab27821c39a4d6070c633cc952596c2177ee379882d8978580ec6cfea12a5a29282ce5a2bbf797ff529b55430e5da957db45af49659304436e5
7
- data.tar.gz: 83522e3c058a48ec43d7681d2ff5c755a517b97fd73cbfbf8bc4ed2af79d760b4b9600b6f33b970db8bd425e60e6a7bbe024b43427c783d2b54f8ec4dc9c4602