switch_point 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f01102cca54c7b028bddcf7663ae5ec5de4fd28
4
- data.tar.gz: 0b2d26c5a70f7dbe7341f6b7a81ebed58219bf2d
3
+ metadata.gz: 3044b7a1ae2a9a6e022a82440cfaeaaabe90da64
4
+ data.tar.gz: 7640bd6b1a38e93f84f54c2980d32848fa2260da
5
5
  SHA512:
6
- metadata.gz: e293b56dd3daa2873beb81602d02ee2959597c5a8d73a7e986ad4d274074e9bff0955919ccd417235e30ed5a688c33b08c1a0df1b1c09fe0ce292d4de594da20
7
- data.tar.gz: a4bab4cf2b8c799310880756afae5a55505aaaccd9a2a56c512afde07786954326fa44c35243614b68d96d0a42fdcbe86cca5a068e3657400044b215806d78c6
6
+ metadata.gz: 641b875847d36f8878eb954d0a950384d770ecc4b779c36ddfd5d741c94f103c1d0422a823c8b03a1c2f610c540c8bf2aedbac9bfa423f119b0e7159c081d20d
7
+ data.tar.gz: 78da2c2d465bd5660946c7b74bf1ad23c00ca2deae3af478737dcbad716316bf4d2ffccd2333359816ae81d2bb8e1c996bcc54e91f3c0e1adfd04cdf918437e1
data/.rubocop.yml ADDED
@@ -0,0 +1,36 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ Style/AlignParameters:
4
+ Enabled: false
5
+
6
+ Style/BlockDelimiters:
7
+ Enabled: false
8
+
9
+ Style/GuardClause:
10
+ Enabled: false
11
+
12
+ Style/HashSyntax:
13
+ Exclude:
14
+ - Rakefile
15
+
16
+ Style/IfUnlessModifier:
17
+ Enabled: false
18
+
19
+ Style/Lambda:
20
+ Enabled: false
21
+
22
+ Style/Next:
23
+ Enabled: false
24
+
25
+ Style/PercentLiteralDelimiters:
26
+ PreferredDelimiters:
27
+ '%w': '[]'
28
+
29
+ Style/RaiseArgs:
30
+ EnforcedStyle: compact
31
+
32
+ Style/SignalException:
33
+ Enabled: false
34
+
35
+ Style/TrailingComma:
36
+ Enabled: false
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,17 @@
1
+ Metrics/AbcSize:
2
+ Enabled: false
3
+
4
+ Metrics/ClassLength:
5
+ Enabled: false
6
+
7
+ Metrics/CyclomaticComplexity:
8
+ Enabled: false
9
+
10
+ Metrics/LineLength:
11
+ Enabled: false
12
+
13
+ Metrics/MethodLength:
14
+ Enabled: false
15
+
16
+ Style/Documentation:
17
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.7.0 (2015-10-16)
2
+ - `Model.with_readonly` and `Model.with_writable` now raises error when the Model doesn't use switch_point
3
+
1
4
  ## 0.6.0 (2015-04-14)
2
5
  - Add `SwitchPoint::QueryCache` middleware
3
6
  - `Model.cache` and `Model.uncached` is now hooked by switch_point
data/Rakefile CHANGED
@@ -1,10 +1,13 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
 
3
- task :default => :spec
3
+ task :default => [:spec, :rubocop]
4
4
 
5
5
  require 'rspec/core/rake_task'
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
8
+ require 'rubocop/rake_task'
9
+ RuboCop::RakeTask.new(:rubocop)
10
+
8
11
  desc 'Run benchmark'
9
12
  task :benchmark do
10
13
  sh 'ruby', 'benchmark/proxy.rb'
data/benchmark/proxy.rb CHANGED
@@ -8,7 +8,6 @@ SwitchPoint.configure do |config|
8
8
  writable: :proxy_writable
9
9
  end
10
10
 
11
-
12
11
  class Plain < ActiveRecord::Base
13
12
  end
14
13
 
@@ -1,5 +1,8 @@
1
1
  module SwitchPoint
2
2
  class Config
