graph LR
Request_Routing_Tenant_Identification["Request Routing & Tenant Identification"]
Schema_Management_Service["Schema Management Service"]
Tenant_Data_Management_Service["Tenant Data Management Service"]
Migration_Service["Migration Service"]
Administrative_Tools["Administrative Tools"]
URL_Generation_Utility["URL Generation Utility"]
Request_Routing_Tenant_Identification -- "instructs to activate schema" --> Schema_Management_Service
Request_Routing_Tenant_Identification -- "routes data manipulation requests" --> Tenant_Data_Management_Service
Schema_Management_Service -- "provides active schema context" --> Tenant_Data_Management_Service
Tenant_Data_Management_Service -- "initiates schema provisioning" --> Schema_Management_Service
Administrative_Tools -- "issues tenant management commands" --> Tenant_Data_Management_Service
Administrative_Tools -- "triggers database migrations" --> Migration_Service
Migration_Service -- "requests schema switching" --> Schema_Management_Service
click Request_Routing_Tenant_Identification href "https://github.com/CodeBoarding/GeneratedOnBoardings/blob/main/django-tenant-schemas/Request_Routing_Tenant_Identification.md" "Details"
click Schema_Management_Service href "https://github.com/CodeBoarding/GeneratedOnBoardings/blob/main/django-tenant-schemas/Schema_Management_Service.md" "Details"
click Tenant_Data_Management_Service href "https://github.com/CodeBoarding/GeneratedOnBoardings/blob/main/django-tenant-schemas/Tenant_Data_Management_Service.md" "Details"
click Migration_Service href "https://github.com/CodeBoarding/GeneratedOnBoardings/blob/main/django-tenant-schemas/Migration_Service.md" "Details"
click Administrative_Tools href "https://github.com/CodeBoarding/GeneratedOnBoardings/blob/main/django-tenant-schemas/Administrative_Tools.md" "Details"
The django-tenant-schemas architecture is designed to provide robust multi-tenancy by dynamically managing database schemas. At its core, the Request Routing & Tenant Identification component intercepts incoming requests to determine the active tenant, subsequently instructing the Schema Management Service to switch to the correct database schema. This ensures that all application logic, primarily handled by the Tenant Data Management Service for tenant-specific data operations, operates within an isolated data context. Administrative tasks, facilitated by the Administrative Tools, interact with the Tenant Data Management Service for tenant lifecycle management and trigger the Migration Service to apply schema changes across all tenants, with the Schema Management Service orchestrating the schema switching during migrations. The URL Generation Utility supports tenant-aware URL creation, completing the multi-tenant application ecosystem. This modular design emphasizes clear separation of concerns, making it suitable for scalable web applications requiring strong data isolation.
Request Routing & Tenant Identification [Expand]
Serves as the primary entry point for all incoming HTTP requests, responsible for identifying the current tenant and routing requests to the appropriate database schema. It also ensures that subsequent database operations are correctly scoped to the identified tenant.
Related Classes/Methods:
tenant_schemas.middleware.process_request:42-71tenant_schemas.middleware.get_tenant:112-122tenant_schemas.middleware.hostname_from_request:36-40tenant_schemas.routers.allow_syncdb:37-39tenant_schemas.routers.allow_migrate:12-35
Schema Management Service [Expand]
The central authority for managing and switching database schemas. It handles the activation of public or tenant-specific schemas and validates schema names, ensuring strict data isolation between tenants.
Related Classes/Methods:
tenant_schemas.postgresql_backend.base.set_schema:74-91tenant_schemas.postgresql_backend.base.set_tenant:66-72tenant_schemas.postgresql_backend.base.set_schema_to_public:93-97tenant_schemas.postgresql_backend.base._check_schema_name:38-40
Tenant Data Management Service [Expand]
Encapsulates the business logic for creating, saving, and deleting tenant records. A key responsibility is orchestrating the physical schema creation process when a new tenant is onboarded.
Related Classes/Methods:
tenant_schemas.models.save:56-78tenant_schemas.models.delete:80-94tenant_schemas.models.create_schema:96-120
Migration Service [Expand]
Responsible for applying database migrations across different schemas. It can execute migrations on the public schema or iterate through all active tenants to apply migrations to each tenant's dedicated schema, ensuring schema consistency.
Related Classes/Methods:
tenant_schemas.migration_executors.base.run_migrations:54-61tenant_schemas.migration_executors.base.run_tenant_migrations:63-64
Administrative Tools [Expand]
Provides a command-line interface for administrative tasks related to tenants. This includes creating new tenants, running tenant-specific commands, and managing tenant data, primarily used by system administrators.
Related Classes/Methods:
tenant_schemas.management.commands.__init__.handle:209-235tenant_schemas.management.commands.__init__.add_arguments:168-207tenant_schemas.management.commands.__init__.execute_command:60-74
A utility component designed to assist in generating tenant-aware URLs within Django templates. It ensures that all generated links correctly point to the current tenant's domain or path, maintaining context across the application.
Related Classes/Methods: