module Apartment

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 22
def configure
  yield self if block_given?
end
connection_class() click to toggle source
# File lib/apartment.rb, line 65
def connection_class
  @connection_class || ActiveRecord::Base
end
database_schema_file() click to toggle source
# File lib/apartment.rb, line 69
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 34
def db_config_for(tenant)
  (tenants_with_config[tenant] || connection_config).with_indifferent_access
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 40
def db_migrate_tenants
  return @db_migrate_tenants if defined?(@db_migrate_tenants)

  @db_migrate_tenants = true
end
default_schema() click to toggle source
# File lib/apartment.rb, line 51
def default_schema
  @default_schema || "public" # TODO 'public' is postgres specific
end
Also aliased as: default_tenant
default_tenant()
Alias for: default_schema
excluded_models() click to toggle source

Default to empty array

# File lib/apartment.rb, line 47
def excluded_models
  @excluded_models || []
end
extract_tenant_config() click to toggle source
# File lib/apartment.rb, line 90
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 55
def parallel_migration_threads
  @parallel_migration_threads || 0
end
persistent_schemas() click to toggle source
# File lib/apartment.rb, line 61
def persistent_schemas
  @persistent_schemas || []
end
pg_excluded_names() click to toggle source
# File lib/apartment.rb, line 81
def pg_excluded_names
  @pg_excluded_names || []
end
reset() click to toggle source

Reset all the config for Apartment

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

  @seed_data_file = "#{Rails.root}/db/seeds.rb"
end
tenant_names() click to toggle source
# File lib/apartment.rb, line 26
def tenant_names
  extract_tenant_config.keys.map(&:to_s)
end
tenants_with_config() click to toggle source
# File lib/apartment.rb, line 30
def tenants_with_config
  extract_tenant_config
end