acts_as_partitionable 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -45,22 +45,28 @@ module Acts
45
45
  raise ::ActiveRecord::ConfigurationError.new
46
46
  end
47
47
  end
48
-
49
- partition_db_klass ||= eval <<-TOS
50
- class #{self.name}Partitionable#{partition_name} < #{self.name}
51
- # setting abstract_class to true so that ActiveRecord doesn't look
52
- # for a db table for this base model class
53
- self.abstract_class = true
54
- def self.find_every(options)
55
- (options ||= {})[:readonly] = true if #{partition_db_klass_readonly}
56
- super(options)
57
- end
58
- def self.readonly?; #{partition_db_klass_readonly}; end
59
- end
60
- #{self.name}Partitionable#{partition_name}
61
- TOS
62
- partition_db_klass.establish_connection(db_config)
63
48
 
49
+ self_name = (self.name.include?('::') ? '' : '::') + self.name
50
+ partition_db_klass = ("#{self_name}Partitionable#{partition_name}".constantize rescue nil)
51
+
52
+ if partition_db_klass.nil?
53
+ partition_db_klass ||= eval <<-TOS
54
+ class #{self_name}Partitionable#{partition_name} < #{self_name}
55
+ # setting abstract_class to true so that ActiveRecord doesn't look
56
+ # for a db table for this base model class
57
+ self.abstract_class = true
58
+ def self.find_every(options)
59
+ (options ||= {})[:readonly] = true if #{partition_db_klass_readonly}
60
+ super(options)
61
+ end
62
+ def self.readonly?; #{partition_db_klass_readonly}; end
63
+ end
64
+ #{self_name}Partitionable#{partition_name}
65
+ TOS
66
+
67
+ partition_db_klass.establish_connection(db_config)
68
+ end
69
+
64
70
  class_eval <<-PART_PROC_END
65
71
  def self.#{partition_name}(&block)
66
72
  return #{partition_db_klass}.module_eval(&block) if block_given?
@@ -1,20 +1,24 @@
1
1
  # CONFIDENTIAL AND PROPRIETARY. © 2007 Revolution Health Group LLC. All rights reserved.
2
+
2
3
  module ActiveRecord
3
4
  class Base
4
5
  class << self
5
6
  alias_method :no_cp_remove_connection, :remove_connection
6
7
  alias_method :no_cp_establish_connection, :establish_connection
8
+ alias_method :no_cp_clear_active_connections!, :clear_active_connections!
7
9
 
8
10
  @@pooled_connections ||= {}
9
11
  def pooled_connections
10
12
  @@pooled_connections[Thread.current.object_id] ||= {}
11
13
  end
12
14
 
13
- def close_all_connections
14
- pooled_connections.each do | key, value |
15
- pooled_connections.delete(key)
16
- active_connections.select { |klass, konn| no_cp_remove_connection(klass.constantize) if konn == value[:connection] }
15
+ def clear_active_connections!
16
+ active_connections.each do |name, conn|
17
+ conn_key = nil
18
+ pooled_connections.each { |key, value| conn_key = key if value != nil && value[:connection] == conn }
19
+ pooled_connections.delete(conn_key) if not conn_key.nil?
17
20
  end
21
+ no_cp_clear_active_connections!
18
22
  end
19
23
 
20
24
  def remove_connection(klass=self)
@@ -34,15 +38,38 @@ module ActiveRecord
34
38
  no_cp_remove_connection(klass)
35
39
  end # remove_connection
36
40
 
37
- def establish_connection(spec = nil)
38
- if spec != nil and spec.kind_of?(ActiveRecord::Base::ConnectionSpecification)
39
- @partitionable_adapter_methods ||= []
40
- if not @partitionable_adapter_methods.include?(spec.adapter_method.to_s)
41
- @partitionable_adapter_methods << (spec.adapter_method.to_s)
42
-
43
- self.class_eval <<-REUSE_END
41
+ def establish_connection(spec = nil)
42
+ pool_connection(spec) if spec != nil and spec.kind_of?(ActiveRecord::Base::ConnectionSpecification)
43
+ no_cp_establish_connection(spec)
44
+ end # establish_connection
45
+
46
+
47
+ # closing all is pretty final, you don't want to do this unless you are really all done
48
+ def close_all_connections
49
+ ar_konn = nil
50
+ pooled_connections.each do | key, value |
51
+ pooled_connections.delete(key)
52
+ active_connections.select do |klass, konn|
53
+ if konn == value[:connection]
54
+ result = no_cp_remove_connection(klass.constantize)
55
+ ar_konn = result if klass.constantize == ActiveRecord::Base
56
+ end
57
+ end
58
+ end
59
+ ar_konn
60
+ end
61
+
62
+ private
63
+ def pool_connection(spec)
64
+ @partitionable_adapter_methods ||= []
65
+
66
+ if not @partitionable_adapter_methods.include?(spec.adapter_method.to_s)
67
+ @partitionable_adapter_methods << (spec.adapter_method.to_s)
68
+
69
+ if not ActiveRecord::Base.methods.include?("ar_#{spec.adapter_method}")
70
+ ActiveRecord::Base.class_eval <<-REUSE_END
44
71
  class << self
45
- alias_method :ar_#{spec.adapter_method}, :#{spec.adapter_method}
72
+ alias_method :ar_#{spec.adapter_method}, :#{spec.adapter_method}
46
73
  end
47
74
  def self.#{spec.adapter_method}(config)
48
75
  conn_key = config.to_s
@@ -54,11 +81,16 @@ module ActiveRecord
54
81
  pooled_connections[conn_key][:connection] rescue nil
55
82
  end
56
83
  REUSE_END
57
- end
58
- end
59
- no_cp_establish_connection(spec)
60
- end # establish_connection
61
- end
84
+ end # if not method included
85
+ end # if not adapter included
86
+ end # pool_connection
87
+
88
+
89
+ def reset_ref_counts
90
+ ActiveRecord::Base.pooled_connections.each {| key, value | value[:count] = 1 }
91
+ end # reset_ref_counts
92
+
93
+ end # self
62
94
  end # Pool
63
95
  end # Connection
64
96
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: acts_as_partitionable
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2007-04-20 00:00:00 -04:00
6
+ version: 0.0.3
7
+ date: 2007-04-24 00:00:00 -04:00
8
8
  summary: acts_as_partitionable allows one to use multiple DBs to partition data for models
9
9
  require_paths:
10
10
  - lib