shackles 1.1.0 → 1.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 71107d30a7307374ffbf19d585bcfcfdc7be7cd2
4
- data.tar.gz: eb3b93f08eb0aaad52dd3c790ad90e2c25bb7006
3
+ metadata.gz: 7a69564d49b648b3d2a4ff5b5c5c3d69c0e32239
4
+ data.tar.gz: 0dabf823afc9141faa5089be875a55c300bb22f8
5
5
  SHA512:
6
- metadata.gz: af803e0e4278db28f9c00ebfdd06218262ab048e1a44cd56dbb227adef84d9fd29036dd4b48627e5f611329a3258564d55b2ee1eae35e35e8e61769949cc3dd7
7
- data.tar.gz: 8ce9c41baa33ea174084017662d9f87549bcae1a46d34caa5ffae54c81fa51e7cd86192c69aad912417dc7980321aacf23e110a7ef35db75c37a77e2439415d7
6
+ metadata.gz: b2cd537e44ee955172d4c849fa88c3dd6439e66020906d5c436fdd7db6371edb5c761728c4650eb87a4535ce3c60c975fd80c355169a0b9117b1a66aa1a689ee
7
+ data.tar.gz: 7f564cf199a5a239c32438406c92a209f9dec1365e47767b1e393f0e97894e3e806f194ca39e1dcfe7728a125ffa0b00da23ded3ec494dc810986e53de9b9f6f
data/lib/shackles.rb CHANGED
@@ -3,7 +3,7 @@ require 'set'
3
3
  module Shackles
4
4
  class << self
5
5
  def environment
6
- @environment ||= :master
6
+ Thread.current[:shackles_environment] ||= :master
7
7
  end
8
8
 
9
9
  def global_config
@@ -63,7 +63,7 @@ module Shackles
63
63
  activated_environments << environment
64
64
  yield
65
65
  ensure
66
- @environment = old_environment
66
+ Thread.current[:shackles_environment] = old_environment
67
67
  ActiveRecord::Base.connection_handler = ensure_handler unless Rails.env.test?
68
68
  end
69
69
  end
@@ -73,7 +73,7 @@ module Shackles
73
73
  environment ||= :master
74
74
  save_handler
75
75
  old_environment = self.environment
76
- @environment = environment
76
+ Thread.current[:shackles_environment] = environment
77
77
  ActiveRecord::Base.connection_handler = ensure_handler unless Rails.env.test?
78
78
  old_environment
79
79
  end
@@ -90,8 +90,12 @@ module Shackles
90
90
  new_handler = @connection_handlers[environment] = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
91
91
  pools = ActiveRecord::Base.connection_handler.send(:owner_to_pool)
92
92
  pools.each_pair do |model, pool|
93
- model = model.constantize
94
- new_handler.establish_connection(model, pool.spec)
93
+ if Rails.version < '5'
94
+ model = model.constantize
95
+ new_handler.establish_connection(model, pool.spec)
96
+ else
97
+ new_handler.establish_connection(pool.spec)
98
+ end
95
99
  end
96
100
  end
97
101
  new_handler
@@ -31,8 +31,19 @@ module Shackles
31
31
  end
32
32
  end
33
33
 
34
- def initialize(config, adapter_method)
35
- super(config.deep_symbolize_keys, adapter_method)
34
+ if Rails.version < '5'
35
+ def initialize(config, adapter_method)
36
+ super(config.deep_symbolize_keys, adapter_method)
37
+ end
38
+ else
39
+ def initialize(name, config, adapter_method)
40
+ super(name, config.deep_symbolize_keys, adapter_method)
41
+ end
42
+ end
43
+
44
+ def initialize_dup(original)
45
+ @current_config = nil
46
+ super
36
47
  end
37
48
 
38
49
  def config
@@ -1,3 +1,3 @@
1
1
  module Shackles
2
- VERSION = "1.1.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -13,6 +13,10 @@ end
13
13
  describe Shackles do
14
14
  ConnectionSpecification = ActiveRecord::ConnectionAdapters::ConnectionSpecification
15
15
 
