Class: PgMultitenantSchemas::Context
- Inherits:
-
Object
- Object
- PgMultitenantSchemas::Context
- Defined in:
- lib/pg_multitenant_schemas/context.rb
Overview
Thread-safe tenant context management
The Context class manages the current tenant and schema in a thread-safe manner using Thread.current storage. This ensures proper isolation in multi-threaded environments like Rails servers.
Class Method Summary collapse
-
.create_tenant_schema(tenant_or_schema) ⇒ void
Create a new PostgreSQL schema for a tenant.
-
.current_schema ⇒ String
Get the current tenant schema name.
-
.current_schema=(schema_name) ⇒ String
Set the current tenant schema name.
-
.current_tenant ⇒ Object?
Get the current tenant object.
-
.current_tenant=(tenant) ⇒ Object
Set the current tenant object.
-
.drop_tenant_schema(tenant_or_schema, cascade: true) ⇒ void
Drop a PostgreSQL schema for a tenant.
-
.reset! ⇒ void
Reset the current tenant context to default.
-
.switch_to_schema(schema_name) ⇒ void
Switch to a specific schema.
-
.switch_to_tenant(tenant) ⇒ void
Switch to a specific tenant's schema.
-
.with_tenant(tenant_or_schema) { ... } ⇒ Object
Execute a block within a tenant context.
Class Method Details
.create_tenant_schema(tenant_or_schema) ⇒ void
This method returns an undefined value.
Create a new PostgreSQL schema for a tenant
165 166 167 168 169 170 171 172 |
# File 'lib/pg_multitenant_schemas/context.rb', line 165 def create_tenant_schema(tenant_or_schema) schema_name = if tenant_or_schema.respond_to?(:subdomain) tenant_or_schema.subdomain else tenant_or_schema.to_s end SchemaSwitcher.create_schema(schema_name) end |
.current_schema ⇒ String
Get the current tenant schema name
43 44 45 |
# File 'lib/pg_multitenant_schemas/context.rb', line 43 def current_schema Thread.current[:pg_multitenant_current_schema] || PgMultitenantSchemas.configuration.default_schema end |
.current_schema=(schema_name) ⇒ String
Set the current tenant schema name
51 52 53 |
# File 'lib/pg_multitenant_schemas/context.rb', line 51 def current_schema=(schema_name) Thread.current[:pg_multitenant_current_schema] = schema_name end |
.current_tenant ⇒ Object?
Get the current tenant object
26 27 28 |
# File 'lib/pg_multitenant_schemas/context.rb', line 26 def current_tenant Thread.current[:pg_multitenant_current_tenant] end |
.current_tenant=(tenant) ⇒ Object
Set the current tenant object
34 35 36 |
# File 'lib/pg_multitenant_schemas/context.rb', line 34 def current_tenant=(tenant) Thread.current[:pg_multitenant_current_tenant] = tenant end |
.drop_tenant_schema(tenant_or_schema, cascade: true) ⇒ void
This method returns an undefined value.
Drop a PostgreSQL schema for a tenant
185 186 187 188 189 190 191 192 |
# File 'lib/pg_multitenant_schemas/context.rb', line 185 def drop_tenant_schema(tenant_or_schema, cascade: true) schema_name = if tenant_or_schema.respond_to?(:subdomain) tenant_or_schema.subdomain else tenant_or_schema.to_s end SchemaSwitcher.drop_schema(schema_name, cascade: cascade) end |
.reset! ⇒ void
This method returns an undefined value.
Reset the current tenant context to default
Clears both tenant and schema context and restores the default schema
62 63 64 65 66 |
# File 'lib/pg_multitenant_schemas/context.rb', line 62 def reset! Thread.current[:pg_multitenant_current_tenant] = nil Thread.current[:pg_multitenant_current_schema] = nil switch_to_schema(PgMultitenantSchemas.configuration.default_schema) end |
.switch_to_schema(schema_name) ⇒ void
This method returns an undefined value.
Switch to a specific schema
74 75 76 77 78 |
# File 'lib/pg_multitenant_schemas/context.rb', line 74 def switch_to_schema(schema_name) schema_name = PgMultitenantSchemas.configuration.default_schema if schema_name.blank? SchemaSwitcher.switch_schema(schema_name) self.current_schema = schema_name end |
.switch_to_tenant(tenant) ⇒ void
This method returns an undefined value.
Switch to a specific tenant's schema
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pg_multitenant_schemas/context.rb', line 89 def switch_to_tenant(tenant) if tenant schema_name = tenant.respond_to?(:subdomain) ? tenant.subdomain : tenant.to_s switch_to_schema(schema_name) self.current_tenant = tenant else switch_to_schema(PgMultitenantSchemas.configuration.default_schema) self.current_tenant = nil end end |
.with_tenant(tenant_or_schema) { ... } ⇒ Object
Execute a block within a tenant context
All database queries within the block will be executed in the tenant's schema. The previous context is restored after the block completes, even if an error occurs.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/pg_multitenant_schemas/context.rb', line 117 def with_tenant(tenant_or_schema) schema_name, tenant = extract_schema_and_tenant(tenant_or_schema) previous_tenant = current_tenant previous_schema = current_schema begin switch_to_schema(schema_name) self.current_tenant = tenant yield if block_given? ensure restore_previous_context(previous_tenant, previous_schema) end end |