Module: PgMultitenantSchemas

Defined in:
lib/pg_multitenant_schemas.rb,
lib/pg_multitenant_schemas/ui.rb,
lib/pg_multitenant_schemas/errors.rb,
lib/pg_multitenant_schemas/context.rb,
lib/pg_multitenant_schemas/version.rb,
lib/pg_multitenant_schemas/migrator.rb,
lib/pg_multitenant_schemas/ui/engine.rb,
lib/pg_multitenant_schemas/configuration.rb,
lib/pg_multitenant_schemas/rails/railtie.rb,
lib/pg_multitenant_schemas/schema_switcher.rb,
lib/pg_multitenant_schemas/tenant_resolver.rb,
lib/pg_multitenant_schemas/migration_executor.rb,
lib/pg_multitenant_schemas/rails/model_concern.rb,
lib/pg_multitenant_schemas/rails/controller_concern.rb,
lib/pg_multitenant_schemas/migration_status_reporter.rb,
lib/pg_multitenant_schemas/migration_display_reporter.rb,
lib/pg_multitenant_schemas/migration_schema_operations.rb,
lib/pg_multitenant_schemas/rails/generators/install_generator.rb,
lib/pg_multitenant_schemas/rails/generators/tenant_migration_generator.rb,
lib/pg_multitenant_schemas/ui/app/controllers/pg_multitenant_schemas/ui/tenants_controller.rb,
sig/pg_multitenant_schemas.rbs

Overview

PgMultitenantSchemas provides PostgreSQL schema-based multitenancy functionality.

Defined Under Namespace

Modules: Generators, MigrationDisplayReporter, MigrationExecutor, MigrationSchemaOperations, MigrationStatusReporter, Rails, UI Classes: Configuration, ConfigurationError, ConnectionError, Context, Error, Migrator, SchemaExists, SchemaNotFound, SchemaSwitcher, TenantNotFound, TenantResolver

Constant Summary collapse

VERSION =

Returns:

  • (String)
"0.3.6"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_schemaString

Get the current tenant schema name

Returns:

  • (String)

    The current schema name

See Also:



56
57
58
# File 'lib/pg_multitenant_schemas.rb', line 56

def current_schema
  @current_schema
end

.current_tenantObject?

Get the current tenant object

Returns:

  • (Object, nil)

    The current tenant or nil if not set

See Also:



45
46
47
# File 'lib/pg_multitenant_schemas.rb', line 45

def current_tenant
  @current_tenant
end

Class Method Details

.configurationConfiguration

Get the current configuration

Returns:



149
150
151
# File 'lib/pg_multitenant_schemas.rb', line 149

def self.configuration
  @configuration ||= Configuration.new
end

.configure { ... } ⇒ Object

Configure the gem

Examples:

PgMultitenantSchemas.configure do |config|
  config.tenant_model_class = 'Tenant'
  config.default_schema = 'public'
end

Yields:

  • Yields the configuration object for configuration



161
162
163
# File 'lib/pg_multitenant_schemas.rb', line 161

def self.configure
  yield(configuration) if block_given?
end

.create_tenant_schema(tenant_or_schema) ⇒ void

This method returns an undefined value.

Create a new tenant schema

Parameters:

  • tenant_or_schema (Object, String)

    The tenant object or schema name

See Also:



104
105
106
# File 'lib/pg_multitenant_schemas.rb', line 104

def create_tenant_schema(tenant_or_schema)
  Context.create_tenant_schema(tenant_or_schema)
end

.drop_tenant_schema(tenant_or_schema, cascade: true) ⇒ void

This method returns an undefined value.

Drop a tenant schema

Parameters:

  • tenant_or_schema (Object, String)

    The tenant object or schema name

  • cascade (Boolean) (defaults to: true)

    Whether to drop dependent objects (default: true)

See Also:



114
115
116
# File 'lib/pg_multitenant_schemas.rb', line 114

def drop_tenant_schema(tenant_or_schema, cascade: true)
  Context.drop_tenant_schema(tenant_or_schema, cascade: cascade)
end

.extract_subdomain(hostname) ⇒ String?

Extract tenant subdomain from a hostname

Parameters:

  • hostname (String)

    The hostname to extract from

Returns:

  • (String, nil)

    The extracted subdomain or nil

See Also:



123
124
125
# File 'lib/pg_multitenant_schemas.rb', line 123

def extract_subdomain(hostname)
  TenantResolver.extract_subdomain(hostname)
end

.find_tenant_by_subdomain(subdomain) ⇒ Object?

Find a tenant by subdomain

Parameters:

  • subdomain (String)

    The subdomain to search for

Returns:

  • (Object, nil)

    The tenant object or nil if not found

See Also:



132
133
134
# File 'lib/pg_multitenant_schemas.rb', line 132

def find_tenant_by_subdomain(subdomain)
  TenantResolver.find_tenant_by_subdomain(subdomain)
end

.reset!void

This method returns an undefined value.

Reset the current tenant context



67
68
69
# File 'lib/pg_multitenant_schemas.rb', line 67

def reset!
  Context.reset!
end

.resolve_tenant_from_request(request) ⇒ Object?

Resolve tenant from Rails request

Parameters:

  • request (ActionDispatch::Request)

    The Rails request object

Returns:

  • (Object, nil)

    The tenant object or nil if not resolved

See Also:



141
142
143
# File 'lib/pg_multitenant_schemas.rb', line 141

def resolve_tenant_from_request(request)
  TenantResolver.resolve_tenant_from_request(request)
end

.switch_to_schema(schema_name) ⇒ void

This method returns an undefined value.

Switch to a specific schema

Parameters:

  • schema_name (String)

    The schema name to switch to

See Also:



76
77
78
# File 'lib/pg_multitenant_schemas.rb', line 76

def switch_to_schema(schema_name)
  Context.switch_to_schema(schema_name)
end

.switch_to_tenant(tenant) ⇒ void

This method returns an undefined value.

Switch to a specific tenant's schema

Parameters:

  • tenant (Object, String)

    The tenant object or schema name

See Also:



85
86
87
# File 'lib/pg_multitenant_schemas.rb', line 85

def switch_to_tenant(tenant)
  Context.switch_to_tenant(tenant)
end

.with_tenant(tenant_or_schema) { ... } ⇒ Object

Execute a block within a tenant context

Parameters:

  • tenant_or_schema (Object, String)

    The tenant object or schema name

Yields:

  • Executes the given block in the tenant's schema context

Returns:

  • (Object)

    The return value of the block

See Also:



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

def with_tenant(tenant_or_schema, &)
  Context.with_tenant(tenant_or_schema, &)
end