switch_point 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/switch_point/config.rb +7 -4
- data/lib/switch_point/model.rb +4 -4
- data/lib/switch_point/proxy.rb +24 -3
- data/lib/switch_point/version.rb +1 -1
- data/spec/switch_point/model_spec.rb +23 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: caaba1e17cb79206eb52931dadba051e12aa10a4
|
4
|
+
data.tar.gz: a5ad7388ad8989ddd5c6a9ad9dd3a33474d4b6a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18ccaf6142ea40e8e9974cf7fb85f7b8d0bf8fe0737128f5745b66c896b39a8af5d32e5c3b238a5c1a0486a09686f5d69aa409f774f74c394182dc0e330d075d
|
7
|
+
data.tar.gz: 3d4eb2f58f0e548348250567bae0e01cc873f3a4a703dd9727565cadeaad83fad295cb016e0f4d5fddd2d8fe26cec1b42de84f6d75e4f4faaf974d2280244a77
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
## 0.2.1 (2014-05-29)
|
2
|
+
- Add Proxy#switch_name to switch proxy configuration
|
3
|
+
- Fix weird nil error when Config#define_switch_point isn't called yet
|
4
|
+
|
1
5
|
## 0.2.0 (2014-05-29)
|
2
6
|
- Always send destructive operations to writable connection
|
3
7
|
- Fix bug on pooled connections
|
data/lib/switch_point/config.rb
CHANGED
@@ -2,12 +2,15 @@ module SwitchPoint
|
|
2
2
|
class Config
|
3
3
|
def define_switch_point(name, config)
|
4
4
|
assert_valid_config!(config)
|
5
|
+
switch_points[name] = config
|
6
|
+
end
|
7
|
+
|
8
|
+
def switch_points
|
5
9
|
@switch_points ||= {}
|
6
|
-
@switch_points[name] = config
|
7
10
|
end
|
8
11
|
|
9
12
|
def database_name(name, mode)
|
10
|
-
|
13
|
+
switch_points[name][mode]
|
11
14
|
end
|
12
15
|
|
13
16
|
def model_name(name, mode)
|
@@ -15,11 +18,11 @@ module SwitchPoint
|
|
15
18
|
end
|
16
19
|
|
17
20
|
def fetch(name)
|
18
|
-
|
21
|
+
switch_points.fetch(name)
|
19
22
|
end
|
20
23
|
|
21
24
|
def keys
|
22
|
-
|
25
|
+
switch_points.keys
|
23
26
|
end
|
24
27
|
|
25
28
|
private
|
data/lib/switch_point/model.rb
CHANGED
@@ -31,15 +31,15 @@ module SwitchPoint
|
|
31
31
|
@switch_point_name = name
|
32
32
|
end
|
33
33
|
|
34
|
+
def switch_point_proxy
|
35
|
+
ProxyRepository.checkout(@switch_point_name)
|
36
|
+
end
|
37
|
+
|
34
38
|
private
|
35
39
|
|
36
40
|
def assert_existing_switch_point!(name)
|
37
41
|
SwitchPoint.config.fetch(name)
|
38
42
|
end
|
39
|
-
|
40
|
-
def switch_point_proxy
|
41
|
-
ProxyRepository.checkout(@switch_point_name)
|
42
|
-
end
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
data/lib/switch_point/proxy.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
module SwitchPoint
|
2
2
|
class Proxy
|
3
|
+
attr_reader :initial_name
|
4
|
+
|
3
5
|
def initialize(name)
|
4
|
-
@
|
6
|
+
@initial_name = name
|
7
|
+
@current_name = name
|
5
8
|
[:readonly, :writable].each do |mode|
|
6
9
|
model = define_model(SwitchPoint.config.model_name(name, mode))
|
7
|
-
@models[mode] = model
|
8
10
|
model.establish_connection(SwitchPoint.config.database_name(name, mode))
|
9
11
|
memorize_switch_point(name, mode, model.connection)
|
10
12
|
end
|
@@ -46,8 +48,27 @@ module SwitchPoint
|
|
46
48
|
@mode = saved_mode
|
47
49
|
end
|
48
50
|
|
51
|
+
def switch_name(new_name, &block)
|
52
|
+
if block
|
53
|
+
begin
|
54
|
+
old_name = @current_name
|
55
|
+
@current_name = new_name
|
56
|
+
block.call
|
57
|
+
ensure
|
58
|
+
@current_name = old_name
|
59
|
+
end
|
60
|
+
else
|
61
|
+
@current_name = new_name
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def reset_name!
|
66
|
+
@current_name = @initial_name
|
67
|
+
end
|
68
|
+
|
49
69
|
def connection
|
50
|
-
@
|
70
|
+
ProxyRepository.checkout(@current_name) # Ensure the target proxy is created
|
71
|
+
Proxy.const_get(SwitchPoint.config.model_name(@current_name, @mode)).connection
|
51
72
|
end
|
52
73
|
end
|
53
74
|
end
|
data/lib/switch_point/version.rb
CHANGED
@@ -142,4 +142,27 @@ RSpec.describe SwitchPoint::Model do
|
|
142
142
|
end
|
143
143
|
end
|
144
144
|
end
|
145
|
+
|
146
|
+
describe '.switch_name' do
|
147
|
+
after do
|
148
|
+
Book.switch_point_proxy.reset_name!
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'switches proxy configuration' do
|
152
|
+
Book.switch_point_proxy.switch_name(:comment)
|
153
|
+
expect(Book).to connect_to('comment_readonly.sqlite3')
|
154
|
+
expect(Publisher).to connect_to('comment_readonly.sqlite3')
|
155
|
+
end
|
156
|
+
|
157
|
+
context 'with block' do
|
158
|
+
it 'switches proxy configuration locally' do
|
159
|
+
Book.switch_point_proxy.switch_name(:comment) do
|
160
|
+
expect(Book).to connect_to('comment_readonly.sqlite3')
|
161
|
+
expect(Publisher).to connect_to('comment_readonly.sqlite3')
|
162
|
+
end
|
163
|
+
expect(Book).to connect_to('main_readonly.sqlite3')
|
164
|
+
expect(Publisher).to connect_to('main_readonly.sqlite3')
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
145
168
|
end
|