switch_point 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.rubocop.yml +23 -15
- data/.rubocop_todo.yml +0 -15
- data/.travis.yml +17 -13
- data/Appraisals +26 -39
- data/CHANGELOG.md +4 -0
- data/Gemfile +2 -5
- data/README.md +17 -0
- data/Rakefile +3 -1
- data/benchmark/proxy.rb +3 -1
- data/gemfiles/rails_3.2.gemfile +3 -8
- data/gemfiles/rails_4.0.gemfile +3 -8
- data/gemfiles/rails_4.1.gemfile +3 -8
- data/gemfiles/rails_4.2.gemfile +3 -8
- data/gemfiles/rails_5.0.gemfile +3 -8
- data/gemfiles/rails_5.1.gemfile +11 -0
- data/gemfiles/rails_5.2.gemfile +11 -0
- data/gemfiles/rails_6.0.gemfile +11 -0
- data/lib/switch_point.rb +5 -3
- data/lib/switch_point/config.rb +7 -0
- data/lib/switch_point/connection.rb +4 -1
- data/lib/switch_point/error.rb +2 -0
- data/lib/switch_point/model.rb +2 -0
- data/lib/switch_point/proxy.rb +6 -2
- data/lib/switch_point/proxy_repository.rb +2 -0
- data/lib/switch_point/query_cache.rb +2 -0
- data/lib/switch_point/version.rb +3 -1
- data/spec/models.rb +12 -4
- data/spec/spec_helper.rb +12 -2
- data/spec/switch_point/model_spec.rb +3 -1
- data/spec/switch_point/query_cache_spec.rb +13 -1
- data/spec/switch_point_spec.rb +46 -0
- data/switch_point.gemspec +8 -7
- metadata +24 -17
- data/gemfiles/rails_edge.gemfile +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2e3915fe82c56723bcc2ac31421b8158cadde91d89378ff5769c846cba2899fd
|
4
|
+
data.tar.gz: c98d107a1108cbacb334823219e06d14cfafdc24ba9f4dc821f479d96b531987
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2eec3b0ca5d8d22b5fb9f302c0c9b7dfa71409c7e1d49a8bca4d760eda64f4b9bb1d4779284fa3cccfdebee734aa2d67bb66de315519f461de6d98c983aa7365
|
7
|
+
data.tar.gz: 2ed6d067c24d5642f5e92267014563898c4212fc5c2e31c6cf892e5846979f53c33c139a9528762a3f5058272ad13ea294a60e6158e7ea247c20600156e7168b
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,48 +1,56 @@
|
|
1
1
|
inherit_from: .rubocop_todo.yml
|
2
2
|
|
3
3
|
AllCops:
|
4
|
+
TargetRubyVersion: 2.5
|
4
5
|
DisplayCopNames: true
|
6
|
+
NewCops: enable
|
7
|
+
Exclude:
|
8
|
+
- gemfiles/*.gemfile
|
5
9
|
|
6
|
-
|
10
|
+
Layout/ArgumentAlignment:
|
7
11
|
Enabled: false
|
8
|
-
|
9
|
-
|
12
|
+
Layout/LineLength:
|
13
|
+
Enabled: false
|
14
|
+
Layout/ParameterAlignment:
|
10
15
|
Enabled: false
|
11
16
|
|
17
|
+
Naming/FileName:
|
18
|
+
Exclude:
|
19
|
+
- Appraisals
|
20
|
+
|
21
|
+
Style/AccessModifierDeclarations:
|
22
|
+
Enabled: false
|
12
23
|
Style/Alias:
|
13
24
|
EnforcedStyle: prefer_alias_method
|
14
|
-
|
15
25
|
Style/BlockDelimiters:
|
16
26
|
Enabled: false
|
17
|
-
|
18
27
|
Style/GuardClause:
|
19
28
|
Enabled: false
|
20
|
-
|
21
29
|
Style/HashSyntax:
|
22
30
|
Exclude:
|
23
31
|
- Rakefile
|
24
|
-
|
25
32
|
Style/IfUnlessModifier:
|
26
33
|
Enabled: false
|
27
|
-
|
28
34
|
Style/Lambda:
|
29
35
|
Enabled: false
|
30
|
-
|
31
36
|
Style/Next:
|
32
37
|
Enabled: false
|
33
|
-
|
34
38
|
Style/PercentLiteralDelimiters:
|
35
39
|
PreferredDelimiters:
|
36
40
|
'%w': '[]'
|
37
|
-
|
38
41
|
Style/RaiseArgs:
|
39
42
|
EnforcedStyle: compact
|
40
|
-
|
43
|
+
Style/SafeNavigation:
|
44
|
+
Enabled: false
|
41
45
|
Style/SignalException:
|
42
46
|
Enabled: false
|
43
|
-
|
44
|
-
Style/TrailingCommaInLiteral:
|
47
|
+
Style/SoleNestedConditional:
|
45
48
|
Enabled: false
|
46
|
-
|
47
49
|
Style/TrailingCommaInArguments:
|
48
50
|
Enabled: false
|
51
|
+
Style/TrailingCommaInArrayLiteral:
|
52
|
+
Enabled: false
|
53
|
+
Style/TrailingCommaInHashLiteral:
|
54
|
+
Enabled: false
|
55
|
+
Metrics:
|
56
|
+
Enabled: false
|
data/.rubocop_todo.yml
CHANGED
@@ -1,17 +1,2 @@
|
|
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
1
|
Style/Documentation:
|
17
2
|
Enabled: false
|
data/.travis.yml
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
- 2.
|
4
|
-
- 2.
|
5
|
-
-
|
6
|
-
- jruby-head
|
3
|
+
- 2.5.8
|
4
|
+
- 2.6.6
|
5
|
+
- 2.7.2
|
7
6
|
- ruby-head
|
8
7
|
gemfile:
|
9
8
|
- gemfiles/rails_3.2.gemfile
|
@@ -11,16 +10,21 @@ gemfile:
|
|
11
10
|
- gemfiles/rails_4.1.gemfile
|
12
11
|
- gemfiles/rails_4.2.gemfile
|
13
12
|
- gemfiles/rails_5.0.gemfile
|
14
|
-
- gemfiles/
|
15
|
-
|
13
|
+
- gemfiles/rails_5.1.gemfile
|
14
|
+
- gemfiles/rails_5.2.gemfile
|
15
|
+
- gemfiles/rails_6.0.gemfile
|
16
16
|
after_script:
|
17
17
|
- bundle exec rake benchmark
|
18
|
-
|
18
|
+
jobs:
|
19
19
|
allow_failures:
|
20
20
|
- rvm: ruby-head
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
21
|
+
exclude:
|
22
|
+
# Rails 3.2 - 4.2 doesn't support Ruby 2.7
|
23
|
+
- rvm: 2.7.2
|
24
|
+
gemfile: gemfiles/rails_3.2.gemfile
|
25
|
+
- rvm: 2.7.2
|
26
|
+
gemfile: gemfiles/rails_4.0.gemfile
|
27
|
+
- rvm: 2.7.2
|
28
|
+
gemfile: gemfiles/rails_4.1.gemfile
|
29
|
+
- rvm: 2.7.2
|
30
|
+
gemfile: gemfiles/rails_4.2.gemfile
|
data/Appraisals
CHANGED
@@ -1,79 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
appraise 'rails-3.2' do
|
2
|
-
gem 'activerecord', '~> 3.2'
|
4
|
+
gem 'activerecord', '~> 3.2.0'
|
3
5
|
|
4
6
|
platforms :ruby do
|
5
|
-
gem 'sqlite3'
|
6
|
-
end
|
7
|
-
|
8
|
-
platforms :jruby do
|
9
|
-
gem 'json'
|
10
|
-
gem 'activerecord-jdbcsqlite3-adapter'
|
7
|
+
gem 'sqlite3', '~> 1.3.5'
|
11
8
|
end
|
12
9
|
end
|
13
10
|
|
14
11
|
appraise 'rails-4.0' do
|
15
|
-
gem 'activerecord', '~> 4.0'
|
12
|
+
gem 'activerecord', '~> 4.0.0'
|
16
13
|
|
17
14
|
platforms :ruby do
|
18
|
-
gem 'sqlite3'
|
19
|
-
end
|
20
|
-
|
21
|
-
platforms :jruby do
|
22
|
-
gem 'json'
|
23
|
-
gem 'activerecord-jdbcsqlite3-adapter'
|
15
|
+
gem 'sqlite3', '~> 1.3.6'
|
24
16
|
end
|
25
17
|
end
|
26
18
|
|
27
19
|
appraise 'rails-4.1' do
|
28
|
-
gem 'activerecord', '~> 4.1'
|
20
|
+
gem 'activerecord', '~> 4.1.0'
|
29
21
|
|
30
22
|
platforms :ruby do
|
31
|
-
gem 'sqlite3'
|
32
|
-
end
|
33
|
-
|
34
|
-
platforms :jruby do
|
35
|
-
gem 'json'
|
36
|
-
gem 'activerecord-jdbcsqlite3-adapter'
|
23
|
+
gem 'sqlite3', '~> 1.3.6'
|
37
24
|
end
|
38
25
|
end
|
39
26
|
|
40
27
|
appraise 'rails-4.2' do
|
41
|
-
gem 'activerecord', '~> 4.2'
|
28
|
+
gem 'activerecord', '~> 4.2.0'
|
42
29
|
|
43
30
|
platforms :ruby do
|
44
|
-
gem 'sqlite3'
|
45
|
-
end
|
46
|
-
|
47
|
-
platforms :jruby do
|
48
|
-
gem 'json'
|
49
|
-
gem 'activerecord-jdbcsqlite3-adapter'
|
31
|
+
gem 'sqlite3', '~> 1.3.6'
|
50
32
|
end
|
51
33
|
end
|
52
34
|
|
53
35
|
appraise 'rails-5.0' do
|
54
|
-
gem 'activerecord', '
|
36
|
+
gem 'activerecord', '~> 5.0.0'
|
55
37
|
|
56
38
|
platforms :ruby do
|
57
|
-
gem 'sqlite3'
|
39
|
+
gem 'sqlite3', '~> 1.3.6'
|
58
40
|
end
|
41
|
+
end
|
42
|
+
|
43
|
+
appraise 'rails-5.1' do
|
44
|
+
gem 'activerecord', '~> 5.1.0'
|
59
45
|
|
60
|
-
platforms :
|
61
|
-
gem '
|
62
|
-
gem 'activerecord-jdbcsqlite3-adapter'
|
46
|
+
platforms :ruby do
|
47
|
+
gem 'sqlite3'
|
63
48
|
end
|
64
49
|
end
|
65
50
|
|
66
|
-
appraise 'rails-
|
67
|
-
gem 'activerecord',
|
68
|
-
gem 'arel', git: 'https://github.com/rails/arel'
|
51
|
+
appraise 'rails-5.2' do
|
52
|
+
gem 'activerecord', '~> 5.2.0'
|
69
53
|
|
70
54
|
platforms :ruby do
|
71
55
|
gem 'sqlite3'
|
72
56
|
end
|
57
|
+
end
|
58
|
+
|
59
|
+
appraise 'rails-6.0' do
|
60
|
+
gem 'activerecord', '~> 6.0.0'
|
73
61
|
|
74
|
-
platforms :
|
75
|
-
gem '
|
76
|
-
gem 'activerecord-jdbcsqlite3-adapter', git: 'https://github.com/jruby/activerecord-jdbc-adapter', branch: 'master'
|
62
|
+
platforms :ruby do
|
63
|
+
gem 'sqlite3'
|
77
64
|
end
|
78
65
|
end
|
79
66
|
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
source 'https://rubygems.org'
|
2
4
|
|
3
5
|
# Specify your gem's dependencies in switch_point.gemspec
|
@@ -6,8 +8,3 @@ gemspec
|
|
6
8
|
platforms :ruby do
|
7
9
|
gem 'sqlite3'
|
8
10
|
end
|
9
|
-
|
10
|
-
platforms :jruby do
|
11
|
-
gem 'json'
|
12
|
-
gem 'activerecord-jdbcsqlite3-adapter'
|
13
|
-
end
|
data/README.md
CHANGED
@@ -6,6 +6,23 @@
|
|
6
6
|
|
7
7
|
Switching database connection between readonly one and writable one.
|
8
8
|
|
9
|
+
## Maintenance notice
|
10
|
+
switch_point won't support upcoming ActiveRecord v6.1 or later.
|
11
|
+
Developers should use the builtin multiple database feature introduced in ActiveRecord v6.0.
|
12
|
+
https://guides.rubyonrails.org/active_record_multiple_databases.html
|
13
|
+
Thus the supported ActiveRecord version is v3.2, v4.0, v4.1, v4.2, v5.0, v5.1, and v5.2.
|
14
|
+
|
15
|
+
switch_point won't accept any new features. Bug fixes might be accepted.
|
16
|
+
If you'd like to add a new feature (and/or support ActiveRecord >= v6.1), feel free to fork switch_point gem.
|
17
|
+
|
18
|
+
### Migration from switch_point to ActiveRecord multiple database feature
|
19
|
+
1. Upgrade your activerecord gem to v6.0
|
20
|
+
- ActiveRecord v6.0 is the only series which supports both builtin multiple database feature and switch_point.
|
21
|
+
2. Change your application to use ActiveRecord multiple database feature
|
22
|
+
- If you'd like to keep the number of connections during this step, it would require some tricks.
|
23
|
+
3. Remove switch_point gem from your Gemfile
|
24
|
+
4. Upgrade your activerecord gem to v6.1 or later
|
25
|
+
|
9
26
|
## Installation
|
10
27
|
|
11
28
|
Add this line to your application's Gemfile:
|
data/Rakefile
CHANGED
data/benchmark/proxy.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'benchmark/ips'
|
2
4
|
require 'switch_point'
|
3
5
|
require 'active_record'
|
@@ -32,7 +34,7 @@ ActiveRecord::Base.configurations = {
|
|
32
34
|
ActiveRecord::Base.establish_connection(:default)
|
33
35
|
|
34
36
|
Plain.connection.execute('CREATE TABLE plains (id integer primary key autoincrement)')
|
35
|
-
[
|
37
|
+
%i[readonly writable].each do |mode|
|
36
38
|
ProxyBase.public_send("with_#{mode}") do
|
37
39
|
%w[proxy1s proxy2s].each do |table|
|
38
40
|
ProxyBase.connection.execute("CREATE TABLE #{table} (id integer primary key autoincrement)")
|
data/gemfiles/rails_3.2.gemfile
CHANGED
@@ -2,15 +2,10 @@
|
|
2
2
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
|
5
|
-
gem "activerecord", "~> 3.2"
|
5
|
+
gem "activerecord", "~> 3.2.0"
|
6
6
|
|
7
7
|
platforms :ruby do
|
8
|
-
gem "sqlite3"
|
8
|
+
gem "sqlite3", "~> 1.3.5"
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
gem "json"
|
13
|
-
gem "activerecord-jdbcsqlite3-adapter"
|
14
|
-
end
|
15
|
-
|
16
|
-
gemspec :path => "../"
|
11
|
+
gemspec path: "../"
|
data/gemfiles/rails_4.0.gemfile
CHANGED
@@ -2,15 +2,10 @@
|
|
2
2
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
|
5
|
-
gem "activerecord", "~> 4.0"
|
5
|
+
gem "activerecord", "~> 4.0.0"
|
6
6
|
|
7
7
|
platforms :ruby do
|
8
|
-
gem "sqlite3"
|
8
|
+
gem "sqlite3", "~> 1.3.6"
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
gem "json"
|
13
|
-
gem "activerecord-jdbcsqlite3-adapter"
|
14
|
-
end
|
15
|
-
|
16
|
-
gemspec :path => "../"
|
11
|
+
gemspec path: "../"
|
data/gemfiles/rails_4.1.gemfile
CHANGED
@@ -2,15 +2,10 @@
|
|
2
2
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
|
5
|
-
gem "activerecord", "~> 4.1"
|
5
|
+
gem "activerecord", "~> 4.1.0"
|
6
6
|
|
7
7
|
platforms :ruby do
|
8
|
-
gem "sqlite3"
|
8
|
+
gem "sqlite3", "~> 1.3.6"
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
gem "json"
|
13
|
-
gem "activerecord-jdbcsqlite3-adapter"
|
14
|
-
end
|
15
|
-
|
16
|
-
gemspec :path => "../"
|
11
|
+
gemspec path: "../"
|
data/gemfiles/rails_4.2.gemfile
CHANGED
@@ -2,15 +2,10 @@
|
|
2
2
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
|
5
|
-
gem "activerecord", "~> 4.2"
|
5
|
+
gem "activerecord", "~> 4.2.0"
|
6
6
|
|
7
7
|
platforms :ruby do
|
8
|
-
gem "sqlite3"
|
8
|
+
gem "sqlite3", "~> 1.3.6"
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
gem "json"
|
13
|
-
gem "activerecord-jdbcsqlite3-adapter"
|
14
|
-
end
|
15
|
-
|
16
|
-
gemspec :path => "../"
|
11
|
+
gemspec path: "../"
|
data/gemfiles/rails_5.0.gemfile
CHANGED
@@ -2,15 +2,10 @@
|
|
2
2
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
|
5
|
-
gem "activerecord", "
|
5
|
+
gem "activerecord", "~> 5.0.0"
|
6
6
|
|
7
7
|
platforms :ruby do
|
8
|
-
gem "sqlite3"
|
8
|
+
gem "sqlite3", "~> 1.3.6"
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
gem "json"
|
13
|
-
gem "activerecord-jdbcsqlite3-adapter"
|
14
|
-
end
|
15
|
-
|
16
|
-
gemspec :path => "../"
|
11
|
+
gemspec path: "../"
|
data/lib/switch_point.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support/lazy_load_hooks'
|
2
4
|
require 'switch_point/config'
|
3
5
|
require 'switch_point/version'
|
@@ -13,7 +15,7 @@ module SwitchPoint
|
|
13
15
|
end
|
14
16
|
|
15
17
|
def readonly_all!
|
16
|
-
config.
|
18
|
+
config.each_key do |name|
|
17
19
|
readonly!(name)
|
18
20
|
end
|
19
21
|
end
|
@@ -23,7 +25,7 @@ module SwitchPoint
|
|
23
25
|
end
|
24
26
|
|
25
27
|
def writable_all!
|
26
|
-
config.
|
28
|
+
config.each_key do |name|
|
27
29
|
writable!(name)
|
28
30
|
end
|
29
31
|
end
|
@@ -64,7 +66,7 @@ ActiveSupport.on_load(:active_record) do
|
|
64
66
|
require 'switch_point/model'
|
65
67
|
require 'switch_point/query_cache'
|
66
68
|
|
67
|
-
ActiveRecord::Base.
|
69
|
+
ActiveRecord::Base.include SwitchPoint::Model
|
68
70
|
ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
|
69
71
|
prepend SwitchPoint::Connection
|
70
72
|
end
|
data/lib/switch_point/config.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SwitchPoint
|
2
4
|
class Config
|
3
5
|
attr_accessor :auto_writable
|
@@ -34,12 +36,17 @@ module SwitchPoint
|
|
34
36
|
switch_points.keys
|
35
37
|
end
|
36
38
|
|
39
|
+
def each_key(&block)
|
40
|
+
switch_points.each_key(&block)
|
41
|
+
end
|
42
|
+
|
37
43
|
private
|
38
44
|
|
39
45
|
def assert_valid_config!(config)
|
40
46
|
unless config.key?(:readonly) || config.key?(:writable)
|
41
47
|
raise ArgumentError.new(':readonly or :writable must be specified')
|
42
48
|
end
|
49
|
+
|
43
50
|
if config.key?(:readonly)
|
44
51
|
unless config[:readonly].is_a?(Symbol)
|
45
52
|
raise TypeError.new(":readonly's value must be Symbol")
|
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'switch_point/error'
|
2
4
|
require 'switch_point/proxy_repository'
|
3
5
|
|
4
6
|
module SwitchPoint
|
5
7
|
module Connection
|
6
8
|
# See ActiveRecord::ConnectionAdapters::QueryCache
|
7
|
-
DESTRUCTIVE_METHODS = [
|
9
|
+
DESTRUCTIVE_METHODS = %i[insert update delete].freeze
|
8
10
|
|
9
11
|
DESTRUCTIVE_METHODS.each do |method_name|
|
10
12
|
define_method(method_name) do |*args, &block|
|
@@ -26,6 +28,7 @@ module SwitchPoint
|
|
26
28
|
if switch_point[:mode] != :writable
|
27
29
|
raise Error.new("ActiveRecord::Base's switch_points must be writable, but #{switch_point[:name]} is #{switch_point[:mode]}")
|
28
30
|
end
|
31
|
+
|
29
32
|
purge_readonly_query_cache(proxy)
|
30
33
|
end
|
31
34
|
end
|
data/lib/switch_point/error.rb
CHANGED
data/lib/switch_point/model.rb
CHANGED
data/lib/switch_point/proxy.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'switch_point/error'
|
2
4
|
|
3
5
|
module SwitchPoint
|
4
6
|
class Proxy
|
5
7
|
attr_reader :initial_name
|
6
8
|
|
7
|
-
AVAILABLE_MODES = [
|
9
|
+
AVAILABLE_MODES = %i[writable readonly].freeze
|
8
10
|
DEFAULT_MODE = :readonly
|
9
11
|
|
10
12
|
def initialize(name)
|
@@ -28,7 +30,7 @@ module SwitchPoint
|
|
28
30
|
# Re-use writable connection
|
29
31
|
Proxy.const_get(SwitchPoint.config.model_name(name, :writable))
|
30
32
|
else
|
31
|
-
|
33
|
+
ActiveRecord::Base
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
@@ -38,6 +40,7 @@ module SwitchPoint
|
|
38
40
|
if mode != :writable
|
39
41
|
raise Error.new("ActiveRecord::Base's switch_points must be writable, but #{name} is #{mode}")
|
40
42
|
end
|
43
|
+
|
41
44
|
switch_points = pool.spec.config[:switch_points] || []
|
42
45
|
switch_points << switch_point
|
43
46
|
pool.spec.config[:switch_points] = switch_points
|
@@ -97,6 +100,7 @@ module SwitchPoint
|
|
97
100
|
unless AVAILABLE_MODES.include?(new_mode)
|
98
101
|
raise ArgumentError.new("Unknown mode: #{new_mode}")
|
99
102
|
end
|
103
|
+
|
100
104
|
saved_mode = thread_local_mode
|
101
105
|
self.thread_local_mode = new_mode
|
102
106
|
block.call
|
data/lib/switch_point/version.rb
CHANGED
data/spec/models.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
SwitchPoint.configure do |config|
|
2
4
|
config.define_switch_point :main,
|
3
5
|
readonly: :main_readonly,
|
@@ -30,8 +32,7 @@ class Book < ActiveRecord::Base
|
|
30
32
|
|
31
33
|
private
|
32
34
|
|
33
|
-
def do_after_save
|
34
|
-
end
|
35
|
+
def do_after_save; end
|
35
36
|
end
|
36
37
|
|
37
38
|
class Book2 < ActiveRecord::Base
|
@@ -91,7 +92,7 @@ base =
|
|
91
92
|
else
|
92
93
|
{ adapter: 'sqlite3' }
|
93
94
|
end
|
94
|
-
|
95
|
+
databases = {
|
95
96
|
'main_readonly' => base.merge(database: 'main_readonly.sqlite3'),
|
96
97
|
'main_writable' => base.merge(database: 'main_writable.sqlite3'),
|
97
98
|
'main2_readonly' => base.merge(database: 'main2_readonly.sqlite3'),
|
@@ -100,8 +101,15 @@ ActiveRecord::Base.configurations = {
|
|
100
101
|
'user' => base.merge(database: 'user.sqlite3'),
|
101
102
|
'comment_readonly' => base.merge(database: 'comment_readonly.sqlite3'),
|
102
103
|
'comment_writable' => base.merge(database: 'comment_writable.sqlite3'),
|
103
|
-
'default' => base.merge(database: 'default.sqlite3')
|
104
|
+
'default' => base.merge(database: 'default.sqlite3'),
|
104
105
|
}
|
106
|
+
ActiveRecord::Base.configurations =
|
107
|
+
# ActiveRecord.gem_version was introduced in ActiveRecord 4.0
|
108
|
+
if ActiveRecord.respond_to?(:gem_version) && ActiveRecord.gem_version >= Gem::Version.new('5.1.0')
|
109
|
+
{ 'test' => databases }
|
110
|
+
else
|
111
|
+
databases
|
112
|
+
end
|
105
113
|
ActiveRecord::Base.establish_connection(:default)
|
106
114
|
|
107
115
|
# XXX: Check connection laziness
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
ENV['RAILS_ENV'] ||= 'test'
|
4
|
+
|
1
5
|
require 'coveralls'
|
2
6
|
require 'simplecov'
|
3
7
|
|
@@ -57,8 +61,14 @@ RSpec.configure do |config|
|
|
57
61
|
end
|
58
62
|
|
59
63
|
config.after(:suite) do
|
60
|
-
ActiveRecord::Base.configurations.
|
61
|
-
|
64
|
+
if ActiveRecord::Base.configurations.respond_to?(:configs_for)
|
65
|
+
ActiveRecord::Base.configurations.configs_for.each do |c|
|
66
|
+
FileUtils.rm_f(c.config['database'])
|
67
|
+
end
|
68
|
+
else
|
69
|
+
ActiveRecord::Base.configurations.each_value do |c|
|
70
|
+
FileUtils.rm_f(c[:database])
|
71
|
+
end
|
62
72
|
end
|
63
73
|
end
|
64
74
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.describe SwitchPoint::Model do
|
2
4
|
describe '.use_switch_point' do
|
3
5
|
after do
|
@@ -436,7 +438,7 @@ RSpec.describe SwitchPoint::Model do
|
|
436
438
|
end
|
437
439
|
expect(Book.with_writable { Book.count }).to eq(1)
|
438
440
|
|
439
|
-
expect { book.transaction_with(Book3) {} }.to raise_error(SwitchPoint::Error)
|
441
|
+
expect { book.transaction_with(Book3) {} }.to raise_error(SwitchPoint::Error) # rubocop:disable Lint/EmptyBlock
|
440
442
|
end
|
441
443
|
end
|
442
444
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'rack'
|
3
5
|
|
@@ -23,6 +25,16 @@ RSpec.describe SwitchPoint::QueryCache do
|
|
23
25
|
end
|
24
26
|
|
25
27
|
describe '#call' do
|
28
|
+
before do
|
29
|
+
# Ensure the connection is established.
|
30
|
+
# The query cache is enabled only when connected.
|
31
|
+
# https://github.com/rails/rails/commit/25fc1f584def4c1bc36be805833194d8aee55b3a
|
32
|
+
[Nanika1, Nanika2].each do |model|
|
33
|
+
model.with_readonly { model.connection }
|
34
|
+
model.with_writable { model.connection }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
26
38
|
it 'enables query cache of all models' do
|
27
39
|
env = {}
|
28
40
|
expect(app.call(env)).to eq(:result)
|
@@ -35,7 +47,7 @@ RSpec.describe SwitchPoint::QueryCache do
|
|
35
47
|
context 'when names are specified' do
|
36
48
|
let(:app) do
|
37
49
|
Rack::Builder.new do
|
38
|
-
use SwitchPoint::QueryCache, [
|
50
|
+
use SwitchPoint::QueryCache, %i[main nanika1]
|
39
51
|
run TestApp.new
|
40
52
|
end
|
41
53
|
end
|
data/spec/switch_point_spec.rb
CHANGED
@@ -1,4 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.describe SwitchPoint do
|
4
|
+
describe '.writable_all!' do
|
5
|
+
after do
|
6
|
+
SwitchPoint.readonly_all!
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'changes connection globally' do
|
10
|
+
expect(Book).to connect_to('main_readonly.sqlite3')
|
11
|
+
expect(Book3).to connect_to('main2_readonly.sqlite3')
|
12
|
+
expect(Comment).to connect_to('comment_readonly.sqlite3')
|
13
|
+
expect(User).to connect_to('user.sqlite3')
|
14
|
+
expect(BigData).to connect_to('main_readonly_special.sqlite3')
|
15
|
+
SwitchPoint.writable_all!
|
16
|
+
expect(Book).to connect_to('main_writable.sqlite3')
|
17
|
+
expect(Book3).to connect_to('main2_writable.sqlite3')
|
18
|
+
expect(Comment).to connect_to('comment_writable.sqlite3')
|
19
|
+
expect(User).to connect_to('user.sqlite3')
|
20
|
+
expect(BigData).to connect_to('main_writable.sqlite3')
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'affects thread-globally' do
|
24
|
+
SwitchPoint.writable_all!
|
25
|
+
Thread.start do
|
26
|
+
expect(Book).to connect_to('main_writable.sqlite3')
|
27
|
+
expect(Book3).to connect_to('main2_writable.sqlite3')
|
28
|
+
expect(Comment).to connect_to('comment_writable.sqlite3')
|
29
|
+
expect(User).to connect_to('user.sqlite3')
|
30
|
+
expect(BigData).to connect_to('main_writable.sqlite3')
|
31
|
+
end.join
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'within with block' do
|
35
|
+
it 'changes the current mode' do
|
36
|
+
SwitchPoint.writable_all!
|
37
|
+
Book.with_readonly do
|
38
|
+
expect(Book).to connect_to('main_readonly.sqlite3')
|
39
|
+
end
|
40
|
+
expect(Book).to connect_to('main_writable.sqlite3')
|
41
|
+
Book.with_writable do
|
42
|
+
expect(Book).to connect_to('main_writable.sqlite3')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
2
48
|
describe '.writable!' do
|
3
49
|
after do
|
4
50
|
SwitchPoint.readonly!(:main)
|
data/switch_point.gemspec
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require 'switch_point/version'
|
5
6
|
|
@@ -17,16 +18,16 @@ Gem::Specification.new do |spec|
|
|
17
18
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
20
|
spec.require_paths = ['lib']
|
20
|
-
spec.required_ruby_version = '>= 2.
|
21
|
+
spec.required_ruby_version = '>= 2.5.0'
|
21
22
|
|
22
23
|
spec.add_development_dependency 'appraisal'
|
23
24
|
spec.add_development_dependency 'benchmark-ips'
|
24
25
|
spec.add_development_dependency 'bundler'
|
25
|
-
spec.add_development_dependency 'coveralls'
|
26
|
+
spec.add_development_dependency 'coveralls', '>= 0.8.22'
|
26
27
|
spec.add_development_dependency 'rack'
|
27
28
|
spec.add_development_dependency 'rake'
|
28
29
|
spec.add_development_dependency 'rspec', '>= 3.0'
|
29
|
-
spec.add_development_dependency 'rubocop', '>= 0.
|
30
|
-
spec.add_development_dependency 'simplecov', '
|
31
|
-
spec.add_dependency 'activerecord', '>= 3.2.0'
|
30
|
+
spec.add_development_dependency 'rubocop', '>= 0.50.0'
|
31
|
+
spec.add_development_dependency 'simplecov', '~> 0.16.1' # XXX: The latest coveralls still depends on old version
|
32
|
+
spec.add_dependency 'activerecord', '>= 3.2.0', '< 6.1.0'
|
32
33
|
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.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kohei Suzuki
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: appraisal
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 0.8.22
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 0.8.22
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rack
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,28 +114,28 @@ dependencies:
|
|
114
114
|
requirements:
|
115
115
|
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: 0.
|
117
|
+
version: 0.50.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.50.0
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
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.16.1
|
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.16.1
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
140
|
name: activerecord
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
@@ -143,6 +143,9 @@ dependencies:
|
|
143
143
|
- - ">="
|
144
144
|
- !ruby/object:Gem::Version
|
145
145
|
version: 3.2.0
|
146
|
+
- - "<"
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: 6.1.0
|
146
149
|
type: :runtime
|
147
150
|
prerelease: false
|
148
151
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -150,6 +153,9 @@ dependencies:
|
|
150
153
|
- - ">="
|
151
154
|
- !ruby/object:Gem::Version
|
152
155
|
version: 3.2.0
|
156
|
+
- - "<"
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: 6.1.0
|
153
159
|
description: Switching database connection between readonly one and writable one.
|
154
160
|
email:
|
155
161
|
- eagletmt@gmail.com
|
@@ -175,7 +181,9 @@ files:
|
|
175
181
|
- gemfiles/rails_4.1.gemfile
|
176
182
|
- gemfiles/rails_4.2.gemfile
|
177
183
|
- gemfiles/rails_5.0.gemfile
|
178
|
-
- gemfiles/
|
184
|
+
- gemfiles/rails_5.1.gemfile
|
185
|
+
- gemfiles/rails_5.2.gemfile
|
186
|
+
- gemfiles/rails_6.0.gemfile
|
179
187
|
- lib/switch_point.rb
|
180
188
|
- lib/switch_point/config.rb
|
181
189
|
- lib/switch_point/connection.rb
|
@@ -195,7 +203,7 @@ homepage: https://github.com/eagletmt/switch_point
|
|
195
203
|
licenses:
|
196
204
|
- MIT
|
197
205
|
metadata: {}
|
198
|
-
post_install_message:
|
206
|
+
post_install_message:
|
199
207
|
rdoc_options: []
|
200
208
|
require_paths:
|
201
209
|
- lib
|
@@ -203,16 +211,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
203
211
|
requirements:
|
204
212
|
- - ">="
|
205
213
|
- !ruby/object:Gem::Version
|
206
|
-
version: 2.
|
214
|
+
version: 2.5.0
|
207
215
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
208
216
|
requirements:
|
209
217
|
- - ">="
|
210
218
|
- !ruby/object:Gem::Version
|
211
219
|
version: '0'
|
212
220
|
requirements: []
|
213
|
-
|
214
|
-
|
215
|
-
signing_key:
|
221
|
+
rubygems_version: 3.1.4
|
222
|
+
signing_key:
|
216
223
|
specification_version: 4
|
217
224
|
summary: Switching database connection between readonly one and writable one.
|
218
225
|
test_files:
|
data/gemfiles/rails_edge.gemfile
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "https://rubygems.org"
|
4
|
-
|
5
|
-
gem "activerecord", :git => "https://github.com/rails/rails"
|
6
|
-
gem "arel", :git => "https://github.com/rails/arel"
|
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
|
-
|
17
|
-
gemspec :path => "../"
|