3
+ attr_accessor :auto_writable
4
+ alias_method :auto_writable?, :auto_writable
5
+
3
6
  def initialize
4
7
  self.auto_writable = false
5
8
  end
@@ -9,14 +12,6 @@ module SwitchPoint
9
12
  switch_points[name] = config
10
13
  end
11
14
 
12
- def auto_writable=(val)
13
- @auto_writable = val
14
- end
15
-
16
- def auto_writable?
17
- @auto_writable
18
- end
19
-
20
15
  def switch_points
21
16
  @switch_points ||= {}
22
17
  end
@@ -28,8 +23,6 @@ module SwitchPoint
28
23
  def model_name(name, mode)
29
24
  if fetch(name)[mode]
30
25
  "#{name}_#{mode}".camelize
31
- else
32
- nil
33
26
  end
34
27
  end
35
28
 
@@ -44,15 +37,15 @@ module SwitchPoint
44
37
  private
45
38
 
46
39
  def assert_valid_config!(config)
47
- unless config.has_key?(:readonly) || config.has_key?(:writable)
40
+ unless config.key?(:readonly) || config.key?(:writable)
48
41
  raise ArgumentError.new(':readonly or :writable must be specified')
49
42
  end
50
- if config.has_key?(:readonly)
43
+ if config.key?(:readonly)
51
44
  unless config[:readonly].is_a?(Symbol)
52
45
  raise TypeError.new(":readonly's value must be Symbol")
53
46
  end
54
47
  end
55
- if config.has_key?(:writable)
48
+ if config.key?(:writable)
56
49
  unless config[:writable].is_a?(Symbol)
57
50
  raise TypeError.new(":writable's value must be Symbol")
58
51
  end
@@ -1,3 +1,4 @@
1
+ require 'switch_point/error'
1
2
  require 'switch_point/proxy_repository'
2
3
 
3
4
  module SwitchPoint
@@ -8,7 +9,7 @@ module SwitchPoint
8
9
  DESTRUCTIVE_METHODS.each do |method_name|
9
10
  define_method(:"#{method_name}_with_switch_point") do |*args, &block|
10
11
  parent_method = :"#{method_name}_without_switch_point"
11
- if self.pool.equal?(ActiveRecord::Base.connection_pool)
12
+ if pool.equal?(ActiveRecord::Base.connection_pool)
12
13
  Connection.handle_base_connection(self, parent_method, *args, &block)
13
14
  else
14
15
  Connection.handle_generated_connection(self, parent_method, method_name, *args, &block)
@@ -16,16 +17,13 @@ module SwitchPoint
16
17
  end
17
18
  end
18
19
 
19
- class ReadonlyError < StandardError
20
- end
21
-
22
20
  def self.handle_base_connection(conn, parent_method, *args, &block)
23
21
  switch_points = conn.pool.spec.config[:switch_points]
24
22
  if switch_points
25
23
  switch_points.each do |switch_point|
26
24
  proxy = ProxyRepository.find(switch_point[:name])
27
25
  if switch_point[:mode] != :writable
28
- raise RuntimeError.new("ActiveRecord::Base's switch_points must be writable, but #{switch_point[:name]} is #{switch_point[:mode]}")
26
+ raise Error.new("ActiveRecord::Base's switch_points must be writable, but #{switch_point[:name]} is #{switch_point[:mode]}")
29
27
  end
30
28
  purge_readonly_query_cache(proxy)
31
29
  end
@@ -48,7 +46,7 @@ module SwitchPoint
48
46
  purge_readonly_query_cache(proxy)
49
47
  conn.send(parent_method, *args, &block)
50
48
  else
51
- raise RuntimeError.new("Unknown mode #{switch_point[:mode]} is given with #{name}")
49
+ raise Error.new("Unknown mode #{switch_point[:mode]} is given with #{name}")
52
50
  end
53
51
  else
54
52
  conn.send(parent_method, *args, &block)
@@ -0,0 +1,10 @@
1
+ module SwitchPoint
2
+ class Error < StandardError
3
+ end
4
+
5
+ class ReadonlyError < Error
6
+ end
7
+
8
+ class UnconfiguredError < Error
9
+ end
10
+ end
@@ -1,3 +1,4 @@
1
+ require 'switch_point/error'
1
2
  require 'switch_point/proxy_repository'
