Skip to content

Commit 1ae38e6

Browse files
committed
Fix test: follow ActiveRecord 8.1 behavior
In ActiveRecord 8.1+, replica connections cannot execute lock queries, so it raises ActiveRecord::ReadOnlyError instead of ActiveRecord::Deadlocked.
1 parent dd79e4e commit 1ae38e6

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/activerecord/debug_errors/ext/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def display_processlist
4343
logger.error "-----------"
4444
logger.error "PROCESSLIST"
4545
logger.error "-----------"
46-
ActiveRecord::Base.connection.execute("SHOW FULL PROCESSLIST").each do |row|
46+
self.execute("SHOW FULL PROCESSLIST").each do |row|
4747
logger.error row.join("\t")
4848
end
4949
end
@@ -52,7 +52,7 @@ def display_innodb_status_section(section_name)
5252
sql = "SHOW ENGINE INNODB STATUS"
5353
status = nil
5454
begin
55-
status = ActiveRecord::Base.connection.execute(sql).first[2]
55+
status = self.execute(sql).first[2]
5656
rescue ActiveRecord::StatementInvalid => e
5757
logger.error "Failed to execute '#{sql}': #{e.message}"
5858
return

spec/activerecord/debug_errors/ext/connection_adapters/abstract_mysql_adapter_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ def cause_deadlock(role:)
5858

5959
context "when the user doesn't have the permission to execute 'SHOW ENGINE INNODB STATUS'" do
6060
it "displays an error message" do
61+
# In ActiveRecord 8.1+, replica connections cannot execute lock queries,
62+
# so it raises ActiveRecord::ReadOnlyError instead of ActiveRecord::Deadlocked.
63+
expected_error = ActiveRecord.version < Gem::Version.new("8.1") ? ActiveRecord::Deadlocked : ActiveRecord::ReadOnlyError
64+
6165
expect {
6266
ActiveRecord::Base.connected_to(role: :reading) do
6367
cause_deadlock(role: :reading)
6468
end
65-
}.to raise_error(ActiveRecord::Deadlocked)
69+
}.to raise_error(expected_error)
6670
expect(log.string).to include("Failed to execute")
6771
end
6872
end

0 commit comments

Comments
 (0)