switch_point 0.7.0 → 0.8.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: 3044b7a1ae2a9a6e022a82440cfaeaaabe90da64
4
- data.tar.gz: 7640bd6b1a38e93f84f54c2980d32848fa2260da
3
+ metadata.gz: 4afa0b36c03b080fc4800b6237d223dc185c9128
4
+ data.tar.gz: 5c06160f5c593b5b0798535af848e925d74b1d67
5
5
  SHA512:
6
- metadata.gz: 641b875847d36f8878eb954d0a950384d770ecc4b779c36ddfd5d741c94f103c1d0422a823c8b03a1c2f610c540c8bf2aedbac9bfa423f119b0e7159c081d20d
7
- data.tar.gz: 78da2c2d465bd5660946c7b74bf1ad23c00ca2deae3af478737dcbad716316bf4d2ffccd2333359816ae81d2bb8e1c996bcc54e91f3c0e1adfd04cdf918437e1
6
+ metadata.gz: d910ecf318574828396c4bfcf384dff8ee1f6282693ff19f000115ea92c3dea3df896271ea9b62e23ccbb5baab1ea8cd7337e337bbcbad5fb407bc019ef77195
7
+ data.tar.gz: 397bfbe529603a7f4d588c013e1e3ed75774559df8a377c33dd7330651e137741de7cd65337ca2d6b98a4276d02520f5cb5106d7ec75dc99f6f0521cdb7185a3
@@ -1,8 +1,17 @@
1
1
  inherit_from: .rubocop_todo.yml
2
2
 
3
+ AllCops:
4
+ DisplayCopNames: true
5
+
6
+ Performance/RedundantBlockCall:
7
+ Enabled: false
8
+
3
9
  Style/AlignParameters:
4
10
  Enabled: false
5
11
 
12
+ Style/Alias:
13
+ EnforcedStyle: prefer_alias_method
14
+
6
15
  Style/BlockDelimiters:
7
16
  Enabled: false
8
17
 
@@ -32,5 +41,8 @@ Style/RaiseArgs:
32
41
  Style/SignalException:
33
42
  Enabled: false
34
43
 
35
- Style/TrailingComma:
44
+ Style/TrailingCommaInLiteral:
45
+ Enabled: false
46
+
47
+ Style/TrailingCommaInArguments:
36
48
  Enabled: false
@@ -1,14 +1,16 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
4
- - 2.1
5
- - 2.2
3
+ - 2.2.5
4
+ - 2.3.1
5
+ - jruby-9.1.0.0
6
+ - jruby-head
6
7
  - ruby-head
7
8
  gemfile:
8
9
  - gemfiles/rails_3.2.gemfile
9
10
  - gemfiles/rails_4.0.gemfile
10
11
  - gemfiles/rails_4.1.gemfile
11
12
  - gemfiles/rails_4.2.gemfile
13
+ - gemfiles/rails_5.0.gemfile
12
14
  - gemfiles/rails_edge.gemfile
13
15
  sudo: false
14
16
  after_script:
@@ -16,10 +18,9 @@ after_script:
16
18
  matrix:
17
19
  allow_failures:
18
20
  - rvm: ruby-head
21
+ - rvm: jruby-head
19
22
  - gemfile: gemfiles/rails_edge.gemfile
20
- exclude:
21
- # Rails 5 requires to run on Ruby 2.2.0 or newer.
22
- - rvm: 2.0.0
23
- gemfile: gemfiles/rails_edge.gemfile
24
- - rvm: 2.1
25
- gemfile: gemfiles/rails_edge.gemfile
23
+ # Wait for activerecord-jdbcsqlite3-adapter's support
24
+ # https://github.com/jruby/activerecord-jdbc-adapter/labels/rails-5.x
25
+ - rvm: jruby-9.1.0.0
26
+ gemfile: gemfiles/rails_5.0.gemfile
data/Appraisals CHANGED
@@ -1,22 +1,80 @@
1
1
  appraise 'rails-3.2' do
2
2
  gem 'activerecord', '~> 3.2'
3
+
4
+ platforms :ruby do
5
+ gem 'sqlite3'
6
+ end
7
+
8
+ platforms :jruby do
9
+ gem 'json'
10
+ gem 'activerecord-jdbcsqlite3-adapter'
11
+ end
3
12
  end
4
13
 
5
14
  appraise 'rails-4.0' do
6
15
  gem 'activerecord', '~> 4.0'
