From 854f80913925e557375002058c390d45f6314bd1 Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Mon, 5 Aug 2024 16:39:49 -0300 Subject: [PATCH] fix: fixes to the db connection of specs --- CHANGELOG.md | 12 ++++++ Gemfile.lock | 2 +- ci/Gemfile.rails-5.2.lock | 2 +- ci/Gemfile.rails-6.0.lock | 2 +- ci/Gemfile.rails-6.1.lock | 2 +- ci/Gemfile.rails-7.0.lock | 2 +- ci/Gemfile.rails-7.1.lock | 2 +- lib/esse/active_record/version.rb | 2 +- .../collection_connected_to_spec.rb | 43 +++++++++++++++++-- spec/support/models.rb | 27 +++++++----- 10 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..32d6511 --- /dev/null +++ b/CHANGELOG.md @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 9fdb2e0..2260ebe 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/ci/Gemfile.rails-5.2.lock b/ci/Gemfile.rails-5.2.lock index 7c8e3b2..0e4c8fa 100644 --- a/ci/Gemfile.rails-5.2.lock +++ b/ci/Gemfile.rails-5.2.lock @@ -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) diff --git a/ci/Gemfile.rails-6.0.lock b/ci/Gemfile.rails-6.0.lock index 2934703..b0b12f1 100644 --- a/ci/Gemfile.rails-6.0.lock +++ b/ci/Gemfile.rails-6.0.lock @@ -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) diff --git a/ci/Gemfile.rails-6.1.lock b/ci/Gemfile.rails-6.1.lock index 770b451..b295f2a 100644 --- a/ci/Gemfile.rails-6.1.lock +++ b/ci/Gemfile.rails-6.1.lock @@ -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) diff --git a/ci/Gemfile.rails-7.0.lock b/ci/Gemfile.rails-7.0.lock index 2934703..b0b12f1 100644 --- a/ci/Gemfile.rails-7.0.lock +++ b/ci/Gemfile.rails-7.0.lock @@ -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) diff --git a/ci/Gemfile.rails-7.1.lock b/ci/Gemfile.rails-7.1.lock index 2b8fd78..a876446 100644 --- a/ci/Gemfile.rails-7.1.lock +++ b/ci/Gemfile.rails-7.1.lock @@ -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) diff --git a/lib/esse/active_record/version.rb b/lib/esse/active_record/version.rb index 6175be2..b398b29 100644 --- a/lib/esse/active_record/version.rb +++ b/lib/esse/active_record/version.rb @@ -2,6 +2,6 @@ module Esse module ActiveRecord - VERSION = '0.3.6' + VERSION = '0.3.7' end end diff --git a/spec/esse/active_record/collection_connected_to_spec.rb b/spec/esse/active_record/collection_connected_to_spec.rb index ccf89eb..70c5a43 100644 --- a/spec/esse/active_record/collection_connected_to_spec.rb +++ b/spec/esse/active_record/collection_connected_to_spec.rb @@ -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 diff --git a/spec/support/models.rb b/spec/support/models.rb index b97750b..d4708da 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -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 @@ -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