db-charmer 1.5.3 → 1.5.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +8 -0
- data/README.rdoc +23 -0
- data/VERSION +1 -1
- data/db-charmer.gemspec +2 -2
- data/lib/db_charmer.rb +43 -0
- data/lib/db_charmer/active_record_extensions.rb +21 -1
- data/lib/db_charmer/connection_factory.rb +1 -1
- data/lib/db_charmer/connection_proxy.rb +6 -1
- metadata +3 -3
data/CHANGES
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
1.5.3 -> 1.5.4:
|
2
|
+
|
3
|
+
Added DbCharmer.with_remapped_databases, so that you can change the connection for
|
4
|
+
many models simultaneously, and implicitly. Very useful for work where you want to use
|
5
|
+
a particular slave for a whole range of database access.
|
6
|
+
|
7
|
+
----------------------------------------------------------------------------------------
|
8
|
+
|
1
9
|
1.5.1 -> 1.5.3:
|
2
10
|
|
3
11
|
Few changes:
|
data/README.rdoc
CHANGED
@@ -232,6 +232,29 @@ and HABTM associations connection switching:
|
|
232
232
|
@user.on_slave.posts
|
233
233
|
|
234
234
|
|
235
|
+
=== Bulk Connection Management
|
236
|
+
|
237
|
+
Sometimes you want to run code where a large number of tables may be used, and you'd like
|
238
|
+
them all to use an alternate database. You can now do this:
|
239
|
+
|
240
|
+
DbCharmer.with_remapped_databases(:logs => :big_logs_slave) { ... }
|
241
|
+
|
242
|
+
Any model whose default database is +:logs+ (e.g., <tt>db_charmer :connection => :logs</tt>)
|
243
|
+
will now have its connection switched to +:big_logs_slave+ in that block. This is lower
|
244
|
+
precedence than any other +DbCharmer+ method, so <tt>Model.on_db(:foo).find(...)</tt> and
|
245
|
+
such things will still use the database they specify, not the one that model was remapped
|
246
|
+
to.
|
247
|
+
|
248
|
+
You can specify any number of remappings at once, and you can also use +:master+ as a database
|
249
|
+
name that matches any model that has not had its connection set by +DbCharmer+ at all.
|
250
|
+
|
251
|
+
*Note*: +DbCharmer+ works via +alias_method_chain+ in model classes. It is very careful
|
252
|
+
to only patch the models it needs to. However, if you use +with_remapped_databases+ and
|
253
|
+
remap the default database (+:master+), then it has no choice but to patch all subclasses
|
254
|
+
of +ActiveRecord::Base+. This should not cause any serious problems or any big performance
|
255
|
+
impact, but it is worth noting.
|
256
|
+
|
257
|
+
|
235
258
|
=== Named Scopes Support
|
236
259
|
|
237
260
|
To make it easier for +DbCharmer+ users to use connections switching methods with named scopes,
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.5.
|
1
|
+
1.5.4
|
data/db-charmer.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{db-charmer}
|
8
|
-
s.version = "1.5.
|
8
|
+
s.version = "1.5.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alexey Kovyrin"]
|
12
|
-
s.date = %q{2010-03-
|
12
|
+
s.date = %q{2010-03-12}
|
13
13
|
s.description = %q{ActiveRecord Connections Magic (slaves, multiple connections, etc)}
|
14
14
|
s.email = %q{alexey@kovyrin.net}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/db_charmer.rb
CHANGED
@@ -19,6 +19,37 @@ module DbCharmer
|
|
19
19
|
return Rails.logger if defined?(Rails)
|
20
20
|
@logger ||= Logger.new(STDERR)
|
21
21
|
end
|
22
|
+
|
23
|
+
def self.with_remapped_databases(mappings, &proc)
|
24
|
+
old_mappings = ActiveRecord::Base.db_charmer_database_remappings
|
25
|
+
begin
|
26
|
+
ActiveRecord::Base.db_charmer_database_remappings = mappings
|
27
|
+
if mappings[:master] || mappings['master']
|
28
|
+
with_all_hijacked(&proc)
|
29
|
+
else
|
30
|
+
proc.call
|
31
|
+
end
|
32
|
+
ensure
|
33
|
+
ActiveRecord::Base.db_charmer_database_remappings = old_mappings
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.hijack_new_classes?
|
38
|
+
@@hijack_new_classes
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
@@hijack_new_classes = false
|
43
|
+
def self.with_all_hijacked
|
44
|
+
old_hijack_new_classes = @@hijack_new_classes
|
45
|
+
begin
|
46
|
+
@@hijack_new_classes = true
|
47
|
+
ActiveRecord::Base.send(:subclasses).each { |s| s.hijack_connection! }
|
48
|
+
yield
|
49
|
+
ensure
|
50
|
+
@@hijack_new_classes = old_hijack_new_classes
|
51
|
+
end
|
52
|
+
end
|
22
53
|
end
|
23
54
|
|
24
55
|
class Object
|
@@ -101,3 +132,15 @@ ActiveRecord::Base.extend(DbCharmer::DbMagic::ClassMethods)
|
|
101
132
|
|
102
133
|
# Setup association preload magic
|
103
134
|
ActiveRecord::Base.extend(DbCharmer::AssociationPreload::ClassMethods)
|
135
|
+
|
136
|
+
class ActiveRecord::Base
|
137
|
+
class << self
|
138
|
+
def inherited_with_hijacking(subclass)
|
139
|
+
out = inherited_without_hijacking(subclass)
|
140
|
+
hijack_connection! if DbCharmer.hijack_new_classes?
|
141
|
+
out
|
142
|
+
end
|
143
|
+
|
144
|
+
alias_method_chain :inherited, :hijacking
|
145
|
+
end
|
146
|
+
end
|
@@ -63,12 +63,32 @@ module DbCharmer
|
|
63
63
|
db_charmer_connection_level.zero?
|
64
64
|
end
|
65
65
|
|
66
|
+
#-----------------------------------------------------------------------------
|
67
|
+
@@db_charmer_database_remappings = Hash.new
|
68
|
+
def db_charmer_remapped_connection
|
69
|
+
return nil if (db_charmer_connection_level || 0) > 0
|
70
|
+
name = db_charmer_connection_proxy.db_charmer_connection_name if db_charmer_connection_proxy
|
71
|
+
name = (name || :master).to_sym
|
72
|
+
|
73
|
+
remapped = @@db_charmer_database_remappings[name]
|
74
|
+
return DbCharmer::ConnectionFactory.connect(remapped, true) if remapped
|
75
|
+
end
|
76
|
+
|
77
|
+
def db_charmer_database_remappings
|
78
|
+
@@db_charmer_database_remappings
|
79
|
+
end
|
80
|
+
|
81
|
+
def db_charmer_database_remappings=(mappings)
|
82
|
+
raise "Mappings must be nil or respond to []" if mappings && (! mappings.respond_to?(:[]))
|
83
|
+
@@db_charmer_database_remappings = mappings || { }
|
84
|
+
end
|
85
|
+
|
66
86
|
#-----------------------------------------------------------------------------
|
67
87
|
def hijack_connection!
|
68
88
|
return if self.respond_to?(:connection_with_magic)
|
69
89
|
class << self
|
70
90
|
def connection_with_magic
|
71
|
-
db_charmer_connection_proxy || connection_without_magic
|
91
|
+
db_charmer_remapped_connection || db_charmer_connection_proxy || connection_without_magic
|
72
92
|
end
|
73
93
|
alias_method_chain :connection, :magic
|
74
94
|
end
|
@@ -21,7 +21,7 @@ module DbCharmer
|
|
21
21
|
# Establish connection with a specified name
|
22
22
|
def self.establish_connection(db_name, should_exist = false)
|
23
23
|
abstract_class = generate_abstract_class(db_name, should_exist)
|
24
|
-
DbCharmer::ConnectionProxy.new(abstract_class)
|
24
|
+
DbCharmer::ConnectionProxy.new(abstract_class, db_name)
|
25
25
|
end
|
26
26
|
|
27
27
|
# Generate an abstract AR class with specified connection established
|
@@ -1,7 +1,12 @@
|
|
1
1
|
module DbCharmer
|
2
2
|
class ConnectionProxy < BlankSlate
|
3
|
-
def initialize(abstract_class)
|
3
|
+
def initialize(abstract_class, db_name)
|
4
4
|
@abstract_connection_class = abstract_class
|
5
|
+
@db_name = db_name
|
6
|
+
end
|
7
|
+
|
8
|
+
def db_charmer_connection_name
|
9
|
+
@db_name
|
5
10
|
end
|
6
11
|
|
7
12
|
def method_missing(meth, *args, &block)
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 5
|
8
|
-
-
|
9
|
-
version: 1.5.
|
8
|
+
- 4
|
9
|
+
version: 1.5.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Alexey Kovyrin
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-12 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|