# rails-on-services/apartment architecture

> Architecture diagram of the GitHub repository rails-on-services/apartment, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rails-on-services/apartment
- Repository: https://github.com/rails-on-services/apartment

## Overview

##### Main Components

a. Core Components:
- Tenant Management System
- Database Adapters Layer
- Elevator Middleware System
- Migration Handler
- Configuration Manager

b. Database Support:
- PostgreSQL (with schema support)
- MySQL
- SQLite3

##### Key Architectural Elements

Layer 1: Application Integration
**┌─────────────────────────────────────────┐**
- Rails Application             │
- ┘
- ↓
**┌─────────────────────────────────────────┐**
- Elevator Middleware             │
- (Subdomain/Domain/Host/Custom Router)  │
- ┘

Layer 2: Core Apartment System
**┌─────────────────────────────────────────┐**
- Tenant Management               │
- (Creation/Switching/Dropping)        │
- ┘
- ↓
**┌─────────────────────────────────────────┐**
- Configuration Manager           │
- (Excluded Models/Persistent Schemas) │
- ┘

Layer 3: Database Abstraction
**┌─────────────────────────────────────────┐**
- Adapter Layer                 │
- ┬────────────┬─────────────┤
- PostgreSQL │   MySQL    │   SQLite3   │
- (Schemas)  │           │             │
- ┴────────────┴─────────────┘

##### Important Relationships

- Database-specific implementations connected to the Adapter Layer

##### Additional Elements

Callouts for:
- Persistent Schema Management
- Extension Support (particularly for PostgreSQL)
- Model Exclusion System
- Environment Handling

##### Notes Section

Notes regarding:
- Multi-server setup capabilities
- Background worker integration
- Testing considerations
- Performance implications

Remember to include version compatibility information and note the key differences between database adapters, particularly PostgreSQL's schema support versus other databases' separate database approach.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Application Layer"
        Rails["Rails Application"]
    end

    subgraph "Middleware Layer"
        Elevator["Elevator Middleware"]:::middleware
        click Elevator "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/elevators/generic.rb"
        
        subgraph "Elevator Types"
            Subdomain["Subdomain Elevator"]:::middleware
            Domain["Domain Elevator"]:::middleware
            Host["Host Elevator"]:::middleware
            FirstSub["First Subdomain Elevator"]:::middleware
            click Subdomain "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/elevators/subdomain.rb"
            click Domain "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/elevators/domain.rb"
            click Host "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/elevators/host.rb"
            click FirstSub "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/elevators/first_subdomain.rb"
        end
    end

    subgraph "Core System"
        TenantMgmt["Tenant Management System"]:::core
        ConfigMgr["Configuration Manager"]:::core
        Migration["Migration Handler"]:::core
        click TenantMgmt "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/tenant.rb"
        click ConfigMgr "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/railtie.rb"
        click Migration "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/migrator.rb"

        subgraph "Integration Components"
            Console["Console Integration"]:::integration
            Tasks["Task Management"]:::integration
            ActiveRecord["Active Record Integration"]:::integration
            click Console "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/console.rb"
            click Tasks "https://github.com/rails-on-services/apartment/blob/development/lib/tasks/apartment.rake"
            click ActiveRecord "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/active_record/connection_handling.rb"
        end
    end

    subgraph "Database Layer"
        Adapter["Database Adapter Layer"]:::adapter
        click Adapter "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/adapters/abstract_adapter.rb"
        
        subgraph "Database Adapters"
            Postgres["PostgreSQL\n(Schema Support)"]:::db
            MySQL["MySQL"]:::db
            SQLite["SQLite3"]:::db
            click Postgres "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/adapters/postgresql_adapter.rb"
            click MySQL "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/adapters/mysql2_adapter.rb"
            click SQLite "https://github.com/rails-on-services/apartment/blob/development/lib/apartment/adapters/sqlite3_adapter.rb"
        end
    end

    %% Relationships
    Rails --> Elevator
    Elevator --> TenantMgmt
    Elevator --> |Uses| ConfigMgr
    TenantMgmt --> |Manages| Adapter
    ConfigMgr --> |Configures| Adapter
    Migration --> |Uses| Adapter
    ActiveRecord --> |Extends| Adapter
    
    Elevator --> Subdomain
    Elevator --> Domain
    Elevator --> Host
    Elevator --> FirstSub
    
    Adapter --> Postgres
    Adapter --> MySQL
    Adapter --> SQLite

    %% Styling
    classDef core fill:#2374ab,stroke:#000,stroke-width:2px,color:#fff
    classDef middleware fill:#47a025,stroke:#000,stroke-width:2px,color:#fff
    classDef adapter fill:#ff8c42,stroke:#000,stroke-width:2px,color:#fff
    classDef db fill:#ffb347,stroke:#000,stroke-width:2px,color:#fff
    classDef integration fill:#845ec2,stroke:#000,stroke-width:2px,color:#fff

    %% Legend
    subgraph Legend
        CoreL["Core Component"]:::core
        MiddlewareL["Middleware Component"]:::middleware
        AdapterL["Adapter Component"]:::adapter
        DBL["Database Component"]:::db
        IntegrationL["Integration Component"]:::integration
    end
```
