Skip to content

Commit

Permalink
Fix test failures for AR 6.1 and AR 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lorint committed Jan 12, 2023
1 parent 9cc4a25 commit c54f95b
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions lib/trilogy_adapter/backwards_ar_compatibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ def initialize(version_string, full_version_string = nil)
# For ActiveRecord <= 7.0
def with_raw_connection(allow_retry: false, uses_transaction: true)
@lock.synchronize do
@raw_connection = nil unless instance_variable_defined?(:@raw_connection)
verify! unless @verified # || (@raw_connection.server_status & 1).positive?
@raw_connection = @connection || nil unless instance_variable_defined?(:@raw_connection)
verify! unless @verified # || (@raw_connection&.closed? == false && (@raw_connection.server_status & 1).positive?)
materialize_transactions if uses_transaction
begin
yield @raw_connection
rescue StandardError => exception
@verified = false unless exception.is_a?(Deadlocked) || exception.is_a?(LockWaitTimeout)
# raise translate_exception_class(exception, nil, nil)
# raise translate_exception(exception, message: exception.message, sql: nil, binds: nil)
raise exception
raise
end
end
end
Expand Down Expand Up @@ -97,27 +95,48 @@ def reconnect!
end
alias :reset! :reconnect!

def exec_rollback_db_transaction
# 16384 tests the bit flag for SERVER_SESSION_STATE_CHANGED, which gets set when the
# last statement executed has caused a change in the server's state.
if active? || (@raw_connection.server_status & 16384).positive?
super
else
@verified = false
end
end

# For ActiveRecord <= 6.1
if AbstractMysqlAdapter.instance_method(:execute).parameters.length < 3
# Adds an #execute specific to the TrilogyAdapter that allows
# (but disregards) +async+ and other keyword parameters.
alias raw_execute execute
def execute(sql, name = nil, **kwargs)
@raw_connection = nil unless instance_variable_defined?(:@raw_connection)
# 16384 tests the bit flag for SERVER_SESSION_STATE_CHANGED, which gets set when the
# last statement executed has caused a change in the server's state.
# Was: (!@verified && !active?)
reconnect if @raw_connection.nil? || (!@verified && (@raw_connection&.server_status & 16384).zero?)
reconnect if @raw_connection.nil? || (!@verified && (@raw_connection.server_status & 16384).zero?)
if default_timezone == :local
@raw_connection.query_flags |= ::Trilogy::QUERY_FLAGS_LOCAL_TIMEZONE
else
@raw_connection.query_flags &= ~::Trilogy::QUERY_FLAGS_LOCAL_TIMEZONE
end
raw_execute(sql, name)
rescue => original_exception
@verified = false unless original_exception.is_a?(Deadlocked) || original_exception.is_a?(LockWaitTimeout)
rescue => exception
return if exception.is_a?(Deadlocked)

@verified = false unless exception.is_a?(LockWaitTimeout)
raise
end
else # For ActiveRecord 7.0
def execute(sql, name = nil, **kwargs)
sql = transform_query(sql)
check_if_write_query(sql)
begin
super
rescue => exception
@verified = true if exception.is_a?(ActiveRecord::StatementInvalid) && (@raw_connection.server_status & 16384).positive?
raise
end
end
end

private
Expand Down

0 comments on commit c54f95b

Please sign in to comment.