multi_connection 0.0.1
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 +14 -0
- data/.rspec +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +5 -0
- data/lib/multi_connection.rb +97 -0
- data/lib/multi_connection/railite.rb +9 -0
- data/lib/multi_connection/version.rb +3 -0
- data/multi_connection.gemspec +28 -0
- data/spec/multi_connection_spec.rb +25 -0
- data/spec/spec_helper.rb +31 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 813b4c924052a1fd5e41615110da84fc58f6d121
|
4
|
+
data.tar.gz: 7dcf40d50238ae9d386d2b6c87a97b0a6136d047
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 02d44a0071beaf68edb6236f1a1532a56c97eaca838240154de7141f613aed8bf62dd46f9087da97a36c2d1b9040b8db2d4ee228ca816eef524e287a61ac325a
|
7
|
+
data.tar.gz: 177fddd14c812ae83c3a2163d3e3bb9f087f933214b25aeef7a13e5215dd791a3a9e900143a248c9c3a1b03efab8985aab535cd7b2b218ab53b8d8da5d09dd6f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 wenjun.yan
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# MultiConnection
|
2
|
+
|
3
|
+
Multiple database connections for rails
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'multi_connection'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install multi_connection
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### API
|
24
|
+
|
25
|
+
- `switch_to(:database)` database should be defined in you `database.yml` file.
|
26
|
+
- alias `open`
|
27
|
+
|
28
|
+
### Example
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
ActiveRecord::Base.switch_to(:production_slave) {
|
32
|
+
User.find(1)
|
33
|
+
}
|
34
|
+
|
35
|
+
ActiveRecord::Base.open(:production_slave) {
|
36
|
+
User.find(1)
|
37
|
+
}
|
38
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require "thread_safe"
|
2
|
+
require "multi_connection/version"
|
3
|
+
|
4
|
+
module MultiConnection
|
5
|
+
module ConnectionHandling
|
6
|
+
def clear_active_connections!
|
7
|
+
connection_handler.clear_active_connections!
|
8
|
+
ghost_connection_handler.clear_active_connections!
|
9
|
+
end
|
10
|
+
|
11
|
+
def clear_reloadable_connections!
|
12
|
+
connection_handler.clear_reloadable_connections!
|
13
|
+
ghost_connection_handler.clear_reloadable_connections!
|
14
|
+
end
|
15
|
+
|
16
|
+
def clear_all_connections!
|
17
|
+
connection_handler.clear_all_connections!
|
18
|
+
ghost_connection_handler.clear_all_connections!
|
19
|
+
end
|
20
|
+
|
21
|
+
# Connect to another database.
|
22
|
+
# And restore the previous connection at the end of the block.
|
23
|
+
#
|
24
|
+
# spec - a symbol or string
|
25
|
+
#
|
26
|
+
# Note, #switch_to will change the connection handler which means
|
27
|
+
# all subsequent queries in that block will be sent to the new
|
28
|
+
# database.
|
29
|
+
#
|
30
|
+
# ActiveRecord::Base.switch_to(:another_db) {
|
31
|
+
# # query sent to another_db
|
32
|
+
# }
|
33
|
+
#
|
34
|
+
# This is thread safe since connection_handler is local to current
|
35
|
+
# thread according to ActiveSupport::PerThreadRegistry.
|
36
|
+
#
|
37
|
+
# Yield a block
|
38
|
+
def switch_to(spec)
|
39
|
+
old_handler = connection_handler
|
40
|
+
self.connection_handler = ghost_connection_handler
|
41
|
+
self.connection_handler.spec = spec
|
42
|
+
yield
|
43
|
+
ensure
|
44
|
+
self.connection_handler.spec = nil
|
45
|
+
self.connection_handler = old_handler
|
46
|
+
end
|
47
|
+
alias_method :open, :switch_to
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def ghost_connection_handler
|
52
|
+
@ghost_connection_handler ||=
|
53
|
+
::MultiConnection::ConnectionAdapters::ConnectionHandler.new
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
module ConnectionAdapters
|
58
|
+
class ConnectionHandler < ::ActiveRecord::ConnectionAdapters::ConnectionHandler
|
59
|
+
attr_accessor :spec
|
60
|
+
|
61
|
+
def initialize
|
62
|
+
@spec_to_pool = ThreadSafe::Cache.new(:initial_capacity => 2)
|
63
|
+
end
|
64
|
+
|
65
|
+
def connection_pool_list
|
66
|
+
@spec_to_pool.values.compact
|
67
|
+
end
|
68
|
+
|
69
|
+
def establish_connection(owner, spec)
|
70
|
+
@spec_to_pool[self.spec] =
|
71
|
+
::ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
|
72
|
+
end
|
73
|
+
|
74
|
+
def remove_connection(spec)
|
75
|
+
if pool = @spec_to_pool[spec]
|
76
|
+
pool.automatic_reconnect = false
|
77
|
+
pool.disconnect!
|
78
|
+
pool.spec.config
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def retrieve_connection_pool(klass=nil)
|
83
|
+
# Base.establish_connection will resolve the spec for us
|
84
|
+
# and call our #establish_connection method
|
85
|
+
@spec_to_pool[spec] || ::ActiveRecord::Base.establish_connection(spec)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
begin
|
93
|
+
require 'rails'
|
94
|
+
require 'multi_connection/railtie'
|
95
|
+
rescue LoadError
|
96
|
+
end
|
97
|
+
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module MultiConnection
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
initializer 'multi_connection', after: 'active_record.initialize_database' do
|
4
|
+
ActiveSupport.on_load :active_record do
|
5
|
+
::ActiveRecord::Base.extend ::MultiConnection::ConnectionHandling
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'multi_connection/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "multi_connection"
|
8
|
+
spec.version = MultiConnection::VERSION
|
9
|
+
spec.authors = ["wenjun.yan"]
|
10
|
+
spec.email = ["mylastnameisyan@gmail.com"]
|
11
|
+
spec.summary = %q{rails multiple database connections}
|
12
|
+
spec.description = %q{rails multiple database connections}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.0.0"
|
24
|
+
spec.add_development_dependency "sqlite3"
|
25
|
+
|
26
|
+
spec.add_dependency "activerecord", ">= 3.2.0"
|
27
|
+
spec.add_dependency "thread_safe"
|
28
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
RSpec.describe MultiConnection do
|
2
|
+
|
3
|
+
def switch_to(spec, &block)
|
4
|
+
ActiveRecord::Base.switch_to(spec, &block)
|
5
|
+
end
|
6
|
+
|
7
|
+
describe "Write to default database" do
|
8
|
+
before { User.create }
|
9
|
+
it { expect(User.count).to eq 1 }
|
10
|
+
it { expect(switch_to(:db2) { User.count }).to eq 0 }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "Write to another database" do
|
14
|
+
before { User.switch_to(:db2) { User.create } }
|
15
|
+
it { expect(User.count).to eq 0 }
|
16
|
+
it { expect(switch_to(:db2) { User.count }).to eq 1 }
|
17
|
+
end
|
18
|
+
|
19
|
+
it("should be thread safe") {
|
20
|
+
Thread.new { switch_to(:db2) { User.create } }.join
|
21
|
+
expect(User.count).to eq 0
|
22
|
+
expect(switch_to(:db2) { User.count }).to eq 1
|
23
|
+
}
|
24
|
+
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'multi_connection'
|
3
|
+
|
4
|
+
class ActiveRecord::Base
|
5
|
+
extend MultiConnection::ConnectionHandling
|
6
|
+
end
|
7
|
+
|
8
|
+
ActiveRecord::Base.configurations = {
|
9
|
+
'default' => { adapter: 'sqlite3', database: 'default', timeout: 100 },
|
10
|
+
'db2' => { adapter: 'sqlite3', database: 'db2', timeout: 100 },
|
11
|
+
}
|
12
|
+
|
13
|
+
class User < ActiveRecord::Base; end
|
14
|
+
|
15
|
+
def setup_db
|
16
|
+
ActiveRecord::Base.establish_connection :db2
|
17
|
+
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS users')
|
18
|
+
ActiveRecord::Base.connection.execute('CREATE TABLE users (id integer primary key autoincrement)')
|
19
|
+
ActiveRecord::Base.establish_connection :default
|
20
|
+
ActiveRecord::Base.connection.execute('DROP TABLE IF EXISTS users')
|
21
|
+
ActiveRecord::Base.connection.execute('CREATE TABLE users (id integer primary key autoincrement)')
|
22
|
+
end
|
23
|
+
|
24
|
+
def remove_db_file
|
25
|
+
`rm -f default db2`
|
26
|
+
end
|
27
|
+
|
28
|
+
RSpec.configure do |config|
|
29
|
+
config.before(:each) { setup_db }
|
30
|
+
config.after(:all) { remove_db_file }
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: multi_connection
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- wenjun.yan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-03 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
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.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.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.2.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.2.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: thread_safe
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: rails multiple database connections
|
98
|
+
email:
|
99
|
+
- mylastnameisyan@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/multi_connection.rb
|
111
|
+
- lib/multi_connection/railite.rb
|
112
|
+
- lib/multi_connection/version.rb
|
113
|
+
- multi_connection.gemspec
|
114
|
+
- spec/multi_connection_spec.rb
|
115
|
+
- spec/spec_helper.rb
|
116
|
+
homepage: ''
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.4.1
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: rails multiple database connections
|
140
|
+
test_files:
|
141
|
+
- spec/multi_connection_spec.rb
|
142
|
+
- spec/spec_helper.rb
|