-
Notifications
You must be signed in to change notification settings - Fork 163
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
6 changed files
with
129 additions
and
11 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
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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
# `expected_dbs` should be a Hash of the form: | ||
# { | ||
# primary: [:writing, :reading], | ||
# replica: [:reading], | ||
# } | ||
RSpec::Matchers.define :exactly_query_databases do |expected_dbs| | ||
match do |block| | ||
@expected_dbs = expected_dbs.transform_values(&:to_set).with_indifferent_access | ||
@actual_dbs = Hash.new { |h, k| h[k] = Set.new }.with_indifferent_access | ||
|
||
ActiveSupport::Notifications. | ||
subscribe("sql.active_record") do |_name, _start, _finish, _id, payload| | ||
pool = payload.fetch(:connection).pool | ||
|
||
next if pool.is_a?(ActiveRecord::ConnectionAdapters::NullPool) | ||
|
||
name = pool.db_config.name | ||
role = pool.role | ||
|
||
@actual_dbs[name] << role | ||
end | ||
|
||
block.call | ||
|
||
@actual_dbs == @expected_dbs | ||
end | ||
|
||
failure_message do |_block| | ||
"expected to query exactly #{@expected_dbs}, but queried #{@actual_dbs}" | ||
end | ||
|
||
supports_block_expectations | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class SecondaryRecord < ActiveRecord::Base | ||
self.abstract_class = true | ||
|
||
connects_to database: { writing: :secondary, reading: :secondary } | ||
end |