shackles 1.1.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/shackles/connection_specification.rb +13 -2
- data/lib/shackles/helper_methods.rb +3 -1
- data/lib/shackles/version.rb +1 -1
- data/lib/shackles.rb +11 -5
- data/spec/shackles_spec.rb +59 -31
- metadata +27 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4692b2c9a32072b9a591234bad820ad8845a13af
|
4
|
+
data.tar.gz: 34809b1f70d5741b40688bd59f2fe9c2d94fa88a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '09955aed6683cadf221f6cd3b461528b7250e84fa2754c06108d9c8bb31b550af95795c4f35e49ffaf7914aefc5b35d1e4784cfadf9112784cdb757ab5bfcf8a'
|
7
|
+
data.tar.gz: 89a7a2bf11d701f900c8f6020e25d4f3a10641f467625dadb7d0b22c706299f4e070f0a7519cfb9e724ea53901007bb845b6708390be08551d314caedda2ef2f
|
@@ -31,8 +31,19 @@ module Shackles
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
|
35
|
-
|
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,7 +1,9 @@
|
|
1
1
|
module Shackles
|
2
2
|
module HelperMethods
|
3
3
|
def self.included(base)
|
4
|
-
base.
|
4
|
+
base.singleton_class.include(ClassMethods)
|
5
|
+
# call shackle_class_method on the class itself, which then calls shackle_method on the singleton_class
|
6
|
+
base.singleton_class.singleton_class.include(ClassMethods)
|
5
7
|
end
|
6
8
|
|
7
9
|
# see readme for example usage
|
data/lib/shackles/version.rb
CHANGED
data/lib/shackles.rb
CHANGED
@@ -3,7 +3,7 @@ require 'set'
|
|
3
3
|
module Shackles
|
4
4
|
class << self
|
5
5
|
def environment
|
6
|
-
|
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
|
-
|
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
|
-
|
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,14 @@ 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
|
-
|
94
|
-
|
93
|
+
if Rails.version < '5'
|
94
|
+
model = model.constantize
|
95
|
+
new_handler.establish_connection(model, pool.spec)
|
96
|
+
elsif Rails.version < '5.1'
|
97
|
+
new_handler.establish_connection(pool.spec)
|
98
|
+
else
|
99
|
+
new_handler.establish_connection(pool.spec.config)
|
100
|
+
end
|
95
101
|
end
|
96
102
|
end
|
97
103
|
new_handler
|
data/spec/shackles_spec.rb
CHANGED
@@ -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].
|
30
|
-
spec.config[:database].
|
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].
|
33
|
-
spec.config[:database].
|
36
|
+
expect(spec.config[:username]).to eq('deploy')
|
37
|
+
expect(spec.config[:database]).to eq('master')
|
34
38
|
end
|
35
|
-
spec.config[:username].
|
36
|
-
spec.config[:database].
|
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].
|
39
|
-
spec.config[:database].
|
42
|
+
expect(spec.config[:username]).to eq('canvas')
|
43
|
+
expect(spec.config[:database]).to eq('slave')
|
40
44
|
end
|
41
|
-
spec.config[:username].
|
42
|
-
spec.config[:database].
|
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].
|
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].
|
62
|
+
expect(spec.config[:username]).to eq('deploy')
|
59
63
|
end
|
60
|
-
spec.config[:username].
|
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].
|
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].
|
77
|
-
spec.config[:username].
|
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].
|
80
|
-
spec.config[:username].
|
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.
|
84
|
-
external_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].
|
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.
|
111
|
-
ActiveRecord::Base.connection.
|
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).
|
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.
|
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.
|
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.
|
150
|
+
expect(ActiveRecord::Base.connection_pool).not_to be_connected
|
133
151
|
Shackles.activate(:slave) do
|
134
|
-
ActiveRecord::Base.connection_pool.
|
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.
|
4
|
+
version: 1.4.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:
|
11
|
+
date: 2017-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '4.0'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '5.
|
22
|
+
version: '5.2'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,27 @@ dependencies:
|
|
29
29
|
version: '4.0'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '5.
|
32
|
+
version: '5.2'
|
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.2'
|
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.2'
|
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: '
|
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: '
|
108
|
+
version: '3.0'
|
89
109
|
- !ruby/object:Gem::Dependency
|
90
110
|
name: sqlite3
|
91
111
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,10 +156,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
156
|
version: '0'
|
137
157
|
requirements: []
|
138
158
|
rubyforge_project:
|
139
|
-
rubygems_version: 2.
|
159
|
+
rubygems_version: 2.6.11
|
140
160
|
signing_key:
|
141
161
|
specification_version: 4
|
142
162
|
summary: ActiveRecord database environment switching for slaves and least-privilege
|
143
163
|
test_files:
|
144
164
|
- spec/shackles_spec.rb
|
145
|
-
has_rdoc:
|