module Apartment

Apartment main definitions

Require this file to append Apartment rake tasks to ActiveRecord db rake tasks Enabled by default in the initializer

Constants

ACCESSOR_METHODS
AdapterNotFound

Raised when apartment cannot find the adapter specified in config/database.yml

ApartmentError

Exceptions

FileNotFound

Raised when apartment cannot find the file to be loaded

TenantExists

The Tenant attempting to be created already exists

TenantNotFound

Tenant specified is unknown

VERSION
WRITER_METHODS

Public Class Methods

configure() { |self| ... } click to toggle source

configure apartment with available options

# File lib/apartment.rb, line 47
def configure
  yield self if block_given?
end
connection_class() click to toggle source
# File lib/apartment.rb, line 103
def connection_class
  @connection_class || ActiveRecord::Base
end
connection_config() click to toggle source
# File lib/apartment.rb, line 39
def connection_config
  connection_db_config.configuration_hash
end
database_schema_file() click to toggle source
# File lib/apartment.rb, line 107
def database_schema_file
  return @database_schema_file if defined?(@database_schema_file)

  @database_schema_file = Rails.root.join('db/schema.rb')
end
db_config_for(tenant) click to toggle source
# File lib/apartment.rb, line 63
def db_config_for(tenant)
  (tenants_with_config[tenant] || connection_config)
end
db_migrate_tenant_missing_strategy() click to toggle source

How to handle tenant missing on db:migrate defaults to :rescue_exception available options: rescue_exception, raise_exception, create_tenant

# File lib/apartment.rb, line 78
def db_migrate_tenant_missing_strategy
  valid = %i[rescue_exception raise_exception create_tenant]
  value = @db_migrate_tenant_missing_strategy || :rescue_exception

  return value if valid.include?(value)

  key_name  = 'config.db_migrate_tenant_missing_strategy'
  opt_names = valid.join(', ')

  raise ApartmentError, "Option #{value} not valid for `#{key_name}`. Use one of #{opt_names}"
end
db_migrate_tenants() click to toggle source

Whether or not db:migrate should also migrate tenants defaults to true

# File lib/apartment.rb, line 69
def db_migrate_tenants
  return @db_migrate_tenants if defined?(@db_migrate_tenants)

  @db_migrate_tenants = true
end
excluded_models() click to toggle source

Default to empty array

# File lib/apartment.rb, line 91
def excluded_models
  @excluded_models || []
end
extract_tenant_config() click to toggle source
# File lib/apartment.rb, line 130
def extract_tenant_config
  return {} unless @tenant_names

  values = @tenant_names.respond_to?(:call) ? @tenant_names.call : @tenant_names
  unless values.is_a? Hash
    values = values.each_with_object({}) do |tenant, hash|
      hash[tenant] = connection_config
    end
  end
  values.with_indifferent_access
rescue ActiveRecord::StatementInvalid
  {}
end
parallel_migration_threads() click to toggle source
# File lib/apartment.rb, line 95
def parallel_migration_threads
  @parallel_migration_threads || 0
end
persistent_schemas() click to toggle source
# File lib/apartment.rb, line 99
def persistent_schemas
  @persistent_schemas || []
end
pg_excluded_names() click to toggle source
# File lib/apartment.rb, line 119
def pg_excluded_names
  @pg_excluded_names || []
end
reset() click to toggle source

Reset all the config for Apartment

# File lib/apartment.rb, line 124
def reset
  (ACCESSOR_METHODS + WRITER_METHODS).each do |method|
    remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}")
  end
end
seed_data_file() click to toggle source
# File lib/apartment.rb, line 113
def seed_data_file
  return @seed_data_file if defined?(@seed_data_file)

  @seed_data_file = Rails.root.join('db/seeds.rb')
end
tenant_names() click to toggle source
# File lib/apartment.rb, line 51
def tenant_names
  extract_tenant_config.keys.map(&:to_s)
end
tenants_with_config() click to toggle source
# File lib/apartment.rb, line 55
def tenants_with_config
  extract_tenant_config
end
tld_length=(_) click to toggle source
# File lib/apartment.rb, line 59
def tld_length=(_)
  Apartment::Deprecation.warn('`config.tld_length` have no effect because it was removed in https://github.com/influitive/apartment/pull/309')
end