Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
462 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,4 +1,6 @@ | |||
.rvmrc | .rvmrc | ||
.rspec | .rspec | ||
.idea | .idea | ||
pkg | pkg | ||
.bundle | |||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,30 @@ | |||
module ActiveShard | |||
module ActiveRecord | |||
class PoolFactory | |||
|
|||
attr_accessor :connection_pool_class | |||
attr_accessor :schema_pool_class | |||
|
|||
# Initializes a new PoolFactory | |||
# | |||
# @param [Hash] options | |||
# @option options [Class] connection_pool_class | |||
# @option options [Class] schema_pool_class | |||
# | |||
def initialize( options={} ) | |||
@connection_pool_class = options[:connection_pool_class] | |||
@schema_pool_class = options[:schema_pool_class] | |||
end | |||
|
|||
|
|||
def create_connection_pool( definition ) | |||
|
|||
end | |||
|
|||
def create_schema_pool( definition ) | |||
|
|||
end | |||
|
|||
end | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,28 +1,26 @@ | |||
module ActiveShard | module ActiveShard | ||
|
|
||
# Handles current and schema shard resolution using the current | # Handles current shard resolution using the provided scope | ||
# scope and | # | ||
class ShardLookupHandler | class ShardLookupHandler | ||
|
|
||
# Initializes a shard lookup handler | # Initializes a shard lookup handler | ||
# | # | ||
# @param [Hash] options | # @param [Hash] options | ||
# @option options [Scope,ScopeManager] :scope | # @option options [Scope,ScopeManager] :scope | ||
# @option options [Config] :config | |||
# scope instances | |||
# | # | ||
def initialize( options={} ) | def initialize( options={} ) | ||
@scope = options[:scope] | @scope = options[:scope] | ||
@config = options[:config] | |||
end | end | ||
|
|
||
# Returns the active shard for the provided schema, or nil if none. | |||
# | |||
# @param [Symbol] schema_name | |||
# @return [Symbol, nil] shard name if any | |||
# | |||
def lookup_active_shard( schema_name ) | def lookup_active_shard( schema_name ) | ||
@scope.active_shard_for_schema( schema_name ) | @scope.active_shard_for_schema( schema_name ) | ||
end | end | ||
|
|
||
def lookup_schema_shard( schema_name ) | |||
@config.schema_shard_name_by_schema( schema_name ) | |||
end | |||
|
|||
end | end | ||
end | end |
89 changes: 89 additions & 0 deletions
89
spec/active_shard/active_record/connection_handler_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,89 @@ | |||
require 'spec_helper' | |||
require 'active_shard/active_record/connection_handler' | |||
|
|||
module ActiveShard::ActiveRecord | |||
|
|||
describe ConnectionHandler do | |||
let :shard_definitions do | |||
yaml = <<-EOY | |||
test: | |||
schema_one: | |||
shard_one: | |||
adapter: sqlite3 | |||
database: spec/output/shard_one.db | |||
shard_two: | |||
adapter: sqlite3 | |||
database: spec/output/shard_two.db | |||
schema_two: | |||
shard_three: | |||
adapter: sqlite3 | |||
database: spec/output/shard_three.db | |||
shard_four: | |||
adapter: sqlite3 | |||
database: spec/output/shard_four.db | |||
shard_five: | |||
adapter: sqlite3 | |||
database: spec/output/shard_five.db | |||
schema_three: | |||
shard_six: | |||
adapter: sqlite3 | |||
database: spec/output/shard_six.db | |||
shard_seven: | |||
adapter: sqlite3 | |||
database: spec/output/shard_seven.db | |||
EOY | |||
ActiveShard::ShardDefinition.from_yaml( yaml )[:test] | |||
end | |||
|
|||
describe "#retrieve_connection_pool" do | |||
context "with schema/shards" do | |||
let :lookup_handler do | |||
handler = mock(:handler) | |||
handler.stub!(:lookup_active_shard).and_return(nil) | |||
handler | |||
end | |||
|
|||
let :handler do | |||
ConnectionHandler.new( shard_definitions, :shard_lookup => lookup_handler ) | |||
end | |||
|
|||
context "with active shards :schema_one => :shard_two, :schema_two => :shard_four" do | |||
let :lookup_handler do | |||
handler = mock(:handler) | |||
handler.stub!(:lookup_active_shard).with(:schema_one).and_return(:shard_two) | |||
handler.stub!(:lookup_active_shard).with(:schema_two).and_return(:shard_four) | |||
handler.stub!(:lookup_active_shard).with(:schema_three).and_return(nil) | |||
handler | |||
end | |||
|
|||
it "should return connection_pool for shard_two when klass.schema_name == :schema_one" do | |||
pool = handler.retrieve_connection_pool( mock(:klass, :schema_name => :schema_one) ) | |||
pool.spec.config[:adapter].should == 'sqlite3' | |||
pool.spec.config[:database].should == 'spec/output/shard_two.db' | |||
pool.should be_instance_of( ::ActiveRecord::ConnectionAdapters::ConnectionPool ) | |||
end | |||
|
|||
it "should return connection_pool for shard_four when klass.schema_name == :schema_two" do | |||
pool = handler.retrieve_connection_pool( mock(:klass, :schema_name => :schema_two) ) | |||
pool.spec.config[:adapter].should == 'sqlite3' | |||
pool.spec.config[:database].should == 'spec/output/shard_four.db' | |||
pool.should be_instance_of( ::ActiveRecord::ConnectionAdapters::ConnectionPool ) | |||
end | |||
|
|||
it "should return schema_connection_pool for shard_six when klass.schema_name == :schema_three" do | |||
pool = handler.retrieve_connection_pool( mock(:klass, :schema_name => :schema_three) ) | |||
pool.spec.config[:adapter].should == 'sqlite3' | |||
pool.spec.config[:database].should == 'spec/output/shard_six.db' | |||
pool.should be_instance_of( ::ActiveShard::ActiveRecord::SchemaConnectionPool ) | |||
end | |||
end | |||
|
|||
|
|||
end | |||
|
|||
end | |||
|
|||
|
|||
end | |||
|
|||
end |
12 changes: 12 additions & 0 deletions
12
spec/active_shard/active_record/schema_connection_pool_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,12 @@ | |||
require 'spec_helper' | |||
require 'active_shard/active_record/schema_connection_pool' | |||
|
|||
module ActiveShard::ActiveRecord | |||
|
|||
describe SchemaConnectionPool do | |||
|
|||
|
|||
|
|||
end | |||
|
|||
end |
Oops, something went wrong.