2
3
 
3
4
  module SwitchPoint
@@ -40,7 +41,7 @@ module SwitchPoint
40
41
  if switch_point_proxy
41
42
  switch_point_proxy.with_readonly(&block)
42
43
  else
43
- block.call
44
+ raise UnconfiguredError.new("#{name} isn't configured to use switch_point")
44
45
  end
45
46
  end
46
47
 
@@ -48,7 +49,7 @@ module SwitchPoint
48
49
  if switch_point_proxy
49
50
  switch_point_proxy.with_writable(&block)
50
51
  else
51
- block.call
52
+ raise UnconfiguredError.new("#{name} isn't configured to use switch_point")
52
53
  end
53
54
  end
54
55
 
@@ -69,11 +70,11 @@ module SwitchPoint
69
70
 
70
71
  def transaction_with(*models, &block)
71
72
  unless can_transaction_with?(*models)
72
- raise RuntimeError.new("switch_point's model names must be consistent")
73
+ raise Error.new("switch_point's model names must be consistent")
73
74
  end
74
75
 
75
76
  with_writable do
76
- self.transaction(&block)
77
+ transaction(&block)
77
78
  end
78
79
  end
79
80
 
@@ -90,8 +91,6 @@ module SwitchPoint
90
91
  model.instance_variable_get(:@switch_point_name),
91
92
  :writable
92
93
  )
93
- else
94
- nil
95
94
  end
96
95
  end
97
96
 
@@ -1,3 +1,5 @@
1
+ require 'switch_point/error'
2
+
1
3
  module SwitchPoint
2
4
  class Proxy
3
5
  attr_reader :initial_name
@@ -34,12 +36,12 @@ module SwitchPoint
34
36
  switch_point = { name: name, mode: mode }
35
37
  if pool.equal?(ActiveRecord::Base.connection_pool)
36
38
  if mode != :writable
37
- raise RuntimeError.new("ActiveRecord::Base's switch_points must be writable, but #{name} is #{mode}")
39
+ raise Error.new("ActiveRecord::Base's switch_points must be writable, but #{name} is #{mode}")
38
40
  end
39
41
  switch_points = pool.spec.config[:switch_points] || []
40
42
  switch_points << switch_point
41
43
  pool.spec.config[:switch_points] = switch_points
42
- elsif pool.spec.config.has_key?(:switch_point)
44
+ elsif pool.spec.config.key?(:switch_point)
43
45
  # Only :writable is specified
44
46
  else
45
47
  pool.spec.config[:switch_point] = switch_point
@@ -95,7 +97,7 @@ module SwitchPoint
95
97
  unless AVAILABLE_MODES.include?(new_mode)
96
98
  raise ArgumentError.new("Unknown mode: #{new_mode}")
97
99
  end
98
- saved_mode = self.thread_local_mode
100
+ saved_mode = thread_local_mode
99
101
  self.thread_local_mode = new_mode
100
102
  block.call
101
103
  ensure
@@ -1,3 +1,3 @@
1
1
  module SwitchPoint
2
- VERSION = "0.6.0"
2
+ VERSION = '0.7.0'
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -57,8 +57,8 @@ RSpec.configure do |config|
57
57
  end
58
58
 
59
59
  config.after(:suite) do
60
- ActiveRecord::Base.configurations.each_value do |config|
61
- FileUtils.rm_f(config[:database])
60
+ ActiveRecord::Base.configurations.each_value do |c|
61
+ FileUtils.rm_f(c[:database])
62
62
  end
63
63
  end
64
64
 
@@ -33,8 +33,8 @@ RSpec.describe SwitchPoint::Model do
33
33
 
34
34
  context 'when auto_writable is disabled' do
35
35
  it 'raises error when destructive query is requested in readonly mode' do
