Skip to content

Commit

Permalink
Fixed issue with Arel not finding the proper connection
Browse files Browse the repository at this point in the history
  • Loading branch information
brasten committed Aug 24, 2011
1 parent a068f82 commit 9d69516
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions lib/active_shard/active_record/connection_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def initialize( options={} )
@shard_lookup = options[ :shard_lookup ]

@shard_definitions = []
@connection_pools = {}
@connection_pools = ConnectionPoolHash.new( self )
@schema_pools = {}
end

Expand Down Expand Up @@ -117,9 +117,41 @@ def connection_pool_id( *args )
schema_name = definition.nil? ? args.shift : definition.schema
shard_name = definition.nil? ? args.shift : definition.name

"#{schema_name.to_s}+#{shard_name.to_s}".to_sym
PoolKey.new( schema_name, shard_name )
end

class PoolKey
attr_reader :schema, :shard

def initialize( schema, shard )
@schema = schema.nil? ? nil : schema.to_sym
@shard = shard.nil? ? nil : shard.to_sym
end

def hash
[self.schema, self.shard].hash
end

def eql?(other)
(self.schema == other.schema &&
self.shard == other.shard)
end
end

class ConnectionPoolHash < Hash
def initialize( connection_handler )
@connection_handler = connection_handler
end

def [](val)
case val
when PoolKey
super
else
@connection_handler.retrieve_connection_pool( val.constantize )
end
end
end

end
end
Expand Down

0 comments on commit 9d69516

Please sign in to comment.