db-switch 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +86 -0
- data/Rakefile +6 -0
- data/db-switch.gemspec +31 -0
- data/lib/db-switch.rb +2 -0
- data/lib/db-switch/active_record_base.rb +26 -0
- data/lib/db-switch/log_subscriber.rb +15 -0
- data/lib/db-switch/railtie.rb +18 -0
- data/lib/db-switch/thread_variables.rb +10 -0
- data/lib/db-switch/version.rb +3 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9386730abb6a43273dde21685ba599dd5f2a03f0
|
4
|
+
data.tar.gz: c7dfcffd2a19423dc4de0b761c813407566ac297
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c5dcd0b3f043f0be5f1bfb3d6fbf04f3013c216bdaa4f356a9034f4c127a56a03dee1ca464530a62b5c08b48bd376e0fdcc56df9b0ea09c08c183896c7d499ba
|
7
|
+
data.tar.gz: e93b14b1cb755b13b9af46176b45dd492db6e944ddb47c7284770007ca939b649268d1ffdcd946d849903e5a772f823a8beed44cb99563181bf4a24af91f6b66
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Anjlab
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# Db Switch
|
2
|
+
|
3
|
+
[](https://travis-ci.org/anjlab/db-switch)
|
4
|
+
|
5
|
+
This is a Rails 5 gem which allows you to use different database connections in your projects
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'db-switch'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install db-switch
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
**First**, you need to define your databases. In Rails, you define databases in `database.yml`. This is also the place where you can add your custom connections:
|
26
|
+
|
27
|
+
```yaml
|
28
|
+
development:
|
29
|
+
adapter: postgresql
|
30
|
+
database: dev_database
|
31
|
+
host: localhost
|
32
|
+
pool: 5
|
33
|
+
|
34
|
+
test:
|
35
|
+
adapter: sqlite3
|
36
|
+
database: test_db_1.sqlite3
|
37
|
+
pool: 5
|
38
|
+
|
39
|
+
replica_one:
|
40
|
+
development:
|
41
|
+
adapter: postgresql
|
42
|
+
database: dev_replica
|
43
|
+
host: localhost
|
44
|
+
pool: 5
|
45
|
+
|
46
|
+
test:
|
47
|
+
adapter: sqlite3
|
48
|
+
database: test_db_2.sqlite3
|
49
|
+
pool: 5
|
50
|
+
```
|
51
|
+
|
52
|
+
In the above sample we have defined `replica_one` alternative database connection, for both development and test environments.
|
53
|
+
|
54
|
+
**Second**, specify connection in your code:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
ActiveRecord::Base.connect_to(:replica_one) do
|
58
|
+
@products = Product.where('created_at > ?', 1.day.ago).to_a
|
59
|
+
end
|
60
|
+
|
61
|
+
# or somewhere inside your ActiveRecord model
|
62
|
+
|
63
|
+
def load_products
|
64
|
+
connect_to(:replica_one) do
|
65
|
+
Product.where('created_at > ?', 1.day.ago).to_a
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
```
|
70
|
+
|
71
|
+
This will query data from the `replica_one` database specified above.
|
72
|
+
|
73
|
+
**That's it! Use it!**
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
1. Fork it
|
78
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
79
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
80
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
81
|
+
5. Create new Pull Request
|
82
|
+
|
83
|
+
## License
|
84
|
+
|
85
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
86
|
+
|
data/Rakefile
ADDED
data/db-switch.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'db-switch/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'db-switch'
|
7
|
+
spec.version = DbSwitch::VERSION
|
8
|
+
spec.authors = ['Anjlab']
|
9
|
+
spec.email = ['sergey.glukhov@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Simple database connection switcher for Rails 5'
|
12
|
+
spec.description = 'Dead-simple gem for Rails 5 that allows to switch between database connections on-the-fly'
|
13
|
+
spec.homepage = 'https://github.com/anjlab/db-switch'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
26
|
+
spec.add_development_dependency 'pg', '~> 0.19'
|
27
|
+
|
28
|
+
# Rails 5 dependencies
|
29
|
+
spec.add_runtime_dependency 'activerecord', '~> 5.0'
|
30
|
+
spec.add_runtime_dependency 'railties', '~> 5.0'
|
31
|
+
end
|
data/lib/db-switch.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module DbSwitch::ActiveRecordBase
|
2
|
+
include DbSwitch::ThreadVariables
|
3
|
+
|
4
|
+
def connect_to(connection_name)
|
5
|
+
begin
|
6
|
+
self.db_switch_connection_name = connection_name.to_s
|
7
|
+
if ActiveRecord::Base.connection_handler.connected?(connection_name.to_s).nil?
|
8
|
+
db_config = ActiveRecord::Base.configurations[connection_name.to_s]
|
9
|
+
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)
|
12
|
+
ActiveRecord::Base.connection_handler.establish_connection spec
|
13
|
+
else
|
14
|
+
self.db_switch_connection_name = nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
yield if block_given?
|
18
|
+
ensure
|
19
|
+
self.db_switch_connection_name = nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def connection_specification_name
|
24
|
+
db_switch_connection_name || super
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module DbSwitch::LogSubscriber
|
2
|
+
include DbSwitch::ThreadVariables
|
3
|
+
|
4
|
+
attr_accessor :color_value
|
5
|
+
|
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
|
10
|
+
|
11
|
+
def debug(msg)
|
12
|
+
msg = color("[#{db_switch_connection_name}]", color_value || :CYAN, true) + msg if db_switch_connection_name
|
13
|
+
super(msg)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'db-switch/thread_variables'
|
2
|
+
require 'db-switch/active_record_base'
|
3
|
+
require 'db-switch/log_subscriber'
|
4
|
+
|
5
|
+
require 'rails'
|
6
|
+
|
7
|
+
module DbSwitch
|
8
|
+
class Railtie < ::Rails::Railtie
|
9
|
+
initializer 'db-switch->active_record' do
|
10
|
+
ActiveSupport.on_load(:active_record) do
|
11
|
+
# extend AR functionality
|
12
|
+
ActiveRecord::Base.send(:extend, DbSwitch::ActiveRecordBase)
|
13
|
+
# extend SQL query logger
|
14
|
+
ActiveRecord::LogSubscriber.send(:prepend, DbSwitch::LogSubscriber)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module DbSwitch::ThreadVariables
|
2
|
+
def db_switch_connection_name
|
3
|
+
Thread.current[:db_switch_connection_name]
|
4
|
+
end
|
5
|
+
|
6
|
+
def db_switch_connection_name=(value)
|
7
|
+
Thread.current[:db_switch_connection_name] = value
|
8
|
+
end
|
9
|
+
# attr_accessor :db_switch_connection_name # notice: this is not thread safe! (just for testing purposes)
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: db-switch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anjlab
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pg
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.19'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.19'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: railties
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.0'
|
97
|
+
description: Dead-simple gem for Rails 5 that allows to switch between database connections
|
98
|
+
on-the-fly
|
99
|
+
email:
|
100
|
+
- sergey.glukhov@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".travis.yml"
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- db-switch.gemspec
|
113
|
+
- lib/db-switch.rb
|
114
|
+
- lib/db-switch/active_record_base.rb
|
115
|
+
- lib/db-switch/log_subscriber.rb
|
116
|
+
- lib/db-switch/railtie.rb
|
117
|
+
- lib/db-switch/thread_variables.rb
|
118
|
+
- lib/db-switch/version.rb
|
119
|
+
homepage: https://github.com/anjlab/db-switch
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
metadata: {}
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 2.6.6
|
140
|
+
signing_key:
|
141
|
+
specification_version: 4
|
142
|
+
summary: Simple database connection switcher for Rails 5
|
143
|
+
test_files: []
|