module Apartment
def configure
def configure yield self if block_given? end
def connection_class
def connection_class @connection_class || ActiveRecord::Base end
def connection_config
def connection_config connection_db_config.configuration_hash end
def database_schema_file
def database_schema_file return @database_schema_file if defined?(@database_schema_file) @database_schema_file = Rails.root.join('db/schema.rb') end
def db_config_for(tenant)
def db_config_for(tenant) (tenants_with_config[tenant] || connection_config) end
def db_migrate_tenant_missing_strategy
defaults to :rescue_exception
How to handle tenant missing on db:migrate
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
def db_migrate_tenants
Whether or not db:migrate should also migrate tenants
def db_migrate_tenants return @db_migrate_tenants if defined?(@db_migrate_tenants) @db_migrate_tenants = true end
def excluded_models
def excluded_models @excluded_models || [] end
def extract_tenant_config
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
def parallel_migration_threads
def parallel_migration_threads @parallel_migration_threads || 0 end
def persistent_schemas
def persistent_schemas @persistent_schemas || [] end
def pg_excluded_names
def pg_excluded_names @pg_excluded_names || [] end
def reset
def reset (ACCESSOR_METHODS + WRITER_METHODS).each do |method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") end end
def seed_data_file
def seed_data_file return @seed_data_file if defined?(@seed_data_file) @seed_data_file = Rails.root.join('db/seeds.rb') end
def tenant_names
def tenant_names extract_tenant_config.keys.map(&:to_s) end
def tenants_with_config
def tenants_with_config extract_tenant_config end
def tld_length=(_)
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