db-switch 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -1
- data/.rubocop.yml +16 -0
- data/.travis.yml +3 -2
- data/Appraisals +9 -0
- data/Rakefile +1 -1
- data/db-switch.gemspec +2 -0
- data/lib/db-switch.rb +1 -1
- data/lib/db-switch/active_record_base.rb +23 -11
- data/lib/db-switch/log_subscriber.rb +20 -11
- data/lib/db-switch/railtie.rb +1 -1
- data/lib/db-switch/thread_variables.rb +10 -8
- data/lib/db-switch/version.rb +1 -1
- metadata +33 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 349594e5e378fb58e5c4d22c352218c7da091c91
|
4
|
+
data.tar.gz: d01d7d462e99fe60d5bafcb982e42ce8e0022f13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4db0e07aebed82b9a6288ca55900f062c39a4d8d848e2c3de52d9cb3bbf63bdc262c68d5ae0cc81799854e787436825989a5a987cf01e190f77623a9ce9aa5e
|
7
|
+
data.tar.gz: bb4edc848a5eec04b88a5f5b9959d90da1b5699df578ad3ef82e063184ec4fbee859e88039758f7becdca6705c61c27eaca756762ccd1a3e4492c2613e0cf078
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Documentation:
|
2
|
+
Enabled: false
|
3
|
+
Metrics/MethodLength:
|
4
|
+
Enabled: false
|
5
|
+
Metrics/LineLength:
|
6
|
+
Max: 120
|
7
|
+
Metrics/AbcSize:
|
8
|
+
Enabled: false
|
9
|
+
Metrics/BlockLength:
|
10
|
+
Enabled: false
|
11
|
+
Metrics/CyclomaticComplexity:
|
12
|
+
Enabled: false
|
13
|
+
Metrics/PerceivedComplexity:
|
14
|
+
Enabled: false
|
15
|
+
Naming/FileName:
|
16
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/Appraisals
ADDED
data/Rakefile
CHANGED
data/db-switch.gemspec
CHANGED
@@ -24,6 +24,8 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_development_dependency 'rake', '~> 10.0'
|
25
25
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
26
26
|
spec.add_development_dependency 'pg', '~> 0.19'
|
27
|
+
spec.add_development_dependency 'rubocop', '0.50.0'
|
28
|
+
spec.add_development_dependency 'appraisal', '~> 2.1'
|
27
29
|
|
28
30
|
# Rails 5 dependencies
|
29
31
|
spec.add_runtime_dependency 'activerecord', '~> 5.0'
|
data/lib/db-switch.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require 'db-switch/version'
|
2
|
-
require 'db-switch/railtie'
|
2
|
+
require 'db-switch/railtie'
|
@@ -1,14 +1,22 @@
|
|
1
|
-
module DbSwitch
|
2
|
-
|
1
|
+
module DbSwitch
|
2
|
+
module ActiveRecordBase
|
3
|
+
include DbSwitch::ThreadVariables
|
3
4
|
|
4
|
-
|
5
|
-
begin
|
5
|
+
def connect_to(connection_name)
|
6
6
|
self.db_switch_connection_name = connection_name.to_s
|
7
7
|
if ActiveRecord::Base.connection_handler.connected?(connection_name.to_s).nil?
|
8
|
-
db_config =
|
8
|
+
db_config =
|
9
|
+
ActiveRecord::Base.configurations[connection_name.to_s] ||
|
10
|
+
connection_configurations[connection_name.to_s]
|
11
|
+
|
9
12
|
if db_config && db_config[Rails.env.to_s]
|
10
|
-
|
11
|
-
|
13
|
+
spec =
|
14
|
+
if Rails::VERSION::MINOR.zero?
|
15
|
+
resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(db_config)
|
16
|
+
resolver.spec(Rails.env.to_sym, connection_name.to_s)
|
17
|
+
else
|
18
|
+
db_config[Rails.env.to_s].merge(name: connection_name.to_s)
|
19
|
+
end
|
12
20
|
ActiveRecord::Base.connection_handler.establish_connection spec
|
13
21
|
else
|
14
22
|
self.db_switch_connection_name = nil
|
@@ -18,9 +26,13 @@ module DbSwitch::ActiveRecordBase
|
|
18
26
|
ensure
|
19
27
|
self.db_switch_connection_name = nil
|
20
28
|
end
|
21
|
-
end
|
22
29
|
|
23
|
-
|
24
|
-
|
30
|
+
def connection_specification_name
|
31
|
+
db_switch_connection_name || super
|
32
|
+
end
|
33
|
+
|
34
|
+
def connection_configurations
|
35
|
+
@connection_configurations ||= YAML.load_file(Rails.root.join('config/database.yml'))
|
36
|
+
end
|
25
37
|
end
|
26
|
-
end
|
38
|
+
end
|
@@ -1,15 +1,24 @@
|
|
1
|
-
module DbSwitch
|
2
|
-
|
1
|
+
module DbSwitch
|
2
|
+
module LogSubscriber
|
3
|
+
include DbSwitch::ThreadVariables
|
3
4
|
|
4
|
-
|
5
|
+
attr_accessor :color_value
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
def colorize_payload_name(name, payload_name)
|
8
|
+
if db_switch_connection_name
|
9
|
+
self.color_value =
|
10
|
+
if payload_name.blank? || payload_name == 'SQL'
|
11
|
+
:MAGENTA
|
12
|
+
else
|
13
|
+
:CYAN
|
14
|
+
end
|
15
|
+
end
|
16
|
+
super
|
17
|
+
end
|
10
18
|
|
11
|
-
|
12
|
-
|
13
|
-
|
19
|
+
def debug(msg)
|
20
|
+
msg = color("[#{db_switch_connection_name}]", color_value || :CYAN, true) + msg if db_switch_connection_name
|
21
|
+
super(msg)
|
22
|
+
end
|
14
23
|
end
|
15
|
-
end
|
24
|
+
end
|
data/lib/db-switch/railtie.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
-
module DbSwitch
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module DbSwitch
|
2
|
+
module ThreadVariables
|
3
|
+
def db_switch_connection_name
|
4
|
+
Thread.current[:db_switch_connection_name]
|
5
|
+
end
|
5
6
|
|
6
|
-
|
7
|
-
|
7
|
+
def db_switch_connection_name=(value)
|
8
|
+
Thread.current[:db_switch_connection_name] = value
|
9
|
+
end
|
10
|
+
# attr_accessor :db_switch_connection_name # notice: this is not thread safe! (just for testing purposes)
|
8
11
|
end
|
9
|
-
|
10
|
-
end
|
12
|
+
end
|
data/lib/db-switch/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db-switch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anjlab
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,34 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0.19'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.50.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.50.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: appraisal
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.1'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.1'
|
69
97
|
- !ruby/object:Gem::Dependency
|
70
98
|
name: activerecord
|
71
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,7 +132,9 @@ extra_rdoc_files: []
|
|
104
132
|
files:
|
105
133
|
- ".gitignore"
|
106
134
|
- ".rspec"
|
135
|
+
- ".rubocop.yml"
|
107
136
|
- ".travis.yml"
|
137
|
+
- Appraisals
|
108
138
|
- Gemfile
|
109
139
|
- LICENSE.txt
|
110
140
|
- README.md
|
@@ -136,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
166
|
version: '0'
|
137
167
|
requirements: []
|
138
168
|
rubyforge_project:
|
139
|
-
rubygems_version: 2.6.
|
169
|
+
rubygems_version: 2.6.13
|
140
170
|
signing_key:
|
141
171
|
specification_version: 4
|
142
172
|
summary: Simple database connection switcher for Rails 5
|