16
+
17
+ platforms :ruby do
18
+ gem 'sqlite3'
19
+ end
20
+
21
+ platforms :jruby do
22
+ gem 'json'
23
+ gem 'activerecord-jdbcsqlite3-adapter'
24
+ end
7
25
  end
8
26
 
9
27
  appraise 'rails-4.1' do
10
28
  gem 'activerecord', '~> 4.1'
29
+
30
+ platforms :ruby do
31
+ gem 'sqlite3'
32
+ end
33
+
34
+ platforms :jruby do
35
+ gem 'json'
36
+ gem 'activerecord-jdbcsqlite3-adapter'
37
+ end
11
38
  end
12
39
 
13
40
  appraise 'rails-4.2' do
14
41
  gem 'activerecord', '~> 4.2'
42
+
43
+ platforms :ruby do
44
+ gem 'sqlite3'
45
+ end
46
+
47
+ platforms :jruby do
48
+ gem 'json'
49
+ gem 'activerecord-jdbcsqlite3-adapter'
50
+ end
51
+ end
52
+
53
+ appraise 'rails-5.0' do
54
+ gem 'activerecord', '>= 5.0.0.rc1'
55
+
56
+ platforms :ruby do
57
+ gem 'sqlite3'
58
+ end
59
+
60
+ platforms :jruby do
61
+ gem 'json'
62
+ gem 'activerecord-jdbcsqlite3-adapter'
63
+ end
15
64
  end
16
65
 
17
66
  appraise 'rails-edge' do
18
67
  gem 'activerecord', git: 'https://github.com/rails/rails'
19
68
  gem 'arel', git: 'https://github.com/rails/arel'
69
+
70
+ platforms :ruby do
71
+ gem 'sqlite3'
72
+ end
73
+
74
+ platforms :jruby do
75
+ gem 'json'
76
+ gem 'activerecord-jdbcsqlite3-adapter', git: 'https://github.com/jruby/activerecord-jdbc-adapter', branch: 'master'
77
+ end
20
78
  end
21
79
 
22
80
  # vim: set ft=ruby:
@@ -1,3 +1,11 @@
1
+ ## 0.8.0 (2016-06-06)
2
+ - Drop Ruby 2.0.0 and 2.1 support
3
+ - Add `AR::Base#with_readonly` and `AR::Base#with_writable`
4
+ - short-hand for `AR::Base.with_readonly` and `AR::Base.with_writable`
5
+ - Add `AR::Base#transaction_with`
6
+ - short-hand for `AR::Base.transaction_with`
7
+ - Fix warnings for Rails 5.0
8
+
1
9
  ## 0.7.0 (2015-10-16)
2
10
  - `Model.with_readonly` and `Model.with_writable` now raises error when the Model doesn't use switch_point
3
11
 
data/Gemfile CHANGED
@@ -2,3 +2,12 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in switch_point.gemspec
4
4
  gemspec
5
+
6
+ platforms :ruby do
7
+ gem 'sqlite3'
8
+ end
9
+
10
+ platforms :jruby do
11
+ gem 'json'
12
+ gem 'activerecord-jdbcsqlite3-adapter'
13
+ end
@@ -4,4 +4,13 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "activerecord", "~> 3.2"
6
6
 
7
+ platforms :ruby do
8
+ gem "sqlite3"
9
+ end
10
+
11
+ platforms :jruby do
12
+ gem "json"
13
+ gem "activerecord-jdbcsqlite3-adapter"
14
+ end
15
+
7
16
  gemspec :path => "../"
@@ -4,4 +4,13 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "activerecord", "~> 4.0"
6
6
 
7
+ platforms :ruby do
8
+ gem "sqlite3"
9
+ end
10
+
11
+ platforms :jruby do
12
+ gem "json"
13
+ gem "activerecord-jdbcsqlite3-adapter"
14
+ end
15
+
7
16
  gemspec :path => "../"
@@ -4,4 +4,13 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "activerecord", "~> 4.1"
6
6
 
7
+ platforms :ruby do
8
+ gem "sqlite3"
9
+ end
10
+
11
+ platforms :jruby do
12
+ gem "json"
13
+ gem "activerecord-jdbcsqlite3-adapter"
14
+ end
15
+
7
16
  gemspec :path => "../"
@@ -4,4 +4,13 @@ source "https://rubygems.org"
4
4
 
5
5
  gem "activerecord", "~> 4.2"