36
- expect { Book.create }.to raise_error(SwitchPoint::Connection::ReadonlyError)
37
- expect { Book.with_readonly { Book.create } }.to raise_error(SwitchPoint::Connection::ReadonlyError)
36
+ expect { Book.create }.to raise_error(SwitchPoint::ReadonlyError)
37
+ expect { Book.with_readonly { Book.create } }.to raise_error(SwitchPoint::ReadonlyError)
38
38
  expect { Book.with_writable { Book.create } }.to_not raise_error
39
39
  end
40
40
  end
@@ -217,7 +217,7 @@ RSpec.describe SwitchPoint::Model do
217
217
  expect(Book.connection.query_cache.size).to eq(1)
218
218
  Book.with_writable do
219
219
  Book.create
220
- FileUtils.cp('main_writable.sqlite3', 'main_readonly.sqlite3') # XXX: emulate replication
220
+ FileUtils.cp('main_writable.sqlite3', 'main_readonly.sqlite3') # XXX: emulate replication
221
221
  end
222
222
  expect(Book.connection.query_cache.size).to eq(0)
223
223
  expect(Book.count).to eq(1)
@@ -227,8 +227,8 @@ RSpec.describe SwitchPoint::Model do
227
227
  end
228
228
 
229
229
  context 'without use_switch_point' do
230
- it 'bypasses given block' do
231
- expect(Note.with_writable { :bypass }).to eq(:bypass)
230
+ it 'raises error' do
231
+ expect { Note.with_writable { :bypass } }.to raise_error(SwitchPoint::UnconfiguredError)
232
232
  end
233
233
  end
234
234
 
@@ -263,7 +263,7 @@ RSpec.describe SwitchPoint::Model do
263
263
 
264
264
  describe '#with_mode' do
265
265
  it 'raises error if unknown mode is given' do
266
- expect { SwitchPoint::ProxyRepository.checkout(:main).with_mode(:typo) }.to raise_error
266
+ expect { SwitchPoint::ProxyRepository.checkout(:main).with_mode(:typo) }.to raise_error(ArgumentError)
267
267
  end
268
268
  end
269
269
 
@@ -291,14 +291,14 @@ RSpec.describe SwitchPoint::Model do
291
291
  end
292
292
 
293
293
  describe '.transaction_with' do
294
- context "when each model has a same writable" do
294
+ context 'when each model has a same writable' do
295
295
  before do
296
296
  @before_book_count = Book.count
297
297
  @before_book2_count = Book2.count
298
298
 
299
299
  Book.transaction_with(Book2) do
300
- new_book = Book.create
301
- new_book2 = Book2.create
300
+ Book.create
301
+ Book2.create
302
302
  end
303
303
 
304
304
  @after_book_count = Book.with_writable do
@@ -324,25 +324,25 @@ RSpec.describe SwitchPoint::Model do
324
324
  end
325
325
  end
326
326
 
327
- context "when each model has a other writable" do
327
+ context 'when each model has a other writable' do
328
328
  it {
329
329
  expect {
330
330
  Book.transaction_with(Book3) do
331
- new_book = Book.create
332
- new_book3 = Book3.create
331
+ Book.create
332
+ Book3.create
333
333
  end
334
- }.to raise_error RuntimeError
334
+ }.to raise_error(SwitchPoint::Error)
335
335
  }
336
336
  end
337
337
 
338
- context "when raise exception in transaction that include some model, and models each have other writable" do
338
+ context 'when raise exception in transaction that include some model, and models each have other writable' do
339
339
  before do
340
340
  @before_book_count = Book.count
341
341
  @before_book3_count = Book3.count
342
342
 
343
343
  Book.transaction_with(Book2) do
344
- new_book = Book.create
345
- new_book3 = Book3.with_writable do
344
+ Book.create
345
+ Book3.with_writable do
346
346
  Book3.create
347
347
  end
348
348
  raise ActiveRecord::Rollback
@@ -366,7 +366,7 @@ RSpec.describe SwitchPoint::Model do
366
366
  end
367
367
  end
368
368
 
369
- context "when nested transaction_with then parent transaction rollbacked" do
369
+ context 'when nested transaction_with then parent transaction rollbacked' do
370
370
  before do