16
+ def spec_args(conf, adapter)
17
+ Rails.version < '5' ? [conf, adapter] : ['dummy', conf, adapter]
18
+ end
19
+
16
20
  it "should allow changing environments" do
17
21
  conf = {
18
22
  :adapter => 'postgresql',
@@ -25,21 +29,21 @@ describe Shackles do
25
29
  :database => 'slave'
26
30
  }
27
31
  }
28
- spec = ConnectionSpecification.new(conf, 'adapter')
29
- spec.config[:username].should == 'canvas'
30
- spec.config[:database].should == 'master'
32
+ spec = ConnectionSpecification.new(*spec_args(conf, 'adapter'))
33
+ expect(spec.config[:username]).to eq('canvas')
34
+ expect(spec.config[:database]).to eq('master')
31
35
  Shackles.activate(:deploy) do
32
- spec.config[:username].should == 'deploy'
33
- spec.config[:database].should == 'master'
36
+ expect(spec.config[:username]).to eq('deploy')
37
+ expect(spec.config[:database]).to eq('master')
34
38
  end
35
- spec.config[:username].should == 'canvas'
36
- spec.config[:database].should == 'master'
39
+ expect(spec.config[:username]).to eq('canvas')
40
+ expect(spec.config[:database]).to eq('master')
37
41
  Shackles.activate(:slave) do
38
- spec.config[:username].should == 'canvas'
39
- spec.config[:database].should == 'slave'
42
+ expect(spec.config[:username]).to eq('canvas')
43
+ expect(spec.config[:database]).to eq('slave')
40
44
  end
41
- spec.config[:username].should == 'canvas'
42
- spec.config[:database].should == 'master'
45
+ expect(spec.config[:username]).to eq('canvas')
46
+ expect(spec.config[:database]).to eq('master')
43
47
  end
44
48
 
45
49
  it "should allow using hash insertions" do
@@ -52,12 +56,12 @@ describe Shackles do
52
56
  :username => 'deploy'
53
57
  }
54
58
  }
55
- spec = ConnectionSpecification.new(conf, 'adapter')
56
- spec.config[:username].should == 'canvas'
59
+ spec = ConnectionSpecification.new(*spec_args(conf, 'adapter'))
60
+ expect(spec.config[:username]).to eq('canvas')
57
61
  Shackles.activate(:deploy) do
58
- spec.config[:username].should == 'deploy'
62
+ expect(spec.config[:username]).to eq('deploy')
59
63
  end
60
- spec.config[:username].should == 'canvas'
64
+ expect(spec.config[:username]).to eq('canvas')
61
65
  end
62
66
 
63
67
  it "should be cache coherent with modifying the config" do
@@ -70,21 +74,35 @@ describe Shackles do
70
74
  :username => 'deploy'
71
75
  }
72
76
  }
73
- spec = ConnectionSpecification.new(conf.dup, 'adapter')
74
- spec.config[:username].should == 'canvas'
77
+ spec = ConnectionSpecification.new(*spec_args(conf.dup, 'adapter'))
78
+ expect(spec.config[:username]).to eq('canvas')
75
79
  spec.config[:schema_search_path] = 'bob'
76
- spec.config[:schema_search_path].should == 'bob'
77
- spec.config[:username].should == 'bob'
80
+ expect(spec.config[:schema_search_path]).to eq('bob')
81
+ expect(spec.config[:username]).to eq('bob')
78
82
  Shackles.activate(:deploy) do
79
- spec.config[:schema_search_path].should == 'bob'
80
- spec.config[:username].should == 'deploy'
83
+ expect(spec.config[:schema_search_path]).to eq('bob')
84
+ expect(spec.config[:username]).to eq('deploy')
81
85
  end
82
86
  external_config = spec.config.dup
83
- external_config.class.should == Hash
84
- external_config.should == spec.config
87
+ expect(external_config.class).to eq(Hash)
88
+ expect(external_config).to eq(spec.config)
85
89
 
86
90
  spec.config = conf.dup
