activerecord-connections 0.0.2 → 0.0.3
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.
- data/README.markdown +1 -2
- data/activerecord-connections.gemspec +1 -1
- data/lib/active_record/connections.rb +45 -46
- data/test/active_record/connections_test.rb +8 -0
- metadata +65 -74
data/README.markdown
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# ActiveRecord::Connections
|
1
|
+
# ActiveRecord::Connections
|
2
2
|
|
3
3
|
ActiveRecord::Connections provides a new way to manage multi-tenant applications based on multiples databases.
|
4
4
|
|
@@ -47,7 +47,6 @@ If you are using Rails, you could use this way:
|
|
47
47
|
|
48
48
|
### Known issues
|
49
49
|
|
50
|
-
* Do not work with multi-threads
|
51
50
|
* Do not manage activerecord migrations for different databases
|
52
51
|
* Similar objects of different connections do not differ
|
53
52
|
* Coverage is not the best of the world (you could help easily)
|
@@ -1,65 +1,64 @@
|
|
1
1
|
require 'active_support'
|
2
|
-
require 'active_support/concern'
|
3
2
|
|
4
3
|
module ActiveRecord
|
5
4
|
module Connections
|
6
5
|
autoload :ConnectionProxy, 'active_record/connections/connection_proxy'
|
7
6
|
|
8
|
-
|
7
|
+
# Using on ApplicationController:
|
8
|
+
#
|
9
|
+
# class ApplicationController < ActionController::Base
|
10
|
+
# before_filter :handle_customer
|
11
|
+
#
|
12
|
+
# protected
|
13
|
+
#
|
14
|
+
# def handle_customer(&block)
|
15
|
+
# customer = Customer.find_by_domain!(request.domain)
|
16
|
+
# ActiveRecord::Base.using_connection(customer.id, customer.connection_spec, &block)
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# Using directly on models:
|
21
|
+
#
|
22
|
+
# customer = Customer.first
|
23
|
+
#
|
24
|
+
# ActiveRecord::Base.using_connection(customer.id, customer.connection_spec) do
|
25
|
+
# User.count # => 3
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
def using_connection(connection_name, connection_spec)
|
29
|
+
self.proxy_connection = ConnectionProxy.new(connection_name, connection_spec)
|
9
30
|
|
10
|
-
|
11
|
-
|
12
|
-
|
31
|
+
def self.connection_pool
|
32
|
+
connection_handler.retrieve_connection_pool(proxy_connection)
|
33
|
+
end
|
13
34
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
# class ApplicationController < ActionController::Base
|
18
|
-
# before_filter :handle_customer
|
19
|
-
#
|
20
|
-
# protected
|
21
|
-
#
|
22
|
-
# def handle_customer(&block)
|
23
|
-
# customer = Customer.find_by_domain!(request.domain)
|
24
|
-
# ActiveRecord::Base.using_connection(customer.id, customer.connection_spec, &block)
|
25
|
-
# end
|
26
|
-
# end
|
27
|
-
#
|
28
|
-
# Using directly on models:
|
29
|
-
#
|
30
|
-
# customer = Customer.first
|
31
|
-
#
|
32
|
-
# ActiveRecord::Base.using_connection(customer.id, customer.connection_spec) do
|
33
|
-
# User.count # => 3
|
34
|
-
# end
|
35
|
-
#
|
36
|
-
def using_connection(connection_name, connection_spec)
|
37
|
-
self.proxy_connection = ConnectionProxy.new(connection_name, connection_spec)
|
35
|
+
def self.retrieve_connection
|
36
|
+
connection_handler.retrieve_connection(proxy_connection)
|
37
|
+
end
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
yield
|
40
|
+
ensure
|
41
|
+
self.proxy_connection = nil
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
def self.connection_pool
|
44
|
+
connection_handler.retrieve_connection_pool(self)
|
45
|
+
end
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
def self.retrieve_connection
|
48
|
+
connection_handler.retrieve_connection(self)
|
49
|
+
end
|
50
|
+
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
52
|
+
def proxy_connection
|
53
|
+
Thread.current["ActiveRecord::Connections.proxy_connection"]
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
57
|
-
end
|
58
|
-
end
|
56
|
+
def proxy_connection=(proxy_connection)
|
57
|
+
Thread.current["ActiveRecord::Connections.proxy_connection"] = proxy_connection
|
59
58
|
end
|
60
59
|
end
|
61
60
|
end
|
62
61
|
|
63
62
|
ActiveSupport.on_load(:active_record) do
|
64
|
-
|
63
|
+
extend ActiveRecord::Connections
|
65
64
|
end
|
@@ -66,6 +66,14 @@ class ActiveRecord::ConnectionsTest < MiniTest::Unit::TestCase
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
+
def test_proxy_connection
|
70
|
+
Thread.new do
|
71
|
+
ActiveRecord::Base.proxy_connection = 'proxy connection from another thread'
|
72
|
+
end.join
|
73
|
+
|
74
|
+
assert_nil ActiveRecord::Base.proxy_connection
|
75
|
+
end
|
76
|
+
|
69
77
|
protected
|
70
78
|
|
71
79
|
def each_customer(&block)
|
metadata
CHANGED
@@ -1,94 +1,89 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-connections
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.2
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Gabriel Sobrinho
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
dependencies:
|
16
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-12-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
17
15
|
name: activesupport
|
18
|
-
|
19
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &70157427173000 !ruby/object:Gem::Requirement
|
20
17
|
none: false
|
21
|
-
requirements:
|
22
|
-
- -
|
23
|
-
- !ruby/object:Gem::Version
|
24
|
-
version:
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
25
22
|
type: :runtime
|
26
|
-
version_requirements: *id001
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: activerecord
|
29
23
|
prerelease: false
|
30
|
-
|
24
|
+
version_requirements: *70157427173000
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
requirement: &70157427172480 !ruby/object:Gem::Requirement
|
31
28
|
none: false
|
32
|
-
requirements:
|
33
|
-
- -
|
34
|
-
- !ruby/object:Gem::Version
|
35
|
-
version:
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
36
33
|
type: :runtime
|
37
|
-
version_requirements: *id002
|
38
|
-
- !ruby/object:Gem::Dependency
|
39
|
-
name: rake
|
40
34
|
prerelease: false
|
41
|
-
|
35
|
+
version_requirements: *70157427172480
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &70157427171940 !ruby/object:Gem::Requirement
|
42
39
|
none: false
|
43
|
-
requirements:
|
44
|
-
- -
|
45
|
-
- !ruby/object:Gem::Version
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
46
43
|
version: 0.8.7
|
47
44
|
type: :development
|
48
|
-
version_requirements: *id003
|
49
|
-
- !ruby/object:Gem::Dependency
|
50
|
-
name: minitest
|
51
45
|
prerelease: false
|
52
|
-
|
46
|
+
version_requirements: *70157427171940
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: minitest
|
49
|
+
requirement: &70157427171400 !ruby/object:Gem::Requirement
|
53
50
|
none: false
|
54
|
-
requirements:
|
55
|
-
- -
|
56
|
-
- !ruby/object:Gem::Version
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
57
54
|
version: 2.3.1
|
58
55
|
type: :development
|
59
|
-
version_requirements: *id004
|
60
|
-
- !ruby/object:Gem::Dependency
|
61
|
-
name: minitest-colorize
|
62
56
|
prerelease: false
|
63
|
-
|
57
|
+
version_requirements: *70157427171400
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: minitest-colorize
|
60
|
+
requirement: &70157427170660 !ruby/object:Gem::Requirement
|
64
61
|
none: false
|
65
|
-
requirements:
|
66
|
-
- -
|
67
|
-
- !ruby/object:Gem::Version
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
68
65
|
version: 0.0.4
|
69
66
|
type: :development
|
70
|
-
version_requirements: *id005
|
71
|
-
- !ruby/object:Gem::Dependency
|
72
|
-
name: sqlite3
|
73
67
|
prerelease: false
|
74
|
-
|
68
|
+
version_requirements: *70157427170660
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: &70157427169760 !ruby/object:Gem::Requirement
|
75
72
|
none: false
|
76
|
-
requirements:
|
77
|
-
- -
|
78
|
-
- !ruby/object:Gem::Version
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
79
76
|
version: 1.3.4
|
80
77
|
type: :development
|
81
|
-
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70157427169760
|
82
80
|
description: A new way to manage multi-tenant applications based on multiples databases
|
83
|
-
email:
|
81
|
+
email:
|
84
82
|
- gabriel.sobrinho@gmail.com
|
85
83
|
executables: []
|
86
|
-
|
87
84
|
extensions: []
|
88
|
-
|
89
85
|
extra_rdoc_files: []
|
90
|
-
|
91
|
-
files:
|
86
|
+
files:
|
92
87
|
- .gitignore
|
93
88
|
- Gemfile
|
94
89
|
- MIT-LICENSE
|
@@ -104,35 +99,31 @@ files:
|
|
104
99
|
- test/support/contact.rb
|
105
100
|
- test/support/customer.rb
|
106
101
|
- test/test_helper.rb
|
107
|
-
has_rdoc: true
|
108
102
|
homepage: https://github.com/sobrinho/activerecord-connections
|
109
103
|
licenses: []
|
110
|
-
|
111
104
|
post_install_message:
|
112
105
|
rdoc_options: []
|
113
|
-
|
114
|
-
require_paths:
|
106
|
+
require_paths:
|
115
107
|
- lib
|
116
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
109
|
none: false
|
118
|
-
requirements:
|
119
|
-
- -
|
120
|
-
- !ruby/object:Gem::Version
|
121
|
-
version:
|
122
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
115
|
none: false
|
124
|
-
requirements:
|
125
|
-
- -
|
126
|
-
- !ruby/object:Gem::Version
|
127
|
-
version:
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
128
120
|
requirements: []
|
129
|
-
|
130
121
|
rubyforge_project:
|
131
|
-
rubygems_version: 1.
|
122
|
+
rubygems_version: 1.8.11
|
132
123
|
signing_key:
|
133
124
|
specification_version: 3
|
134
125
|
summary: A new way to manage multi-tenant applications based on multiples databases
|
135
|
-
test_files:
|
126
|
+
test_files:
|
136
127
|
- test/active_record/connections_test.rb
|
137
128
|
- test/db/migrate/20110822114329_create_customers.rb
|
138
129
|
- test/db/migrate/20110822114413_create_contacts.rb
|