Skip to content

Commit

Permalink
fix: fixes to the db connection of specs
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Aug 5, 2024
1 parent 30e7f79 commit 854f809
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 21 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.3.7 - 2024-08-05
* Add `connected_to` to the collection for custom connection handling

## 0.0.1
The first release of the esse-active_record plugin
* Added: Initial implementation of the plugin
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
esse-active_record (0.3.6)
esse-active_record (0.3.7)
activerecord (>= 4.2, < 8)
esse (>= 0.3.0)

Expand Down
2 changes: 1 addition & 1 deletion ci/Gemfile.rails-5.2.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse-active_record (0.3.6)
esse-active_record (0.3.7)
activerecord (>= 4.2, < 8)
esse (>= 0.3.0)

Expand Down
2 changes: 1 addition & 1 deletion ci/Gemfile.rails-6.0.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse-active_record (0.3.6)
esse-active_record (0.3.7)
activerecord (>= 4.2, < 8)
esse (>= 0.3.0)

Expand Down
2 changes: 1 addition & 1 deletion ci/Gemfile.rails-6.1.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse-active_record (0.3.6)
esse-active_record (0.3.7)
activerecord (>= 4.2, < 8)
esse (>= 0.3.0)

Expand Down
2 changes: 1 addition & 1 deletion ci/Gemfile.rails-7.0.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse-active_record (0.3.6)
esse-active_record (0.3.7)
activerecord (>= 4.2, < 8)
esse (>= 0.3.0)

Expand Down
2 changes: 1 addition & 1 deletion ci/Gemfile.rails-7.1.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
esse-active_record (0.3.6)
esse-active_record (0.3.7)
activerecord (>= 4.2, < 8)
esse (>= 0.3.0)

Expand Down
2 changes: 1 addition & 1 deletion lib/esse/active_record/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Esse
module ActiveRecord
VERSION = '0.3.6'
VERSION = '0.3.7'
end
end
43 changes: 40 additions & 3 deletions spec/esse/active_record/collection_connected_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,53 @@
let(:collection_class) do
klass = Class.new(described_class)
klass.base_scope = -> { State }
klass.connected_to(role: :reading, shard: :replica)
klass.connected_to(role: :reading)
klass
end

it 'uses the custom connection' do
expect(ActiveRecord::Base).to receive(:connected_to).with(role: :reading, shard: :replica).and_call_original
expect(ActiveRecord::Base).to receive(:connected_to).with(role: :reading).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]])
expect { |b| instance.each(&b) }.to yield_successive_args([il_state])
il_state.destroy
end
end

describe '#each_batch_ids using custom connection', sharding: true do
let(:collection_class) do
klass = Class.new(described_class)
klass.base_scope = -> { State }
klass.connected_to(role: :reading)
klass
end

it 'uses the custom connection' do
expect(ActiveRecord::Base).to receive(:connected_to).with(role: :reading).and_call_original
il_state = State.create!(name: 'Illinois', abbr_name: 'IL')

instance = collection_class.new
expect { |b| instance.each_batch_ids(&b) }.to yield_successive_args([il_state.id])
il_state.destroy
end
end

describe '#count using custom connection', sharding: true do
let(:collection_class) do
klass = Class.new(described_class)
klass.base_scope = -> { State }
klass.connected_to(role: :reading)
klass
end

it 'uses the custom connection' do
expect(ActiveRecord::Base).to receive(:connected_to).with(role: :reading).and_call_original
il_state = State.create!(name: 'Illinois', abbr_name: 'IL')

instance = collection_class.new
expect(instance.count).to eq(1)
il_state.destroy
end
end
end
27 changes: 16 additions & 11 deletions spec/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
ActiveRecord::Base.logger = Logger.new($stdout)
end

ACTIVE_RECORD_DEFAULT_ENV = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call.to_sym
active_record_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call

ActiveRecord::Base.configurations = {
ACTIVE_RECORD_DEFAULT_ENV.to_s => {
'adapter' => 'sqlite3',
'database' => ':memory:',
},
'replica' => {
'adapter' => 'sqlite3',
'database' => ':memory:',
},
active_record_env => {
primary: {
adapter: 'sqlite3',
database: '/tmp/esse-active_record.db',
},
secondary: {
adapter: 'sqlite3',
database: '/tmp/esse-active_record.db',
}
}
}
ActiveRecord::Base.establish_connection(ACTIVE_RECORD_DEFAULT_ENV)
if File.exist?('/tmp/esse-active_record.db')
File.delete('/tmp/esse-active_record.db')
end
ActiveRecord::Base.establish_connection(:primary)

ActiveRecord::Schema.define do
self.verbose = false
Expand Down Expand Up @@ -44,7 +49,7 @@ class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

if ::ActiveRecord.gem_version >= Gem::Version.new('6.0.0')
connects_to database: { writing: ACTIVE_RECORD_DEFAULT_ENV, reading: :replica }
connects_to database: { writing: :primary, reading: :secondary }
end
end

Expand Down

0 comments on commit 854f809

Please sign in to comment.