module MultiTenant

Truncates only the tables that have been modified, according to sequence values

Add generic warning when queries fail and there is no tenant set

Constants

VERSION

Public Class Methods

current_tenant() click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 49
def self.current_tenant
  RequestStore.store[:current_tenant]
end
current_tenant=(tenant) click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 45
def self.current_tenant=(tenant)
  RequestStore.store[:current_tenant] = tenant
end
current_tenant_class() click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 61
def self.current_tenant_class
  if current_tenant_is_id?
    MultiTenant.default_tenant_class || fail('Only have tenant id, and no default tenant class set')
  elsif current_tenant
    MultiTenant.current_tenant.class.name
  end
end
current_tenant_id() click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 53
def self.current_tenant_id
  current_tenant_is_id? ? current_tenant : current_tenant.try(:id)
end
current_tenant_is_id?() click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 57
def self.current_tenant_is_id?
  current_tenant.is_a?(String) || current_tenant.is_a?(Integer)
end
default_tenant_class() click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 14
def self.default_tenant_class; @@default_tenant_class ||= nil; end
default_tenant_class=(tenant_class) click to toggle source

In some cases we only have an ID - if defined we'll return the default tenant class in such cases

# File lib/activerecord-multi-tenant/multi_tenant.rb, line 13
def self.default_tenant_class=(tenant_class); @@default_tenant_class = tenant_class; end
enable_query_monitor() click to toggle source
# File lib/activerecord-multi-tenant/query_monitor.rb, line 5
def self.enable_query_monitor; @@enable_query_monitor = true; end
enable_with_lock_workaround() click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 23
def self.enable_with_lock_workaround; @@enable_with_lock_workaround = true; end
enable_write_only_mode() click to toggle source

Write-only Mode - this only adds the tenant_id to new records, but doesn't require its presence for SELECTs/UPDATEs/DELETEs

# File lib/activerecord-multi-tenant/multi_tenant.rb, line 18
def self.enable_write_only_mode; @@enable_write_only_mode = true; end
load_current_tenant!() click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 69
def self.load_current_tenant!
  return MultiTenant.current_tenant if MultiTenant.current_tenant && !current_tenant_is_id?
  raise 'MultiTenant.current_tenant must be set to load' if MultiTenant.current_tenant.nil?
  klass = MultiTenant.default_tenant_class || fail('Only have tenant id, and no default tenant class set')
  self.current_tenant = klass.find(MultiTenant.current_tenant_id)
end
multi_tenant_model_for_arel(arel) click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 36
def self.multi_tenant_model_for_arel(arel)
  return nil unless arel.respond_to?(:ast)
  if arel.ast.relation.is_a? Arel::Nodes::JoinSource
    MultiTenant.multi_tenant_model_for_table(arel.ast.relation.left.table_name)
  else
    MultiTenant.multi_tenant_model_for_table(arel.ast.relation.table_name)
  end
end
multi_tenant_model_for_table(table_name) click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 31
def self.multi_tenant_model_for_table(table_name)
  @@multi_tenant_models ||= {}
  @@multi_tenant_models[table_name.to_s]
end
partition_key(tenant_name) click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 8
def self.partition_key(tenant_name)
  "#{tenant_name.to_s}_id"
end
query_monitor_enabled?() click to toggle source
# File lib/activerecord-multi-tenant/query_monitor.rb, line 6
def self.query_monitor_enabled?; @@enable_query_monitor; end
register_multi_tenant_model(table_name, model_klass) click to toggle source

Registry that maps table names to models (used by the query rewriter)

# File lib/activerecord-multi-tenant/multi_tenant.rb, line 27
def self.register_multi_tenant_model(table_name, model_klass)
  @@multi_tenant_models ||= {}
  @@multi_tenant_models[table_name.to_s] = model_klass
end
tenant_klass_defined?(tenant_name) click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 4
def self.tenant_klass_defined?(tenant_name)
  !!tenant_name.to_s.classify.safe_constantize
end
with(tenant, &block) click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 76
def self.with(tenant, &block)
  return block.call if self.current_tenant == tenant
  old_tenant = self.current_tenant
  begin
    self.current_tenant = tenant
    return block.call
  ensure
    self.current_tenant = old_tenant
  end
end
with_lock_workaround_enabled?() click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 24
def self.with_lock_workaround_enabled?; @@enable_with_lock_workaround; end
with_write_only_mode_enabled?() click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 19
def self.with_write_only_mode_enabled?; @@enable_write_only_mode ||= false; end
without(&block) click to toggle source
# File lib/activerecord-multi-tenant/multi_tenant.rb, line 87
def self.without(&block)
  return block.call if self.current_tenant.nil?
  old_tenant = self.current_tenant
  begin
    self.current_tenant = nil
    return block.call
  ensure
    self.current_tenant = old_tenant
  end
end