Skip to content

Commit

Permalink
⚠️ "instance variable @raw_connection not initialized" on AR < 7.1
Browse files Browse the repository at this point in the history
follows up 3870272
  • Loading branch information
amatsuda committed Feb 27, 2024
1 parent f0353ba commit 210cde5
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/database_rewinder/multiple_statements_executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@ def supports_multiple_statements?
%w(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter ActiveRecord::ConnectionAdapters::Mysql2Adapter ActiveRecord::ConnectionAdapters::SQLite3Adapter).include? self.class.name
end

def raw_connection_or_connection
defined?(@raw_connection) ? @raw_connection : @connection
end

def execute_multiple(sql)
#TODO Use ADAPTER_NAME when we've dropped AR 4.1 support
case self.class.name
when 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
disable_referential_integrity { log(sql) { (@raw_connection || @connection).exec sql } }
disable_referential_integrity { log(sql) { raw_connection_or_connection.exec sql } }
when 'ActiveRecord::ConnectionAdapters::Mysql2Adapter'
if (@raw_connection || @connection).query_options[:connect_flags] & Mysql2::Client::MULTI_STATEMENTS != 0
if raw_connection_or_connection.query_options[:connect_flags] & Mysql2::Client::MULTI_STATEMENTS != 0
disable_referential_integrity do
_result = log(sql) { (@raw_connection || @connection).query sql }
while (@raw_connection || @connection).next_result
_result = log(sql) { raw_connection_or_connection.query sql }
while raw_connection_or_connection.next_result
# just to make sure that all queries are finished
_result = (@raw_connection || @connection).store_result
_result = raw_connection_or_connection.store_result
end
end
else
query_options = (@raw_connection || @connection).query_options.dup
query_options = raw_connection_or_connection.query_options.dup
query_options[:connect_flags] |= Mysql2::Client::MULTI_STATEMENTS
# opens another connection to the DB
client = Mysql2::Client.new query_options
Expand All @@ -40,7 +44,7 @@ def execute_multiple(sql)
end
end
when 'ActiveRecord::ConnectionAdapters::SQLite3Adapter'
disable_referential_integrity { log(sql) { (@raw_connection || @connection).execute_batch sql } }
disable_referential_integrity { log(sql) { raw_connection_or_connection.execute_batch sql } }
else
raise 'Multiple deletion is not supported with the current database adapter.'
end
Expand Down

0 comments on commit 210cde5

Please sign in to comment.