-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sharding support to the collection
- Loading branch information
Showing
5 changed files
with
111 additions
and
12 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
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Esse::ActiveRecord::Collection, '.connected_to' do | ||
let(:collection_class) { Class.new(described_class) } | ||
|
||
describe '.connected_to' do | ||
it 'sets the connect_with' do | ||
collection_class.connected_to(role: :reading, shard: :default) | ||
expect(collection_class.connect_with).to eq(role: :reading, shard: :default) | ||
end | ||
end | ||
|
||
describe '#each using custom connection', sharding: true do | ||
let(:collection_class) do | ||
klass = Class.new(described_class) | ||
klass.base_scope = -> { State } | ||
klass.connected_to(role: :reading, shard: :replica) | ||
klass | ||
end | ||
|
||
it 'uses the custom connection' do | ||
expect(ActiveRecord::Base).to receive(:connected_to).with(role: :reading, shard: :replica).and_call_original | ||
il_state = State.create!(name: 'Illinois', abbr_name: 'IL') | ||
|
||
instance = collection_class.new | ||
expect { |b| instance.each(&b) }.to yield_successive_args([[il_state]]) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module ShardingHook | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
around do |example| | ||
if example.metadata[:sharding] && | ||
!ActiveRecord::Base.respond_to?(:connected_to) | ||
skip 'ActiveRecord::Base.connected_to is not available in this version of Rails' | ||
return | ||
end | ||
|
||
example.run | ||
end | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.include ShardingHook | ||
end |