Module: Apartment

Extended by:
Forwardable
Defined in:
lib/apartment.rb,
lib/apartment/model.rb,
lib/apartment/tenant.rb,
lib/apartment/railtie.rb,
lib/apartment/version.rb,
lib/apartment/migrator.rb,
lib/apartment/deprecation.rb,
lib/apartment/custom_console.rb,
lib/apartment/elevators/host.rb,
lib/apartment/log_subscriber.rb,
lib/apartment/elevators/domain.rb,
lib/apartment/elevators/generic.rb,
lib/apartment/tasks/task_helper.rb,
lib/apartment/tasks/enhancements.rb,
lib/apartment/elevators/host_hash.rb,
lib/apartment/elevators/subdomain.rb,
lib/apartment/adapters/mysql2_adapter.rb,
lib/apartment/adapters/postgis_adapter.rb,
lib/apartment/adapters/sqlite3_adapter.rb,
lib/apartment/adapters/abstract_adapter.rb,
lib/apartment/elevators/first_subdomain.rb,
lib/apartment/adapters/jdbc_mysql_adapter.rb,
lib/apartment/adapters/postgresql_adapter.rb,
lib/apartment/adapters/abstract_jdbc_adapter.rb,
lib/apartment/adapters/jdbc_postgresql_adapter.rb,
lib/generators/apartment/install/install_generator.rb

Overview

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

Defined Under Namespace

Modules: Adapters, CustomConsole, Deprecation, Elevators, Migrator, Model, PostgreSqlAdapterPatch, TaskHelper, Tenant Classes: InstallGenerator, LogSubscriber, Railtie, RakeTaskEnhancer

Constant Summary collapse

ACCESSOR_METHODS =
%i[use_schemas use_sql seed_after_create prepend_environment default_tenant
append_environment with_multi_server_setup tenant_presence_check active_record_log].freeze
WRITER_METHODS =
%i[tenant_names database_schema_file excluded_models
persistent_schemas connection_class
db_migrate_tenants db_migrate_tenant_missing_strategy seed_data_file
parallel_migration_threads pg_excluded_names].freeze
ApartmentError =

Exceptions

Class.new(StandardError)
AdapterNotFound =

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

Class.new(ApartmentError)
FileNotFound =

Raised when apartment cannot find the file to be loaded

Class.new(ApartmentError)
TenantNotFound =

Tenant specified is unknown

Class.new(ApartmentError)
TenantExists =

The Tenant attempting to be created already exists

Class.new(ApartmentError)
VERSION =
'2.12.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure {|_self| ... } ⇒ Object

configure apartment with available options

Yields:

  • (_self)

Yield Parameters:

  • _self (Apartment)

    the object that the method was called on



47
48
49
# File 'lib/apartment.rb', line 47

def configure
  yield self if block_given?
end

.connection_classObject



103
104
105
# File 'lib/apartment.rb', line 103

def connection_class
  @connection_class || ActiveRecord::Base
end

.database_schema_fileObject



107
108
109
110
111
# 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) ⇒ Object



63
64
65
# File 'lib/apartment.rb', line 63

def db_config_for(tenant)
  (tenants_with_config[tenant] || connection_config)
end

.db_migrate_tenant_missing_strategyObject

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

Raises:



78
79
80
81
82
83
84
85
86
87
88
# 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_tenantsObject

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



69
70
71
72
73
# 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_modelsObject

Default to empty array



91
92
93
# File 'lib/apartment.rb', line 91

def excluded_models
  @excluded_models || []
end

.extract_tenant_configObject



130
131
132
133
134
135
136
137
138
139
140
141
142
# 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_threadsObject



95
96
97
# File 'lib/apartment.rb', line 95

def parallel_migration_threads
  @parallel_migration_threads || 0
end

.persistent_schemasObject



99
100
101
# File 'lib/apartment.rb', line 99

def persistent_schemas
  @persistent_schemas || []
end

.pg_excluded_namesObject



119
120
121
# File 'lib/apartment.rb', line 119

def pg_excluded_names
  @pg_excluded_names || []
end

.resetObject

Reset all the config for Apartment



124
125
126
127
128
# 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_fileObject



113
114
115
116
117
# 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_namesObject



51
52
53
# File 'lib/apartment.rb', line 51

def tenant_names
  extract_tenant_config.keys.map(&:to_s)
end

.tenants_with_configObject



55
56
57
# File 'lib/apartment.rb', line 55

def tenants_with_config
  extract_tenant_config
end

.tld_length=(_) ⇒ Object



59
60
61
# 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

Instance Method Details

#connection_configObject



39
40
41
# File 'lib/apartment.rb', line 39

def connection_config
  connection_db_config.configuration_hash
end