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:
- Removal of
company_id: Thecompany_idcolumn was removed from tenant-specific tables likeecodesign_analyses, as the tenant schema inherently provides this separation. - Tenant Migrations: New migrations were created for tables like
aee_company_report_subscriptionsto ensure they exist within the tenant database. - Idempotent Migrations: Migrations were created or updated to ensure that tables like
tiresandaee_generated_reportsare consistently present in the tenant schema. - 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.