db-charmer 1.6.19 → 1.7.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. data/CHANGES +14 -2
  2. data/README.rdoc +89 -4
  3. data/Rakefile +4 -0
  4. data/db-charmer.gemspec +17 -15
  5. data/lib/db_charmer.rb +34 -43
  6. data/lib/db_charmer/{abstract_adapter_extensions.rb → abstract_adapter/log_formatting.rb} +5 -3
  7. data/lib/db_charmer/action_controller/force_slave_reads.rb +65 -0
  8. data/lib/db_charmer/{association_preload.rb → active_record/association_preload.rb} +2 -2
  9. data/lib/db_charmer/{active_record_extensions.rb → active_record/class_attributes.rb} +19 -40
  10. data/lib/db_charmer/active_record/connection_switching.rb +77 -0
  11. data/lib/db_charmer/{db_magic.rb → active_record/db_magic.rb} +18 -8
  12. data/lib/db_charmer/active_record/finder_overrides.rb +61 -0
  13. data/lib/db_charmer/active_record/migration/multi_db_migrations.rb +71 -0
  14. data/lib/db_charmer/active_record/multi_db_proxy.rb +65 -0
  15. data/lib/db_charmer/active_record/named_scope/scope_proxy.rb +26 -0
  16. data/lib/db_charmer/active_record/sharding.rb +40 -0
  17. data/lib/db_charmer/connection_factory.rb +1 -1
  18. data/lib/db_charmer/core_extensions.rb +10 -0
  19. data/lib/db_charmer/force_slave_reads.rb +36 -0
  20. data/lib/db_charmer/sharding.rb +0 -36
  21. data/lib/db_charmer/sharding/method/db_block_group_map.rb +3 -3
  22. data/lib/db_charmer/sharding/method/db_block_map.rb +3 -3
  23. data/lib/db_charmer/sharding/stub_connection.rb +4 -4
  24. data/lib/db_charmer/version.rb +10 -0
  25. data/lib/tasks/databases.rake +6 -6
  26. metadata +28 -21
  27. data/VERSION +0 -1
  28. data/lib/db_charmer/connection_switch.rb +0 -40
  29. data/lib/db_charmer/finder_overrides.rb +0 -56
  30. data/lib/db_charmer/multi_db_migrations.rb +0 -67
  31. data/lib/db_charmer/multi_db_proxy.rb +0 -63
  32. data/lib/db_charmer/scope_proxy.rb +0 -22
@@ -1,67 +0,0 @@
1
- module DbCharmer
2
- module MultiDbMigrations
3
- def self.extended(base)
4
- class << base
5
- alias_method_chain :migrate, :db_wrapper
6
- end
7
- end
8
-
9
- @@multi_db_names = {}
10
- def multi_db_names
11
- puts "Retrieving connections for #{self.name}"
12
- @@multi_db_names[self.name] || @@multi_db_names['ActiveRecord::Migration']
13
- end
14
-
15
- def multi_db_names=(names)
16
- puts "Setting connections for #{self.name}"
17
- @@multi_db_names[self.name] = names
18
- end
19
-
20
- def migrate_with_db_wrapper(direction)
21
- if names = multi_db_names
22
- names.each do |multi_db_name|
23
- on_db(multi_db_name) do
24
- migrate_without_db_wrapper(direction)
25
- end
26
- end
27
- else
28
- migrate_without_db_wrapper(direction)
29
- end
30
- end
31
-
32
- def on_db(db_name)
33
- name = db_name.is_a?(Hash) ? db_name[:connection_name] : db_name.inspect
34
- announce "Switching connection to #{name}"
35
- # Switch connection
36
- old_proxy = ActiveRecord::Base.db_charmer_connection_proxy
37
- db_name = nil if db_name == :default
38
- ActiveRecord::Base.switch_connection_to(db_name, DbCharmer.connections_should_exist?)
39
- # Yield the block
40
- yield
41
- ensure
42
- # Switch it back
43
- ActiveRecord::Base.verify_active_connections!
44
- announce "Switching connection back"
45
- ActiveRecord::Base.switch_connection_to(old_proxy)
46
- end
47
-
48
- def db_magic(opts = {})
49
- # Collect connections from all possible options
50
- conns = [ opts[:connection], opts[:connections] ]
51
- conns << shard_connections(opts[:sharded_connection]) if opts[:sharded_connection]
52
-
53
- # Get a unique set of connections
54
- conns = conns.flatten.compact.uniq
55
- raise ArgumentError, "No connection name - no magic!" unless conns.any?
56
-
57
- # Save connections
58
- self.multi_db_names = conns
59
- end
60
-
61
- # Return a list of connections to shards in a sharded connection
62
- def shard_connections(conn_name)
63
- conn = DbCharmer::Sharding.sharded_connection(conn_name)
64
- conn.shard_connections
65
- end
66
- end
67
- end
@@ -1,63 +0,0 @@
1
- module DbCharmer
2
- module MultiDbProxy
3
- # Simple proxy class that switches connections and then proxies all the calls
4
- # This class is used to implement chained on_db calls
5
- class OnDbProxy < BlankSlate
6
- def initialize(proxy_target, slave)
7
- @proxy_target = proxy_target
8
- @slave = slave
9
- end
10
-
11
- private
12
-
13
- def method_missing(meth, *args, &block)
14
- # Switch connection and proxy the method call
15
- @proxy_target.on_db(@slave) do |proxy_target|
16
- res = proxy_target.__send__(meth, *args, &block)
17
-
18
- # If result is a scope/association, return a new proxy for it, otherwise return the result itself
19
- (res.proxy?) ? OnDbProxy.new(res, @slave) : res
20
- end
21
- end
22
- end
23
-
24
- module ClassMethods
25
- def on_db(con, proxy_target = nil)
26
- proxy_target ||= self
27
-
28
- # Chain call
29
- return OnDbProxy.new(proxy_target, con) unless block_given?
30
-
31
- # Block call
32
- begin
33
- self.db_charmer_connection_level += 1
34
- old_proxy = db_charmer_connection_proxy
35
- switch_connection_to(con, DbCharmer.connections_should_exist?)
36
- yield(proxy_target)
37
- ensure
38
- switch_connection_to(old_proxy)
39
- self.db_charmer_connection_level -= 1
40
- end
41
- end
42
- end
43
-
44
- module InstanceMethods
45
- def on_db(con, proxy_target = nil, &block)
46
- proxy_target ||= self
47
- self.class.on_db(con, proxy_target, &block)
48
- end
49
- end
50
-
51
- module MasterSlaveClassMethods
52
- def on_slave(con = nil, proxy_target = nil, &block)
53
- con ||= db_charmer_random_slave
54
- raise ArgumentError, "No slaves found in the class and no slave connection given" unless con
55
- on_db(con, proxy_target, &block)
56
- end
57
-
58
- def on_master(proxy_target = nil, &block)
59
- on_db(db_charmer_default_connection, proxy_target, &block)
60
- end
61
- end
62
- end
63
- end
@@ -1,22 +0,0 @@
1
- module DbCharmer
2
- module ScopeProxy
3
- module InstanceMethods
4
- def proxy?
5
- true
6
- end
7
-
8
- def on_db(con, proxy_target = nil, &block)
9
- proxy_target ||= self
10
- proxy_scope.on_db(con, proxy_target, &block)
11
- end
12
-
13
- def on_slave(con = nil, &block)
14
- proxy_scope.on_slave(con, self, &block)
15
- end
16
-
17
- def on_master(&block)
18
- proxy_scope.on_master(self, &block)
19
- end
20
- end
21
- end
22
- end