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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9386730abb6a43273dde21685ba599dd5f2a03f0
4
- data.tar.gz: c7dfcffd2a19423dc4de0b761c813407566ac297
3
+ metadata.gz: 349594e5e378fb58e5c4d22c352218c7da091c91
4
+ data.tar.gz: d01d7d462e99fe60d5bafcb982e42ce8e0022f13
5
5
  SHA512:
6
- metadata.gz: c5dcd0b3f043f0be5f1bfb3d6fbf04f3013c216bdaa4f356a9034f4c127a56a03dee1ca464530a62b5c08b48bd376e0fdcc56df9b0ea09c08c183896c7d499ba
7
- data.tar.gz: e93b14b1cb755b13b9af46176b45dd492db6e944ddb47c7284770007ca939b649268d1ffdcd946d849903e5a772f823a8beed44cb99563181bf4a24af91f6b66
6
+ metadata.gz: b4db0e07aebed82b9a6288ca55900f062c39a4d8d848e2c3de52d9cb3bbf63bdc262c68d5ae0cc81799854e787436825989a5a987cf01e190f77623a9ce9aa5e
7
+ data.tar.gz: bb4edc848a5eec04b88a5f5b9959d90da1b5699df578ad3ef82e063184ec4fbee859e88039758f7becdca6705c61c27eaca756762ccd1a3e4492c2613e0cf078
data/.gitignore CHANGED
@@ -8,4 +8,6 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  .idea/
11
- *.log
11
+ *.log
12
+ gemfiles/
13
+ *.gem
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
@@ -1,5 +1,6 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.1
5
- before_install: gem install bundler -v 1.13.4
4
+ - 2.3.4
5
+ - 2.4.1
6
+ script: appraisal install && appraisal rake && rubocop --fail-level R
data/Appraisals ADDED
@@ -0,0 +1,9 @@
1
+ appraise 'rails-5.0' do
2
+ gem 'activerecord', '~> 5.0.0'
3
+ gem 'railties', '~> 5.0.0'
4
+ end
5
+
6
+ appraise 'rails-5.1' do
7
+ gem 'activerecord', '~> 5.1.0'
8
+ gem 'railties', '~> 5.1.0'
9
+ end
data/Rakefile CHANGED
@@ -3,4 +3,4 @@ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
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::ActiveRecordBase
2
- include DbSwitch::ThreadVariables
1
+ module DbSwitch
2
+ module ActiveRecordBase
3
+ include DbSwitch::ThreadVariables
3
4
 
4
- def connect_to(connection_name)
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 = ActiveRecord::Base.configurations[connection_name.to_s]
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
- resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(db_config)
11
- spec = resolver.spec(Rails.env.to_sym, connection_name.to_s)
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
- def connection_specification_name
24
- db_switch_connection_name || super
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::LogSubscriber
2
- include DbSwitch::ThreadVariables
1
+ module DbSwitch
2
+ module LogSubscriber
3
+ include DbSwitch::ThreadVariables
3
4
 
4
- attr_accessor :color_value
5
+ attr_accessor :color_value
5
6
 
6
- def colorize_payload_name(name, payload_name)
7
- self.color_value = payload_name.blank? || payload_name == 'SQL' ? :MAGENTA : :CYAN if db_switch_connection_name
8
- super
9
- end
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
- def debug(msg)
12
- msg = color("[#{db_switch_connection_name}]", color_value || :CYAN, true) + msg if db_switch_connection_name
13
- super(msg)
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
@@ -15,4 +15,4 @@ module DbSwitch
15
15
  end
16
16
  end
17
17
  end
18
- end
18
+ end
@@ -1,10 +1,12 @@
1
- module DbSwitch::ThreadVariables
2
- def db_switch_connection_name
3
- Thread.current[:db_switch_connection_name]
4
- end
1
+ module DbSwitch
2
+ module ThreadVariables
3
+ def db_switch_connection_name
4
+ Thread.current[:db_switch_connection_name]
5
+ end
5
6
 
6
- def db_switch_connection_name=(value)
7
- Thread.current[:db_switch_connection_name] = value
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
- # attr_accessor :db_switch_connection_name # notice: this is not thread safe! (just for testing purposes)
10
- end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module DbSwitch
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'.freeze
3
3
  end
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.1.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: 2016-10-17 00:00:00.000000000 Z
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.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