Module: PostgresMultitenant

Defined in:
lib/postgres_multitenant.rb,
lib/postgres_multitenant/railtie.rb,
lib/postgres_multitenant/version.rb,
lib/postgres_multitenant/middleware.rb,
lib/postgres_multitenant/configuration.rb,
lib/postgres_multitenant/schema_manager.rb,
lib/postgres_multitenant/tenant_schema_loader.rb,
lib/postgres_multitenant/connection_pool_extensions.rb,
lib/generators/postgres_multitenant/install/install_generator.rb

Overview

PostgresMultitenant module provides multi-tenancy support for Rails applications using PostgreSQL schemas.

Defined Under Namespace

Modules: ConnectionPoolExtensions, Generators Classes: Configuration, Middleware, Railtie, SchemaDropError, SchemaManager, TenantNotFoundError, TenantSchemaLoader, UnsupportedAdapterError

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.configurationObject



17
18
19
# File 'lib/postgres_multitenant.rb', line 17

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



21
22
23
# File 'lib/postgres_multitenant.rb', line 21

def configure
  yield(configuration)
end

.create(tenant_name) ⇒ Object

Convenience method to create tenant



54
55
56
# File 'lib/postgres_multitenant.rb', line 54

def create(tenant_name)
  SchemaManager.create_schema(tenant_name)
end

.currentObject

Get current tenant



64
65
66
# File 'lib/postgres_multitenant.rb', line 64

def current
  SchemaManager.current_schema
end

.drop(tenant_name) ⇒ Object

Convenience method to drop tenant



59
60
61
# File 'lib/postgres_multitenant.rb', line 59

def drop(tenant_name)
  SchemaManager.drop_schema(tenant_name)
end

.find_tenant_by_subdomain(subdomain) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/postgres_multitenant.rb', line 29

def find_tenant_by_subdomain(subdomain)
  SchemaManager.with_schema('public') do
    if configuration.tenant_finder
      configuration.tenant_finder&.call(subdomain)
    else
      find_tenant_using_class(subdomain)
    end
  end
end

.reset!Object

Reset to public schema



74
75
76
# File 'lib/postgres_multitenant.rb', line 74

def reset!
  SchemaManager.reset!
end

.schema_name_for(tenant) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/postgres_multitenant.rb', line 39

def schema_name_for(tenant)
  return configuration.tenant_schema_name_proc.call(tenant) if configuration.tenant_schema_name_proc

  return tenant.schema_name if tenant.respond_to?(:schema_name)
  return tenant.subdomain.tr('-', '_') if tenant.respond_to?(:subdomain)

  nil
end

.switch(tenant_name, &block) ⇒ Object

Convenience method to switch schemas



49
50
51
# File 'lib/postgres_multitenant.rb', line 49

def switch(tenant_name, &block)
  SchemaManager.with_schema(tenant_name, &block)
end

.tenant_classObject



25
26
27
# File 'lib/postgres_multitenant.rb', line 25

def tenant_class
  configuration.tenant_class
end

.tenantsObject

List all tenants



69
70
71
# File 'lib/postgres_multitenant.rb', line 69

def tenants
  SchemaManager.tenant_schemas
end