Sha256: 0dbc10ae43a7230387b0b4843b6c163dc3ec797bb6a33c2d886415795861c2cd

Contents?: true

Size: 750 Bytes

Versions: 8

Compression:

Stored size: 750 Bytes

Contents

# frozen_string_literal: true

module ActiveRecord
  # This is monkeypatching activerecord to ensure that whenever a new connection is established it
  # switches to the same tenant as before the connection switching. This problem is more evident when
  # using read replica in Rails 6
  module ConnectionHandling
    def connected_to_with_tenant(database: nil, role: nil, prevent_writes: false, &blk)
      current_tenant = Apartment::Tenant.current

      connected_to_without_tenant(database: database, role: role, prevent_writes: prevent_writes) do
        Apartment::Tenant.switch!(current_tenant)
        yield(blk)
      end
    end

    alias connected_to_without_tenant connected_to
    alias connected_to connected_to_with_tenant
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ros-apartment-2.11.0 lib/apartment/active_record/connection_handling.rb
ros-apartment-2.10.0 lib/apartment/active_record/connection_handling.rb
ros-apartment-2.9.0 lib/apartment/active_record/connection_handling.rb
ros-apartment-2.8.1 lib/apartment/active_record/connection_handling.rb
ros-apartment-2.8.1.rc2 lib/apartment/active_record/connection_handling.rb
ros-apartment-2.8.1.rc1 lib/apartment/active_record/connection_handling.rb
ros-apartment-2.8.0 lib/apartment/active_record/connection_handling.rb
ros-apartment-2.7.2 lib/apartment/active_record/connection_handling.rb