Sha256: 2a7af04e7d5393e1cdfbb90baf3d2cd7b49cd701b9af420d5b2bad90de2bc5e9

Contents?: true

Size: 658 Bytes

Versions: 2

Compression:

Stored size: 658 Bytes

Contents

# Workaround for https://github.com/citusdata/citus/issues/1236
# "SELECT ... FOR UPDATE is not supported for router-plannable queries"

class ActiveRecord::Base
  alias :with_lock_orig :with_lock
  def with_lock(&block)
    if self.class.respond_to?(:scoped_by_tenant?) && MultiTenant.current_tenant_id && MultiTenant.with_lock_workaround_enabled?
      transaction do
        self.class.unscoped.where(id: id).update_all(id: id) # No-op UPDATE that locks the row
        reload # This is just to act similar to the default Rails approach, in case someone relies on the reload
        yield
      end
    else
      with_lock_orig(&block)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord-multi-tenant-0.3.3 lib/activerecord-multi-tenant/with_lock.rb
activerecord-multi-tenant-0.3.2 lib/activerecord-multi-tenant/with_lock.rb