Multi tenancy

From event4u-app/agent-config · View interactive version

Multi tenancy is an AI agent skill from the GitHub repository event4u-app/agent-config. Use when working with the multi-tenant architecture — customer DB switching, FQDN routing, tenant isolation, or cross-tenant operations.

How to use this skill

Skill file

<!-- Framework scope: Laravel only. Uses Eloquent model conventions,

Laravel Context, and Artisan traits — concepts not portable across

frameworks. Other stacks need a stack-native multi-tenancy skill. -->

multi-tenancy

When to use

Use this skill when working with tenant-specific data, customer database connections, or any code that touches the dual-database architecture.

Two tenancy modes exist: database-per-tenant (this body) and shared-schema

(tenant_id in every tenant table: child foreign keys carry tenant_id, the

parent is UNIQUE (tenant_id, id)) — the full key rules are in

CODE0.

Do NOT use when:

Procedure: Work with multi-tenancy

  1. Gather context — read agents/reference/docs/ for multi-tenant architecture, config/database.php for connection definitions, and search for the tenant switching service.
  2. Identify connection — determine whether the code touches central, tenant, or both databases. Set $connection explicitly on any new model.
  3. Implement — write the feature using the correct connection. Use the tenant switching service for cross-tenant operations. Never mix connections in a single query.
  4. Verify isolation — inspect the code for tenant leaks: global scopes, missing $connection, shared caches, or job serialization without tenant context.
  5. Test — write a test that exercises the tenant boundary: seed tenant-specific data, switch context, verify correct data is returned and other tenants' data is invisible.

Architecture overview

Request → Identify Tenant (JWT / subdomain / API key)
        → Lookup credentials from central database
        → Reconfigure tenant connection at runtime
        → All tenant queries use tenant connection

Dual-database pattern

| Connection type | Purpose | Scope |

|---|---|---|

| Central / shared | Global data — tenants, config, shared resources | Shared across all tenants |

| Tenant / customer | Tenant-specific data — domain entities | One per tenant |

The central connection is typically the default connection.

The tenant connection starts empty and is configured dynamically per request.

Additional connections

Projects may have additional connections for admin operations, provisioning, or monitoring. Check config/database.php.

Core tenant switching service

Scope: Laravel-specific (see frontmatter). For non-Laravel multi-tenant systems, the concepts below still apply but the implementation differs — consult the framework's connection / session / DI conventions.

Search the codebase for the service responsible for tenant switching. Typical responsibilities:

  1. Store tenant context (e.g., in Laravel Context or a singleton)
  2. Load tenant configuration
  3. Set monitoring context (tenant ID, name, domain)
  4. Reconfigure the database connection with tenant credentials
  5. Bind tenant-specific services via the container

Model conventions

Setting `$connection`

Every model must explicitly set its connection:

// Central models
class Tenant extends Model
{
    protected $connection = 'central_database';
}

// Tenant models
class Project extends Model
{
    protected $connection = 'tenant_database';
}

Check the project for the actual connection names and namespace conventions.

Tenant isolation rules

Testing with tenants

Common pitfalls

| Problem | Solution |

|---|---|

| Query on customer_database returns empty | Customer not set yet — check middleware order |

| Race condition in parallel tests | Each test process gets its own DB (parallel testing with separate DBs) |

| Wrong tenant data in background jobs | Serialize customer ID, re-resolve in job's handle() method |

| Migration on wrong connection | Specify --database=customer_database or set $connection in migration |

Output format

  1. Tenant-aware code with correct DB connection switching
  2. Verification that tenant isolation is maintained

Auto-trigger keywords

Gotcha

Do NOT

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.