371
371
  @before_book_count = Book.count
372
372
  @before_book3_count = Book3.count
@@ -7,7 +7,7 @@ class TestApp
7
7
  [Nanika1, Nanika2].each do |model|
8
8
  r = model.with_readonly { model.connection.query_cache_enabled }
9
9
  w = model.with_writable { model.connection.query_cache_enabled }
10
- state[model.name] = { readonly: r, writable: r }
10
+ state[model.name] = { readonly: r, writable: w }
11
11
  end
12
12
  env[:state] = state
13
13
  :result
@@ -45,7 +45,7 @@ RSpec.describe SwitchPoint::QueryCache do
45
45
  expect(app.call(env)).to eq(:result)
46
46
  expect(env[:state]).to eq(
47
47
  'Nanika1' => { readonly: true, writable: true },
48
- 'Nanika2' => { readonly: false, writable: false },
48
+ 'Nanika2' => { readonly: false, writable: true },
49
49
  )
50
50
  end
51
51
  end
data/switch_point.gemspec CHANGED
@@ -4,27 +4,28 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'switch_point/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "switch_point"
7
+ spec.name = 'switch_point'
8
8
  spec.version = SwitchPoint::VERSION
9
- spec.authors = ["Kohei Suzuki"]
10
- spec.email = ["eagletmt@gmail.com"]
11
- spec.summary = %q{Switching database connection between readonly one and writable one.}
12
- spec.description = %q{Switching database connection between readonly one and writable one.}
13
- spec.homepage = "https://github.com/eagletmt/switch_point"
14
- spec.license = "MIT"
9
+ spec.authors = ['Kohei Suzuki']
10
+ spec.email = ['eagletmt@gmail.com']
11
+ spec.summary = 'Switching database connection between readonly one and writable one.'
12
+ spec.description = 'Switching database connection between readonly one and writable one.'
13
+ spec.homepage = 'https://github.com/eagletmt/switch_point'
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency "appraisal"
22
- spec.add_development_dependency "benchmark-ips"
23
- spec.add_development_dependency "bundler"
24
- spec.add_development_dependency "coveralls"
25
- spec.add_development_dependency "rack"
26
- spec.add_development_dependency "rake"
27
- spec.add_development_dependency "rspec", ">= 3.0"
28
- spec.add_development_dependency "sqlite3"
29
- spec.add_dependency "activerecord", ">= 3.2.0"
21
+ spec.add_development_dependency 'appraisal'
22
+ spec.add_development_dependency 'benchmark-ips'
23
+ spec.add_development_dependency 'bundler'
24
+ spec.add_development_dependency 'coveralls'
25
+ spec.add_development_dependency 'rack'
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rspec', '>= 3.0'
28
+ spec.add_development_dependency 'rubocop'
29
+ spec.add_development_dependency 'sqlite3'
30
+ spec.add_dependency 'activerecord', '>= 3.2.0'
30
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switch_point
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kohei Suzuki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-13 00:00:00.000000000 Z
11
+ date: 2015-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appraisal
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: sqlite3
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -145,6 +159,8 @@ extra_rdoc_files: []
145
159
  files:
146
160
  - ".gitignore"
147
161
  - ".rspec"
162
+ - ".rubocop.yml"
163
+ - ".rubocop_todo.yml"
148
164
  - ".travis.yml"
149
165
  - Appraisals
150
166
  - CHANGELOG.md
@@ -162,6 +178,7 @@ files:
162
178
  - lib/switch_point.rb
163
179
  - lib/switch_point/config.rb
164
180
  - lib/switch_point/connection.rb
181
+ - lib/switch_point/error.rb
165
182
  - lib/switch_point/model.rb
166
183
  - lib/switch_point/proxy.rb
167
184
  - lib/switch_point/proxy_repository.rb
@@ -193,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
210
  version: '0'
194
211
  requirements: []
195
212
  rubyforge_project:
196
- rubygems_version: 2.4.5
213
+ rubygems_version: 2.4.5.1
197
214
  signing_key:
198
215
  specification_version: 4
199
216
  summary: Switching database connection between readonly one and writable one.