6
6
 
7
+ platforms :ruby do
8
+ gem "sqlite3"
9
+ end
10
+
11
+ platforms :jruby do
12
+ gem "json"
13
+ gem "activerecord-jdbcsqlite3-adapter"
14
+ end
15
+
7
16
  gemspec :path => "../"
@@ -0,0 +1,16 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", ">= 5.0.0.rc1"
6
+
7
+ platforms :ruby do
8
+ gem "sqlite3"
9
+ end
10
+
11
+ platforms :jruby do
12
+ gem "json"
13
+ gem "activerecord-jdbcsqlite3-adapter"
14
+ end
15
+
16
+ gemspec :path => "../"
@@ -5,4 +5,13 @@ source "https://rubygems.org"
5
5
  gem "activerecord", :git => "https://github.com/rails/rails"
6
6
  gem "arel", :git => "https://github.com/rails/arel"
7
7
 
8
+ platforms :ruby do
9
+ gem "sqlite3"
10
+ end
11
+
12
+ platforms :jruby do
13
+ gem "json"
14
+ gem "activerecord-jdbcsqlite3-adapter", :git => "https://github.com/jruby/activerecord-jdbc-adapter", :branch => "master"
15
+ end
16
+
8
17
  gemspec :path => "../"
@@ -66,9 +66,6 @@ ActiveSupport.on_load(:active_record) do
66
66
 
67
67
  ActiveRecord::Base.send(:include, SwitchPoint::Model)
68
68
  ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
69
- include SwitchPoint::Connection
70
- SwitchPoint::Connection::DESTRUCTIVE_METHODS.each do |method_name|
71
- alias_method_chain method_name, :switch_point
72
- end
69
+ prepend SwitchPoint::Connection
73
70
  end
74
71
  end
@@ -4,20 +4,21 @@ require 'switch_point/proxy_repository'
4
4
  module SwitchPoint
5
5
  module Connection
6
6
  # See ActiveRecord::ConnectionAdapters::QueryCache
7
- DESTRUCTIVE_METHODS = [:insert, :update, :delete]
7
+ DESTRUCTIVE_METHODS = [:insert, :update, :delete].freeze
8
8
 
9
9
  DESTRUCTIVE_METHODS.each do |method_name|
10
- define_method(:"#{method_name}_with_switch_point") do |*args, &block|
11
- parent_method = :"#{method_name}_without_switch_point"
10
+ define_method(method_name) do |*args, &block|
12
11
  if pool.equal?(ActiveRecord::Base.connection_pool)
13
- Connection.handle_base_connection(self, parent_method, *args, &block)
12
+ Connection.handle_base_connection(self)
13
+ super(*args, &block)
14
14
  else
15
+ parent_method = method(method_name).super_method
15
16
  Connection.handle_generated_connection(self, parent_method, method_name, *args, &block)
16
17
  end
17
18
  end
18
19
  end
19
20
 
20
- def self.handle_base_connection(conn, parent_method, *args, &block)
21
+ def self.handle_base_connection(conn)
21
22
  switch_points = conn.pool.spec.config[:switch_points]
22
23
  if switch_points
23
24
  switch_points.each do |switch_point|
@@ -28,7 +29,6 @@ module SwitchPoint
28
29
  purge_readonly_query_cache(proxy)
29
30
  end
30
31
  end
31
- conn.send(parent_method, *args, &block)
32
32
  end
33
33
 
34
34
  def self.handle_generated_connection(conn, parent_method, method_name, *args, &block)
@@ -44,12 +44,12 @@ module SwitchPoint
44
44
  end
45
45
  when :writable
46
46
  purge_readonly_query_cache(proxy)
47
- conn.send(parent_method, *args, &block)
47
+ parent_method.call(*args, &block)
48
48
  else
49
49
  raise Error.new("Unknown mode #{switch_point[:mode]} is given with #{name}")
50
50
  end
51
51
  else
52
- conn.send(parent_method, *args, &block)
52
+ parent_method.call(*args, &block)
53
53
  end
54
54
  end
55
55
 
@@ -4,39 +4,26 @@ require 'switch_point/proxy_repository'
4
4
  module SwitchPoint
5
5
  module Model
6
6
  def self.included(model)
7
+ super
7
8
  model.singleton_class.class_eval do
8
9
  include ClassMethods
