pg_multitenant_schemas 0.1.3 โ 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/CHANGELOG.md +46 -0
- data/README.md +269 -16
- data/docs/README.md +77 -0
- data/docs/configuration.md +340 -0
- data/docs/context.md +292 -0
- data/docs/errors.md +498 -0
- data/docs/integration_testing.md +454 -0
- data/docs/migrator.md +291 -0
- data/docs/rails_integration.md +468 -0
- data/docs/schema_switcher.md +182 -0
- data/docs/tenant_resolver.md +394 -0
- data/docs/testing.md +358 -0
- data/examples/context_management.rb +198 -0
- data/examples/migration_workflow.rb +50 -0
- data/examples/rails_integration/controller_examples.rb +368 -0
- data/examples/schema_operations.rb +124 -0
- data/lib/pg_multitenant_schemas/configuration.rb +4 -4
- data/lib/pg_multitenant_schemas/migration_display_reporter.rb +30 -0
- data/lib/pg_multitenant_schemas/migration_executor.rb +81 -0
- data/lib/pg_multitenant_schemas/migration_schema_operations.rb +54 -0
- data/lib/pg_multitenant_schemas/migration_status_reporter.rb +65 -0
- data/lib/pg_multitenant_schemas/migrator.rb +89 -0
- data/lib/pg_multitenant_schemas/schema_switcher.rb +40 -66
- data/lib/pg_multitenant_schemas/tasks/advanced_tasks.rake +21 -0
- data/lib/pg_multitenant_schemas/tasks/basic_tasks.rake +20 -0
- data/lib/pg_multitenant_schemas/tasks/pg_multitenant_schemas.rake +53 -143
- data/lib/pg_multitenant_schemas/tasks/tenant_tasks.rake +65 -0
- data/lib/pg_multitenant_schemas/tenant_task_helpers.rb +102 -0
- data/lib/pg_multitenant_schemas/version.rb +1 -1
- data/lib/pg_multitenant_schemas.rb +10 -5
- data/pg_multitenant_schemas.gemspec +10 -9
- data/rails_integration/app/controllers/application_controller.rb +6 -0
- data/rails_integration/app/models/tenant.rb +6 -0
- metadata +39 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bb953481e57f8425d7a272414d51e9225d224f6975f7e519051566f8e6c58406
|
|
4
|
+
data.tar.gz: 3512040a1448b3a8cca8f829717ecc373510234bc53e2d84243272ea16903846
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c4e77e04b02112a32d8ac953ab0c88a662cc599ec1fc498f6c3205c3a4596ba74e26c514add5de50df58414ff4dfd62a65241e2dbe26f1dbfcdd3093eab25426
|
|
7
|
+
data.tar.gz: 267b65ce20ab82f817c1ecd959dcd3781ad7f0b00434a381cf3a7968cf1e5363bf57519d26e07cf3bc9d75b90797f2b394c2a3bca8ef060b52e01768ce8c10e4
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.4.3
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,52 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
### ๐ **Migration System Overhaul**
|
|
11
|
+
- **NEW: Automated Migration Management**: Complete migration system for multi-tenant schemas
|
|
12
|
+
- **NEW: PgMultitenantSchemas::Migrator**: Comprehensive migration management class
|
|
13
|
+
- **NEW: Simplified Rake Tasks**: Modern `tenants:*` namespace with intuitive commands
|
|
14
|
+
- **NEW: Bulk Operations**: Single-command migration across all tenant schemas
|
|
15
|
+
- **NEW: Enhanced Status Reporting**: Detailed migration status with progress indicators
|
|
16
|
+
|
|
17
|
+
### โจ **Enhanced Features**
|
|
18
|
+
- **Automated Tenant Setup**: `setup_tenant()` creates schema + runs migrations
|
|
19
|
+
- **Migration Status Tracking**: Real-time status across all tenant schemas
|
|
20
|
+
- **Error Resilience**: Graceful error handling per tenant during bulk operations
|
|
21
|
+
- **Tenant Creation Workflow**: `create_tenant_with_schema()` for complete tenant setup
|
|
22
|
+
- **Rollback Support**: Individual tenant rollback capabilities
|
|
23
|
+
|
|
24
|
+
### ๐ง **Developer Experience**
|
|
25
|
+
- **Intuitive Commands**: `rails tenants:migrate`, `rails tenants:status`, `rails tenants:create`
|
|
26
|
+
- **Progress Feedback**: Visual progress indicators during migration operations
|
|
27
|
+
- **Safety Features**: Confirmation prompts for destructive operations
|
|
28
|
+
- **Legacy Compatibility**: Deprecated old commands with migration path
|
|
29
|
+
- **Example Scripts**: Complete workflow documentation and examples
|
|
30
|
+
|
|
31
|
+
## [0.2.0] - 2025-09-06
|
|
32
|
+
|
|
33
|
+
### ๐ **BREAKING CHANGES**
|
|
34
|
+
- **Modernized for Rails 8+ and Ruby 3.4+**: Removed backward compatibility
|
|
35
|
+
- **Simplified API**: Removed dual API support for cleaner codebase
|
|
36
|
+
- **Rails 8+ Only**: Updated dependencies to require ActiveRecord >= 8.0
|
|
37
|
+
- **Ruby 3.2+ Required**: Minimum Ruby version increased from 3.1.0 to 3.2.0
|
|
38
|
+
|
|
39
|
+
### โจ **Added**
|
|
40
|
+
- Modern Ruby 3.3+ features and performance optimizations
|
|
41
|
+
- Enhanced configuration defaults for Rails 8+ applications
|
|
42
|
+
- Improved error messages and developer experience
|
|
43
|
+
|
|
44
|
+
### ๐ง **Changed**
|
|
45
|
+
- Simplified `SchemaSwitcher` API (removed connection parameter overloads)
|
|
46
|
+
- Updated default connection class to `ApplicationRecord`
|
|
47
|
+
- Enhanced excluded subdomains and TLD lists
|
|
48
|
+
- Automatic Rails logger integration
|
|
49
|
+
|
|
50
|
+
### ๐๏ธ **Removed**
|
|
51
|
+
- Backward compatibility layers
|
|
52
|
+
- Dual API support (old conn parameter methods)
|
|
53
|
+
- Legacy Rails version compatibility code
|
|
54
|
+
- Ruby < 3.3 support
|
|
55
|
+
|
|
10
56
|
## [0.1.3] - 2025-09-03
|
|
11
57
|
|
|
12
58
|
### Added
|
data/README.md
CHANGED
|
@@ -3,18 +3,27 @@
|
|
|
3
3
|
[](https://badge.fury.io/rb/pg_multitenant_schemas)
|
|
4
4
|
[](https://github.com/yourusername/pg_multitenant_schemas/actions/workflows/main.yml)
|
|
5
5
|
|
|
6
|
-
A Ruby gem that provides PostgreSQL schema-based multitenancy with automatic tenant resolution
|
|
6
|
+
A modern Ruby gem that provides PostgreSQL schema-based multitenancy with automatic tenant resolution and schema switching. Built for Rails 8+ and Ruby 3.3+, focusing on security, performance, and developer experience.
|
|
7
7
|
|
|
8
|
-
## Features
|
|
8
|
+
## โจ Features
|
|
9
9
|
|
|
10
10
|
- ๐ข **Schema-based multitenancy** - Complete tenant isolation using PostgreSQL schemas
|
|
11
|
-
- ๐ **Automatic schema switching** - Seamlessly switch between tenant schemas
|
|
11
|
+
- ๐ **Automatic schema switching** - Seamlessly switch between tenant schemas
|
|
12
12
|
- ๐ **Subdomain resolution** - Extract tenant from request subdomains
|
|
13
|
-
-
|
|
14
|
-
-
|
|
13
|
+
- ๏ฟฝ **Rails 8+ optimized** - Built for modern Rails applications
|
|
14
|
+
- ๏ฟฝ๏ธ **Security-first design** - Database-level tenant isolation
|
|
15
15
|
- ๐งต **Thread-safe** - Safe for concurrent operations
|
|
16
16
|
- ๐ **Comprehensive logging** - Track schema operations
|
|
17
|
-
- โก **High performance** - Minimal overhead
|
|
17
|
+
- โก **High performance** - Minimal overhead with clean API
|
|
18
|
+
|
|
19
|
+
## ๐ Requirements
|
|
20
|
+
|
|
21
|
+
## Requirements
|
|
22
|
+
|
|
23
|
+
- Ruby 3.4+
|
|
24
|
+
- Rails 8.0+
|
|
25
|
+
- PostgreSQL 12+
|
|
26
|
+
- **pg gem**: 1.5 or higher
|
|
18
27
|
|
|
19
28
|
## Installation
|
|
20
29
|
|
|
@@ -53,20 +62,90 @@ end
|
|
|
53
62
|
|
|
54
63
|
## Usage
|
|
55
64
|
|
|
56
|
-
###
|
|
65
|
+
### Migration Management
|
|
57
66
|
|
|
58
|
-
|
|
59
|
-
# Switch to a tenant schema
|
|
60
|
-
PgMultitenantSchemas::SchemaSwitcher.switch_schema('tenant_123')
|
|
67
|
+
The gem provides automated migration management across all tenant schemas:
|
|
61
68
|
|
|
62
|
-
|
|
63
|
-
PgMultitenantSchemas::SchemaSwitcher.create_schema('tenant_456')
|
|
69
|
+
#### Running Migrations
|
|
64
70
|
|
|
65
|
-
|
|
66
|
-
|
|
71
|
+
```bash
|
|
72
|
+
# Migrate all tenant schemas at once (recommended)
|
|
73
|
+
rails tenants:migrate
|
|
74
|
+
|
|
75
|
+
# Migrate a specific tenant
|
|
76
|
+
rails tenants:migrate_tenant[acme_corp]
|
|
67
77
|
|
|
68
|
-
#
|
|
69
|
-
|
|
78
|
+
# Check migration status across all tenants
|
|
79
|
+
rails tenants:status
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
#### Setting Up New Tenants
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
# Create new tenant with full setup (schema + migrations)
|
|
86
|
+
rails tenants:create[new_tenant]
|
|
87
|
+
|
|
88
|
+
# Create tenant with attributes using JSON
|
|
89
|
+
rails tenants:new['{"subdomain":"acme","name":"ACME Corp","domain":"acme.com"}']
|
|
90
|
+
|
|
91
|
+
# Setup schemas for all existing tenants (migration from single-tenant)
|
|
92
|
+
rails tenants:setup
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### Migration Workflow
|
|
96
|
+
|
|
97
|
+
1. **Create your migration** as usual:
|
|
98
|
+
```bash
|
|
99
|
+
rails generate migration AddEmailToUsers email:string
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
2. **Deploy to all tenants** with a single command:
|
|
103
|
+
```bash
|
|
104
|
+
rails tenants:migrate
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
3. **Check status** to verify migrations across tenants:
|
|
108
|
+
```bash
|
|
109
|
+
rails tenants:status
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
This shows detailed migration status:
|
|
113
|
+
```
|
|
114
|
+
๐ Migration Status Report
|
|
115
|
+
โธ acme_corp: โ
Up to date (5 migrations)
|
|
116
|
+
โธ beta_corp: โ ๏ธ 2 pending migrations
|
|
117
|
+
โธ demo_corp: โ
Up to date (5 migrations)
|
|
118
|
+
|
|
119
|
+
๐ Overall: 2/3 tenants current, 2 migrations pending
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The migrator automatically:
|
|
123
|
+
- Runs migrations across all tenant schemas
|
|
124
|
+
- Provides detailed progress feedback
|
|
125
|
+
- Handles errors gracefully per tenant
|
|
126
|
+
- Maintains migration version tracking per schema
|
|
127
|
+
- Supports rollbacks for individual tenants
|
|
128
|
+
|
|
129
|
+
#### Advanced Migration Operations
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
# List all tenant schemas
|
|
133
|
+
rails tenants:list
|
|
134
|
+
|
|
135
|
+
# Rollback specific tenant
|
|
136
|
+
rails tenants:rollback[tenant_name,2] # rollback 2 steps
|
|
137
|
+
|
|
138
|
+
# Drop tenant schema (DANGEROUS - requires confirmation)
|
|
139
|
+
rails tenants:drop[old_tenant]
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### Programmatic Access
|
|
143
|
+
|
|
144
|
+
```ruby
|
|
145
|
+
# Use the Migrator directly in your code
|
|
146
|
+
PgMultitenantSchemas::Migrator.migrate_all
|
|
147
|
+
PgMultitenantSchemas::Migrator.setup_tenant('new_client')
|
|
148
|
+
PgMultitenantSchemas::Migrator.migration_status
|
|
70
149
|
```
|
|
71
150
|
|
|
72
151
|
### Tenant Resolution
|
|
@@ -130,6 +209,180 @@ class Tenant < ApplicationRecord
|
|
|
130
209
|
end
|
|
131
210
|
```
|
|
132
211
|
|
|
212
|
+
## ๐๏ธ Multi-Tenant Architecture Principles
|
|
213
|
+
|
|
214
|
+
### Core Principle: Tenant Isolation
|
|
215
|
+
|
|
216
|
+
**In well-designed multi-tenant applications, tenants should NOT communicate with each other directly.** Each tenant operates in complete isolation for security, compliance, and data integrity.
|
|
217
|
+
|
|
218
|
+
```ruby
|
|
219
|
+
# โ
GOOD: Isolated tenant operations
|
|
220
|
+
PgMultitenantSchemas.with_tenant(tenant) do
|
|
221
|
+
# All operations are isolated to this tenant's schema
|
|
222
|
+
User.create!(name: "John", email: "john@example.com")
|
|
223
|
+
Order.where(status: "pending").update_all(status: "processed")
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# โ BAD: Cross-tenant data sharing (security risk!)
|
|
227
|
+
# Never do: tenant_a.users.merge(tenant_b.users)
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### When Cross-Schema Operations Are Appropriate
|
|
231
|
+
|
|
232
|
+
There are only **3 legitimate use cases** for cross-schema operations:
|
|
233
|
+
|
|
234
|
+
#### 1. **Platform Analytics & Reporting** (Admin-only)
|
|
235
|
+
```ruby
|
|
236
|
+
# Platform owner needs aggregate statistics across all tenants
|
|
237
|
+
def platform_analytics
|
|
238
|
+
PgMultitenantSchemas::SchemaSwitcher.with_connection do |conn|
|
|
239
|
+
conn.execute(<<~SQL)
|
|
240
|
+
SELECT
|
|
241
|
+
schemaname as tenant,
|
|
242
|
+
COUNT(*) as total_users,
|
|
243
|
+
SUM(revenue) as total_revenue
|
|
244
|
+
FROM (
|
|
245
|
+
SELECT 'tenant_a' as schemaname, COUNT(*) as users,
|
|
246
|
+
(SELECT SUM(amount) FROM tenant_a.orders) as revenue
|
|
247
|
+
UNION ALL
|
|
248
|
+
SELECT 'tenant_b' as schemaname, COUNT(*) as users,
|
|
249
|
+
(SELECT SUM(amount) FROM tenant_b.orders) as revenue
|
|
250
|
+
) stats
|
|
251
|
+
GROUP BY schemaname;
|
|
252
|
+
SQL
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
#### 2. **Tenant Migration & Data Operations** (Admin-only)
|
|
258
|
+
```ruby
|
|
259
|
+
# Moving data between environments or consolidating tenants
|
|
260
|
+
def migrate_tenant_data(from_tenant, to_tenant)
|
|
261
|
+
PgMultitenantSchemas.with_tenant(from_tenant) do
|
|
262
|
+
users = User.all.to_a
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
PgMultitenantSchemas.with_tenant(to_tenant) do
|
|
266
|
+
users.each { |user| User.create!(user.attributes.except('id')) }
|
|
267
|
+
end
|
|
268
|
+
end
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
#### 3. **Shared Reference Data** (Read-only)
|
|
272
|
+
```ruby
|
|
273
|
+
# Shared lookup tables that all tenants can read (e.g., countries, currencies)
|
|
274
|
+
class Country < ApplicationRecord
|
|
275
|
+
self.table_name = 'public.countries' # Shared across all schemas
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# In tenant context, can still access shared data
|
|
279
|
+
PgMultitenantSchemas.with_tenant(tenant) do
|
|
280
|
+
user = User.create!(name: "John", country: Country.find_by(code: 'US'))
|
|
281
|
+
end
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### ๐ซ Anti-Patterns to Avoid
|
|
285
|
+
|
|
286
|
+
```ruby
|
|
287
|
+
# โ NEVER: Direct tenant-to-tenant communication
|
|
288
|
+
def share_data_between_tenants(tenant_a, tenant_b)
|
|
289
|
+
# This violates tenant isolation!
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
# โ NEVER: Cross-tenant user authentication
|
|
293
|
+
def authenticate_user_across_tenants(email)
|
|
294
|
+
# Users should only exist in their tenant's schema
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# โ NEVER: Cross-tenant business logic
|
|
298
|
+
def process_order_with_other_tenant_data(order_id, other_tenant)
|
|
299
|
+
# Business logic should be isolated per tenant
|
|
300
|
+
end
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### Why Tenant Isolation Matters
|
|
304
|
+
|
|
305
|
+
1. **Security**: Prevents accidental data leaks between customers
|
|
306
|
+
2. **Compliance**: GDPR, HIPAA, SOX require strict data separation
|
|
307
|
+
3. **Performance**: Each tenant's queries are optimized for their data size
|
|
308
|
+
4. **Reliability**: One tenant's issues don't affect others
|
|
309
|
+
5. **Scalability**: Easy to move tenants to different database servers
|
|
310
|
+
|
|
311
|
+
### Architecture Recommendation
|
|
312
|
+
|
|
313
|
+
```ruby
|
|
314
|
+
# โ
Proper multi-tenant architecture
|
|
315
|
+
class ApplicationController < ActionController::Base
|
|
316
|
+
include PgMultitenantSchemas::Rails::ControllerConcern
|
|
317
|
+
|
|
318
|
+
before_action :authenticate_user!
|
|
319
|
+
before_action :resolve_tenant
|
|
320
|
+
before_action :ensure_user_belongs_to_tenant
|
|
321
|
+
|
|
322
|
+
private
|
|
323
|
+
|
|
324
|
+
def resolve_tenant
|
|
325
|
+
@tenant = resolve_tenant_from_subdomain
|
|
326
|
+
switch_to_tenant(@tenant) if @tenant
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
def ensure_user_belongs_to_tenant
|
|
330
|
+
# Ensure current user can only access their tenant's data
|
|
331
|
+
redirect_to_login unless current_user.tenant == @tenant
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
**Bottom Line**: Cross-tenant operations should be **extremely rare**, **admin-only**, and **carefully audited**. The vast majority of your application should operate within a single tenant's schema.
|
|
337
|
+
|
|
338
|
+
## ๐ Documentation
|
|
339
|
+
|
|
340
|
+
### Complete Architecture Documentation
|
|
341
|
+
|
|
342
|
+
Detailed documentation for each core component:
|
|
343
|
+
|
|
344
|
+
- **[๐ Core Architecture Overview](docs/README.md)** - Complete system architecture
|
|
345
|
+
- **[๐ง Schema Switcher](docs/schema_switcher.md)** - Low-level PostgreSQL schema operations
|
|
346
|
+
- **[๐งต Context Management](docs/context.md)** - Thread-safe tenant context handling
|
|
347
|
+
- **[๐ Migration System](docs/migrator.md)** - Automated migration management
|
|
348
|
+
- **[โ๏ธ Configuration](docs/configuration.md)** - Gem settings and customization
|
|
349
|
+
- **[๐ Tenant Resolver](docs/tenant_resolver.md)** - Tenant identification strategies
|
|
350
|
+
- **[๐ค๏ธ Rails Integration](docs/rails_integration.md)** - Framework components and patterns
|
|
351
|
+
- **[๐จ Error Handling](docs/errors.md)** - Exception classes and error management
|
|
352
|
+
|
|
353
|
+
### Examples and Patterns
|
|
354
|
+
|
|
355
|
+
- **[Schema Operations](examples/schema_operations.rb)** - Core schema management
|
|
356
|
+
- **[Context Management](examples/context_management.rb)** - Thread-safe tenant switching
|
|
357
|
+
- **[Migration Workflow](examples/migration_workflow.rb)** - Automated migration examples
|
|
358
|
+
- **[Rails Controllers](examples/rails_integration/controller_examples.rb)** - Framework integration patterns
|
|
359
|
+
|
|
360
|
+
## ๐งช Testing
|
|
361
|
+
|
|
362
|
+
This gem includes a comprehensive test suite with both unit and integration tests.
|
|
363
|
+
|
|
364
|
+
### Running Tests
|
|
365
|
+
|
|
366
|
+
```bash
|
|
367
|
+
# Run unit tests only (fast, no database required)
|
|
368
|
+
bundle exec rspec
|
|
369
|
+
|
|
370
|
+
# Run integration tests (requires PostgreSQL)
|
|
371
|
+
bundle exec rspec --tag integration
|
|
372
|
+
|
|
373
|
+
# Run all tests
|
|
374
|
+
bundle exec rspec --no-tag
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
### Test Categories
|
|
378
|
+
|
|
379
|
+
- **Unit Tests** (65 examples): Fast, isolated component testing
|
|
380
|
+
- **Integration Tests** (21 examples): Real PostgreSQL multi-schema operations
|
|
381
|
+
- **Performance Tests**: Memory usage and thread safety validation
|
|
382
|
+
- **Edge Cases**: Error handling and boundary condition testing
|
|
383
|
+
|
|
384
|
+
See [Testing Guide](docs/testing.md) for detailed information about the test suite and [Integration Testing Guide](docs/integration_testing.md) for PostgreSQL integration testing details.
|
|
385
|
+
|
|
133
386
|
## Development
|
|
134
387
|
|
|
135
388
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/docs/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# PG Multitenant Schemas - Core Architecture Documentation
|
|
2
|
+
|
|
3
|
+
This directory contains detailed documentation for each core component of the PG Multitenant Schemas gem.
|
|
4
|
+
|
|
5
|
+
## ๐ Core Components Overview
|
|
6
|
+
|
|
7
|
+
| Component | Purpose | Documentation |
|
|
8
|
+
|-----------|---------|---------------|
|
|
9
|
+
| **SchemaSwitcher** | Low-level PostgreSQL schema operations | [schema_switcher.md](schema_switcher.md) |
|
|
10
|
+
| **Context** | Thread-safe tenant context management | [context.md](context.md) |
|
|
11
|
+
| **Migrator** | Automated migration management system | [migrator.md](migrator.md) |
|
|
12
|
+
| **Configuration** | Gem configuration and settings | [configuration.md](configuration.md) |
|
|
13
|
+
| **TenantResolver** | Tenant identification and resolution | [tenant_resolver.md](tenant_resolver.md) |
|
|
14
|
+
| **Rails Integration** | Rails framework integration components | [rails_integration.md](rails_integration.md) |
|
|
15
|
+
| **Errors** | Custom exception classes | [errors.md](errors.md) |
|
|
16
|
+
| **Testing** | RSpec test suite and testing guide | [testing.md](testing.md) |
|
|
17
|
+
| **Integration Testing** | PostgreSQL integration testing guide | [integration_testing.md](integration_testing.md) |
|
|
18
|
+
|
|
19
|
+
## ๐๏ธ Architecture Flow
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
HTTP Request
|
|
23
|
+
โ
|
|
24
|
+
TenantResolver (identifies tenant)
|
|
25
|
+
โ
|
|
26
|
+
Context (sets tenant context)
|
|
27
|
+
โ
|
|
28
|
+
SchemaSwitcher (switches PostgreSQL schema)
|
|
29
|
+
โ
|
|
30
|
+
Rails Application (executes in tenant schema)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## ๐ง Key Concepts
|
|
34
|
+
|
|
35
|
+
### Schema-Based Multitenancy
|
|
36
|
+
- Each tenant gets their own PostgreSQL schema
|
|
37
|
+
- Complete data isolation between tenants
|
|
38
|
+
- Shared application code, separate data
|
|
39
|
+
|
|
40
|
+
### Thread-Safe Context
|
|
41
|
+
- Tenant context is stored per thread
|
|
42
|
+
- Safe for concurrent requests
|
|
43
|
+
- Automatic context restoration
|
|
44
|
+
|
|
45
|
+
### Automated Migrations
|
|
46
|
+
- Single command migrates all tenant schemas
|
|
47
|
+
- Error handling per tenant
|
|
48
|
+
- Progress tracking and status reporting
|
|
49
|
+
|
|
50
|
+
## ๐ Getting Started
|
|
51
|
+
|
|
52
|
+
1. **Read the [Configuration Guide](configuration.md)** to understand setup options
|
|
53
|
+
2. **Explore [Schema Switcher](schema_switcher.md)** for core PostgreSQL operations
|
|
54
|
+
3. **Learn [Context Management](context.md)** for tenant switching
|
|
55
|
+
4. **Master [Migration System](migrator.md)** for database management
|
|
56
|
+
5. **Understand [Rails Integration](rails_integration.md)** for framework features
|
|
57
|
+
6. **Review [Testing Guide](testing.md)** for development and testing practices
|
|
58
|
+
|
|
59
|
+
## ๐งช Testing and Development
|
|
60
|
+
|
|
61
|
+
- **[Testing Guide](testing.md)**: Comprehensive RSpec test suite documentation
|
|
62
|
+
- **[Integration Testing](integration_testing.md)**: PostgreSQL integration testing guide
|
|
63
|
+
- **Test Execution**: `bundle exec rspec` (unit tests) and `bundle exec rspec --tag integration` (integration tests)
|
|
64
|
+
|
|
65
|
+
## ๐ Debug and Troubleshooting
|
|
66
|
+
|
|
67
|
+
- Check [Errors Documentation](errors.md) for exception handling
|
|
68
|
+
- Review [TenantResolver](tenant_resolver.md) for tenant identification issues
|
|
69
|
+
- Examine Context state for switching problems
|
|
70
|
+
- Use [Testing Guide](testing.md) for debugging test failures
|
|
71
|
+
|
|
72
|
+
## ๐ Additional Resources
|
|
73
|
+
|
|
74
|
+
- [Main README](../README.md) - Getting started guide
|
|
75
|
+
- [CHANGELOG](../CHANGELOG.md) - Version history
|
|
76
|
+
- [Examples](../examples/) - Usage examples
|
|
77
|
+
- [Specs](../spec/) - Test suite
|