Permalink
Browse files

All tests passing on AR 2.3.10, 3.0.0 and 3.0.3

  • Loading branch information...
1 parent 1f30855 commit 32dc66407f21561b41ef4828ba687a4ab186c163 @mperham mperham committed Nov 21, 2010
View
@@ -1,5 +1,7 @@
require 'active_record'
require 'active_record/version'
+require 'active_record/connection_adapters/abstract/connection_pool'
+require 'active_record/connection_adapters/abstract/connection_specification'
require 'data_fabric/version'
# DataFabric adds a new level of flexibility to ActiveRecord connection handling.
@@ -18,8 +18,30 @@ def to_s
end
end
+ class PoolProxy
+ def initialize(proxy)
+ @proxy = proxy
+ end
+ def connection
+ @proxy
+ end
+ def release_connection
+ DataFabric.logger.debug { 'data_fabric does not implement release_connection' }
+ end
+ def spec
+ @proxy.spec
+ end
+ def with_connection
+ yield @proxy
+ end
+ def connected?
+ @proxy.connected?
+ end
+ end
+
class ConnectionProxy
cattr_accessor :shard_pools
+ attr_accessor :spec
def initialize(model_class, options)
@model_class = model_class
@@ -70,6 +92,10 @@ def connected?
current_pool.connected?
end
+ def connection
+ current_pool.connection
+ end
+
private
def in_transaction?
@@ -90,7 +116,7 @@ def spec_for(config)
config = config.symbolize_keys
adapter_method = "#{config[:adapter]}_connection"
initialize_adapter(config[:adapter])
- ActiveRecord::Base::ConnectionSpecification.new(config, adapter_method)
+ @spec = ActiveRecord::Base::ConnectionSpecification.new(config, adapter_method)
end
def initialize_adapter(adapter)
@@ -113,16 +139,12 @@ def connection_name_builder
clauses << @prefix if @prefix
clauses << @shard_group if @shard_group
clauses << StringProxy.new { DataFabric.active_shard(@shard_group) } if @shard_group
- clauses << Rails.env
+ clauses << RAILS_ENV
clauses << StringProxy.new { current_role } if @replicated
clauses
end
end
- def connection
- current_pool.connection
- end
-
def set_role(role)
Thread.current[:data_fabric_role] = role
end
@@ -135,4 +157,5 @@ def master
with_master { return connection }
end
end
+
end
@@ -1,4 +1,3 @@
-require 'active_record/connection_adapters/abstract/connection_pool'
require 'data_fabric/connection_proxy'
class ActiveRecord::ConnectionAdapters::ConnectionHandler
@@ -23,25 +22,7 @@ def self.included(model)
module ClassMethods
def data_fabric(options)
DataFabric.logger.info { "Creating data_fabric proxy for class #{name}" }
- @proxy = DataFabric::ConnectionProxy.new(self, options)
-
- class << self
- def connection
- @proxy || superclass.connection
- end
-
- def connected?
- @proxy.connected?
- end
-
- def remove_connection(klass=self)
- DataFabric.logger.warn { "remove_connection not implemented by data_fabric" }
- end
-
- def connection_pool
- raise "dynamic connection switching means you cannot get direct access to a pool"
- end
- end
+ connection_handler.connection_pools[name] = PoolProxy.new(ConnectionProxy.new(self, options))
end
end
end
View
@@ -14,21 +14,29 @@ class TheWholeEnchilada < ActiveRecord::Base
end
class AdapterMock < ActiveRecord::ConnectionAdapters::AbstractAdapter
- # Minimum required to perform a find with no results
+ # Minimum required to perform a find with no results.
+ # Works on 2.3.10, 3.0.0 and 3.0.3.
def columns(table_name, name=nil)
- []
+ [ActiveRecord::ConnectionAdapters::Column.new('id', 0, :integer, false)]
+ end
+ def primary_key(name)
+ :id
+ end
+ def adapter_name
+ 'mysql'
end
def select(sql, name=nil)
[]
end
def execute(sql, name=nil)
- 0
+ []
end
-
- def name
- 'fake-db'
+ def tables
+ ["enchiladas", "the_whole_burritos"]
+ end
+ def table_exists?(name)
+ true
end
-
def method_missing(name, *args)
raise ArgumentError, "#{self.class.name} missing '#{name}': #{args.inspect}"
end
View
@@ -16,11 +16,7 @@ def setup
def test_features
DataFabric.activate_shard :city => :dallas do
assert_equal 'fiveruns_city_dallas_test_slave', TheWholeBurrito.connection.connection_name
-
- assert_raises RuntimeError do
- TheWholeBurrito.connection_pool
- end
-
+ assert_equal DataFabric::PoolProxy, TheWholeBurrito.connection_pool.class
assert !TheWholeBurrito.connected?
# Should use the slave
View
@@ -5,7 +5,7 @@
Dir.chdir(ROOT_PATH)
require 'rubygems'
-require 'minitest/unit'
+require 'test/unit'
require 'erb'
require 'logger'
@@ -18,7 +18,7 @@
require 'active_record'
require 'active_record/version'
ActiveRecord::Base.logger = Logger.new(STDOUT)
-ActiveRecord::Base.logger.level = Logger::DEBUG
+ActiveRecord::Base.logger.level = Logger::WARN
require 'data_fabric'

0 comments on commit 32dc664

Please sign in to comment.