9
- alias_method_chain :connection, :switch_point
10
- alias_method_chain :cache, :switch_point
11
- alias_method_chain :uncached, :switch_point
10
+ prepend MonkeyPatch
12
11
  end
13
12
  end
14
13
 
15
- module ClassMethods
16
- def connection_with_switch_point
17
- if switch_point_proxy
18
- switch_point_proxy.connection
19
- else
20
- connection_without_switch_point
21
- end
22
- end
14
+ def with_readonly(&block)
15
+ self.class.with_readonly(&block)
16
+ end
23
17
 
24
- def cache_with_switch_point(&block)
25
- if switch_point_proxy
26
- switch_point_proxy.cache(&block)
27
- else
28
- cache_without_switch_point(&block)
29
- end
30
- end
18
+ def with_writable(&block)
19
+ self.class.with_writable(&block)
20
+ end
31
21
 
32
- def uncached_with_switch_point(&block)
33
- if switch_point_proxy
34
- switch_point_proxy.uncached(&block)
35
- else
36
- uncached_without_switch_point(&block)
37
- end
38
- end
22
+ def transaction_with(*models, &block)
23
+ self.class.transaction_with(*models, &block)
24
+ end
39
25
 
26
+ module ClassMethods
40
27
  def with_readonly(&block)
41
28
  if switch_point_proxy
42
29
  switch_point_proxy.with_readonly(&block)
@@ -59,7 +46,7 @@ module SwitchPoint
59
46
  end
60
47
 
61
48
  def switch_point_proxy
62
- if @switch_point_name
49
+ if defined?(@switch_point_name)
63
50
  ProxyRepository.checkout(@switch_point_name)
64
51
  elsif self == ActiveRecord::Base
65
52
  nil
@@ -97,5 +84,31 @@ module SwitchPoint
97
84
  writable_switch_points.uniq.size == 1
98
85
  end
99
86
  end
87
+
88
+ module MonkeyPatch
89
+ def connection
90
+ if switch_point_proxy
91
+ switch_point_proxy.connection
92
+ else
93
+ super
94
+ end
95
+ end
96
+
97
+ def cache(&block)
98
+ if switch_point_proxy
99
+ switch_point_proxy.cache(&block)
100
+ else
101
+ super
102
+ end
103
+ end
104
+
105
+ def uncached(&block)
106
+ if switch_point_proxy
107
+ switch_point_proxy.uncached(&block)
108
+ else
109
+ super
110
+ end
111
+ end
112
+ end
100
113
  end
101
114
  end
@@ -4,7 +4,7 @@ module SwitchPoint
4
4
  class Proxy
5
5
  attr_reader :initial_name
6
6
 
7
- AVAILABLE_MODES = [:writable, :readonly]
7
+ AVAILABLE_MODES = [:writable, :readonly].freeze
8
8
  DEFAULT_MODE = :readonly
9
9
 
10
10
  def initialize(name)
@@ -1,3 +1,3 @@
1
1
  module SwitchPoint
2
- VERSION = '0.7.0'
2
+ VERSION = '0.8.0'.freeze
3
3
  end
@@ -85,7 +85,12 @@ class DerivedNanika2 < AbstractNanika
85
85
  use_switch_point :main2
86
86
  end
87
87
 
