Skip to content

Commit

Permalink
Addressing Rails Edge breaking change with insert_all
Browse files Browse the repository at this point in the history
  • Loading branch information
ron-shinall committed Mar 10, 2024
1 parent 85f1934 commit a79b6ac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
18 changes: 11 additions & 7 deletions app/models/solid_cache/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ def id_range

private
def upsert_all_no_query_cache(payloads)
insert_all = ActiveRecord::InsertAll.new(
self,
add_key_hash_and_byte_size(payloads),
unique_by: upsert_unique_by,
on_duplicate: :update,
update_only: upsert_update_only
)
args = [ self,
connection_for_insert_all,
add_key_hash_and_byte_size(payloads) ].compact
options = { unique_by: upsert_unique_by,
on_duplicate: :update,
update_only: upsert_update_only }
insert_all = ActiveRecord::InsertAll.new(*args, **options)
sql = connection.build_insert_sql(ActiveRecord::InsertAll::Builder.new(insert_all))

message = +"#{self} "
Expand All @@ -86,6 +86,10 @@ def upsert_all_no_query_cache(payloads)
connection.send exec_query_method, sql, message
end

def connection_for_insert_all
Rails.version >= "7.2" ? connection : nil
end

def add_key_hash_and_byte_size(payloads)
payloads.map do |payload|
payload.dup.tap do |payload|
Expand Down
9 changes: 7 additions & 2 deletions test/unit/execution_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_active_record_instrumention_expiry
def test_no_connections_uninstrumented
skip if multi_cluster?

ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(:connection).raises(ActiveRecord::StatementInvalid)
ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(connection).raises(ActiveRecord::StatementInvalid)

cache = lookup_store(expires_in: 60, active_record_instrumentation: false)

Expand All @@ -124,7 +124,7 @@ def test_no_connections_uninstrumented
def test_no_connections_instrumented
skip if multi_cluster?

ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(:connection).raises(ActiveRecord::StatementInvalid)
ActiveRecord::ConnectionAdapters::ConnectionPool.any_instance.stubs(connection).raises(ActiveRecord::StatementInvalid)

cache = lookup_store(expires_in: 60)

Expand All @@ -148,4 +148,9 @@ def report(error, handled:, severity:, context:, source: nil)
errors << [ error, { context: context, handled: handled, level: severity, source: source } ]
end
end

private
def connection
Rails.version >= "7.2" ? :lease_connection : :connection
end
end

0 comments on commit a79b6ac

Please sign in to comment.