Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Extended by:
MultiTenant::CopyFromClient, MultiTenant::ModelExtensionsClassMethods
Defined in:
lib/activerecord-multi-tenant/with_lock.rb,
lib/activerecord-multi-tenant/default_scope.rb

Overview

Workaround for github.com/citusdata/citus/issues/1236 “SELECT … FOR UPDATE is not supported for router-plannable queries”

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MultiTenant::CopyFromClient

copy_from_client

Methods included from MultiTenant::ModelExtensionsClassMethods

multi_tenant

Class Method Details

.unscopedObject



4
5
6
7
8
9
10
11
12
# File 'lib/activerecord-multi-tenant/default_scope.rb', line 4

def unscoped
  scope = if respond_to?(:scoped_by_tenant?) && MultiTenant.current_tenant_id
    unscoped_orig.where(arel_table[self.partition_key].eq(MultiTenant.current_tenant_id))
  else
    unscoped_orig
  end

  block_given? ? scope.scoping { yield } : scope
end

.unscoped_origObject



3
# File 'lib/activerecord-multi-tenant/default_scope.rb', line 3

alias :unscoped_orig :unscoped

Instance Method Details

#lock!(lock = true) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/activerecord-multi-tenant/with_lock.rb', line 6

def lock!(lock = true)
  if lock && persisted? && self.class.respond_to?(:scoped_by_tenant?) && MultiTenant.current_tenant_id && MultiTenant.with_lock_workaround_enabled?
    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
    self
  else
    lock_orig(lock)
  end
end

#lock_origObject



5
# File 'lib/activerecord-multi-tenant/with_lock.rb', line 5

alias :lock_orig :lock!