PHP Laravel

Tenant Schema Migration in Reimpact Platform

The Reimpact platform is undergoing significant architectural changes to better support multi-tenancy. This post details recent efforts to fully migrate to a tenant-based schema, enhancing data isolation and scalability.

Completing the Tenant Migration

The focus has been on ensuring that all relevant models and database tables are correctly associated with the tenant schema. This involves adding the HasTenantTable trait to models and removing direct references to company IDs from tenant-specific tables.

Models and Traits

Several models were updated to include the HasTenantTable trait. This ensures that these models interact with the tenant-specific database schema instead of the public schema. For example:

use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Database\Concerns\HasTenantTable;

class ProductProvider extends Model
{
    use HasTenantTable;

    // ... model definition ...
}

The HasTenantTable trait automatically scopes queries to the current tenant, ensuring data isolation.

Database Schema Changes

To complete the migration, several steps were taken:

  1. Removal of company_id: The company_id column was removed from tenant-specific tables like ecodesign_analyses, as the tenant schema inherently provides this separation.
  2. Tenant Migrations: New migrations were created for tables like aee_company_report_subscriptions to ensure they exist within the tenant database.
  3. Idempotent Migrations: Migrations were created or updated to ensure that tables like tires and aee_generated_reports are consistently present in the tenant schema.
  4. Dropping Legacy Tables: A significant number of empty or legacy tables from the public schema were dropped, including sales, purchases, materials, and several others, to clean up the database and prevent accidental usage.

Impact and Benefits

By completing the tenant schema migration, the Reimpact platform achieves better data isolation, improved scalability, and a cleaner database structure. This simplifies future development and maintenance efforts.

Tenant Schema Migration in Reimpact Platform
GERARDO RUIZ

GERARDO RUIZ

Author

Share: