connection_manager 0.3.10 → 0.3.11
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/connection_manager/connection_builder.rb +1 -1
- data/lib/connection_manager/helpers/connection_helpers.rb +30 -8
- data/lib/connection_manager/patches/cross_schema_patch.rb +1 -23
- data/lib/connection_manager/replication.rb +1 -1
- data/lib/connection_manager/using.rb +6 -2
- data/lib/connection_manager/version.rb +1 -1
- data/spec/lib/connection_builder_spec.rb +1 -2
- data/spec/lib/connection_helpers_spec.rb +10 -1
- data/spec/lib/replication_spec.rb +5 -4
- metadata +2 -2
@@ -52,7 +52,7 @@ module ConnectionManager
|
|
52
52
|
klass = Class.new(ActiveRecord::Base)
|
53
53
|
new_connection_class = Object.const_set(class_name, klass)
|
54
54
|
new_connection_class.establish_managed_connection(connection_key)
|
55
|
-
new_connection_class.
|
55
|
+
new_connection_class.use_database(new_connection_class.current_database_name)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
@@ -3,6 +3,26 @@ module ConnectionManager
|
|
3
3
|
module ConnectionHelpers
|
4
4
|
@@managed_connections = HashWithIndifferentAccess.new
|
5
5
|
|
6
|
+
# Returns the database_name of the connection unless set otherwise
|
7
|
+
def current_database_name
|
8
|
+
return "#{connection.config[:database].to_s}" if @current_database_name.blank?
|
9
|
+
@current_database_name
|
10
|
+
end
|
11
|
+
alias :database_name :current_database_name
|
12
|
+
alias :current_schema_name :current_database_name
|
13
|
+
alias :schema_name :current_database_name
|
14
|
+
|
15
|
+
|
16
|
+
# Sometimes we need to manually set the database name, like when the connection
|
17
|
+
# has a database but our table is in a different database/schema but on the
|
18
|
+
# same DMS.
|
19
|
+
def current_database_name=current_database_name
|
20
|
+
@current_database_name= current_database_name
|
21
|
+
end
|
22
|
+
alias :database_name= :current_database_name=
|
23
|
+
alias :current_schema_name= :current_database_name=
|
24
|
+
alias :schema_name= :current_database_name=
|
25
|
+
|
6
26
|
# Returns true if this is a readonly only a readonly model
|
7
27
|
# If the connection.readonly? then the model that uses the connection
|
8
28
|
# must be readonly.
|
@@ -51,16 +71,18 @@ module ConnectionManager
|
|
51
71
|
#
|
52
72
|
# LegacyUser.limit(1).to_sql => "SELECT * FROM `BDUser`.`UserData` LIMIT 1
|
53
73
|
#
|
54
|
-
def use_database(database_name,opts={})
|
55
|
-
self.
|
56
|
-
opts[:table_name_prefix]
|
74
|
+
def use_database(database_name=nil,opts={})
|
75
|
+
self.current_database_name = database_name if database_name
|
76
|
+
opts[:table_name_prefix] = "#{self.current_database_name}." if opts[:table_name_prefix].blank? && self.connection.cross_database_support?
|
77
|
+
unless self.abstract_class?
|
78
|
+
opts[:table_name] = self.table_name if opts[:table_name].blank?
|
79
|
+
opts[:table_name].gsub!(self.table_name_prefix,'') unless self.table_name_prefix.blank?
|
80
|
+
self.table_name = "#{opts[:table_name_prefix]}#{opts[:table_name]}"
|
81
|
+
end
|
57
82
|
self.table_name_prefix = opts[:table_name_prefix] unless opts[:table_name_prefix].blank?
|
58
|
-
opts[:table_name] ||= self.table_name
|
59
|
-
opts[:table_name] = opts[:table_name].to_s.split('.').last if self.connection.cross_database_support?
|
60
|
-
self.table_name = "#{opts[:table_name_prefix]}#{opts[:table_name]}" unless self.abstract_class? || opts[:table_name].blank?
|
61
83
|
end
|
62
84
|
alias :use_schema :use_database
|
63
|
-
|
85
|
+
|
64
86
|
# Establishes and checks in a connection, normally for abstract classes AKA connection classes.
|
65
87
|
#
|
66
88
|
# Options:
|
@@ -82,7 +104,7 @@ module ConnectionManager
|
|
82
104
|
self.abstract_class = opts[:abstract_class]
|
83
105
|
set_to_readonly if (readonly? || opts[:readonly] || self.connection.readonly?)
|
84
106
|
add_managed_connections(yml_key,opts[:class_name])
|
85
|
-
use_database(self.
|
107
|
+
use_database(self.current_database_name,opts)
|
86
108
|
end
|
87
109
|
|
88
110
|
# Override ActiveRecord::Base instance method readonly? to force
|
@@ -36,27 +36,5 @@ if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR <= 2
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
# We need to make sure we override existing database_name name methods
|
43
|
-
module ActiveRecord
|
44
|
-
class Base
|
45
|
-
class << self
|
46
|
-
# Returns the database_name of the connection unless set otherwise
|
47
|
-
def database_name
|
48
|
-
@database_name = "#{connection.config[:database].to_s}" if @database_name.blank?
|
49
|
-
@database_name
|
50
|
-
end
|
51
|
-
alias :schema_name :database_name
|
52
|
-
|
53
|
-
# Sometimes we need to manually set the database name, like when the connection
|
54
|
-
# has a database but our table is in a different database/schema but on the
|
55
|
-
# same DMS.
|
56
|
-
def database_name=database_name
|
57
|
-
@database_name = database_name
|
58
|
-
end
|
59
|
-
alias :schema_name= :database_name=
|
60
|
-
end
|
61
39
|
end
|
62
|
-
end
|
40
|
+
end
|
@@ -38,7 +38,7 @@ module ConnectionManager
|
|
38
38
|
options = {:name => "slaves"}.merge!(connections.extract_options!)
|
39
39
|
options[:type] ||= :slaves
|
40
40
|
options[:build_replicants] = true if (options[:build_replicants].blank? && options[:type] == :masters)
|
41
|
-
|
41
|
+
use_database(current_database_name, :table_name => table_name) # make sure the base class has current_database_name set
|
42
42
|
connections = connection.replication_keys(options[:type]) if connections.blank?
|
43
43
|
set_replications_to_method(connections,options[:name])
|
44
44
|
build_repliciation_class_method(options)
|
@@ -6,6 +6,7 @@ module ConnectionManager
|
|
6
6
|
d = fetch_duplicate_class(connection_class_name)
|
7
7
|
r = ActiveRecord::Relation.new(d, d.arel_table)
|
8
8
|
r = r.readonly if d.connection.readonly?
|
9
|
+
r = r.from(d.quoted_table_name) unless (ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 0)
|
9
10
|
r
|
10
11
|
end
|
11
12
|
|
@@ -28,10 +29,11 @@ module ConnectionManager
|
|
28
29
|
# return the connection class as AR sometimes uses the the superclass
|
29
30
|
# connection
|
30
31
|
def build_dup_class(connection_class_name)
|
32
|
+
use_database(self.current_database_name) # make sure we are consistent from super to dup
|
31
33
|
con_class = connection_class_name.constantize
|
32
|
-
db_name = con_class.
|
34
|
+
db_name = con_class.current_database_name
|
33
35
|
dup_klass = dup
|
34
|
-
dup_klass.class_eval <<-STR
|
36
|
+
dup_klass.class_eval <<-STR
|
35
37
|
self.use_database('#{db_name}',{:table_name => '#{table_name}'})
|
36
38
|
class << self
|
37
39
|
def model_name
|
@@ -50,6 +52,7 @@ module ConnectionManager
|
|
50
52
|
STR
|
51
53
|
|
52
54
|
self.const_set("#{connection_class_name}Dup", dup_klass)
|
55
|
+
"#{self.name}::#{connection_class_name}Dup".constantize
|
53
56
|
end
|
54
57
|
end
|
55
58
|
|
@@ -68,6 +71,7 @@ module ConnectionManager
|
|
68
71
|
d = klass.using(connection_class_name)
|
69
72
|
relation = clone
|
70
73
|
relation.instance_variable_set(:@klass, d.klass)
|
74
|
+
relation = relation.from(d.quoted_table_name) unless (ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 0)
|
71
75
|
relation
|
72
76
|
end
|
73
77
|
end
|
@@ -16,6 +16,11 @@ describe ConnectionManager::ConnectionHelpers do
|
|
16
16
|
}, {:readonly => true})
|
17
17
|
end
|
18
18
|
|
19
|
+
class MyPrefixedConnection < MyConnectionClass
|
20
|
+
self.abstract_class = true
|
21
|
+
self.use_database("boo")
|
22
|
+
end
|
23
|
+
|
19
24
|
class MyFoo < MyConnectionClass
|
20
25
|
self.table_name = 'foos'
|
21
26
|
end
|
@@ -51,7 +56,7 @@ describe ConnectionManager::ConnectionHelpers do
|
|
51
56
|
describe '#use_database' do
|
52
57
|
it "should set the database/schema for the model to the supplied schema_name" do
|
53
58
|
Fruit.use_database('my_schema')
|
54
|
-
Fruit.
|
59
|
+
Fruit.current_database_name.should eql('my_schema')
|
55
60
|
end
|
56
61
|
|
57
62
|
it "should set the contactinate the schema_name and table_name; and set the table_name to that value" do
|
@@ -66,5 +71,9 @@ describe ConnectionManager::ConnectionHelpers do
|
|
66
71
|
Fruit.table_name_prefix.should eql('my_schema.')
|
67
72
|
end
|
68
73
|
end
|
74
|
+
|
75
|
+
it "should have the correct database name" do
|
76
|
+
MyPrefixedConnection.current_database_name.should eql('boo')
|
77
|
+
end
|
69
78
|
end
|
70
79
|
|
@@ -3,7 +3,7 @@ describe ConnectionManager::Replication do
|
|
3
3
|
|
4
4
|
describe '#database_name' do
|
5
5
|
it "should return the name of the database the model is using" do
|
6
|
-
Fruit.
|
6
|
+
Fruit.current_database_name.should eql('cm_test')
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
@@ -45,6 +45,7 @@ describe ConnectionManager::Replication do
|
|
45
45
|
Fruit.replicated
|
46
46
|
Fruit.slaves.joins(:region).to_sql.should eql(Fruit.joins(:region).to_sql)
|
47
47
|
Fruit.slaves.joins(:fruit_baskets).to_sql.should eql(Fruit.joins(:fruit_baskets).to_sql)
|
48
|
+
Fruit.slaves.joins(:region).joins("LEFT OUTER JOIN `fruit_baskets` ON `fruit_baskets`.`fruit_id` = `cm_test`.`fruits`.`id`").to_sql.should eql(Fruit.joins(:region).joins("LEFT OUTER JOIN `fruit_baskets` ON `fruit_baskets`.`fruit_id` = `cm_test`.`fruits`.`id`").to_sql)
|
48
49
|
Fruit.slaves.includes(:fruit_baskets).to_sql.should eql(Fruit.includes(:fruit_baskets).to_sql)
|
49
50
|
Fruit.slaves.includes(:region).to_sql.should eql(Fruit.includes(:region).to_sql)
|
50
51
|
end
|
@@ -108,9 +109,9 @@ describe ConnectionManager::Replication do
|
|
108
109
|
|
109
110
|
# We'd like this to happen magically some day. Possible in 3.2
|
110
111
|
it "should eager load with replication instances" #do
|
111
|
-
|
112
|
-
|
113
|
-
|
112
|
+
# user = User.slaves.includes(:foos).where(:id => @user.id).first
|
113
|
+
# user.foos.first.should_not be_kind_of(Foo)
|
114
|
+
# end
|
114
115
|
|
115
116
|
context "specifically defined replication association" do
|
116
117
|
it "should eager load with replication instances" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: connection_manager
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.11
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|