88
- base = { adapter: 'sqlite3' }
88
+ base =
89
+ if RUBY_PLATFORM == 'java'
90
+ { adapter: 'jdbcsqlite3' }
91
+ else
92
+ { adapter: 'sqlite3' }
93
+ end
89
94
  ActiveRecord::Base.configurations = {
90
95
  'main_readonly' => base.merge(database: 'main_readonly.sqlite3'),
91
96
  'main_writable' => base.merge(database: 'main_writable.sqlite3'),
@@ -1,7 +1,7 @@
1
1
  require 'coveralls'
2
2
  require 'simplecov'
3
3
 
4
- SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
4
+ SimpleCov.formatters = [
5
5
  SimpleCov::Formatter::HTMLFormatter,
6
6
  Coveralls::SimpleCov::Formatter,
7
7
  ]
@@ -242,6 +242,16 @@ RSpec.describe SwitchPoint::Model do
242
242
  end
243
243
  end
244
244
 
245
+ describe '#with_writable' do
246
+ it 'behaves like .with_writable' do
247
+ book = Book.with_writable { Book.create! }
248
+ book.with_writable do
249
+ expect(Book).to connect_to('main_writable.sqlite3')
250
+ end
251
+ expect(Book).to connect_to('main_readonly.sqlite3')
252
+ end
253
+ end
254
+
245
255
  describe '.with_readonly' do
246
256
  context 'when writable! is called globally' do
247
257
  before do
@@ -261,6 +271,24 @@ RSpec.describe SwitchPoint::Model do
261
271
  end
262
272
  end
263
273
 
274
+ describe '#with_readonly' do
275
+ before do
276
+ SwitchPoint.writable!(:main)
277
+ end
278
+
279
+ after do
280
+ SwitchPoint.readonly!(:main)
281
+ end
282
+
283
+ it 'behaves like .with_readonly' do
284
+ book = Book.create!
285
+ book.with_readonly do
286
+ expect(Book).to connect_to('main_readonly.sqlite3')
287
+ end
288
+ expect(Book).to connect_to('main_writable.sqlite3')
289
+ end
290
+ end
291
+
264
292
  describe '#with_mode' do
265
293
  it 'raises error if unknown mode is given' do
266
294
  expect { SwitchPoint::ProxyRepository.checkout(:main).with_mode(:typo) }.to raise_error(ArgumentError)
@@ -398,6 +426,20 @@ RSpec.describe SwitchPoint::Model do
398
426
  end
399
427
  end
400
428
 
429
+ describe '#transaction_with' do
430
+ it 'behaves like .transaction_with' do
431
+ book = Book.with_writable { Book.create! }
432
+ expect(Book.with_writable { Book.count }).to eq(1)
433
+ book.transaction_with(Book2) do
434
+ Book.create!
435
+ raise ActiveRecord::Rollback
436
+ end
437
+ expect(Book.with_writable { Book.count }).to eq(1)
438
+
439
+ expect { book.transaction_with(Book3) {} }.to raise_error(SwitchPoint::Error)
440
+ end
441
+ end
442
+
401
443
  describe '.cache' do
402
444
  it 'enables query cache for both readonly and writable' do
403
445
  Book.connection
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
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
19
  spec.require_paths = ['lib']
20
+ spec.required_ruby_version = '>= 2.2.0'
20
21
 
21
22
  spec.add_development_dependency 'appraisal'
22
23
  spec.add_development_dependency 'benchmark-ips'
@@ -25,7 +26,7 @@ Gem::Specification.new do |spec|
25
26
  spec.add_development_dependency 'rack'
26
27
  spec.add_development_dependency 'rake'
27
28
  spec.add_development_dependency 'rspec', '>= 3.0'
28
- spec.add_development_dependency 'rubocop'
29
- spec.add_development_dependency 'sqlite3'
29
+ spec.add_development_dependency 'rubocop', '>= 0.37.0'
30
+ spec.add_development_dependency 'simplecov', '>= 0.9.0'
30
31
  spec.add_dependency 'activerecord', '>= 3.2.0'
31
32
  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.7.0
4
+ version: 0.8.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-10-16 00:00:00.000000000 Z
11
+ date: 2016-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appraisal
@@ -114,28 +114,28 @@ dependencies:
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: '0'
117
+ version: 0.37.0
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: '0'
124
+ version: 0.37.0
125
125
  - !ruby/object:Gem::Dependency
126
- name: sqlite3
126
+ name: simplecov
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: '0'
131
+ version: 0.9.0
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: '0'
138
+ version: 0.9.0
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: activerecord
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -174,6 +174,7 @@ files:
174
174
  - gemfiles/rails_4.0.gemfile
175
175
  - gemfiles/rails_4.1.gemfile
176
176
  - gemfiles/rails_4.2.gemfile
177
+ - gemfiles/rails_5.0.gemfile
177
178
  - gemfiles/rails_edge.gemfile
178
179
  - lib/switch_point.rb
179
180
  - lib/switch_point/config.rb
@@ -202,7 +203,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
202
203
  requirements:
203
204
  - - ">="
204
205
  - !ruby/object:Gem::Version
205
- version: '0'
206
+ version: 2.2.0
206
207
  required_rubygems_version: !ruby/object:Gem::Requirement
207
208
  requirements:
208
209
  - - ">="
@@ -210,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
211
  version: '0'
211
212
  requirements: []
212
213
  rubyforge_project:
213
- rubygems_version: 2.4.5.1
214
+ rubygems_version: 2.5.1
214
215
  signing_key:
215
216
  specification_version: 4
216
217
  summary: Switching database connection between readonly one and writable one.