87
- spec.config[:username].should == 'canvas'
91
+ expect(spec.config[:username]).to eq('canvas')
92
+ end
93
+
94
+ it "does not share config objects when dup'ing specs" do
95
+ conf = {
96
+ :adapter => 'postgresql',
97
+ :database => 'master',
98
+ :username => '%{schema_search_path}',
99
+ :schema_search_path => 'canvas',
100
+ :deploy => {
101
+ :username => 'deploy'
102
+ }
103
+ }
104
+ spec = ConnectionSpecification.new(*spec_args(conf.dup, 'adapter'))
105
+ expect(spec.config.object_id).not_to eq spec.dup.config.object_id
88
106
  end
89
107
 
90
108
  describe "activate" do
@@ -107,33 +125,43 @@ describe Shackles do
107
125
  it "should not close connections when switching envs" do
108
126
  conn = ActiveRecord::Base.connection
109
127
  slave_conn = Shackles.activate(:slave) { ActiveRecord::Base.connection }
110
- conn.should_not == slave_conn
111
- ActiveRecord::Base.connection.should == conn
128
+ expect(conn).not_to eq(slave_conn)
129
+ expect(ActiveRecord::Base.connection).to eq(conn)
112
130
  end
113
131
 
114
132
  it "should track all activated environments" do
115
133
  Shackles.activate(:slave) {}
116
134
  Shackles.activate(:custom) {}
117
135
  expected = Set.new([:master, :slave, :custom])
118
- (Shackles.activated_environments & expected).should == expected
136
+ expect(Shackles.activated_environments & expected).to eq(expected)
119
137
  end
120
138
 
121
139
  context "non-transactional" do
122
140
  it "should really disconnect all envs" do
123
141
  ActiveRecord::Base.connection
124
- ActiveRecord::Base.connection_pool.should be_connected
142
+ expect(ActiveRecord::Base.connection_pool).to be_connected
125
143
 
126
144
  Shackles.activate(:slave) do
127
145
  ActiveRecord::Base.connection
128
- ActiveRecord::Base.connection_pool.should be_connected
146
+ expect(ActiveRecord::Base.connection_pool).to be_connected
129
147
  end
130
148
 
131
149
  ActiveRecord::Base.clear_all_connections!
132
- ActiveRecord::Base.connection_pool.should_not be_connected
150
+ expect(ActiveRecord::Base.connection_pool).not_to be_connected
133
151
  Shackles.activate(:slave) do
134
- ActiveRecord::Base.connection_pool.should_not be_connected
152
+ expect(ActiveRecord::Base.connection_pool).not_to be_connected
135
153
  end
136
154
  end
137
155
  end
156
+
157
+ it 'is thread safe' do
158
+ Shackles.activate(:slave) do
159
+ Thread.new do
160
+ Shackles.activate!(:deploy)
161
+ expect(Shackles.environment).to eq :deploy
162
+ end.join
163
+ expect(Shackles.environment).to eq :slave
164
+ end
165
+ end
138
166
  end
139
167
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shackles
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-12 00:00:00.000000000 Z
11
+ date: 2016-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -30,6 +30,26 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '5.1'
33
+ - !ruby/object:Gem::Dependency
34
+ name: railties
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '4.0'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '5.1'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '4.0'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '5.1'
33
53
  - !ruby/object:Gem::Dependency
34
54
  name: appraisal
35
55
  requirement: !ruby/object:Gem::Requirement
@@ -78,14 +98,14 @@ dependencies:
78
98
  requirements:
79
99
  - - "~>"
80
100
  - !ruby/object:Gem::Version
81
- version: '2.0'
101
+ version: '3.0'
82
102
  type: :development
83
103
  prerelease: false
84
104
  version_requirements: !ruby/object:Gem::Requirement
85
105
  requirements:
86
106
  - - "~>"
87
107
  - !ruby/object:Gem::Version
88
- version: '2.0'
108
+ version: '3.0'
89
109
  - !ruby/object:Gem::Dependency
90
110
  name: sqlite3
91
111
  requirement: !ruby/object:Gem::Requirement
@@ -136,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
156
  version: '0'
137
157
  requirements: []
138
158
  rubyforge_project:
139
- rubygems_version: 2.5.1
159
+ rubygems_version: 2.6.4
140
160
  signing_key:
141
161
  specification_version: 4
142
162
  summary: ActiveRecord database environment switching for slaves and least-privilege